test/max_flow_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 14 May 2015 16:07:38 +0200
changeset 1142 2f479109a71d
parent 1087 dd1443e4a34c
child 1165 e0ccc1f0268f
permissions -rw-r--r--
Documentation for VF2 (#597)

The implementation of this feature was sponsored by QuantumBio Inc.
alpar@389
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@389
     2
 *
alpar@389
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@389
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
alpar@389
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@389
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@389
     8
 *
alpar@389
     9
 * Permission to use, modify and distribute this software is granted
alpar@389
    10
 * provided that this copyright notice appears in all copies. For
alpar@389
    11
 * precise terms see the accompanying LICENSE file.
alpar@389
    12
 *
alpar@389
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@389
    14
 * express or implied, and with no claim as to its suitability for any
alpar@389
    15
 * purpose.
alpar@389
    16
 *
alpar@389
    17
 */
alpar@389
    18
alpar@423
    19
#include <iostream>
alpar@389
    20
alpar@389
    21
#include "test_tools.h"
alpar@389
    22
#include <lemon/smart_graph.h>
alpar@389
    23
#include <lemon/preflow.h>
kpeter@1060
    24
#include <lemon/edmonds_karp.h>
alpar@389
    25
#include <lemon/concepts/digraph.h>
alpar@389
    26
#include <lemon/concepts/maps.h>
alpar@389
    27
#include <lemon/lgf_reader.h>
kpeter@394
    28
#include <lemon/elevator.h>
alpar@389
    29
alpar@389
    30
using namespace lemon;
alpar@389
    31
alpar@423
    32
char test_lgf[] =
alpar@423
    33
  "@nodes\n"
alpar@423
    34
  "label\n"
alpar@423
    35
  "0\n"
alpar@423
    36
  "1\n"
alpar@423
    37
  "2\n"
alpar@423
    38
  "3\n"
alpar@423
    39
  "4\n"
alpar@423
    40
  "5\n"
alpar@423
    41
  "6\n"
alpar@423
    42
  "7\n"
alpar@423
    43
  "8\n"
alpar@423
    44
  "9\n"
alpar@423
    45
  "@arcs\n"
alpar@423
    46
  "    label capacity\n"
alpar@423
    47
  "0 1 0     20\n"
alpar@423
    48
  "0 2 1     0\n"
alpar@423
    49
  "1 1 2     3\n"
alpar@423
    50
  "1 2 3     8\n"
alpar@423
    51
  "1 3 4     8\n"
alpar@423
    52
  "2 5 5     5\n"
alpar@423
    53
  "3 2 6     5\n"
alpar@423
    54
  "3 5 7     5\n"
alpar@423
    55
  "3 6 8     5\n"
alpar@423
    56
  "4 3 9     3\n"
alpar@423
    57
  "5 7 10    3\n"
alpar@423
    58
  "5 6 11    10\n"
alpar@423
    59
  "5 8 12    10\n"
alpar@423
    60
  "6 8 13    8\n"
alpar@423
    61
  "8 9 14    20\n"
alpar@423
    62
  "8 1 15    5\n"
alpar@423
    63
  "9 5 16    5\n"
alpar@423
    64
  "@attributes\n"
alpar@423
    65
  "source 1\n"
alpar@423
    66
  "target 8\n";
alpar@423
    67
kpeter@1060
    68
kpeter@1060
    69
// Checks the general interface of a max flow algorithm
kpeter@1060
    70
template <typename GR, typename CAP>
kpeter@1060
    71
struct MaxFlowClassConcept
kpeter@1060
    72
{
kpeter@1060
    73
kpeter@1060
    74
  template <typename MF>
kpeter@1060
    75
  struct Constraints {
kpeter@1060
    76
kpeter@1060
    77
    typedef typename GR::Node Node;
kpeter@1060
    78
    typedef typename GR::Arc Arc;
kpeter@1060
    79
    typedef typename CAP::Value Value;
kpeter@1060
    80
    typedef concepts::ReadWriteMap<Arc, Value> FlowMap;
kpeter@1060
    81
    typedef concepts::WriteMap<Node, bool> CutMap;
kpeter@1060
    82
kpeter@1060
    83
    GR g;
kpeter@1060
    84
    Node n;
kpeter@1060
    85
    Arc e;
kpeter@1060
    86
    CAP cap;
kpeter@1060
    87
    FlowMap flow;
kpeter@1060
    88
    CutMap cut;
kpeter@1060
    89
    Value v;
kpeter@1060
    90
    bool b;
kpeter@1060
    91
kpeter@1060
    92
    void constraints() {
kpeter@1060
    93
      checkConcept<concepts::Digraph, GR>();
kpeter@1060
    94
kpeter@1060
    95
      const Constraints& me = *this;
kpeter@1060
    96
kpeter@1060
    97
      typedef typename MF
kpeter@1060
    98
          ::template SetFlowMap<FlowMap>
kpeter@1060
    99
          ::Create MaxFlowType;
kpeter@1060
   100
      typedef typename MF::Create MaxFlowType2;
kpeter@1060
   101
      MaxFlowType max_flow(me.g, me.cap, me.n, me.n);
kpeter@1060
   102
      const MaxFlowType& const_max_flow = max_flow;
kpeter@1060
   103
kpeter@1060
   104
      max_flow
kpeter@1060
   105
          .capacityMap(cap)
kpeter@1060
   106
          .flowMap(flow)
kpeter@1060
   107
          .source(n)
kpeter@1060
   108
          .target(n);
kpeter@1060
   109
kpeter@1060
   110
      typename MaxFlowType::Tolerance tol = const_max_flow.tolerance();
kpeter@1060
   111
      max_flow.tolerance(tol);
kpeter@1060
   112
kpeter@1060
   113
      max_flow.init();
kpeter@1060
   114
      max_flow.init(cap);
kpeter@1060
   115
      max_flow.run();
kpeter@1060
   116
kpeter@1060
   117
      v = const_max_flow.flowValue();
kpeter@1060
   118
      v = const_max_flow.flow(e);
kpeter@1060
   119
      const FlowMap& fm = const_max_flow.flowMap();
kpeter@1060
   120
kpeter@1060
   121
      b = const_max_flow.minCut(n);
kpeter@1060
   122
      const_max_flow.minCutMap(cut);
kpeter@1060
   123
alpar@1087
   124
      ::lemon::ignore_unused_variable_warning(fm);
kpeter@1060
   125
    }
kpeter@1060
   126
kpeter@1060
   127
  };
kpeter@1060
   128
kpeter@1060
   129
};
kpeter@1060
   130
kpeter@1060
   131
// Checks the specific parts of Preflow's interface
kpeter@394
   132
void checkPreflowCompile()
alpar@389
   133
{
kpeter@1060
   134
  typedef int Value;
alpar@389
   135
  typedef concepts::Digraph Digraph;
kpeter@1060
   136
  typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
kpeter@394
   137
  typedef Elevator<Digraph, Digraph::Node> Elev;
kpeter@394
   138
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
kpeter@394
   139
alpar@389
   140
  Digraph g;
kpeter@1060
   141
  Digraph::Node n;
alpar@389
   142
  CapMap cap;
alpar@389
   143
kpeter@585
   144
  typedef Preflow<Digraph, CapMap>
kpeter@1060
   145
      ::SetElevator<Elev>
kpeter@1060
   146
      ::SetStandardElevator<LinkedElev>
kpeter@1060
   147
      ::Create PreflowType;
kpeter@585
   148
  PreflowType preflow_test(g, cap, n, n);
kpeter@585
   149
  const PreflowType& const_preflow_test = preflow_test;
alpar@389
   150
kpeter@689
   151
  const PreflowType::Elevator& elev = const_preflow_test.elevator();
kpeter@689
   152
  preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
kpeter@585
   153
kpeter@1060
   154
  bool b = preflow_test.init(cap);
alpar@389
   155
  preflow_test.startFirstPhase();
alpar@389
   156
  preflow_test.startSecondPhase();
alpar@389
   157
  preflow_test.runMinCut();
alpar@389
   158
alpar@1087
   159
  ::lemon::ignore_unused_variable_warning(b);
alpar@389
   160
}
alpar@389
   161
kpeter@1060
   162
// Checks the specific parts of EdmondsKarp's interface
kpeter@1060
   163
void checkEdmondsKarpCompile()
kpeter@1060
   164
{
kpeter@1060
   165
  typedef int Value;
kpeter@1060
   166
  typedef concepts::Digraph Digraph;
kpeter@1060
   167
  typedef concepts::ReadMap<Digraph::Arc, Value> CapMap;
kpeter@1060
   168
  typedef Elevator<Digraph, Digraph::Node> Elev;
kpeter@1060
   169
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
kpeter@1060
   170
kpeter@1060
   171
  Digraph g;
kpeter@1060
   172
  Digraph::Node n;
kpeter@1060
   173
  CapMap cap;
kpeter@1060
   174
kpeter@1060
   175
  EdmondsKarp<Digraph, CapMap> ek_test(g, cap, n, n);
kpeter@1060
   176
kpeter@1060
   177
  ek_test.init(cap);
kpeter@1060
   178
  bool b = ek_test.checkedInit(cap);
kpeter@1060
   179
  b = ek_test.augment();
kpeter@1060
   180
  ek_test.start();
kpeter@1060
   181
alpar@1087
   182
  ::lemon::ignore_unused_variable_warning(b);
kpeter@1060
   183
}
kpeter@1060
   184
kpeter@1060
   185
kpeter@1060
   186
template <typename T>
kpeter@1060
   187
T cutValue (const SmartDigraph& g,
alpar@389
   188
              const SmartDigraph::NodeMap<bool>& cut,
kpeter@1060
   189
              const SmartDigraph::ArcMap<T>& cap) {
alpar@389
   190
kpeter@1060
   191
  T c=0;
alpar@389
   192
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
alpar@389
   193
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
alpar@389
   194
  }
alpar@389
   195
  return c;
alpar@389
   196
}
alpar@389
   197
