1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
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 c = const_suurb_test.totalLength();
111 f = const_suurb_test.flow(e);
112 const SuurballeType::FlowMap& fm =
113 const_suurb_test.flowMap();
114 c = const_suurb_test.potential(n);
115 const SuurballeType::PotentialMap& pm =
116 const_suurb_test.potentialMap();
117 k = const_suurb_test.pathNum();
118 Path<Digraph> p = const_suurb_test.path(k);
120 ignore_unused_variable_warning(fm);
121 ignore_unused_variable_warning(pm);
124 // Check the feasibility of the flow
125 template <typename Digraph, typename FlowMap>
126 bool checkFlow( const Digraph& gr, const FlowMap& flow,
127 typename Digraph::Node s, typename Digraph::Node t,
130 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
131 for (ArcIt e(gr); e != INVALID; ++e)
132 if (!(flow[e] == 0 || flow[e] == 1)) return false;
134 for (NodeIt n(gr); n != INVALID; ++n) {
136 for (OutArcIt e(gr, n); e != INVALID; ++e)
138 for (InArcIt e(gr, n); e != INVALID; ++e)
140 if (n == s && sum != value) return false;
141 if (n == t && sum != -value) return false;
142 if (n != s && n != t && sum != 0) return false;
148 // Check the optimalitiy of the flow
149 template < typename Digraph, typename CostMap,
150 typename FlowMap, typename PotentialMap >
151 bool checkOptimality( const Digraph& gr, const CostMap& cost,
152 const FlowMap& flow, const PotentialMap& pi )
154 // Check the "Complementary Slackness" optimality condition
155 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
157 for (ArcIt e(gr); e != INVALID; ++e) {
158 typename CostMap::Value red_cost =
159 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
160 opt = (flow[e] == 0 && red_cost >= 0) ||
161 (flow[e] == 1 && red_cost <= 0);
168 template <typename Digraph, typename Path>
169 bool checkPath( const Digraph& gr, const Path& path,
170 typename Digraph::Node s, typename Digraph::Node t)
172 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
174 for (int i = 0; i < path.length(); ++i) {
175 if (gr.source(path.nth(i)) != n) return false;
176 n = gr.target(path.nth(i));
184 DIGRAPH_TYPEDEFS(ListDigraph);
186 // Read the test digraph
188 ListDigraph::ArcMap<int> length(digraph);
191 std::istringstream input(test_lgf);
192 DigraphReader<ListDigraph>(digraph, input).
193 arcMap("length", length).
200 Suurballe<ListDigraph> suurballe(digraph, length);
201 check(suurballe.run(s, t) == 2, "Wrong number of paths");
202 check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
203 "The flow is not feasible");
204 check(suurballe.totalLength() == 510, "The flow is not optimal");
205 check(checkOptimality(digraph, length, suurballe.flowMap(),
206 suurballe.potentialMap()),
208 for (int i = 0; i < suurballe.pathNum(); ++i)
209 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
214 Suurballe<ListDigraph> suurballe(digraph, length);
215 check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
216 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
217 "The flow is not feasible");
218 check(suurballe.totalLength() == 1040, "The flow is not optimal");
219 check(checkOptimality(digraph, length, suurballe.flowMap(),
220 suurballe.potentialMap()),
222 for (int i = 0; i < suurballe.pathNum(); ++i)
223 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
226 // Find 5 paths (only 3 can be found)
228 Suurballe<ListDigraph> suurballe(digraph, length);
229 check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
230 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
231 "The flow is not feasible");
232 check(suurballe.totalLength() == 1040, "The flow is not optimal");
233 check(checkOptimality(digraph, length, suurballe.flowMap(),
234 suurballe.potentialMap()),
236 for (int i = 0; i < suurballe.pathNum(); ++i)
237 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");