3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
22 #include <lemon/list_graph.h>
23 #include <lemon/graph_reader.h>
24 #include <lemon/path.h>
25 #include <lemon/suurballe.h>
27 #include "test_tools.h"
29 using namespace lemon;
31 // Checks the feasibility of the flow
32 template <typename Graph, typename FlowMap>
33 bool checkFlow( const Graph& gr, const FlowMap& flow,
34 typename Graph::Node s, typename Graph::Node t,
37 GRAPH_TYPEDEFS(typename Graph);
38 for (EdgeIt e(gr); e != INVALID; ++e)
39 if (!(flow[e] == 0 || flow[e] == 1)) return false;
41 for (NodeIt n(gr); n != INVALID; ++n) {
43 for (OutEdgeIt e(gr, n); e != INVALID; ++e)
45 for (InEdgeIt e(gr, n); e != INVALID; ++e)
47 if (n == s && sum != value) return false;
48 if (n == t && sum != -value) return false;
49 if (n != s && n != t && sum != 0) return false;
55 // Checks the optimalitiy of the flow
56 template < typename Graph, typename CostMap,
57 typename FlowMap, typename PotentialMap >
58 bool checkOptimality( const Graph& gr, const CostMap& cost,
59 const FlowMap& flow, const PotentialMap& pi )
61 // Checking the Complementary Slackness optimality condition
62 GRAPH_TYPEDEFS(typename Graph);
64 for (EdgeIt e(gr); e != INVALID; ++e) {
65 typename CostMap::Value red_cost =
66 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
67 opt = (flow[e] == 0 && red_cost >= 0) ||
68 (flow[e] == 1 && red_cost <= 0);
75 template < typename Graph, typename Path >
76 bool checkPath( const Graph& gr, const Path& path,
77 typename Graph::Node s, typename Graph::Node t)
79 // Checking the Complementary Slackness optimality condition
80 GRAPH_TYPEDEFS(typename Graph);
82 for (int i = 0; i < path.length(); ++i) {
83 if (gr.source(path.nth(i)) != n) return false;
84 n = gr.target(path.nth(i));
92 GRAPH_TYPEDEFS(ListGraph);
94 // Reading the test graph
96 ListGraph::EdgeMap<int> length(graph);
101 fname = std::string(getenv("srcdir"));
103 fname += "/test/min_cost_flow_test.lgf";
105 std::ifstream input(fname.c_str());
106 check(input, "Input file '" << fname << "' not found");
107 GraphReader<ListGraph>(input, graph).
108 readEdgeMap("cost", length).
109 readNode("source", source).
110 readNode("target", target).
116 Suurballe<ListGraph> suurballe(graph, length, source, target);
117 check(suurballe.run(2) == 2, "Wrong number of paths");
118 check(checkFlow(graph, suurballe.flowMap(), source, target, 2),
119 "The flow is not feasible");
120 check(suurballe.totalLength() == 510, "The flow is not optimal");
121 check(checkOptimality(graph, length, suurballe.flowMap(),
122 suurballe.potentialMap()),
124 for (int i = 0; i < suurballe.pathNum(); ++i)
125 check(checkPath(graph, suurballe.path(i), source, target),
131 Suurballe<ListGraph> suurballe(graph, length, source, target);
132 check(suurballe.run(3) == 3, "Wrong number of paths");
133 check(checkFlow(graph, suurballe.flowMap(), source, target, 3),
134 "The flow is not feasible");
135 check(suurballe.totalLength() == 1040, "The flow is not optimal");
136 check(checkOptimality(graph, length, suurballe.flowMap(),
137 suurballe.potentialMap()),
139 for (int i = 0; i < suurballe.pathNum(); ++i)
140 check(checkPath(graph, suurballe.path(i), source, target),
144 // Finding 5 paths (only 3 can be found)
146 Suurballe<ListGraph> suurballe(graph, length, source, target);
147 check(suurballe.run(5) == 3, "Wrong number of paths");
148 check(checkFlow(graph, suurballe.flowMap(), source, target, 3),
149 "The flow is not feasible");
150 check(suurballe.totalLength() == 1040, "The flow is not optimal");
151 check(checkOptimality(graph, length, suurballe.flowMap(),
152 suurballe.potentialMap()),
154 for (int i = 0; i < suurballe.pathNum(); ++i)
155 check(checkPath(graph, suurballe.path(i), source, target),