Dateien nach "ILP" hochladen

This commit is contained in:
kilian 2026-05-21 14:07:09 +02:00
parent 8549749ccf
commit 670d59e18f

View File

@ -15,10 +15,14 @@ HYPEREDGES = {
11: (['p_{0,3}'], ['Caffeine']),
12: (['p_{0,4}'], ['Caffeine']),
13: (['p_{0,5}'], ['Caffeine']),
14: (['Caffeine'], [])
14: (['Caffeine'], []),
}
#Similyrity of Nodes NMR to Measured NMR
VERTICES = {
#Can how to add Values along a Path?
#Can you count position on path?
#Can you save spectra values to add hypergraphposition to spectrainformation?
#Compositespectra vs oredered set of spectra
VERTICES1 = {
1: ([0, 0], ['Xanthine']),
2: ([0, 0], ['p_{0,0}']),
3: ([0, 0], ['p_{0,1}']),
@ -28,16 +32,37 @@ VERTICES = {
7: ([0, 1], ['p_{0,5}']),
8: ([0, 0], ['Caffeine']),
}
VERTICES2 = {
1: ([0], ['Xanthine']),
2: ([0], ['p_{0,0}']),
3: ([0], ['p_{0,1}']),
4: ([1], ['p_{0,2}']),
5: ([0], ['p_{0,3}']),
6: ([0], ['p_{0,4}']),
7: ([1], ['p_{0,5}']),
8: ([0], ['Caffeine']),
}
VERTICES = ['Xanthine', 'p_{0,0}', 'p_{0,1}', 'p_{0,2}', 'p_{0,3}', 'p_{0,4}', 'p_{0,5}', 'Caffeine']
NMRLIKELYHOODS = [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
FIXED_FLOWS = {
1: 1,
14: 1,
}
def build_model(name, hyperedges, vertices, excluded_support=None):
def build_model(name, hyperedges, vertices, nmrlikelihoods, 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
print(f'Vertice: {v}, Similarity: {n[v]}')
vertices = set(v for tails, heads in hyperedges.values() for v in tails + heads)
@ -53,10 +78,15 @@ def build_model(name, hyperedges, vertices, excluded_support=None):
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 = {}
#for v_id in vertices:
#model.addConstr()
if excluded_support:
model.addConstr(quicksum(b[e_id] for e_id in excluded_support) <= len(excluded_support) - 1, name="different_hyperedges",)
model.setObjective(quicksum(quicksum(x[v_id]) for v_id in vertices), GRB.MAXIMIZE) #Maximize Similarity of Nodes
model.setObjective(quicksum(n[v_id] for v_id in vertices), GRB.MINIMIZE) #Maximize Similarity of Nodes
return model, x, b
def positive_entries(variable_dict, threshold=0.5):
@ -76,7 +106,7 @@ 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)
model, x, b = build_model("HypergraphFlow", HYPEREDGES, VERTICES, NMRLIKELYHOODS)
model.optimize()
if model.status != GRB.Status.OPTIMAL:
print("No optimal solution found for the first model.")
@ -85,7 +115,7 @@ def main():
optimal_binary_solution = positive_entries(b)
print_solution("Optimal Solution", optimal_solution, optimal_binary_solution, HYPEREDGES)
excluded_support = list(optimal_binary_solution.keys())
""" excluded_support = list(optimal_binary_solution.keys())
second_model, x2, b2 = build_model("SecondBestHypergraphFlow", HYPEREDGES, excluded_support=excluded_support,)
second_model.optimize()
@ -95,6 +125,6 @@ def main():
print_solution("Second Best Solution", second_solution, second_binary_solution, HYPEREDGES, VERTICES)
else:
print("No optimal solution found for the second best model.")
"""
if __name__ == "__main__":
main()