Fix wrong iteration in ListGraph snapshot, part II. (#598)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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 ::lemon::ignore_unused_variable_warning(f,c);
122 c = const_suurb_test.totalLength();
123 f = const_suurb_test.flow(e);
124 const SuurballeType::FlowMap& fm =
125 const_suurb_test.flowMap();
126 c = const_suurb_test.potential(n);
127 const SuurballeType::PotentialMap& pm =
128 const_suurb_test.potentialMap();
129 k = const_suurb_test.pathNum();
130 Path<Digraph> p = const_suurb_test.path(k);
132 ::lemon::ignore_unused_variable_warning(fm);
133 ::lemon::ignore_unused_variable_warning(pm);
136 // Check the feasibility of the flow
137 template <typename Digraph, typename FlowMap>
138 bool checkFlow( const Digraph& gr, const FlowMap& flow,
139 typename Digraph::Node s, typename Digraph::Node t,
142 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
143 for (ArcIt e(gr); e != INVALID; ++e)
144 if (!(flow[e] == 0 || flow[e] == 1)) return false;
146 for (NodeIt n(gr); n != INVALID; ++n) {
148 for (OutArcIt e(gr, n); e != INVALID; ++e)
150 for (InArcIt e(gr, n); e != INVALID; ++e)
152 if (n == s && sum != value) return false;
153 if (n == t && sum != -value) return false;
154 if (n != s && n != t && sum != 0) return false;
160 // Check the optimalitiy of the flow
161 template < typename Digraph, typename CostMap,
162 typename FlowMap, typename PotentialMap >
163 bool checkOptimality( const Digraph& gr, const CostMap& cost,
164 const FlowMap& flow, const PotentialMap& pi )
166 // Check the "Complementary Slackness" optimality condition
167 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
169 for (ArcIt e(gr); e != INVALID; ++e) {
170 typename CostMap::Value red_cost =
171 cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
172 opt = (flow[e] == 0 && red_cost >= 0) ||
173 (flow[e] == 1 && red_cost <= 0);
180 template <typename Digraph, typename Path>
181 bool checkPath( const Digraph& gr, const Path& path,
182 typename Digraph::Node s, typename Digraph::Node t)
184 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
186 for (int i = 0; i < path.length(); ++i) {
187 if (gr.source(path.nth(i)) != n) return false;
188 n = gr.target(path.nth(i));
196 DIGRAPH_TYPEDEFS(ListDigraph);
198 // Read the test digraph
200 ListDigraph::ArcMap<int> length(digraph);
203 std::istringstream input(test_lgf);
204 DigraphReader<ListDigraph>(digraph, input).
205 arcMap("length", length).
212 Suurballe<ListDigraph> suurballe(digraph, length);
215 check(suurballe.run(s, t) == 2, "Wrong number of paths");
216 check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
217 "The flow is not feasible");
218 check(suurballe.totalLength() == 510, "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 check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
227 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
228 "The flow is not feasible");
229 check(suurballe.totalLength() == 1040, "The flow is not optimal");
230 check(checkOptimality(digraph, length, suurballe.flowMap(),
231 suurballe.potentialMap()),
233 for (int i = 0; i < suurballe.pathNum(); ++i)
234 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
236 // Find 5 paths (only 3 can be found)
237 check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
238 check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
239 "The flow is not feasible");
240 check(suurballe.totalLength() == 1040, "The flow is not optimal");
241 check(checkOptimality(digraph, length, suurballe.flowMap(),
242 suurballe.potentialMap()),
244 for (int i = 0; i < suurballe.pathNum(); ++i)
245 check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
248 // Check fullInit() + start()
250 Suurballe<ListDigraph> suurballe(digraph, length);
251 suurballe.fullInit(s);
254 check(suurballe.start(t) == 2, "Wrong number of paths");
255 check(suurballe.totalLength() == 510, "The flow is not optimal");
258 check(suurballe.start(t, 3) == 3, "Wrong number of paths");
259 check(suurballe.totalLength() == 1040, "The flow is not optimal");
261 // Find 5 paths (only 3 can be found)
262 check(suurballe.start(t, 5) == 3, "Wrong number of paths");
263 check(suurballe.totalLength() == 1040, "The flow is not optimal");