-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mcf_simplex.py
More file actions
51 lines (45 loc) · 1.22 KB
/
Copy pathtest_mcf_simplex.py
File metadata and controls
51 lines (45 loc) · 1.22 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
# Test whether pyMCFSimplex is installed correctly
from pyMCFSimplex import MCFSimplex, new_darray, darray_get
def test_mcf_simplex():
mcf = MCFSimplex()
mcf.LoadDMX(sample_dmx)
mcf.SolveMCF()
if mcf.MCFGetStatus() == 0:
cost = mcf.MCFGetFO()
assert cost == 9.0, "Optimal solution has cost 9"
solution = get_solution(mcf)
assert solution == [1.0, 1.0, 1.0, 2.0, 0.0]
else:
assert False, "No MCF solution found"
def get_solution(mcf: MCFSimplex):
"""
Retrieve solution darray and turn into Python list
"""
length = mcf.MCFm()
solution = new_darray(length)
mcf.MCFGetX(solution)
return [darray_get(solution, i) for i in range(length)]
sample_dmx = """
c
c 'c' lines are comments
c 'p' line indicates problem type, amount of nodes, and amount of arcs
c
p min 5 5
c
c 'n' lines are node ids followed by their demands
c node ids go from 1 to <number of nodes>
c missing nodes get 0 demand
c
n 1 3
n 4 -3
c
c 'a' lines describe arcs / directed edges
c The amount of arcs lines must be exactly equal to the indicated amount of arcs
c An arc consists of start node, end node, min flow, max flow, cost
c
a 1 2 0 2 3
a 2 3 0 2 1
a 3 4 0 2 1
a 1 4 0 2 2
a 1 5 0 2 5
"""