kpeter@1060
   198
template <typename T>
alpar@389
   199
bool checkFlow(const SmartDigraph& g,
kpeter@1060
   200
               const SmartDigraph::ArcMap<T>& flow,
kpeter@1060
   201
               const SmartDigraph::ArcMap<T>& cap,
alpar@389
   202
               SmartDigraph::Node s, SmartDigraph::Node t) {
alpar@389
   203
alpar@389
   204
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
alpar@389
   205
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
alpar@389
   206
  }
alpar@389
   207
alpar@389
   208
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
alpar@389
   209
    if (n == s || n == t) continue;
kpeter@1060
   210
    T sum = 0;
alpar@389
   211
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   212
      sum += flow[e];
alpar@389
   213
    }
alpar@389
   214
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   215
      sum -= flow[e];
alpar@389
   216
    }
alpar@389
   217
    if (sum != 0) return false;
alpar@389
   218
  }
alpar@389
   219
  return true;
alpar@389
   220
}
alpar@389
   221
alpar@923
   222
void initFlowTest()
alpar@923
   223
{
alpar@923
   224
  DIGRAPH_TYPEDEFS(SmartDigraph);
alpar@1092
   225
alpar@923
   226
  SmartDigraph g;
alpar@923
   227
  SmartDigraph::ArcMap<int> cap(g),iflow(g);
alpar@923
   228
  Node s=g.addNode(); Node t=g.addNode();
alpar@923
   229
  Node n1=g.addNode(); Node n2=g.addNode();
alpar@923
   230
  Arc a;
alpar@923
   231
  a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
alpar@923
   232
  a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
alpar@923
   233
  a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
alpar@923
   234
alpar@923
   235
  Preflow<SmartDigraph> pre(g,cap,s,t);
alpar@923
   236
  pre.init(iflow);
alpar@923
   237
  pre.startFirstPhase();
alpar@923
   238
  check(pre.flowValue() == 10, "The incorrect max flow value.");
alpar@923
   239
  check(pre.minCut(s), "Wrong min cut (Node s).");
alpar@923
   240
  check(pre.minCut(n1), "Wrong min cut (Node n1).");
alpar@923
   241
  check(!pre.minCut(n2), "Wrong min cut (Node n2).");
alpar@923
   242
  check(!pre.minCut(t), "Wrong min cut (Node t).");
alpar@923
   243
}
alpar@923
   244
