2 * src/test/min_cost_flow_test.cc - Part of HUGOlib, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
18 #include "test_tools.h"
19 #include <hugo/list_graph.h>
20 #include <hugo/min_cost_flow.h>
31 void check(bool rc, char *msg="") {
32 passed = passed && rc;
34 std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
45 typedef ListGraph::Node Node;
46 typedef ListGraph::Edge Edge;
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();
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);
70 ListGraph::EdgeMap<int> length(graph);
81 ListGraph::EdgeMap<int> capacity(graph);
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);
92 // ConstMap<Edge, int> const1map(1);
93 std::cout << "Mincostflows algorithm test..." << std::endl;
95 MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> >
96 surb_test(graph, length, capacity);
100 check( surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
102 check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
106 check( surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 41,"Two paths, total length should be 41");
108 check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
113 check( surb_test.run(s,t,k) == 3 && surb_test.totalLength() == 64,"Three paths, total length should be 64");
115 check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
118 cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
121 return passed ? 0 : 1;