1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/max_flow_test.cc Wed Oct 17 19:14:07 2018 +0200
1.3 @@ -0,0 +1,440 @@
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-2013
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 +#include <lemon/tolerance.h>
1.33 +
1.34 +using namespace lemon;
1.35 +
1.36 +char test_lgf[] =
1.37 + "@nodes\n"
1.38 + "label\n"
1.39 + "0\n"
1.40 + "1\n"
1.41 + "2\n"
1.42 + "3\n"
1.43 + "4\n"
1.44 + "5\n"
1.45 + "6\n"
1.46 + "7\n"
1.47 + "8\n"
1.48 + "9\n"
1.49 + "@arcs\n"
1.50 + " label capacity\n"
1.51 + "0 1 0 20\n"
1.52 + "0 2 1 0\n"
1.53 + "1 1 2 3\n"
1.54 + "1 2 3 8\n"
1.55 + "1 3 4 8\n"
1.56 + "2 5 5 5\n"
1.57 + "3 2 6 5\n"
1.58 + "3 5 7 5\n"
1.59 + "3 6 8 5\n"
1.60 + "4 3 9 3\n"
1.61 + "5 7 10 3\n"
1.62 + "5 6 11 10\n"
1.63 + "5 8 12 10\n"
1.64 + "6 8 13 8\n"
1.65 + "8 9 14 20\n"
1.66 + "8 1 15 5\n"
1.67 + "9 5 16 5\n"
1.68 + "@attributes\n"
1.69 + "source 1\n"
1.70 + "target 8\n";
1.71 +
1.72 +char test_lgf_float[] =
1.73 + "@nodes\n"
1.74 + "label\n"
1.75 + "0\n"
1.76 + "1\n"
1.77 + "2\n"
1.78 + "3\n"
1.79 + "4\n"
1.80 + "5\n"
1.81 + "6\n"
1.82 + "7\n"
1.83 + "8\n"
1.84 + "9\n"
1.85 + "@arcs\n"
1.86 + " capacity\n"
1.87 + "0 1 0.1\n"
1.88 + "0 2 0.1\n"
1.89 + "0 3 0.1\n"
1.90 + "1 4 0.1\n"
1.91 + "2 4 0.1\n"
1.92 + "3 4 0.1\n"
1.93 + "4 5 0.3\n"
1.94 + "5 6 0.1\n"
1.95 + "5 7 0.1\n"
1.96 + "5 8 0.1\n"
1.97 + "6 9 0.1\n"
1.98 + "7 9 0.1\n"
1.99 + "8 9 0.1\n"
1.100 + "@attributes\n"
1.101 + "source 0\n"
1.102 + "target 9\n";
1.103 +
1.104 +// Checks the general interface of a max flow algorithm
1.105 +template <typename GR, typename CAP>
1.106 +struct MaxFlowClassConcept
1.107 +{
1.108 +
1.109 + template <typename MF>
1.110 + struct Constraints {
1.111 +
1.112 + typedef typename GR::Node Node;
1.113 + typedef typename GR::Arc Arc;
1.114 + typedef typename CAP::Value Value;
1.115 + typedef concepts::ReadWriteMap<Arc, Value> FlowMap;
1.116 + typedef concepts::WriteMap<Node, bool> CutMap;
1.117 +
1.118 + GR g;
1.119 + Node n;
1.120 + Arc e;
1.121 + CAP cap;
1.122 + FlowMap flow;
1.123 + CutMap cut;
1.124 + Value v;
1.125 + bool b;
1.126 +
1.127 + void constraints() {
1.128 + checkConcept<concepts::Digraph, GR>();
1.129 +
1.130 + const Constraints& me = *this;
1.131 +
1.132 + typedef typename MF
1.133 + ::template SetFlowMap<FlowMap>
1.134 + ::Create MaxFlowType;
1.135 + typedef typename MF::Create MaxFlowType2;
1.136 + MaxFlowType max_flow(me.g, me.cap, me.n, me.n);
1.137 + const MaxFlowType& const_max_flow = max_flow;
1.138 +
1.139 + max_flow
1.140 + .capacityMap(cap)
1.141 + .flowMap(flow)
1.142 + .source(n)
1.143 + .target(n);
1.144 +
1.145 + typename MaxFlowType::Tolerance tol = const_max_flow.tolerance();
1.146 + max_flow.tolerance(tol);
1.147 +
1.148 + max_flow.init();
1.149 + max_flow.init(cap);
1.150 + max_flow.run();
1.151 +
1.152 + v = const_max_flow.flowValue();
1.153 + v = const_max_flow.flow(e);
1.154 + const FlowMap& fm = const_max_flow.flowMap();
1.155 +
1.156 + b = const_max_flow.minCut(n);
1.157 + const_max_flow.minCutMap(cut);
1.158 +
1.159 + ::lemon::ignore_unused_variable_warning(fm);
1.160 + }
1.161 +
1.162 + };
1.163 +
1.164 +};
1.165 +
1.166 +// Checks the specific parts of Preflow's interface
1.167 +void checkPreflowCompile()
1.168 +{
1.169 + typedef int Value;
1.170 + typedef concepts::Digraph Digraph;
1.171 + typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
1.172 + typedef Elevator<Digraph, Digraph::Node> Elev;
1.173 + typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
1.174 +
1.175 + Digraph g;
1.176 + Digraph::Node n;
1.177 + CapMap cap;
1.178 +
1.179 + typedef Preflow<Digraph, CapMap>
1.180 + ::SetElevator<Elev>
1.181 + ::SetStandardElevator<LinkedElev>
1.182 + ::Create PreflowType;
1.183 + PreflowType preflow_test(g, cap, n, n);
1.184 + const PreflowType& const_preflow_test = preflow_test;
1.185 +
1.186 + const PreflowType::Elevator& elev = const_preflow_test.elevator();
1.187 + preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
1.188 +
1.189 + bool b = preflow_test.init(cap);
1.190 + preflow_test.startFirstPhase();
1.191 + preflow_test.startSecondPhase();
1.192 + preflow_test.runMinCut();
1.193 +
1.194 + ::lemon::ignore_unused_variable_warning(b);
1.195 +}
1.196 +
1.197 +// Checks the specific parts of EdmondsKarp's interface
1.198 +void checkEdmondsKarpCompile()
1.199 +{
1.200 + typedef int Value;
1.201 + typedef concepts::Digraph Digraph;
1.202 + typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
1.203 +
1.204 + Digraph g;
1.205 + Digraph::Node n;
1.206 + CapMap cap;
1.207 +
1.208 + EdmondsKarp<Digraph, CapMap> ek_test(g, cap, n, n);
1.209 +
1.210 + ek_test.init(cap);
1.211 + bool b = ek_test.checkedInit(cap);
1.212 + b = ek_test.augment();
1.213 + ek_test.start();
1.214 +
1.215 + ::lemon::ignore_unused_variable_warning(b);
1.216 +}
1.217 +
1.218 +
1.219 +template <typename T>
1.220 +T cutValue(const SmartDigraph& g,
1.221 + const SmartDigraph::NodeMap<bool>& cut,
1.222 + const SmartDigraph::ArcMap<T>& cap) {
1.223 +
1.224 + T c = 0;
1.225 + for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
1.226 + if (cut[g.source(e)] && !cut[g.target(e)]) c += cap[e];
1.227 + }
1.228 + return c;
1.229 +}
1.230 +
1.231 +template <typename T>
1.232 +bool checkFlow(const SmartDigraph& g,
1.233 + const SmartDigraph::ArcMap<T>& flow,
1.234 + const SmartDigraph::ArcMap<T>& cap,
1.235 + SmartDigraph::Node s, SmartDigraph::Node t,
1.236 + const Tolerance<T>& tol) {
1.237 +
1.238 + for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
1.239 + if (tol.negative(flow[e]) || tol.less(cap[e], flow[e])) return false;
1.240 + }
1.241 +
1.242 + for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
1.243 + if (n == s || n == t) continue;
1.244 + T sum = 0;
1.245 + for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
1.246 + sum += flow[e];
1.247 + }
1.248 + for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
1.249 + sum -= flow[e];
1.250 + }
1.251 + if (tol.nonZero(sum)) return false;
1.252 + }
1.253 + return true;
1.254 +}
1.255 +
1.256 +void checkInitPreflow()
1.257 +{
1.258 + DIGRAPH_TYPEDEFS(SmartDigraph);
1.259 +
1.260 + SmartDigraph g;
1.261 + SmartDigraph::ArcMap<int> cap(g), iflow(g);
1.262 + Node s = g.addNode(); Node t = g.addNode();
1.263 + Node n1 = g.addNode(); Node n2 = g.addNode();
1.264 + Arc a;
1.265 + a = g.addArc(s, n1); cap[a] = 20; iflow[a] = 20;
1.266 + a = g.addArc(n1, n2); cap[a] = 10; iflow[a] = 0;
1.267 + a = g.addArc(n2, t); cap[a] = 20; iflow[a] = 0;
1.268 +
1.269 + Preflow<SmartDigraph> pre(g, cap, s, t);
1.270 + pre.init(iflow);
1.271 + pre.startFirstPhase();
1.272 +
1.273 + check(pre.flowValue() == 10, "Incorrect max flow value.");
1.274 + check(pre.minCut(s), "Wrong min cut (Node s).");
1.275 + check(pre.minCut(n1), "Wrong min cut (Node n1).");
1.276 + check(!pre.minCut(n2), "Wrong min cut (Node n2).");
1.277 + check(!pre.minCut(t), "Wrong min cut (Node t).");
1.278 +}
1.279 +
1.280 +template <typename MF, typename SF>
1.281 +void checkMaxFlowAlg(const char *input_lgf, typename MF::Value expected) {
1.282 + typedef SmartDigraph Digraph;
1.283 + DIGRAPH_TYPEDEFS(Digraph);
1.284 +
1.285 + typedef typename MF::Value Value;
1.286 + typedef Digraph::ArcMap<Value> CapMap;
1.287 + typedef CapMap FlowMap;
1.288 + typedef BoolNodeMap CutMap;
1.289 +
1.290 + Tolerance<Value> tol;
1.291 +
1.292 + Digraph g;
1.293 + Node s, t;
1.294 + CapMap cap(g);
1.295 + std::istringstream input(input_lgf);
1.296 + DigraphReader<Digraph>(g, input)
1.297 + .arcMap("capacity", cap)
1.298 + .node("source", s)
1.299 + .node("target", t)
1.300 + .run();
1.301 +
1.302 + MF max_flow(g, cap, s, t);
1.303 + max_flow.run();
1.304 +
1.305 + check(!tol.different(expected, max_flow.flowValue()),
1.306 + "Incorrect max flow value.");
1.307 + check(checkFlow(g, max_flow.flowMap(), cap, s, t, tol),
1.308 + "The flow is not feasible.");
1.309 +
1.310 + CutMap min_cut(g);
1.311 + max_flow.minCutMap(min_cut);
1.312 + Value min_cut_value = cutValue(g, min_cut, cap);
1.313 +
1.314 + check(!tol.different(expected, min_cut_value),
1.315 + "Incorrect min cut value.");
1.316 +
1.317 + FlowMap flow(g);
1.318 + for (ArcIt e(g); e != INVALID; ++e) flow[e] = 13 * max_flow.flowMap()[e];
1.319 + for (ArcIt e(g); e != INVALID; ++e) cap[e] = 17 * cap[e];
1.320 + max_flow.init(flow);
1.321 +
1.322 + SF::startFirstPhase(max_flow); // start first phase of the algorithm
1.323 +
1.324 + CutMap min_cut1(g);
1.325 + max_flow.minCutMap(min_cut1);
1.326 + min_cut_value = cutValue(g, min_cut1, cap);
1.327 +
1.328 + check(!tol.different(17 * expected, max_flow.flowValue()),
1.329 + "Incorrect max flow value.");
1.330 + check(!tol.different(17 * expected, min_cut_value),
1.331 + "Incorrect min cut value.");
1.332 +
1.333 + SF::startSecondPhase(max_flow); // start second phase of the algorithm
1.334 +
1.335 + check(checkFlow(g, max_flow.flowMap(), cap, s, t, tol),
1.336 + "The flow is not feasible.");
1.337 +
1.338 + CutMap min_cut2(g);
1.339 + max_flow.minCutMap(min_cut2);
1.340 + min_cut_value = cutValue(g, min_cut2, cap);
1.341 +
1.342 + check(!tol.different(17 * expected, max_flow.flowValue()),
1.343 + "Incorrect max flow value.");
1.344 + check(!tol.different(17 * expected, min_cut_value),
1.345 + "Incorrect min cut value.");
1.346 +
1.347 + max_flow.flowMap(flow);
1.348 +
1.349 + NodeIt tmp1(g, s);
1.350 + ++tmp1;
1.351 + if (tmp1 != INVALID) s = tmp1;
1.352 +
1.353 + NodeIt tmp2(g, t);
1.354 + ++tmp2;
1.355 + if (tmp2 != INVALID) t = tmp2;
1.356 +
1.357 + max_flow.source(s);
1.358 + max_flow.target(t);
1.359 +
1.360 + max_flow.run();
1.361 +
1.362 + CutMap min_cut3(g);
1.363 + max_flow.minCutMap(min_cut3);
1.364 + min_cut_value = cutValue(g, min_cut3, cap);
1.365 +
1.366 + check(!tol.different(max_flow.flowValue(), min_cut_value),
1.367 + "The max flow value or the min cut value is wrong.");
1.368 +}
1.369 +
1.370 +// Struct for calling start functions of a general max flow algorithm
1.371 +template <typename MF>
1.372 +struct GeneralStartFunctions {
1.373 +
1.374 + static void startFirstPhase(MF& mf) {
1.375 + mf.start();
1.376 + }
1.377 +
1.378 + static void startSecondPhase(MF& mf) {
1.379 + ::lemon::ignore_unused_variable_warning(mf);
1.380 + }
1.381 +
1.382 +};
1.383 +
1.384 +// Struct for calling start functions of Preflow
1.385 +template <typename MF>
1.386 +struct PreflowStartFunctions {
1.387 +
1.388 + static void startFirstPhase(MF& mf) {
1.389 + mf.startFirstPhase();
1.390 + }
1.391 +
1.392 + static void startSecondPhase(MF& mf) {
1.393 + mf.startSecondPhase();
1.394 + }
1.395 +
1.396 +};
1.397 +
1.398 +int main() {
1.399 +
1.400 + typedef concepts::Digraph GR;
1.401 + typedef concepts::ReadMap<GR::Arc, int> CM1;
1.402 + typedef concepts::ReadMap<GR::Arc, double> CM2;
1.403 +
1.404 + // Check the interface of Preflow
1.405 + checkConcept< MaxFlowClassConcept<GR, CM1>,
1.406 + Preflow<GR, CM1> >();
1.407 + checkConcept< MaxFlowClassConcept<GR, CM2>,
1.408 + Preflow<GR, CM2> >();
1.409 +
1.410 + // Check the interface of EdmondsKarp
1.411 + checkConcept< MaxFlowClassConcept<GR, CM1>,
1.412 + EdmondsKarp<GR, CM1> >();
1.413 + checkConcept< MaxFlowClassConcept<GR, CM2>,
1.414 + EdmondsKarp<GR, CM2> >();
1.415 +
1.416 + // Check Preflow
1.417 + typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<int> > PType1;
1.418 + typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<float> > PType2;
1.419 + typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<double> > PType3;
1.420 +
1.421 + checkMaxFlowAlg<PType1, PreflowStartFunctions<PType1> >(test_lgf, 13);
1.422 + checkMaxFlowAlg<PType2, PreflowStartFunctions<PType2> >(test_lgf, 13);
1.423 + checkMaxFlowAlg<PType3, PreflowStartFunctions<PType3> >(test_lgf, 13);
1.424 +
1.425 + checkMaxFlowAlg<PType2, PreflowStartFunctions<PType2> >(test_lgf_float, 0.3f);
1.426 + checkMaxFlowAlg<PType3, PreflowStartFunctions<PType3> >(test_lgf_float, 0.3);
1.427 +
1.428 + checkInitPreflow();
1.429 +
1.430 + // Check EdmondsKarp
1.431 + typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<int> > EKType1;
1.432 + typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<float> > EKType2;
1.433 + typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<double> > EKType3;
1.434 +
1.435 + checkMaxFlowAlg<EKType1, GeneralStartFunctions<EKType1> >(test_lgf, 13);
1.436 + checkMaxFlowAlg<EKType2, GeneralStartFunctions<EKType2> >(test_lgf, 13);
1.437 + checkMaxFlowAlg<EKType3, GeneralStartFunctions<EKType3> >(test_lgf, 13);
1.438 +
1.439 + checkMaxFlowAlg<EKType2, GeneralStartFunctions<EKType2> >(test_lgf_float, 0.3f);
1.440 + checkMaxFlowAlg<EKType3, GeneralStartFunctions<EKType3> >(test_lgf_float, 0.3);
1.441 +
1.442 + return 0;
1.443 +}