kpeter@1060
   245
template <typename MF, typename SF>
kpeter@1060
   246
void checkMaxFlowAlg() {
kpeter@1060
   247
  typedef SmartDigraph Digraph;
kpeter@1060
   248
  DIGRAPH_TYPEDEFS(Digraph);
alpar@923
   249
kpeter@1060
   250
  typedef typename MF::Value Value;
kpeter@1060
   251
  typedef Digraph::ArcMap<Value> CapMap;
kpeter@1060
   252
  typedef CapMap FlowMap;
kpeter@1060
   253
  typedef BoolNodeMap CutMap;
alpar@389
   254
alpar@389
   255
  Digraph g;
alpar@389
   256
  Node s, t;
alpar@389
   257
  CapMap cap(g);
alpar@423
   258
  std::istringstream input(test_lgf);
kpeter@1060
   259
  DigraphReader<Digraph>(g,input)
kpeter@1060
   260
      .arcMap("capacity", cap)
kpeter@1060
   261
      .node("source",s)
kpeter@1060
   262
      .node("target",t)
kpeter@1060
   263
      .run();
alpar@389
   264
kpeter@1060
   265
  MF max_flow(g, cap, s, t);
kpeter@1060
   266
  max_flow.run();
alpar@389
   267
kpeter@1060
   268
  check(checkFlow(g, max_flow.flowMap(), cap, s, t),
alpar@389
   269
        "The flow is not feasible.");
alpar@389
   270
alpar@389
   271
  CutMap min_cut(g);
kpeter@1060
   272
  max_flow.minCutMap(min_cut);
kpeter@1060
   273
  Value min_cut_value = cutValue(g, min_cut, cap);
alpar@389
   274
kpeter@1060
   275
  check(max_flow.flowValue() == min_cut_value,
kpeter@1060
   276
        "The max flow value is not equal to the min cut value.");
alpar@389
   277
alpar@389
   278
  FlowMap flow(g);
kpeter@1060
   279
  for (ArcIt e(g); e != INVALID; ++e) flow[e] = max_flow.flowMap()[e];
alpar@389
   280
kpeter@1060
   281
  Value flow_value = max_flow.flowValue();
alpar@389
   282
kpeter@1060
   283
  for (ArcIt e(g); e != INVALID; ++e) cap[e] = 2 * cap[e];
kpeter@1060
   284
  max_flow.init(flow);
kpeter@1060
   285
kpeter@1060
   286
  SF::startFirstPhase(max_flow);       // start first phase of the algorithm
alpar@389
   287
alpar@389
   288
  CutMap min_cut1(g);
kpeter@1060
   289
  max_flow.minCutMap(min_cut1);
kpeter@1060
   290
  min_cut_value = cutValue(g, min_cut1, cap);
alpar@389
   291
kpeter@1060
   292
  check(max_flow.flowValue() == min_cut_value &&
kpeter@1060
   293
        min_cut_value == 2 * flow_value,
alpar@389
   294
        "The max flow value or the min cut value is wrong.");
alpar@389
   295
kpeter@1060
   296
  SF::startSecondPhase(max_flow);       // start second phase of the algorithm
alpar@389
   297
kpeter@1060
   298
  check(checkFlow(g, max_flow.flowMap(), cap, s, t),
alpar@389
   299
        "The flow is not feasible.");
alpar@389
   300
alpar@389
   301
  CutMap min_cut2(g);
kpeter@1060
   302
  max_flow.minCutMap(min_cut2);
kpeter@1060
   303
  min_cut_value = cutValue(g, min_cut2, cap);
alpar@389
   304
kpeter@1060
   305
  check(max_flow.flowValue() == min_cut_value &&
kpeter@1060
   306
        min_cut_value == 2 * flow_value,
kpeter@1060
   307
        "The max flow value or the min cut value was not doubled");
alpar@389
   308
alpar@389
   309
kpeter@1060
   310
  max_flow.flowMap(flow);
alpar@389
   311
kpeter@1060
   312
  NodeIt tmp1(g, s);
alpar@389
   313
  ++tmp1;
kpeter@1060
   314
  if (tmp1 != INVALID) s = tmp1;
alpar@389
   315
kpeter@1060
   316
  NodeIt tmp2(g, t);
alpar@389
   317
  ++tmp2;
kpeter@1060
   318
  if (tmp2 != INVALID) t = tmp2;
alpar@389
   319
kpeter@1060
   320
  max_flow.source(s);
kpeter@1060
   321
  max_flow.target(t);
alpar@389
   322
kpeter@1060
   323
  max_flow.run();
alpar@389
   324
alpar@389
   325
  CutMap min_cut3(g);
kpeter@1060
   326
  max_flow.minCutMap(min_cut3);
kpeter@1060
   327
  min_cut_value=cutValue(g, min_cut3, cap);
alpar@389
   328
kpeter@1060
   329
  check(max_flow.flowValue() == min_cut_value,
kpeter@1060
   330
        "The max flow value or the min cut value is wrong.");
kpeter@1060
   331
}
alpar@389
   332
