Dateien nach "ILP" hochladen

This commit is contained in:
kilian 2026-05-20 12:41:01 +02:00
parent 2841edefc2
commit 8549749ccf

View File

@ -2,31 +2,31 @@ import gurobipy as gp
from gurobipy import GRB, Model, quicksum from gurobipy import GRB, Model, quicksum
HYPEREDGES = { HYPEREDGES = {
1: ([], ["Xanthine"]) 1: ([], ['Xanthine']),
2: (["Xanthine"], ["p_{0,0}"]) 2: (['Xanthine'], ['p_{0,0}']),
3: (["Xanthine"], ["p_{0,1}"]) 3: (['Xanthine'], ['p_{0,1}']),
4: (["Xanthine"], ["p_{0,2}"]) 4: (['Xanthine'], ['p_{0,2}']),
5: (["p_{0,0}"], ["p_{0,3}"]) 5: (['p_{0,0}'], ['p_{0,3}']),
6: (["p_{0,0}"], ["p_{0,4}"]) 6: (['p_{0,0}'], ['p_{0,4}']),
7: (["p_{0,1}"], ["p_{0,3}"]) 7: (['p_{0,1}'], ['p_{0,3}']),
8: (["p_{0,1}"], ["p_{0,5}"]) 8: (['p_{0,1}'], ['p_{0,5}']),
9: (["p_{0,2}"], ["p_{0,4}"]) 9: (['p_{0,2}'], ['p_{0,4}']),
10: (["p_{0,2}"], ["p_{0,5}"]) 10: (['p_{0,2}'], ['p_{0,5}']),
11: (["p_{0,3}"], ["Caffeine"]) 11: (['p_{0,3}'], ['Caffeine']),
12: (["p_{0,4}"], ["Caffeine"]) 12: (['p_{0,4}'], ['Caffeine']),
13: (["p_{0,5}"], ["Caffeine"]) 13: (['p_{0,5}'], ['Caffeine']),
14: (["Caffeine"], []) 14: (['Caffeine'], [])
} }
#Similyrity of Nodes NMR to Measured NMR #Similyrity of Nodes NMR to Measured NMR
VERTICES = { VERTICES = {
1: ([0, 0], ["Xanthine"]) 1: ([0, 0], ['Xanthine']),
2: ([0, 0], ["p_{0,0}"]) 2: ([0, 0], ['p_{0,0}']),
3: ([0, 0], ["p_{0,1}"]) 3: ([0, 0], ['p_{0,1}']),
4: ([1, 0], ["p_{0,2}"]) 4: ([1, 0], ['p_{0,2}']),
5: ([0, 0], ["p_{0,3}"]) 5: ([0, 0], ['p_{0,3}']),
6: ([0, 0], ["p_{0,4}"]) 6: ([0, 0], ['p_{0,4}']),
7: ([0, 1], ["p_{0,5}"]) 7: ([0, 1], ['p_{0,5}']),
8: ([0, 0], ["Caffeine"]) 8: ([0, 0], ['Caffeine']),
} }
FIXED_FLOWS = { FIXED_FLOWS = {
1: 1, 1: 1,
@ -50,9 +50,8 @@ def build_model(name, hyperedges, vertices, excluded_support=None):
model.addConstr(x[e_id] == value, name=f"fixed_flow_{e_id}") model.addConstr(x[e_id] == value, name=f"fixed_flow_{e_id}")
for e_id in hyperedges: for e_id in hyperedges:
model.addGenConstrIndicator(b[e_id], 0, x[e_id] == 0, name=f"unused_implies_zero_{e_id}")
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}")
model.addConstr(x[e_id] >= b[e_id], name=f"used_implies_positive_flow_{e_id}")
if excluded_support: if excluded_support:
model.addConstr(quicksum(b[e_id] for e_id in excluded_support) <= len(excluded_support) - 1, name="different_hyperedges",) model.addConstr(quicksum(b[e_id] for e_id in excluded_support) <= len(excluded_support) - 1, name="different_hyperedges",)
@ -77,7 +76,7 @@ def print_solution(title, flow_solution, binary_solution, hyperedges):
print(f"Number of used hyperedges: {len(binary_solution)}") print(f"Number of used hyperedges: {len(binary_solution)}")
def main(): def main():
model, x, b = build_model("HypergraphFlow", HYPEREDGES, NODES) model, x, b = build_model("HypergraphFlow", HYPEREDGES, VERTICES)
model.optimize() model.optimize()
if model.status != GRB.Status.OPTIMAL: if model.status != GRB.Status.OPTIMAL:
print("No optimal solution found for the first model.") print("No optimal solution found for the first model.")