Dateien nach "ILP/butadien" hochladen

This commit is contained in:
2026-09-03 15:24:01 +02:00
parent 9cbda49942
commit 764fb24e5f
+55 -16
View File
@@ -1,3 +1,4 @@
import math
import gurobipy as gp
from gurobipy import GRB, Model, quicksum
@@ -65,6 +66,27 @@ HYPERGRAPH = {
211: (['Butadien', 'p_{0,11}'], ['p_{0,18}']),
213: ([], ['Butadien']),
}
HYPERGRAPH2 = {
2: (['p_{0,0}'], []),
3: (['p_{0,1}'], []),
4: (['Butadien', 'Butadien'], ['p_{0,0}', 'p_{0,1}']),
6: (['p_{0,2}'], []),
7: (['Butadien', 'Butadien'], ['p_{0,2}']),
17: (['p_{0,4}'], []),
23: (['Butadien', 'p_{0,1}'], ['p_{0,0}', 'p_{0,4}']),
41: (['p_{0,11}'], []),
42: (['p_{0,4}'], ['p_{0,0}', 'p_{0,11}']),
213: ([], ['Butadien']),
}
HYPERGRAPH3 = {
0: (['Butadien'], []),
213: ([], ['Butadien']),
4: (['Butadien', 'Butadien'], ['p_{0,0}', 'Butadien']),
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]
@@ -75,8 +97,11 @@ 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.
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)]
#Implement Edgelikelihood before Model
FIXED_FLOWS = {
213: 3,
#213: 3,
#2: 3,
#4: 1,
#23: 1,
@@ -89,6 +114,7 @@ def build_model(name, hyperedges, vertices, nmrlikelihoodsmax, nmrlikelihoods1,
x = {e_id: model.addVar(vtype=GRB.INTEGER, lb = 0, name = f"x_{e_id}") for e_id in hyperedges}
b = {e_id: model.addVar(vtype=GRB.BINARY, name = f"b_{e_id}") for e_id in hyperedges}
count = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, name = "count")
nmax = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "nmrmax")
n1 = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "nmr1")
n2 = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "nmr2")
@@ -104,10 +130,16 @@ def build_model(name, hyperedges, vertices, nmrlikelihoodsmax, nmrlikelihoods1,
n1[v] = nmr1
n2[v] = nmr2
n3[v] = nmr3
count[v] = 0
#Assigns the edges a likelihood based on the products
for e, (tails, heads) in hyperedges.items():
if heads != [] and tails != []:
en1[e] = math.prod(n1[head] for head in heads)
en2[e] = math.prod(n2[head] for head in heads)
en3[e] = math.prod(n3[head] for head in heads)
enmax[e] = max([en1[e], en2[e], en3[e]])
'''
if heads != [] and tails != []: #Multiplication better?
enmax[e] = quicksum(nmax[head] for head in heads)/len(heads)
en1[e] = quicksum(n1[head] for head in heads)/len(heads)
en2[e] = quicksum(n2[head] for head in heads)/len(heads)
@@ -117,15 +149,15 @@ def build_model(name, hyperedges, vertices, nmrlikelihoodsmax, nmrlikelihoods1,
en1[e] = 0.0
en2[e] = 0.0
en3[e] = 0.0
print(enmax[123])
print(enmax[42])
'''
#print(enmax[123])
#print(enmax[42])
vertices = set(v for tails, heads in hyperedges.values() for v in tails + heads)
for v in vertices:
inflow = quicksum(x[e_id] for e_id, (_, heads) in hyperedges.items() if v in heads)
outflow = quicksum(x[e_id] for e_id, (tails, _) in hyperedges.items() if v in tails)
inflow = quicksum(x[e_id] * heads.count(v) for e_id, (_, heads) in hyperedges.items() if v in heads)
outflow = quicksum(x[e_id] * tails.count(v) for e_id, (tails, _) in hyperedges.items() if v in tails)
model.addConstr(inflow == outflow, name = f"flow_conservation_{v}")
for e_id, value in FIXED_FLOWS.items():
@@ -144,33 +176,40 @@ def build_model(name, hyperedges, vertices, nmrlikelihoodsmax, nmrlikelihoods1,
#Multiplizier den node Wert mit infow + outflow
model.ModelSense = GRB.MAXIMIZE
#Multiply node value with infow or outflow
#Multiply edgelikelihood with the edge use boolean
model.setObjectiveN(
quicksum(enmax[e_id] * x[e_id] for e_id, (_, _) in hyperedges.items()),
quicksum(enmax[e_id] * b[e_id] for e_id, (_, _) in hyperedges.items()),
index = 0,
priority = 2,
name = "maximize_nmr_similarity",
)
#Minimize the overall flow
model.setObjectiveN(
quicksum(x[e_id] for e_id in hyperedges),
quicksum(-1 * x[e_id] for e_id in hyperedges),
index=1,
priority= - 1,
priority= 1,
name="minimize_used_hyperedges",
)
#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)
#Restrict number of used edges to prevent using all available
#model.addConstr(quicksum(x[e_id] for e_id in hyperedges) <= 10)
#model.addConstr(quicksum(x[e_id] for e_id in hyperedges) <= 16)
#2 Butadien create first different molecule:
#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)
#Every item created has to be consumed:
for mol in vertices:
for edge, (heads, tails) in hyperedges.items():
count[mol] += x[edge] * (heads.count(mol) - tails.count(mol))
#model.addConstr(count[mol] == 0)
#No cyclic reaction pairs:
for e_id1, (heads1, tails1) in hyperedges.items():
for e_id2, (heads2, tails2) in hyperedges.items():
if (heads1, tails1) == (tails2, heads2):