From c91b1b0ca91a9e9fd5067e3671363420bd8feb8a Mon Sep 17 00:00:00 2001 From: kilian Date: Wed, 16 Sep 2026 10:20:34 +0200 Subject: [PATCH] Dateien nach "ILP/Kaffee" hochladen --- ILP/Kaffee/caffeinesynthesis.py | 109 +++++++++++++++++++------------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/ILP/Kaffee/caffeinesynthesis.py b/ILP/Kaffee/caffeinesynthesis.py index 40831bb..c9e70d9 100644 --- a/ILP/Kaffee/caffeinesynthesis.py +++ b/ILP/Kaffee/caffeinesynthesis.py @@ -1,7 +1,8 @@ +import numpy as np import gurobipy as gp from gurobipy import GRB, Model, quicksum -HYPEREDGES = { +HYPERGRAPH = { 1: ([], ['Xanthine']), 2: (['Xanthine'], ['1-Methylxanthine']), 3: (['Xanthine'], ['3-Methylxanthine']), @@ -18,38 +19,22 @@ HYPEREDGES = { 14: (['Caffeine'], []), } -VERTICES = ['Xanthine', '1-Methylxanthine', '3-Methylxanthine', '7-Methylxanthine', 'Theophylline', 'Paraxanthine', 'Theobromine', 'Caffeine'] - -#Change to only have one likelihood, where with manual order association -NMRLIKELYHOODS = [0.0, 0.49, 0.43, 0.6, 0.5, 0.51, 0.67, 0.0] -#Results for comparison with 7-Methylxanthine bei shift von 2.5 -NMRLIKELYHOODS1 = [0.46, 0.49, 0.43, 0.6, 0.49, 0.6, 0.45, 0.51] -#Results for comparison with 3,7-Methylxanthine bei shift von 2.5 -NMRLIKELYHOODS2 = [0.45, 0.52, 0.52, 0.63, 0.52, 0.57, 0.59, 0.59] -#Results for comparison with 3,7-Methylxanthine with Methyl C of spectrum 2 bei shift von 2.5 -NMRLIKELYHOODS3 = [0.48, 0.5, 0.51, 0.59, 0.5, 0.51, 0.67, 0.61] - FIXED_FLOWS = { - 1: 1, - 14: 1, + #1: 1, + #14: 1, } -def build_model(name, hyperedges, vertices, nmrlikelihoods, excluded_support=None): +def build_model(name, hyperedges, vertices, ele, el1, el2, 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} - n = model.addVars(vertices, vtype=GRB.CONTINUOUS, lb = 0.0, ub = 1.0, name = "nmr") - - - for v, nmr in zip(vertices, nmrlikelihoods): - n[v] = nmr 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()) + 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(): @@ -64,25 +49,11 @@ def build_model(name, hyperedges, vertices, nmrlikelihoods, excluded_support=Non 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 + #Excluding creation and destruction only three reactions for three nmr + model.addConstr(quicksum(b[e_id] for e_id, (tails, heads) in hyperedges.items() if tails != [] and heads != []) == 3) - #Multiply node value with infow or outflow - model.setObjectiveN( - quicksum(n[t_id[0]] * x[e_id] for e_id, (_, t_id) in hyperedges.items() if t_id != []), - index = 0, - priority = 2, - name = "maximize_nmr_similarity", - ) - - model.setObjectiveN( - quicksum(-1 * x[e_id] for e_id in hyperedges), - index=1, - priority=1, - name="minimize_used_hyperedges", - ) + model.setObjective(quicksum(1000 * ele[e_id] * b[e_id] - x[e_id] for e_id in hyperedges),GRB.MAXIMIZE) return model, x, b @@ -103,25 +74,75 @@ def print_solution(title, flow_solution, binary_solution, hyperedges): print(f"Number of used hyperedges: {len(binary_solution)}") def main(): - model, x, b = build_model("HypergraphFlow", HYPEREDGES, VERTICES, NMRLIKELYHOODS) + VERTICES = ['Xanthine', '1-Methylxanthine', '3-Methylxanthine', '7-Methylxanthine', 'Theophylline', 'Paraxanthine', 'Theobromine', 'Caffeine'] + + #Results for comparison with 7-Methylxanthine bei shift von 2.5 + NMR1 = [0.46, 0.49, 0.43, 0.6, 0.49, 0.6, 0.45, 0.51] + #Results for comparison with 3,7-Methylxanthine bei shift von 2.5 + NMR2 = [0.45, 0.52, 0.52, 0.63, 0.52, 0.57, 0.59, 0.59] + #Change to only have one likelihood, where with manual order association + NMRE = [0.0, 0.49, 0.43, 0.6, 0.5, 0.51, 0.67, 0.0] + + #Chosable parameters + modes = ["Product", "Average"] + mode = modes[0] + normalize = False + + #Kombiniert Molekül mit Wahrscheinlichkeit für NMR1: + VERTICE1 = {} + for vertice, likelihood in zip(VERTICES, NMR1): + VERTICE1[vertice] = likelihood + #Kombiniert Molekül mit Wahrscheinlichkeit für NMR2: + VERTICE2 = {} + for vertice, likelihood in zip(VERTICES, NMR2): + VERTICE2[vertice] = likelihood + #Kombiniert Molekül mit Wahrscheinlichkeit für NMR Empiric: + VERTICEE = {} + for vertice, likelihood in zip(VERTICES, NMRE): + VERTICEE[vertice] = likelihood + + #Kantenwahrscheinlichkeiten für NMR1: + EDGE1 = {} + #Kantenwahrscheinlichkeiten für NMR2: + EDGE2 = {} + #Maximum der verschiedenen Kantenwahrscheinlichkeiten: + EDGEE = {} + + for edge, (tails, heads) in HYPERGRAPH.items(): + if heads == [] or tails == []: + EDGE1[edge] = 0.0 + EDGE2[edge] = 0.0 + EDGEE[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) + EDGEE[edge] = round(np.prod([VERTICEE[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) + EDGEE[edge] = round(sum(VERTICEE[head] for head in heads)/len(heads),2) + + + model, x, b = build_model("HypergraphFlow", HYPERGRAPH, VERTICES, EDGEE, EDGE1, EDGE2) 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, HYPEREDGES) + print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, HYPERGRAPH) """ excluded_support = list(optimal_binary_solution.keys()) - second_model, x2, b2 = build_model("SecondBestHypergraphFlow", HYPEREDGES, excluded_support=excluded_support,) + second_model, x2, b2 = build_model("SecondBestHypergraphFlow", HYPERGRAPH, 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, HYPEREDGES, VERTICES) + print_solution("Second Best Solution", second_solution, second_binary_solution, HYPERGRAPH, VERTICES) else: print("No optimal solution found for the second best model.") - """ + """ if __name__ == "__main__": main() \ No newline at end of file