kpeter@1060
   333
// Struct for calling start functions of a general max flow algorithm
kpeter@1060
   334
template <typename MF>
kpeter@1060
   335
struct GeneralStartFunctions {
kpeter@1060
   336
kpeter@1060
   337
  static void startFirstPhase(MF& mf) {
kpeter@1060
   338
    mf.start();
kpeter@1060
   339
  }
kpeter@1060
   340
kpeter@1060
   341
  static void startSecondPhase(MF& mf) {
alpar@1087
   342
    ::lemon::ignore_unused_variable_warning(mf);
kpeter@1060
   343
  }
kpeter@1060
   344
kpeter@1060
   345
};
kpeter@1060
   346
kpeter@1060
   347
// Struct for calling start functions of Preflow
kpeter@1060
   348
template <typename MF>
kpeter@1060
   349
struct PreflowStartFunctions {
kpeter@1060
   350
kpeter@1060
   351
  static void startFirstPhase(MF& mf) {
kpeter@1060
   352
    mf.startFirstPhase();
kpeter@1060
   353
  }
kpeter@1060
   354
kpeter@1060
   355
  static void startSecondPhase(MF& mf) {
kpeter@1060
   356
    mf.startSecondPhase();
kpeter@1060
   357
  }
kpeter@1060
   358
kpeter@1060
   359
};
kpeter@1060
   360
