-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmin_error_flow_cycles.py
47 lines (40 loc) · 1.76 KB
/
min_error_flow_cycles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import flowpaths as fp
import networkx as nx
def process_solution(graph: nx.DiGraph, model: fp.MinErrorFlow):
if model.is_solved():
solution = model.get_solution()
print(solution["graph"].edges(data=True))
print("error", solution["error"])
print("objective_value", solution["objective_value"])
fp.utils.draw_solution(graph, filename=f"uncorrected_graph_{id(model.get_corrected_graph())}.pdf", flow_attr="flow", draw_options={"show_edge_weights": True})
fp.utils.draw_solution(solution["graph"], filename=f"corrected_graph_{id(model.get_corrected_graph())}.pdf", flow_attr="flow", draw_options={"show_edge_weights": True})
else:
print("Model could not be solved.")
graph = nx.DiGraph()
graph.add_edge("s", "a", flow=7)
graph.add_edge("s", "b", flow=7)
graph.add_edge("a", "b", flow=2)
graph.add_edge("a", "c", flow=4)
graph.add_edge("b", "c", flow=9)
graph.add_edge("c", "d", flow=7)
graph.add_edge("c", "t", flow=7)
graph.add_edge("d", "t", flow=6)
graph.add_edge("t", "s", flow=15)
# We create a the Minimum Error Flow solver with default settings
correction_model = fp.MinErrorFlow(graph, flow_attr="flow")
correction_model.solve()
if correction_model.is_solved():
corrected_graph = correction_model.get_corrected_graph()
process_solution(graph, correction_model)
# We create a the Minimum Error Flow solver, by also requiring that
# the number of different flow values used in the correction is small
# (parameter different_flow_values_epsilon)
correction_model_diff_flow_values = fp.MinErrorFlow(
graph,
flow_attr="flow",
weight_type=int,
few_flow_values_epsilon = 0.25,
)
correction_model_diff_flow_values.solve()
# We process its solution
process_solution(graph, correction_model_diff_flow_values)