np.prod, head tail zuordnungs korrektur

This commit is contained in:
2026-09-07 12:51:05 +02:00
parent 742c32af2b
commit 03055f9946
+41 -28
View File
@@ -1,4 +1,4 @@
import math
import numpy as np
import gurobipy as gp
from gurobipy import GRB, Model, quicksum
@@ -91,15 +91,15 @@ VERTICES = ['Butadien', 'p_{0,0}', 'p_{0,1}', 'p_{0,2}', 'p_{0,3}', 'p_{0,4}', '
#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]
#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]
#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]
#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)]
@@ -127,22 +127,30 @@ EDGEMAX = {}
modes = ["Product", "Average"]
mode = modes[0]
#print(round(np.prod([1, 0.9])))
#Ordnet die Kantenwahrscheinlichkeit basierend auf dem Mittel oder Produkt der Wahrscheinlichkeiten der Produkte
for edge, (heads, tails) in HYPERGRAPH.items():
if heads != [] and tails != [] and mode == "Product":
#Produkt klappt nur, wenn normalisierte Likelihoods
EDGE1[edge] = math.prod(VERTICE1[head] for head in heads)
EDGE2[edge] = math.prod(VERTICE2[head] for head in heads)
EDGE2[edge] = math.prod(VERTICE2[head] for head in heads)
if heads != [] and tails != [] and mode == "Average":
EDGE1[edge] = quicksum(VERTICE1[head] for head in heads)/len(heads)
EDGE2[edge] = quicksum(VERTICE2[head] for head in heads)/len(heads)
EDGE3[edge] = quicksum(VERTICE3[head] for head in heads)/len(heads)
else:
for edge, (tails, heads) in HYPERGRAPH.items():
if heads == [] or tails == []:
EDGE1[edge] = 0.0
EDGE2[edge] = 0.0
EDGE3[edge] = 0.0
EDGEMAX[edge] = max([EDGE1[edge], EDGE2[edge], EDGE2[edge]])
elif mode == "Product":
#Produkt klappt nur, wenn normalisierte Likelihoods
#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":
print(tails)
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[42])
FIXED_FLOWS = {
@@ -180,8 +188,8 @@ def build_model(name, hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelike
count[v] = 0
count = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, name = "count")
for mol in vertices:
for edge, (heads, tails) in hyperedges.items():
count[mol] += x[edge] * (heads.count(mol) - tails.count(mol))
for edge, (tails, heads) in hyperedges.items():
count[mol] += x[edge] * (tails.count(mol) - heads.count(mol))
model.addConstr(count[mol] == 0)
'''
@@ -209,7 +217,7 @@ def build_model(name, hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelike
#Multiply edgelikelihood with the edge use boolean
#Adapt to have
model.setObjectiveN(
quicksum(enmax[e_id] * b[e_id] for e_id, (_, _) in hyperedges.items()),
quicksum(enmax[e_id] * b[e_id] for e_id in hyperedges),
index = 0,
priority = 2,
name = "maximize_nmr_similarity",
@@ -223,20 +231,20 @@ def build_model(name, hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelike
)
#Excluding creation and destruction only three reactions for three nmr
model.addConstr(quicksum(b[e_id] for e_id, (heads, tails) in hyperedges.items() if tails != [] and heads != []) == 3)
model.addConstr(quicksum(b[e_id] for e_id, (tails, heads) in hyperedges.items() if tails != [] and heads != []) == 3)
#Restrict number of used edges to prevent using all available
#model.addConstr(quicksum(x[e_id] for e_id in hyperedges) <= 16)
#2 Butadien create first different molecule and it has to be created first:
model.addConstr(b[4] + b[7] == 1)
model.addConstr(b[213] == 1)
model.addConstr(b[41] == 1)
#model.addConstr(b[41] == 1)
#No cyclic reaction pairs:
for e_id1, (heads1, tails1) in hyperedges.items():
for e_id2, (heads2, tails2) in hyperedges.items():
for e_id1, (tails1, heads1) in hyperedges.items():
for e_id2, (tails2, heads2) in hyperedges.items():
if (heads1, tails1) == (tails2, heads2):
#print(e_id1, e_id2)
model.addConstr((b[e_id1] + b[e_id2]) <= 1)
@@ -247,12 +255,16 @@ def build_model(name, hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelike
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 print_solution(title, flow_solution, binary_solution, hyperedges):
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 print_solution(title, flow_solution, binary_solution, edge_likelihoods, hyperedges):
print(f"\n{title}:")
for e_id in sorted(flow_solution):
flow = flow_solution[e_id]
tails, heads = hyperedges[e_id]
print(f"Hyperedge {e_id}: Flow = {flow}, Tails = {tails}, Heads = {heads}")
likelihood = edge_likelihoods[e_id]
print(f"Hyperedge {e_id}: Flow = {flow}, Tails = {tails}, Heads = {heads}, With likelihood = {likelihood}")
print("\nBinary Variables:")
for e_id in sorted(binary_solution):
print(f"Binary Variable b_{e_id} = {binary_solution[e_id]}")
@@ -268,7 +280,8 @@ def main():
return
optimal_solution = positive_entries(x)
optimal_binary_solution = positive_entries(b)
print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, HYPERGRAPH)
optimal_edgelikelihoods = edgelikelihoods(b)
print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, optimal_edgelikelihoods, HYPERGRAPH)
excluded_support = list(optimal_binary_solution.keys())
second_model, x2, b2 = build_model("SecondBestHypergraphFlow", HYPERGRAPH, EDGEMAX, EDGE1, EDGE2, EDGE3, excluded_support=excluded_support,)
@@ -277,9 +290,9 @@ def main():
if second_model.status == GRB.Status.OPTIMAL:
second_solution = positive_entries(x2)
second_binary_solution = positive_entries(b2)
print_solution("Second Best Solution", second_solution, second_binary_solution, HYPERGRAPH)
second_edgelikelihoods = edgelikelihoods(b2)
print_solution("Second Best Solution", second_solution, second_binary_solution, second_edgelikelihoods, HYPERGRAPH)
else:
print("No optimal solution found for the second best model.")
if __name__ == "__main__":
main()