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 suurb_test.fullInit(n);
106 suurb_test.start(n, k);
107 k = suurb_test.findFlow(n);
108 k = suurb_test.findFlow(n, k);
109 suurb_test.findPaths();
113 c = const_suurb_test.totalLength();
114 f = const_suurb_test.flow(e);
115 const SuurballeType::FlowMap& fm =
116 const_suurb_test.flowMap();
117 c = const_suurb_test.potential(n);
118 const SuurballeType::PotentialMap& pm =
119 const_suurb_test.potentialMap();
120 k = const_suurb_test.pathNum();
121 Path<Digraph> p = const_suurb_test.path(k);
123 ignore_unused_variable_warning(fm);
124 ignore_unused_variable_warning(pm);
127 // Check the feasibility of the flow
128 template <typename Digraph, typename FlowMap>
129 bool checkFlow( const Digraph& gr, const FlowMap& flow,
130 typename Digraph::Node s, typename Digraph::Node t,
133 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
134 for (ArcIt e(gr); e != INVALID; ++e)
135 if (!(flow[e] == 0 || flow[e] == 1)) return false;
137 for (NodeIt n(gr); n != INVALID; ++n) {
139 for (OutArcIt e(gr, n); e != INVALID; ++e)
141 for (InArcIt e(gr, n); e != INVALID; ++e)
143 if (n == s && sum != value) return false;
144 if (n == t && sum != -value) return false;
145 if (n != s && n != t && sum != 0) return false;
151 // Check the optimalitiy of the flow
152 template < typename Digraph, typename CostMap,
153 typename FlowMap, typename PotentialMap >
154 bool checkOptimality( const Digraph& gr, const CostMap& cost,
155 const FlowMap& flow, const PotentialMap& pi )
157 // Check the "Complementary Slackness" optimality condition
158 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
160 for (ArcIt e(gr); e != INVALID; ++e) {
161 typename CostMap::Value red_cost =
162 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
163 opt = (flow[e] == 0 && red_cost >= 0) ||
164 (flow[e] == 1 && red_cost <= 0);
171 template <typename Digraph, typename Path>
172 bool checkPath( const Digraph& gr, const Path& path,
173 typename Digraph::Node s, typename Digraph::Node t)
175 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
177 for (int i = 0; i < path.length(); ++i) {
178 if (gr.source(path.nth(i)) != n) return false;
179 n = gr.target(path.nth(i));
187 DIGRAPH_TYPEDEFS(ListDigraph);
189 // Read the test digraph
191 ListDigraph::ArcMap<int> length(digraph);
194 std::istringstream input(test_lgf);
195 DigraphReader<ListDigraph>(digraph, input).
196 arcMap("length", length).
203 Suurballe<ListDigraph> suurballe(digraph, length);
204 check(suurballe.run(s, t) == 2, "Wrong number of paths");
205 check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
206 "The flow is not feasible");
207 check(suurballe.totalLength() == 510, "The flow is not optimal");
208 check(checkOptimality(digraph, length, suurballe.flowMap(),
209 suurballe.potentialMap()),
211 for (int i = 0; i < suurballe.pathNum(); ++i)
212 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
217 Suurballe<ListDigraph> suurballe(digraph, length);
218 check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
219 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
220 "The flow is not feasible");
221 check(suurballe.totalLength() == 1040, "The flow is not optimal");
222 check(checkOptimality(digraph, length, suurballe.flowMap(),
223 suurballe.potentialMap()),
225 for (int i = 0; i < suurballe.pathNum(); ++i)
226 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
229 // Find 5 paths (only 3 can be found)
231 Suurballe<ListDigraph> suurballe(digraph, length);
232 check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
233 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
234 "The flow is not feasible");
235 check(suurballe.totalLength() == 1040, "The flow is not optimal");
236 check(checkOptimality(digraph, length, suurballe.flowMap(),
237 suurballe.potentialMap()),
239 for (int i = 0; i < suurballe.pathNum(); ++i)
240 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");