kpeter@1060
   361
int main() {
kpeter@1060
   362
kpeter@1060
   363
  typedef concepts::Digraph GR;
kpeter@1060
   364
  typedef concepts::ReadMap<GR::Arc, int> CM1;
kpeter@1060
   365
  typedef concepts::ReadMap<GR::Arc, double> CM2;
kpeter@1060
   366
kpeter@1060
   367
  // Check the interface of Preflow
kpeter@1060
   368
  checkConcept< MaxFlowClassConcept<GR, CM1>,
kpeter@1060
   369
                Preflow<GR, CM1> >();
kpeter@1060
   370
  checkConcept< MaxFlowClassConcept<GR, CM2>,
kpeter@1060
   371
                Preflow<GR, CM2> >();
kpeter@1060
   372
kpeter@1060
   373
  // Check the interface of EdmondsKarp
kpeter@1060
   374
  checkConcept< MaxFlowClassConcept<GR, CM1>,
kpeter@1060
   375
                EdmondsKarp<GR, CM1> >();
kpeter@1060
   376
  checkConcept< MaxFlowClassConcept<GR, CM2>,
kpeter@1060
   377
                EdmondsKarp<GR, CM2> >();
kpeter@1060
   378
kpeter@1060
   379
  // Check Preflow
kpeter@1060
   380
  typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<int> > PType1;
kpeter@1060
   381
  typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<float> > PType2;
kpeter@1060
   382
  checkMaxFlowAlg<PType1, PreflowStartFunctions<PType1> >();
kpeter@1060
   383
  checkMaxFlowAlg<PType2, PreflowStartFunctions<PType2> >();
kpeter@1060
   384
  initFlowTest();
alpar@1092
   385
kpeter@1060
   386
  // Check EdmondsKarp
kpeter@1060
   387
  typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<int> > EKType1;
kpeter@1060
   388
  typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<float> > EKType2;
kpeter@1060
   389
  checkMaxFlowAlg<EKType1, GeneralStartFunctions<EKType1> >();
kpeter@1060
   390
  checkMaxFlowAlg<EKType2, GeneralStartFunctions<EKType2> >();
alpar@389
   391
alpar@923
   392
  initFlowTest();
alpar@1092
   393
alpar@389
   394
  return 0;
alpar@389
   395
}