1 | /* -*- C++ -*- |
---|
2 | * src/test/min_cost_flow_test.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #include <iostream> |
---|
18 | #include "test_tools.h" |
---|
19 | #include <lemon/list_graph.h> |
---|
20 | #include <lemon/min_cost_flow.h> |
---|
21 | //#include <path.h> |
---|
22 | //#include <maps.h> |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | using namespace lemon; |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | bool passed = true; |
---|
30 | /* |
---|
31 | void check(bool rc, char *msg="") { |
---|
32 | passed = passed && rc; |
---|
33 | if(!rc) { |
---|
34 | std::cerr << "Test failed! ("<< msg << ")" << std::endl; \ |
---|
35 | |
---|
36 | |
---|
37 | } |
---|
38 | } |
---|
39 | */ |
---|
40 | |
---|
41 | |
---|
42 | int main() |
---|
43 | { |
---|
44 | |
---|
45 | typedef ListGraph::Node Node; |
---|
46 | typedef ListGraph::Edge Edge; |
---|
47 | |
---|
48 | ListGraph graph; |
---|
49 | |
---|
50 | //Ahuja könyv példája |
---|
51 | |
---|
52 | Node s=graph.addNode(); |
---|
53 | Node v1=graph.addNode(); |
---|
54 | Node v2=graph.addNode(); |
---|
55 | Node v3=graph.addNode(); |
---|
56 | Node v4=graph.addNode(); |
---|
57 | Node v5=graph.addNode(); |
---|
58 | Node t=graph.addNode(); |
---|
59 | |
---|
60 | Edge s_v1=graph.addEdge(s, v1); |
---|
61 | Edge v1_v2=graph.addEdge(v1, v2); |
---|
62 | Edge s_v3=graph.addEdge(s, v3); |
---|
63 | Edge v2_v4=graph.addEdge(v2, v4); |
---|
64 | Edge v2_v5=graph.addEdge(v2, v5); |
---|
65 | Edge v3_v5=graph.addEdge(v3, v5); |
---|
66 | Edge v4_t=graph.addEdge(v4, t); |
---|
67 | Edge v5_t=graph.addEdge(v5, t); |
---|
68 | |
---|
69 | |
---|
70 | ListGraph::EdgeMap<int> length(graph); |
---|
71 | |
---|
72 | length.set(s_v1, 6); |
---|
73 | length.set(v1_v2, 4); |
---|
74 | length.set(s_v3, 10); |
---|
75 | length.set(v2_v4, 5); |
---|
76 | length.set(v2_v5, 1); |
---|
77 | length.set(v3_v5, 4); |
---|
78 | length.set(v4_t, 8); |
---|
79 | length.set(v5_t, 8); |
---|
80 | |
---|
81 | ListGraph::EdgeMap<int> capacity(graph); |
---|
82 | |
---|
83 | capacity.set(s_v1, 2); |
---|
84 | capacity.set(v1_v2, 2); |
---|
85 | capacity.set(s_v3, 1); |
---|
86 | capacity.set(v2_v4, 1); |
---|
87 | capacity.set(v2_v5, 1); |
---|
88 | capacity.set(v3_v5, 1); |
---|
89 | capacity.set(v4_t, 1); |
---|
90 | capacity.set(v5_t, 2); |
---|
91 | |
---|
92 | // ConstMap<Edge, int> const1map(1); |
---|
93 | std::cout << "Mincostflows algorithm test..." << std::endl; |
---|
94 | |
---|
95 | MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > |
---|
96 | surb_test(graph, length, capacity); |
---|
97 | |
---|
98 | int k=1; |
---|
99 | |
---|
100 | check( surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19"); |
---|
101 | |
---|
102 | check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
103 | |
---|
104 | k=2; |
---|
105 | |
---|
106 | check( surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 41,"Two paths, total length should be 41"); |
---|
107 | |
---|
108 | check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
109 | |
---|
110 | |
---|
111 | k=4; |
---|
112 | |
---|
113 | check( surb_test.run(s,t,k) == 3 && surb_test.totalLength() == 64,"Three paths, total length should be 64"); |
---|
114 | |
---|
115 | check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?"); |
---|
116 | |
---|
117 | |
---|
118 | cout << (passed ? "All tests passed." : "Some of the tests failed!!!") |
---|
119 | << endl; |
---|
120 | |
---|
121 | return passed ? 0 : 1; |
---|
122 | |
---|
123 | } |
---|