1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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>
26 #include <lemon/concepts/heap.h>
28 #include "test_tools.h"
30 using namespace lemon;
75 // Check the interface of Suurballe
76 void checkSuurballeCompile()
79 typedef concepts::Digraph Digraph;
81 typedef Digraph::Node Node;
82 typedef Digraph::Arc Arc;
83 typedef concepts::ReadMap<Arc, VType> LengthMap;
85 typedef Suurballe<Digraph, LengthMap> ST;
86 typedef Suurballe<Digraph, LengthMap>
87 ::SetFlowMap<ST::FlowMap>
88 ::SetPotentialMap<ST::PotentialMap>
89 ::SetPath<SimplePath<Digraph> >
90 ::SetHeap<concepts::Heap<VType, Digraph::NodeMap<int> > >
91 ::Create SuurballeType;
97 SuurballeType::FlowMap flow(g);
98 SuurballeType::PotentialMap pi(g);
100 SuurballeType suurb_test(g, len);
101 const SuurballeType& const_suurb_test = suurb_test;
108 k = suurb_test.run(n, n);
109 k = suurb_test.run(n, n, k);
111 suurb_test.fullInit(n);
113 suurb_test.start(n, k);
114 k = suurb_test.findFlow(n);
115 k = suurb_test.findFlow(n, k);
116 suurb_test.findPaths();
120 c = const_suurb_test.totalLength();
121 f = const_suurb_test.flow(e);
122 const SuurballeType::FlowMap& fm =
123 const_suurb_test.flowMap();
124 c = const_suurb_test.potential(n);
125 const SuurballeType::PotentialMap& pm =
126 const_suurb_test.potentialMap();
127 k = const_suurb_test.pathNum();
128 Path<Digraph> p = const_suurb_test.path(k);
130 ignore_unused_variable_warning(fm);
131 ignore_unused_variable_warning(pm);
134 // Check the feasibility of the flow
135 template <typename Digraph, typename FlowMap>
136 bool checkFlow( const Digraph& gr, const FlowMap& flow,
137 typename Digraph::Node s, typename Digraph::Node t,
140 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
141 for (ArcIt e(gr); e != INVALID; ++e)
142 if (!(flow[e] == 0 || flow[e] == 1)) return false;
144 for (NodeIt n(gr); n != INVALID; ++n) {
146 for (OutArcIt e(gr, n); e != INVALID; ++e)
148 for (InArcIt e(gr, n); e != INVALID; ++e)
150 if (n == s && sum != value) return false;
151 if (n == t && sum != -value) return false;
152 if (n != s && n != t && sum != 0) return false;
158 // Check the optimalitiy of the flow
159 template < typename Digraph, typename CostMap,
160 typename FlowMap, typename PotentialMap >
161 bool checkOptimality( const Digraph& gr, const CostMap& cost,
162 const FlowMap& flow, const PotentialMap& pi )
164 // Check the "Complementary Slackness" optimality condition
165 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
167 for (ArcIt e(gr); e != INVALID; ++e) {
168 typename CostMap::Value red_cost =
169 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
170 opt = (flow[e] == 0 && red_cost >= 0) ||
171 (flow[e] == 1 && red_cost <= 0);
178 template <typename Digraph, typename Path>
179 bool checkPath( const Digraph& gr, const Path& path,
180 typename Digraph::Node s, typename Digraph::Node t)
182 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
184 for (int i = 0; i < path.length(); ++i) {
185 if (gr.source(path.nth(i)) != n) return false;
186 n = gr.target(path.nth(i));
194 DIGRAPH_TYPEDEFS(ListDigraph);
196 // Read the test digraph
198 ListDigraph::ArcMap<int> length(digraph);
201 std::istringstream input(test_lgf);
202 DigraphReader<ListDigraph>(digraph, input).
203 arcMap("length", length).
210 Suurballe<ListDigraph> suurballe(digraph, length);
213 check(suurballe.run(s, t) == 2, "Wrong number of paths");
214 check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
215 "The flow is not feasible");
216 check(suurballe.totalLength() == 510, "The flow is not optimal");
217 check(checkOptimality(digraph, length, suurballe.flowMap(),
218 suurballe.potentialMap()),
220 for (int i = 0; i < suurballe.pathNum(); ++i)
221 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
224 check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
225 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
226 "The flow is not feasible");
227 check(suurballe.totalLength() == 1040, "The flow is not optimal");
228 check(checkOptimality(digraph, length, suurballe.flowMap(),
229 suurballe.potentialMap()),
231 for (int i = 0; i < suurballe.pathNum(); ++i)
232 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
234 // Find 5 paths (only 3 can be found)
235 check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
236 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
237 "The flow is not feasible");
238 check(suurballe.totalLength() == 1040, "The flow is not optimal");
239 check(checkOptimality(digraph, length, suurballe.flowMap(),
240 suurballe.potentialMap()),
242 for (int i = 0; i < suurballe.pathNum(); ++i)
243 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
246 // Check fullInit() + start()
248 Suurballe<ListDigraph> suurballe(digraph, length);
249 suurballe.fullInit(s);
252 check(suurballe.start(t) == 2, "Wrong number of paths");
253 check(suurballe.totalLength() == 510, "The flow is not optimal");
256 check(suurballe.start(t, 3) == 3, "Wrong number of paths");
257 check(suurballe.totalLength() == 1040, "The flow is not optimal");
259 // Find 5 paths (only 3 can be found)
260 check(suurballe.start(t, 5) == 3, "Wrong number of paths");
261 check(suurballe.totalLength() == 1040, "The flow is not optimal");