1 | #include <iostream> |
---|
2 | //#include "test_tools.h" |
---|
3 | #include <lemon/list_graph.h> |
---|
4 | #include <mincostflow.h> |
---|
5 | //#include <path.h> |
---|
6 | //#include <maps.h> |
---|
7 | |
---|
8 | using namespace std; |
---|
9 | using namespace lemon; |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | bool passed = true; |
---|
14 | |
---|
15 | void check(bool rc, char *msg="") { |
---|
16 | passed = passed && rc; |
---|
17 | if(!rc) { |
---|
18 | std::cerr << "Test failed! ("<< msg << ")" << std::endl; \ |
---|
19 | |
---|
20 | |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | int main() |
---|
27 | { |
---|
28 | |
---|
29 | typedef ListGraph::Node Node; |
---|
30 | typedef ListGraph::Edge Edge; |
---|
31 | |
---|
32 | ListGraph graph; |
---|
33 | |
---|
34 | //Ahuja könyv példája |
---|
35 | |
---|
36 | Node s=graph.addNode(); |
---|
37 | Node v1=graph.addNode(); |
---|
38 | Node v2=graph.addNode(); |
---|
39 | Node v3=graph.addNode(); |
---|
40 | Node v4=graph.addNode(); |
---|
41 | Node v5=graph.addNode(); |
---|
42 | Node t=graph.addNode(); |
---|
43 | |
---|
44 | ListGraph::NodeMap<int> supply_demand(graph); |
---|
45 | |
---|
46 | supply_demand.set(s, 2); |
---|
47 | supply_demand.set(v1, 3); |
---|
48 | supply_demand.set(v3, -1); |
---|
49 | supply_demand.set(t, -4); |
---|
50 | |
---|
51 | Edge s_v1=graph.addEdge(s, v1); |
---|
52 | Edge v1_v2=graph.addEdge(v1, v2); |
---|
53 | Edge s_v3=graph.addEdge(s, v3); |
---|
54 | Edge v2_v4=graph.addEdge(v2, v4); |
---|
55 | Edge v2_v5=graph.addEdge(v2, v5); |
---|
56 | Edge v3_v5=graph.addEdge(v3, v5); |
---|
57 | Edge v4_t=graph.addEdge(v4, t); |
---|
58 | Edge v5_t=graph.addEdge(v5, t); |
---|
59 | |
---|
60 | |
---|
61 | ListGraph::EdgeMap<int> cost(graph); |
---|
62 | |
---|
63 | cost.set(s_v1, 6); |
---|
64 | cost.set(v1_v2, 4); |
---|
65 | cost.set(s_v3, 10); |
---|
66 | cost.set(v2_v4, 5); |
---|
67 | cost.set(v2_v5, 1); |
---|
68 | cost.set(v3_v5, 4); |
---|
69 | cost.set(v4_t, 8); |
---|
70 | cost.set(v5_t, 8); |
---|
71 | |
---|
72 | /* |
---|
73 | ListGraph::EdgeMap<int> capacity(graph); |
---|
74 | |
---|
75 | capacity.set(s_v1, 2); |
---|
76 | capacity.set(v1_v2, 2); |
---|
77 | capacity.set(s_v3, 1); |
---|
78 | capacity.set(v2_v4, 1); |
---|
79 | capacity.set(v2_v5, 1); |
---|
80 | capacity.set(v3_v5, 1); |
---|
81 | capacity.set(v4_t, 1); |
---|
82 | capacity.set(v5_t, 2); |
---|
83 | */ |
---|
84 | |
---|
85 | // ConstMap<Edge, int> const1map(1); |
---|
86 | std::cout << "Enhanced capacity scaling algorithm test (for the mincostflow problem)..." << std::endl; |
---|
87 | |
---|
88 | MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::NodeMap<int> > |
---|
89 | min_cost_flow_test(graph, cost, supply_demand); |
---|
90 | |
---|
91 | min_cost_flow_test.run(); |
---|
92 | //int k=1; |
---|
93 | check(min_cost_flow_test.checkOptimality(), "Is the primal-dual solution pair really optimal?"); |
---|
94 | |
---|
95 | /* |
---|
96 | check( min_cost_flow_test.run(s,t,k) == 1 && min_cost_flow_test.totalLength() == 19,"One path, total cost should be 19"); |
---|
97 | |
---|
98 | check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
99 | |
---|
100 | k=2; |
---|
101 | |
---|
102 | check( min_cost_flow_test.run(s,t,k) == 2 && min_cost_flow_test.totalLength() == 41,"Two paths, total cost should be 41"); |
---|
103 | |
---|
104 | check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
105 | |
---|
106 | |
---|
107 | k=4; |
---|
108 | |
---|
109 | check( min_cost_flow_test.run(s,t,k) == 3 && min_cost_flow_test.totalLength() == 64,"Three paths, total cost should be 64"); |
---|
110 | |
---|
111 | check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
112 | |
---|
113 | */ |
---|
114 | cout << (passed ? "All tests passed." : "Some of the tests failed!!!") |
---|
115 | << endl; |
---|
116 | |
---|
117 | return passed ? 0 : 1; |
---|
118 | |
---|
119 | } |
---|