-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmin_path_error.py
More file actions
51 lines (43 loc) · 1.92 KB
/
min_path_error.py
File metadata and controls
51 lines (43 loc) · 1.92 KB
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
47
48
49
50
51
import flowpaths as fp
import networkx as nx
def main():
# Create a simple graph
graph = nx.DiGraph()
graph.graph["id"] = "simple_graph"
graph.add_edge("0", "a", flow=6, length=2)
graph.add_edge("0", "b", flow=7, length=4)
graph.add_edge("a", "b", flow=2, length=6)
graph.add_edge("a", "c", flow=5, length=3)
graph.add_edge("b", "c", flow=9, length=1)
graph.add_edge("c", "d", flow=6, length=8)
graph.add_edge("c", "1", flow=7, length=9)
graph.add_edge("d", "1", flow=6, length=4)
# We create a Minimum Path Error solver with default settings,
# by specifying that the flow value of each edge is in the attribute `flow` of the edges,
# and that the number of paths to consider is 3.
mpe_model = fp.kMinPathError(graph, flow_attr="flow", k=3, weight_type=float)
# We solve it
mpe_model.solve()
# We process its solution
process_solution(mpe_model)
edges_to_ignore = [("a", "c")]
# We solve again, by telling the model to ignore the edges in `edges_to_ignore`
# when computing the path slacks (i.e. edge errors)
mpe_model_2 = fp.kMinPathError(graph, flow_attr="flow", k=3, weight_type=int, elements_to_ignore=edges_to_ignore, length_attr="length")
mpe_model_2.solve()
process_solution(mpe_model_2)
edges_to_ignore = [("a", "c")]
# We solve again, by telling the model to ignore the edges in `edges_to_ignore`
# when computing the path slacks (i.e. edge errors)
mpe_model_3 = fp.kMinPathError(graph, flow_attr="flow", k=3, weight_type=int, elements_to_ignore=edges_to_ignore)
mpe_model_3.solve()
process_solution(mpe_model_3)
def process_solution(model: fp.kMinPathError):
if model.is_solved():
print(model.get_solution())
print(model.solve_statistics)
print("model.is_valid_solution()", model.is_valid_solution())
else:
print("Model could not be solved.")
if __name__ == "__main__":
main()