1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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
21 #include <lemon/list_graph.h>
22 #include <lemon/lgf_reader.h>
23 #include <lemon/path.h>
24 #include <lemon/suurballe.h>
25 #include <lemon/concepts/digraph.h>
27 #include "test_tools.h"
29 using namespace lemon;
74 // Check the interface of Suurballe
75 void checkSuurballeCompile()
78 typedef concepts::Digraph Digraph;
80 typedef Digraph::Node Node;
81 typedef Digraph::Arc Arc;
82 typedef concepts::ReadMap<Arc, VType> LengthMap;
84 typedef Suurballe<Digraph, LengthMap> SuurballeType;
90 SuurballeType::FlowMap flow(g);
91 SuurballeType::PotentialMap pi(g);
93 SuurballeType suurb_test(g, len);
94 const SuurballeType& const_suurb_test = suurb_test;
101 k = suurb_test.run(n, n);
102 k = suurb_test.run(n, n, k);
104 k = suurb_test.findFlow(n);
105 k = suurb_test.findFlow(n, k);
106 suurb_test.findPaths();
110 ignore_unused_variable_warning(f,c);
112 c = const_suurb_test.totalLength();
113 f = const_suurb_test.flow(e);
114 const SuurballeType::FlowMap& fm =
115 const_suurb_test.flowMap();
116 c = const_suurb_test.potential(n);
117 const SuurballeType::PotentialMap& pm =
118 const_suurb_test.potentialMap();
119 k = const_suurb_test.pathNum();
120 Path<Digraph> p = const_suurb_test.path(k);
122 ignore_unused_variable_warning(fm);
123 ignore_unused_variable_warning(pm);
126 // Check the feasibility of the flow
127 template <typename Digraph, typename FlowMap>
128 bool checkFlow( const Digraph& gr, const FlowMap& flow,
129 typename Digraph::Node s, typename Digraph::Node t,
132 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
133 for (ArcIt e(gr); e != INVALID; ++e)
134 if (!(flow[e] == 0 || flow[e] == 1)) return false;
136 for (NodeIt n(gr); n != INVALID; ++n) {
138 for (OutArcIt e(gr, n); e != INVALID; ++e)
140 for (InArcIt e(gr, n); e != INVALID; ++e)
142 if (n == s && sum != value) return false;
143 if (n == t && sum != -value) return false;
144 if (n != s && n != t && sum != 0) return false;
150 // Check the optimalitiy of the flow
151 template < typename Digraph, typename CostMap,
152 typename FlowMap, typename PotentialMap >
153 bool checkOptimality( const Digraph& gr, const CostMap& cost,
154 const FlowMap& flow, const PotentialMap& pi )
156 // Check the "Complementary Slackness" optimality condition
157 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
159 for (ArcIt e(gr); e != INVALID; ++e) {
160 typename CostMap::Value red_cost =
161 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
162 opt = (flow[e] == 0 && red_cost >= 0) ||
163 (flow[e] == 1 && red_cost <= 0);
170 template <typename Digraph, typename Path>
171 bool checkPath( const Digraph& gr, const Path& path,
172 typename Digraph::Node s, typename Digraph::Node t)
174 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
176 for (int i = 0; i < path.length(); ++i) {
177 if (gr.source(path.nth(i)) != n) return false;
178 n = gr.target(path.nth(i));
186 DIGRAPH_TYPEDEFS(ListDigraph);
188 // Read the test digraph
190 ListDigraph::ArcMap<int> length(digraph);
193 std::istringstream input(test_lgf);
194 DigraphReader<ListDigraph>(digraph, input).
195 arcMap("length", length).
202 Suurballe<ListDigraph> suurballe(digraph, length);
203 check(suurballe.run(s, t) == 2, "Wrong number of paths");
204 check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
205 "The flow is not feasible");
206 check(suurballe.totalLength() == 510, "The flow is not optimal");
207 check(checkOptimality(digraph, length, suurballe.flowMap(),
208 suurballe.potentialMap()),
210 for (int i = 0; i < suurballe.pathNum(); ++i)
211 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
216 Suurballe<ListDigraph> suurballe(digraph, length);
217 check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
218 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
219 "The flow is not feasible");
220 check(suurballe.totalLength() == 1040, "The flow is not optimal");
221 check(checkOptimality(digraph, length, suurballe.flowMap(),
222 suurballe.potentialMap()),
224 for (int i = 0; i < suurballe.pathNum(); ++i)
225 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
228 // Find 5 paths (only 3 can be found)
230 Suurballe<ListDigraph> suurballe(digraph, length);
231 check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
232 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
233 "The flow is not feasible");
234 check(suurballe.totalLength() == 1040, "The flow is not optimal");
235 check(checkOptimality(digraph, length, suurballe.flowMap(),
236 suurballe.potentialMap()),
238 for (int i = 0; i < suurballe.pathNum(); ++i)
239 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");