import math import gurobipy as gp from gurobipy import GRB, Model, quicksum HYPERGRAPH = { 0: (['Butadien'], []), 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}']), 10: (['p_{0,0}', 'p_{0,1}'], ['Butadien', 'Butadien']), 12: (['p_{0,3}'], []), 13: (['p_{0,0}', 'p_{0,2}'], ['p_{0,3}']), 17: (['p_{0,4}'], []), 18: (['p_{0,1}', 'p_{0,1}'], ['Butadien', 'p_{0,4}']), 19: (['p_{0,5}'], []), 20: (['p_{0,1}', 'p_{0,2}'], ['Butadien', 'p_{0,5}']), 23: (['Butadien', 'p_{0,1}'], ['p_{0,0}', 'p_{0,4}']), 26: (['Butadien', 'p_{0,2}'], ['p_{0,0}', 'p_{0,5}']), 27: (['p_{0,6}'], []), 28: (['p_{0,0}', 'p_{0,1}'], ['p_{0,6}']), 29: (['p_{0,7}'], []), 30: (['Butadien', 'p_{0,0}'], ['p_{0,7}']), 31: (['p_{0,8}'], []), 32: (['Butadien', 'p_{0,1}'], ['p_{0,8}']), 33: (['Butadien', 'p_{0,1}'], ['p_{0,5}']), 34: (['p_{0,9}'], []), 35: (['Butadien', 'p_{0,1}'], ['p_{0,9}']), 36: (['p_{0,10}'], []), 37: (['Butadien', 'p_{0,1}'], ['p_{0,10}']), 38: (['p_{0,3}'], ['p_{0,0}', 'p_{0,2}']), 41: (['p_{0,11}'], []), 42: (['p_{0,4}'], ['p_{0,0}', 'p_{0,11}']), 43: (['p_{0,12}'], []), 44: (['p_{0,5}'], ['p_{0,12}']), 63: (['p_{0,13}'], []), 64: (['p_{0,4}', 'p_{0,6}'], ['p_{0,1}', 'p_{0,13}']), 65: (['p_{0,2}', 'p_{0,4}'], ['p_{0,1}', 'p_{0,5}']), 66: (['p_{0,0}', 'p_{0,4}'], ['Butadien', 'p_{0,1}']), 67: (['Butadien', 'p_{0,4}'], ['p_{0,1}', 'p_{0,1}']), 77: (['p_{0,5}', 'p_{0,6}'], ['p_{0,2}', 'p_{0,13}']), 79: (['p_{0,0}', 'p_{0,5}'], ['Butadien', 'p_{0,2}']), 80: (['Butadien', 'p_{0,5}'], ['p_{0,1}', 'p_{0,2}']), 83: (['p_{0,1}', 'p_{0,5}'], ['p_{0,2}', 'p_{0,4}']), 96: (['Butadien', 'p_{0,6}'], ['p_{0,0}', 'p_{0,13}']), 97: (['p_{0,1}', 'p_{0,6}'], ['Butadien', 'p_{0,13}']), 99: (['p_{0,14}'], []), 100: (['p_{0,0}', 'p_{0,6}'], ['p_{0,14}']), 101: (['p_{0,15}'], []), 102: (['p_{0,0}', 'p_{0,7}'], ['p_{0,15}']), 121: (['p_{0,0}', 'p_{0,4}'], ['p_{0,13}']), 122: (['p_{0,16}'], []), 123: (['p_{0,0}', 'p_{0,4}'], ['p_{0,16}']), 124: (['p_{0,17}'], []), 125: (['Butadien', 'p_{0,7}'], ['p_{0,17}']), 127: (['p_{0,12}'], ['p_{0,5}']), 130: (['p_{0,14}'], ['p_{0,0}', 'p_{0,6}']), 132: (['p_{0,15}'], ['p_{0,0}', 'p_{0,7}']), 135: (['p_{0,0}', 'p_{0,11}'], ['p_{0,4}']), 153: (['Butadien', 'p_{0,13}'], ['p_{0,1}', 'p_{0,6}']), 155: (['p_{0,0}', 'p_{0,13}'], ['Butadien', 'p_{0,6}']), 157: (['p_{0,1}', 'p_{0,13}'], ['p_{0,4}', 'p_{0,6}']), 158: (['p_{0,2}', 'p_{0,13}'], ['p_{0,5}', 'p_{0,6}']), 210: (['p_{0,18}'], []), 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] #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] #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: EDGE1[edge] = 0.0 EDGE2[edge] = 0.0 EDGE3[edge] = 0.0 EDGEMAX[edge] = max([EDGE1[edge], EDGE2[edge], EDGE2[edge]]) FIXED_FLOWS = { #213: 3, #2: 3, #4: 1, #23: 1, #41: 1, #42: 1, } def build_model(name, hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelikelihoods2, edgelikelihoods3, excluded_support=None): model = Model(name) 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} enmax = model.addVars(hyperedges, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "edgenmrmax") en1 = model.addVars(hyperedges, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "edgenmr1") en2 = model.addVars(hyperedges, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "edgenmr2") en3 = model.addVars(hyperedges, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "edgenmr3") for edge, elmax, el1, el2, el3 in zip(hyperedges, edgelikelihoodsmax, edgelikelihoods1, edgelikelihoods2, edgelikelihoods3): enmax[edge] = elmax en1[edge] = el1 en2[edge] = el2 en3[edge] = el3 vertices = set(v for tails, heads in hyperedges.values() for v in tails + heads) #Every item created has to be consumed: ''' for v in vertices: 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)) model.addConstr(count[mol] == 0) ''' for v in vertices: inflow = quicksum(x[e_id] * heads.count(v) for e_id, (_, heads) in hyperedges.items()) outflow = quicksum(x[e_id] * tails.count(v) for e_id, (tails, _) in hyperedges.items()) model.addConstr(inflow == outflow, name = f"flow_conservation_{v}") for e_id, value in FIXED_FLOWS.items(): model.addConstr(x[e_id] == value, name = f"fixed_flow_{e_id}") for e_id in hyperedges: model.addGenConstrIndicator(b[e_id], 0, x[e_id] == 0, name = f"unused_implies_zero_{e_id}") model.addConstr(x[e_id] >= b[e_id], name = f"used_implies_positive_flow_{e_id}") reaction_path = {} if excluded_support: model.addConstr(quicksum(b[e_id] for e_id in excluded_support) <= len(excluded_support) - 1, name = "different_hyperedges",) #Multiplizier den node Wert mit infow + outflow model.ModelSense = GRB.MAXIMIZE #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()), index = 0, priority = 2, name = "maximize_nmr_similarity", ) #Minimize the overall flow model.setObjectiveN( quicksum(-1 * x[e_id] for e_id in hyperedges), index=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) <= 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) #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): #print(e_id1, e_id2) model.addConstr((b[e_id1] + b[e_id2]) <= 1) return model, x, b 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): 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}") print("\nBinary Variables:") for e_id in sorted(binary_solution): print(f"Binary Variable b_{e_id} = {binary_solution[e_id]}") print(f"\nTotal flow: {sum(flow_solution.values())}") print(f"Number of used hyperedges: {len(binary_solution)}") def main(): model, x, b = build_model("HypergraphFlow", HYPERGRAPH, EDGEMAX, EDGE1, EDGE2, EDGE3) model.optimize() if model.status != GRB.Status.OPTIMAL: print("No optimal solution found for the first model.") return optimal_solution = positive_entries(x) optimal_binary_solution = positive_entries(b) print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, HYPERGRAPH) excluded_support = list(optimal_binary_solution.keys()) second_model, x2, b2 = build_model("SecondBestHypergraphFlow", HYPERGRAPH, EDGEMAX, EDGE1, EDGE2, EDGE3, excluded_support=excluded_support,) second_model.optimize() 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) else: print("No optimal solution found for the second best model.") if __name__ == "__main__": main()