1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/max_flow_test.cc Thu Feb 28 23:44:35 2013 +0100
1.3 @@ -0,0 +1,395 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2010
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#include <iostream>
1.23 +
1.24 +#include "test_tools.h"
1.25 +#include <lemon/smart_graph.h>
1.26 +#include <lemon/preflow.h>
1.27 +#include <lemon/edmonds_karp.h>
1.28 +#include <lemon/concepts/digraph.h>
1.29 +#include <lemon/concepts/maps.h>
1.30 +#include <lemon/lgf_reader.h>
1.31 +#include <lemon/elevator.h>
1.32 +
1.33 +using namespace lemon;
1.34 +
1.35 +char test_lgf[] =
1.36 + "@nodes\n"
1.37 + "label\n"
1.38 + "0\n"
1.39 + "1\n"
1.40 + "2\n"
1.41 + "3\n"
1.42 + "4\n"
1.43 + "5\n"
1.44 + "6\n"
1.45 + "7\n"
1.46 + "8\n"
1.47 + "9\n"
1.48 + "@arcs\n"
1.49 + " label capacity\n"
1.50 + "0 1 0 20\n"
1.51 + "0 2 1 0\n"
1.52 + "1 1 2 3\n"
1.53 + "1 2 3 8\n"
1.54 + "1 3 4 8\n"
1.55 + "2 5 5 5\n"
1.56 + "3 2 6 5\n"
1.57 + "3 5 7 5\n"
1.58 + "3 6 8 5\n"
1.59 + "4 3 9 3\n"
1.60 + "5 7 10 3\n"
1.61 + "5 6 11 10\n"
1.62 + "5 8 12 10\n"
1.63 + "6 8 13 8\n"
1.64 + "8 9 14 20\n"
1.65 + "8 1 15 5\n"
1.66 + "9 5 16 5\n"
1.67 + "@attributes\n"
1.68 + "source 1\n"
1.69 + "target 8\n";
1.70 +
1.71 +
1.72 +// Checks the general interface of a max flow algorithm
1.73 +template <typename GR, typename CAP>
1.74 +struct MaxFlowClassConcept
1.75 +{
1.76 +
1.77 + template <typename MF>
1.78 + struct Constraints {
1.79 +
1.80 + typedef typename GR::Node Node;
1.81 + typedef typename GR::Arc Arc;
1.82 + typedef typename CAP::Value Value;
1.83 + typedef concepts::ReadWriteMap<Arc, Value> FlowMap;
1.84 + typedef concepts::WriteMap<Node, bool> CutMap;
1.85 +
1.86 + GR g;
1.87 + Node n;
1.88 + Arc e;
1.89 + CAP cap;
1.90 + FlowMap flow;
1.91 + CutMap cut;
1.92 + Value v;
1.93 + bool b;
1.94 +
1.95 + void constraints() {
1.96 + checkConcept<concepts::Digraph, GR>();
1.97 +
1.98 + const Constraints& me = *this;
1.99 +
1.100 + typedef typename MF
1.101 + ::template SetFlowMap<FlowMap>
1.102 + ::Create MaxFlowType;
1.103 + typedef typename MF::Create MaxFlowType2;
1.104 + MaxFlowType max_flow(me.g, me.cap, me.n, me.n);
1.105 + const MaxFlowType& const_max_flow = max_flow;
1.106 +
1.107 + max_flow
1.108 + .capacityMap(cap)
1.109 + .flowMap(flow)
1.110 + .source(n)
1.111 + .target(n);
1.112 +
1.113 + typename MaxFlowType::Tolerance tol = const_max_flow.tolerance();
1.114 + max_flow.tolerance(tol);
1.115 +
1.116 + max_flow.init();
1.117 + max_flow.init(cap);
1.118 + max_flow.run();
1.119 +
1.120 + v = const_max_flow.flowValue();
1.121 + v = const_max_flow.flow(e);
1.122 + const FlowMap& fm = const_max_flow.flowMap();
1.123 +
1.124 + b = const_max_flow.minCut(n);
1.125 + const_max_flow.minCutMap(cut);
1.126 +
1.127 + ignore_unused_variable_warning(fm);
1.128 + }
1.129 +
1.130 + };
1.131 +
1.132 +};
1.133 +
1.134 +// Checks the specific parts of Preflow's interface
1.135 +void checkPreflowCompile()
1.136 +{
1.137 + typedef int Value;
1.138 + typedef concepts::Digraph Digraph;
1.139 + typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
1.140 + typedef Elevator<Digraph, Digraph::Node> Elev;
1.141 + typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
1.142 +
1.143 + Digraph g;
1.144 + Digraph::Node n;
1.145 + CapMap cap;
1.146 +
1.147 + typedef Preflow<Digraph, CapMap>
1.148 + ::SetElevator<Elev>
1.149 + ::SetStandardElevator<LinkedElev>
1.150 + ::Create PreflowType;
1.151 + PreflowType preflow_test(g, cap, n, n);
1.152 + const PreflowType& const_preflow_test = preflow_test;
1.153 +
1.154 + const PreflowType::Elevator& elev = const_preflow_test.elevator();
1.155 + preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
1.156 +
1.157 + bool b = preflow_test.init(cap);
1.158 + preflow_test.startFirstPhase();
1.159 + preflow_test.startSecondPhase();
1.160 + preflow_test.runMinCut();
1.161 +
1.162 + ignore_unused_variable_warning(b);
1.163 +}
1.164 +
1.165 +// Checks the specific parts of EdmondsKarp's interface
1.166 +void checkEdmondsKarpCompile()
1.167 +{
1.168 + typedef int Value;
1.169 + typedef concepts::Digraph Digraph;
1.170 + typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
1.171 + typedef Elevator<Digraph, Digraph::Node> Elev;
1.172 + typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
1.173 +
1.174 + Digraph g;
1.175 + Digraph::Node n;
1.176 + CapMap cap;
1.177 +
1.178 + EdmondsKarp<Digraph, CapMap> ek_test(g, cap, n, n);
1.179 +
1.180 + ek_test.init(cap);
1.181 + bool b = ek_test.checkedInit(cap);
1.182 + b = ek_test.augment();
1.183 + ek_test.start();
1.184 +
1.185 + ignore_unused_variable_warning(b);
1.186 +}
1.187 +
1.188 +
1.189 +template <typename T>
1.190 +T cutValue (const SmartDigraph& g,
1.191 + const SmartDigraph::NodeMap<bool>& cut,
1.192 + const SmartDigraph::ArcMap<T>& cap) {
1.193 +
1.194 + T c=0;
1.195 + for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
1.196 + if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
1.197 + }
1.198 + return c;
1.199 +}
1.200 +
1.201 +template <typename T>
1.202 +bool checkFlow(const SmartDigraph& g,
1.203 + const SmartDigraph::ArcMap<T>& flow,
1.204 + const SmartDigraph::ArcMap<T>& cap,
1.205 + SmartDigraph::Node s, SmartDigraph::Node t) {
1.206 +
1.207 + for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
1.208 + if (flow[e] < 0 || flow[e] > cap[e]) return false;
1.209 + }
1.210 +
1.211 + for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
1.212 + if (n == s || n == t) continue;
1.213 + T sum = 0;
1.214 + for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
1.215 + sum += flow[e];
1.216 + }
1.217 + for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
1.218 + sum -= flow[e];
1.219 + }
1.220 + if (sum != 0) return false;
1.221 + }
1.222 + return true;
1.223 +}
1.224 +
1.225 +void initFlowTest()
1.226 +{
1.227 + DIGRAPH_TYPEDEFS(SmartDigraph);
1.228 +
1.229 + SmartDigraph g;
1.230 + SmartDigraph::ArcMap<int> cap(g),iflow(g);
1.231 + Node s=g.addNode(); Node t=g.addNode();
1.232 + Node n1=g.addNode(); Node n2=g.addNode();
1.233 + Arc a;
1.234 + a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
1.235 + a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
1.236 + a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
1.237 +
1.238 + Preflow<SmartDigraph> pre(g,cap,s,t);
1.239 + pre.init(iflow);
1.240 + pre.startFirstPhase();
1.241 + check(pre.flowValue() == 10, "The incorrect max flow value.");
1.242 + check(pre.minCut(s), "Wrong min cut (Node s).");
1.243 + check(pre.minCut(n1), "Wrong min cut (Node n1).");
1.244 + check(!pre.minCut(n2), "Wrong min cut (Node n2).");
1.245 + check(!pre.minCut(t), "Wrong min cut (Node t).");
1.246 +}
1.247 +
1.248 +template <typename MF, typename SF>
1.249 +void checkMaxFlowAlg() {
1.250 + typedef SmartDigraph Digraph;
1.251 + DIGRAPH_TYPEDEFS(Digraph);
1.252 +
1.253 + typedef typename MF::Value Value;
1.254 + typedef Digraph::ArcMap<Value> CapMap;
1.255 + typedef CapMap FlowMap;
1.256 + typedef BoolNodeMap CutMap;
1.257 +
1.258 + Digraph g;
1.259 + Node s, t;
1.260 + CapMap cap(g);
1.261 + std::istringstream input(test_lgf);
1.262 + DigraphReader<Digraph>(g,input)
1.263 + .arcMap("capacity", cap)
1.264 + .node("source",s)
1.265 + .node("target",t)
1.266 + .run();
1.267 +
1.268 + MF max_flow(g, cap, s, t);
1.269 + max_flow.run();
1.270 +
1.271 + check(checkFlow(g, max_flow.flowMap(), cap, s, t),
1.272 + "The flow is not feasible.");
1.273 +
1.274 + CutMap min_cut(g);
1.275 + max_flow.minCutMap(min_cut);
1.276 + Value min_cut_value = cutValue(g, min_cut, cap);
1.277 +
1.278 + check(max_flow.flowValue() == min_cut_value,
1.279 + "The max flow value is not equal to the min cut value.");
1.280 +
1.281 + FlowMap flow(g);
1.282 + for (ArcIt e(g); e != INVALID; ++e) flow[e] = max_flow.flowMap()[e];
1.283 +
1.284 + Value flow_value = max_flow.flowValue();
1.285 +
1.286 + for (ArcIt e(g); e != INVALID; ++e) cap[e] = 2 * cap[e];
1.287 + max_flow.init(flow);
1.288 +
1.289 + SF::startFirstPhase(max_flow); // start first phase of the algorithm
1.290 +
1.291 + CutMap min_cut1(g);
1.292 + max_flow.minCutMap(min_cut1);
1.293 + min_cut_value = cutValue(g, min_cut1, cap);
1.294 +
1.295 + check(max_flow.flowValue() == min_cut_value &&
1.296 + min_cut_value == 2 * flow_value,
1.297 + "The max flow value or the min cut value is wrong.");
1.298 +
1.299 + SF::startSecondPhase(max_flow); // start second phase of the algorithm
1.300 +
1.301 + check(checkFlow(g, max_flow.flowMap(), cap, s, t),
1.302 + "The flow is not feasible.");
1.303 +
1.304 + CutMap min_cut2(g);
1.305 + max_flow.minCutMap(min_cut2);
1.306 + min_cut_value = cutValue(g, min_cut2, cap);
1.307 +
1.308 + check(max_flow.flowValue() == min_cut_value &&
1.309 + min_cut_value == 2 * flow_value,
1.310 + "The max flow value or the min cut value was not doubled");
1.311 +
1.312 +
1.313 + max_flow.flowMap(flow);
1.314 +
1.315 + NodeIt tmp1(g, s);
1.316 + ++tmp1;
1.317 + if (tmp1 != INVALID) s = tmp1;
1.318 +
1.319 + NodeIt tmp2(g, t);
1.320 + ++tmp2;
1.321 + if (tmp2 != INVALID) t = tmp2;
1.322 +
1.323 + max_flow.source(s);
1.324 + max_flow.target(t);
1.325 +
1.326 + max_flow.run();
1.327 +
1.328 + CutMap min_cut3(g);
1.329 + max_flow.minCutMap(min_cut3);
1.330 + min_cut_value=cutValue(g, min_cut3, cap);
1.331 +
1.332 + check(max_flow.flowValue() == min_cut_value,
1.333 + "The max flow value or the min cut value is wrong.");
1.334 +}
1.335 +
1.336 +// Struct for calling start functions of a general max flow algorithm
1.337 +template <typename MF>
1.338 +struct GeneralStartFunctions {
1.339 +
1.340 + static void startFirstPhase(MF& mf) {
1.341 + mf.start();
1.342 + }
1.343 +
1.344 + static void startSecondPhase(MF& mf) {
1.345 + ignore_unused_variable_warning(mf);
1.346 + }
1.347 +
1.348 +};
1.349 +
1.350 +// Struct for calling start functions of Preflow
1.351 +template <typename MF>
1.352 +struct PreflowStartFunctions {
1.353 +
1.354 + static void startFirstPhase(MF& mf) {
1.355 + mf.startFirstPhase();
1.356 + }
1.357 +
1.358 + static void startSecondPhase(MF& mf) {
1.359 + mf.startSecondPhase();
1.360 + }
1.361 +
1.362 +};
1.363 +
1.364 +int main() {
1.365 +
1.366 + typedef concepts::Digraph GR;
1.367 + typedef concepts::ReadMap<GR::Arc, int> CM1;
1.368 + typedef concepts::ReadMap<GR::Arc, double> CM2;
1.369 +
1.370 + // Check the interface of Preflow
1.371 + checkConcept< MaxFlowClassConcept<GR, CM1>,
1.372 + Preflow<GR, CM1> >();
1.373 + checkConcept< MaxFlowClassConcept<GR, CM2>,
1.374 + Preflow<GR, CM2> >();
1.375 +
1.376 + // Check the interface of EdmondsKarp
1.377 + checkConcept< MaxFlowClassConcept<GR, CM1>,
1.378 + EdmondsKarp<GR, CM1> >();
1.379 + checkConcept< MaxFlowClassConcept<GR, CM2>,
1.380 + EdmondsKarp<GR, CM2> >();
1.381 +
1.382 + // Check Preflow
1.383 + typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<int> > PType1;
1.384 + typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<float> > PType2;
1.385 + checkMaxFlowAlg<PType1, PreflowStartFunctions<PType1> >();
1.386 + checkMaxFlowAlg<PType2, PreflowStartFunctions<PType2> >();
1.387 + initFlowTest();
1.388 +
1.389 + // Check EdmondsKarp
1.390 + typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<int> > EKType1;
1.391 + typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<float> > EKType2;
1.392 + checkMaxFlowAlg<EKType1, GeneralStartFunctions<EKType1> >();
1.393 + checkMaxFlowAlg<EKType2, GeneralStartFunctions<EKType2> >();
1.394 +
1.395 + initFlowTest();
1.396 +
1.397 + return 0;
1.398 +}