Normalization als parameter und Edgelikelihood berechnung in main methode

This commit is contained in:
2026-09-10 11:49:57 +02:00
parent c5b9d50963
commit 56d8c7b7f5
+72 -70
View File
@@ -87,70 +87,6 @@ HYPERGRAPH3 = {
2: (['p_{0,0}'], []),
}
VERTICES = ['Butadien', 'p_{0,0}', 'p_{0,1}', 'p_{0,2}', 'p_{0,3}', 'p_{0,4}', 'p_{0,5}', 'p_{0,6}', 'p_{0,7}', 'p_{0,8}', 'p_{0,9}', 'p_{0,10}', 'p_{0,11}', 'p_{0,12}', 'p_{0,13}', 'p_{0,14}', 'p_{0,15}', 'p_{0,16}', 'p_{0,17}', 'p_{0,18}']
#Vergleich mit dem NMR von Ethylen und Hexatrien
NMR1 = [0.32, 0.5, 0.87, 0.0, 0.11, 0.58, 0.06, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09, 0.0, 0.0, 0.17, 0.0, 0.09]
#Normalisiert
NMR1 = [0.11, 0.18, 0.31, 0.0, 0.04, 0.21, 0.02, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03, 0.0, 0.0, 0.06, 0.0, 0.03]
#Vergleich mit dem NMR von Ethylen und Octrien
NMR2 = [0.33, 0.43, 0.6, 0.0, 0.13, 0.9, 0.07, 0.01, 0.0, 0.0, 0.01, 0.0, 0.0, 0.01, 0.1, 0.0, 0.0, 0.22, 0.0, 0.13]
#Normalisiert
NMR2 = [0.11, 0.15, 0.21, 0.0, 0.04, 0.31, 0.02, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03, 0.0, 0.0, 0.07, 0.0, 0.04]
#Vergleich mit dem NMR von Ethylen und Benzol
NMR3 = [0.0, 0.32, 0.0, 0.0, 0.0, 0.0, 0.05, 0.0, 0.0, 0.02, 0.0, 0.29, 0.95, 0.17, 0.0, 0.0, 0.0, 0.0, 0.0, 0.05]
#Normalisiert
NMR3 = [0.0, 0.17, 0.0, 0.0, 0.0, 0.0, 0.03, 0.0, 0.0, 0.01, 0.0, 0.16, 0.51, 0.09, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03]
VERTICESSMILES = ['C=CC=C', 'C=C', 'C=CC=CC=C', 'C1CCC(C=C)CC=1', 'C(CCC(C=C)CC=C)=C', 'C=CC=CC=CC=C', 'C1CCC(C=CC=C)CC=1', 'C=CC1C=CCCC1', 'C1CCCCC=1', 'C=CC1CC=CCC1C=C', 'C1CCC(C=C)C(C=C)C=1', 'C(C1CC(C=C)C=CC1)=C', 'C1C=CC=CC=1', 'C(CCC1C=CC=CC1)=C', 'C1C(C=CC=C)CCCC=1', 'C=CC(C=C)CCCC=C', 'C=CCCCCC=C', 'C=CC1C=CC(C=C)CC1', 'C1CC2CCCCC2CC=1', 'C1CC2C=CC=CC2CC=1']
NMRMAX = [max(n1,n2,n3) for n1,n2,n3 in zip(NMR1,NMR2,NMR3)]
#Kombiniert Molekül it Wahrscheinlichkeit für NMR1:
VERTICE1 = {}
for vertice, likelihood in zip(VERTICES, NMR1):
VERTICE1[vertice] = likelihood
#Kombiniert Molekül it Wahrscheinlichkeit für NMR2:
VERTICE2 = {}
for vertice, likelihood in zip(VERTICES, NMR2):
VERTICE2[vertice] = likelihood
#Kombiniert Molekül it Wahrscheinlichkeit für NMR3:
VERTICE3 = {}
for vertice, likelihood in zip(VERTICES, NMR3):
VERTICE3[vertice] = likelihood
#Kantenwahrscheinlichkeiten für NMR1:
EDGE1 = {}
#Kantenwahrscheinlichkeiten für NMR2:
EDGE2 = {}
#Kantenwahrscheinlichkeiten für NMR3:
EDGE3 = {}
#Maximum der verschiedenen Kantenwahrscheinlichkeiten:
EDGEMAX = {}
modes = ["Product", "Average"]
mode = modes[0]
#Ist es besser, die Edukte mit zu berücksichtigen? Dann hin rück gleich wahrscheinlich, aber nur 1 kann gewählt werden
#Ordnet die Kantenwahrscheinlichkeit basierend auf dem Mittel oder Produkt der Wahrscheinlichkeiten der Produkte
for edge, (tails, heads) in HYPERGRAPH.items():
if heads == [] or tails == []:
EDGE1[edge] = 0.0
EDGE2[edge] = 0.0
EDGE3[edge] = 0.0
elif mode == "Product":
#print([VERTICE1[head] for head in heads])
EDGE1[edge] = round(np.prod([VERTICE1[head] for head in heads]),2)
EDGE2[edge] = round(np.prod([VERTICE2[head] for head in heads]),2)
EDGE3[edge] = round(np.prod([VERTICE3[head] for head in heads]),2)
elif mode == "Average":
EDGE1[edge] = round(sum(VERTICE1[head] for head in heads)/len(heads),2)
EDGE2[edge] = round(sum(VERTICE2[head] for head in heads)/len(heads),2)
EDGE3[edge] = round(sum(VERTICE3[head] for head in heads)/len(heads),2)
EDGEMAX[edge] = max([EDGE1[edge], EDGE2[edge], EDGE3[edge]])
#print(EDGEMAX[edge], HYPERGRAPH[edge])
#print(EDGEMAX[4])
#print(EDGEMAX[23])
#print(EDGEMAX[42])
FIXED_FLOWS = {
#213: 3,
@@ -171,7 +107,6 @@ def build_model(name, hyperedges, elmax, el1, el2, el3, excluded_support=None):
vertices = set(v for tails, heads in hyperedges.values() for v in tails + heads)
print(vertices)
#Every item created has to be consumed:
'''
for v in vertices:
@@ -228,7 +163,7 @@ def build_model(name, hyperedges, elmax, el1, el2, el3, excluded_support=None):
#2 Butadien create first different molecule and it has to be created first:
startmolecule = ["Butadien"]
model.addConstr(quicksum(b[e_id] for e_id, (tails, _) in hyperedges.items() if list(set(tails)) == startmolecule) == 1)
#model.addConstr(quicksum(b[e_id] for e_id, (tails, _) in hyperedges.items() if list(set(tails)) == startmolecule) == 1)
#model.addConstr(b[4] + b[7] == 1)
model.addConstr(quicksum(b[e_id] for e_id, (tails, heads) in hyperedges.items() if tails == [] and heads == startmolecule)== 1)
@@ -251,8 +186,8 @@ def build_model(name, hyperedges, elmax, el1, el2, el3, excluded_support=None):
def positive_entries(variable_dict, threshold = 0.5):
return {e_id: var.X for e_id, var in variable_dict.items() if var.X > threshold}
def edgelikelihoods(variable_dict, threshold = 0.5):
return {e_id: EDGEMAX[e_id] for e_id, var in variable_dict.items() if var.X > threshold}
def edgelikelihoods(variable_dict, edgelikelihood, threshold = 0.5):
return {e_id: edgelikelihood[e_id] for e_id, var in variable_dict.items() if var.X > threshold}
def print_solution(title, flow_solution, binary_solution, edge_likelihoods, hyperedges):
print(f"\n{title}:")
@@ -269,6 +204,73 @@ def print_solution(title, flow_solution, binary_solution, edge_likelihoods, hype
print(f"Number of used hyperedges: {len(binary_solution)}")
def main():
VERTICES = ['Butadien', 'p_{0,0}', 'p_{0,1}', 'p_{0,2}', 'p_{0,3}', 'p_{0,4}', 'p_{0,5}', 'p_{0,6}', 'p_{0,7}', 'p_{0,8}', 'p_{0,9}', 'p_{0,10}', 'p_{0,11}', 'p_{0,12}', 'p_{0,13}', 'p_{0,14}', 'p_{0,15}', 'p_{0,16}', 'p_{0,17}', 'p_{0,18}']
#Vergleich mit dem NMR von Ethylen und Hexatrien
NMR1 = [0.32, 0.5, 0.87, 0.0, 0.11, 0.58, 0.06, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09, 0.0, 0.0, 0.17, 0.0, 0.09]
#Vergleich mit dem NMR von Ethylen und Octrien
NMR2 = [0.33, 0.43, 0.6, 0.0, 0.13, 0.9, 0.07, 0.01, 0.0, 0.0, 0.01, 0.0, 0.0, 0.01, 0.1, 0.0, 0.0, 0.22, 0.0, 0.13]
#Vergleich mit dem NMR von Ethylen und Benzol
NMR3 = [0.0, 0.32, 0.0, 0.0, 0.0, 0.0, 0.05, 0.0, 0.0, 0.02, 0.0, 0.29, 0.95, 0.17, 0.0, 0.0, 0.0, 0.0, 0.0, 0.05]
VERTICESSMILES = ['C=CC=C', 'C=C', 'C=CC=CC=C', 'C1CCC(C=C)CC=1', 'C(CCC(C=C)CC=C)=C', 'C=CC=CC=CC=C', 'C1CCC(C=CC=C)CC=1', 'C=CC1C=CCCC1', 'C1CCCCC=1', 'C=CC1CC=CCC1C=C', 'C1CCC(C=C)C(C=C)C=1', 'C(C1CC(C=C)C=CC1)=C', 'C1C=CC=CC=1', 'C(CCC1C=CC=CC1)=C', 'C1C(C=CC=C)CCCC=1', 'C=CC(C=C)CCCC=C', 'C=CCCCCC=C', 'C=CC1C=CC(C=C)CC1', 'C1CC2CCCCC2CC=1', 'C1CC2C=CC=CC2CC=1']
#Chosable parameters
modes = ["Product", "Average"]
mode = modes[1]
normalize = True
if normalize:
print("test")
NMR1 = [round(l/sum(NMR1), 2) for l in NMR1]
NMR2 = [round(l/sum(NMR2), 2) for l in NMR2]
NMR3 = [round(l/sum(NMR3), 2) for l in NMR3]
#Kombiniert Molekül it Wahrscheinlichkeit für NMR1:
VERTICE1 = {}
for vertice, likelihood in zip(VERTICES, NMR1):
VERTICE1[vertice] = likelihood
#Kombiniert Molekül it Wahrscheinlichkeit für NMR2:
VERTICE2 = {}
for vertice, likelihood in zip(VERTICES, NMR2):
VERTICE2[vertice] = likelihood
#Kombiniert Molekül it Wahrscheinlichkeit für NMR3:
VERTICE3 = {}
for vertice, likelihood in zip(VERTICES, NMR3):
VERTICE3[vertice] = likelihood
#Kantenwahrscheinlichkeiten für NMR1:
EDGE1 = {}
#Kantenwahrscheinlichkeiten für NMR2:
EDGE2 = {}
#Kantenwahrscheinlichkeiten für NMR3:
EDGE3 = {}
#Maximum der verschiedenen Kantenwahrscheinlichkeiten:
EDGEMAX = {}
#Ist es besser, die Edukte mit zu berücksichtigen? Dann hin rück gleich wahrscheinlich, aber nur 1 kann gewählt werden
#Ordnet die Kantenwahrscheinlichkeit basierend auf dem Mittel oder Produkt der Wahrscheinlichkeiten der Produkte
for edge, (tails, heads) in HYPERGRAPH.items():
if heads == [] or tails == []:
EDGE1[edge] = 0.0
EDGE2[edge] = 0.0
EDGE3[edge] = 0.0
elif mode == "Product":
#print([VERTICE1[head] for head in heads])
EDGE1[edge] = round(np.prod([VERTICE1[head] for head in heads]),2)
EDGE2[edge] = round(np.prod([VERTICE2[head] for head in heads]),2)
EDGE3[edge] = round(np.prod([VERTICE3[head] for head in heads]),2)
elif mode == "Average":
EDGE1[edge] = round(sum(VERTICE1[head] for head in heads)/len(heads),2)
EDGE2[edge] = round(sum(VERTICE2[head] for head in heads)/len(heads),2)
EDGE3[edge] = round(sum(VERTICE3[head] for head in heads)/len(heads),2)
EDGEMAX[edge] = max([EDGE1[edge], EDGE2[edge], EDGE3[edge]])
#print(EDGEMAX[edge], HYPERGRAPH[edge])
#print(EDGEMAX[4])
#print(EDGEMAX[23])
#print(EDGEMAX[42])
model, x, b = build_model("HypergraphFlow", HYPERGRAPH, EDGEMAX, EDGE1, EDGE2, EDGE3)
model.optimize()
if model.status != GRB.Status.OPTIMAL:
@@ -276,7 +278,7 @@ def main():
return
optimal_solution = positive_entries(x)
optimal_binary_solution = positive_entries(b)
optimal_edgelikelihoods = edgelikelihoods(b)
optimal_edgelikelihoods = edgelikelihoods(b, EDGEMAX)
print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, optimal_edgelikelihoods, HYPERGRAPH)
excluded_support = list(optimal_binary_solution.keys())
@@ -286,7 +288,7 @@ def main():
if second_model.status == GRB.Status.OPTIMAL:
second_solution = positive_entries(x2)
second_binary_solution = positive_entries(b2)
second_edgelikelihoods = edgelikelihoods(b2)
second_edgelikelihoods = edgelikelihoods(b2, EDGEMAX)
print_solution("Second Best Solution", second_solution, second_binary_solution, second_edgelikelihoods, HYPERGRAPH)
else:
print("No optimal solution found for the second best model.")