test/max_flow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 22 Mar 2018 18:55:31 +0100
changeset 1166 e2732b9da429
parent 1165 e0ccc1f0268f
child 1167 e018899c2926
permissions -rw-r--r--
Refactoring and code formatting in max_flow_test.cc (#608)
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
kpeter@1060
   169
  Digraph g;
kpeter@1060
   170
  Digraph::Node n;
kpeter@1060
   171
  CapMap cap;
kpeter@1060
   172
kpeter@1060
   173
  EdmondsKarp<Digraph, CapMap> ek_test(g, cap, n, n);
kpeter@1060
   174
kpeter@1060
   175
  ek_test.init(cap);
kpeter@1060
   176
  bool b = ek_test.checkedInit(cap);
kpeter@1060
   177
  b = ek_test.augment();
kpeter@1060
   178
  ek_test.start();
kpeter@1060
   179
alpar@1087
   180
  ::lemon::ignore_unused_variable_warning(b);
kpeter@1060
   181
}
kpeter@1060
   182
kpeter@1060
   183
kpeter@1060
   184
template <typename T>
kpeter@1166
   185
T cutValue(const SmartDigraph& g,
kpeter@1166
   186
           const SmartDigraph::NodeMap<bool>& cut,
kpeter@1166
   187
           const SmartDigraph::ArcMap<T>& cap) {
alpar@389
   188
kpeter@1166
   189
  T c = 0;
kpeter@1166
   190
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
kpeter@1166
   191
    if (cut[g.source(e)] && !cut[g.target(e)]) c += cap[e];
alpar@389
   192
  }
alpar@389
   193
  return c;
alpar@389
   194
}
alpar@389
   195
kpeter@1060
   196
template <typename T>
alpar@389
   197
bool checkFlow(const SmartDigraph& g,
kpeter@1060
   198
               const SmartDigraph::ArcMap<T>& flow,
kpeter@1060
   199
               const SmartDigraph::ArcMap<T>& cap,
alpar@389
   200
               SmartDigraph::Node s, SmartDigraph::Node t) {
alpar@389
   201
alpar@389
   202
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
alpar@389
   203
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
alpar@389
   204
  }
alpar@389
   205
alpar@389
   206
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
alpar@389
   207
    if (n == s || n == t) continue;
kpeter@1060
   208
    T sum = 0;
alpar@389
   209
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   210
      sum += flow[e];
alpar@389
   211
    }
alpar@389
   212
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   213
      sum -= flow[e];
alpar@389
   214
    }
alpar@389
   215
    if (sum != 0) return false;
alpar@389
   216
  }
alpar@389
   217
  return true;
alpar@389
   218
}
alpar@389
   219
kpeter@1166
   220
void checkInitPreflow()
alpar@923
   221
{
alpar@923
   222
  DIGRAPH_TYPEDEFS(SmartDigraph);
alpar@1092
   223
alpar@923
   224
  SmartDigraph g;
kpeter@1166
   225
  SmartDigraph::ArcMap<int> cap(g), iflow(g);
kpeter@1166
   226
  Node s = g.addNode(); Node t = g.addNode();
kpeter@1166
   227
  Node n1 = g.addNode(); Node n2 = g.addNode();
alpar@923
   228
  Arc a;
kpeter@1166
   229
  a = g.addArc(s, n1); cap[a] = 20; iflow[a] = 20;
kpeter@1166
   230
  a = g.addArc(n1, n2); cap[a] = 10; iflow[a] = 0;
kpeter@1166
   231
  a = g.addArc(n2, t); cap[a] = 20; iflow[a] = 0;
alpar@923
   232
kpeter@1166
   233
  Preflow<SmartDigraph> pre(g, cap, s, t);
alpar@923
   234
  pre.init(iflow);
alpar@923
   235
  pre.startFirstPhase();
kpeter@1166
   236
kpeter@1166
   237
  check(pre.flowValue() == 10, "Incorrect max flow value.");
alpar@923
   238
  check(pre.minCut(s), "Wrong min cut (Node s).");
alpar@923
   239
  check(pre.minCut(n1), "Wrong min cut (Node n1).");
alpar@923
   240
  check(!pre.minCut(n2), "Wrong min cut (Node n2).");
alpar@923
   241
  check(!pre.minCut(t), "Wrong min cut (Node t).");
alpar@923
   242
}
alpar@923
   243
kpeter@1060
   244
template <typename MF, typename SF>
kpeter@1060
   245
void checkMaxFlowAlg() {
kpeter@1060
   246
  typedef SmartDigraph Digraph;
kpeter@1060
   247
  DIGRAPH_TYPEDEFS(Digraph);
alpar@923
   248
kpeter@1060
   249
  typedef typename MF::Value Value;
kpeter@1060
   250
  typedef Digraph::ArcMap<Value> CapMap;
kpeter@1060
   251
  typedef CapMap FlowMap;
kpeter@1060
   252
  typedef BoolNodeMap CutMap;
alpar@389
   253
alpar@389
   254
  Digraph g;
alpar@389
   255
  Node s, t;
alpar@389
   256
  CapMap cap(g);
alpar@423
   257
  std::istringstream input(test_lgf);
kpeter@1060
   258
  DigraphReader<Digraph>(g,input)
kpeter@1060
   259
      .arcMap("capacity", cap)
kpeter@1060
   260
      .node("source",s)
kpeter@1060
   261
      .node("target",t)
kpeter@1060
   262
      .run();
alpar@389
   263
kpeter@1060
   264
  MF max_flow(g, cap, s, t);
kpeter@1060
   265
  max_flow.run();
alpar@389
   266
kpeter@1060
   267
  check(checkFlow(g, max_flow.flowMap(), cap, s, t),
alpar@389
   268
        "The flow is not feasible.");
alpar@389
   269
alpar@389
   270
  CutMap min_cut(g);
kpeter@1060
   271
  max_flow.minCutMap(min_cut);
kpeter@1060
   272
  Value min_cut_value = cutValue(g, min_cut, cap);
alpar@389
   273
kpeter@1060
   274
  check(max_flow.flowValue() == min_cut_value,
kpeter@1060
   275
        "The max flow value is not equal to the min cut value.");
alpar@389
   276
alpar@389
   277
  FlowMap flow(g);
kpeter@1060
   278
  for (ArcIt e(g); e != INVALID; ++e) flow[e] = max_flow.flowMap()[e];
alpar@389
   279
kpeter@1060
   280
  Value flow_value = max_flow.flowValue();
alpar@389
   281
kpeter@1060
   282
  for (ArcIt e(g); e != INVALID; ++e) cap[e] = 2 * cap[e];
kpeter@1060
   283
  max_flow.init(flow);
kpeter@1060
   284
kpeter@1060
   285
  SF::startFirstPhase(max_flow);       // start first phase of the algorithm
alpar@389
   286
alpar@389
   287
  CutMap min_cut1(g);
kpeter@1060
   288
  max_flow.minCutMap(min_cut1);
kpeter@1060
   289
  min_cut_value = cutValue(g, min_cut1, cap);
alpar@389
   290
kpeter@1060
   291
  check(max_flow.flowValue() == min_cut_value &&
kpeter@1060
   292
        min_cut_value == 2 * flow_value,
alpar@389
   293
        "The max flow value or the min cut value is wrong.");
alpar@389
   294
kpeter@1060
   295
  SF::startSecondPhase(max_flow);       // start second phase of the algorithm
alpar@389
   296
kpeter@1060
   297
  check(checkFlow(g, max_flow.flowMap(), cap, s, t),
alpar@389
   298
        "The flow is not feasible.");
alpar@389
   299
alpar@389
   300
  CutMap min_cut2(g);
kpeter@1060
   301
  max_flow.minCutMap(min_cut2);
kpeter@1060
   302
  min_cut_value = cutValue(g, min_cut2, cap);
alpar@389
   303
kpeter@1060
   304
  check(max_flow.flowValue() == min_cut_value &&
kpeter@1060
   305
        min_cut_value == 2 * flow_value,
kpeter@1166
   306
        "The max flow value or the min cut value was not doubled.");
alpar@389
   307
kpeter@1060
   308
  max_flow.flowMap(flow);
alpar@389
   309
kpeter@1060
   310
  NodeIt tmp1(g, s);
alpar@389
   311
  ++tmp1;
kpeter@1060
   312
  if (tmp1 != INVALID) s = tmp1;
alpar@389
   313
kpeter@1060
   314
  NodeIt tmp2(g, t);
alpar@389
   315
  ++tmp2;
kpeter@1060
   316
  if (tmp2 != INVALID) t = tmp2;
alpar@389
   317
kpeter@1060
   318
  max_flow.source(s);
kpeter@1060
   319
  max_flow.target(t);
alpar@389
   320
kpeter@1060
   321
  max_flow.run();
alpar@389
   322
alpar@389
   323
  CutMap min_cut3(g);
kpeter@1060
   324
  max_flow.minCutMap(min_cut3);
kpeter@1166
   325
  min_cut_value = cutValue(g, min_cut3, cap);
alpar@389
   326
kpeter@1060
   327
  check(max_flow.flowValue() == min_cut_value,
kpeter@1060
   328
        "The max flow value or the min cut value is wrong.");
kpeter@1060
   329
}
alpar@389
   330
kpeter@1060
   331
// Struct for calling start functions of a general max flow algorithm
kpeter@1060
   332
template <typename MF>
kpeter@1060
   333
struct GeneralStartFunctions {
kpeter@1060
   334
kpeter@1060
   335
  static void startFirstPhase(MF& mf) {
kpeter@1060
   336
    mf.start();
kpeter@1060
   337
  }
kpeter@1060
   338
kpeter@1060
   339
  static void startSecondPhase(MF& mf) {
alpar@1087
   340
    ::lemon::ignore_unused_variable_warning(mf);
kpeter@1060
   341
  }
kpeter@1060
   342
kpeter@1060
   343
};
kpeter@1060
   344
kpeter@1060
   345
// Struct for calling start functions of Preflow
kpeter@1060
   346
template <typename MF>
kpeter@1060
   347
struct PreflowStartFunctions {
kpeter@1060
   348
kpeter@1060
   349
  static void startFirstPhase(MF& mf) {
kpeter@1060
   350
    mf.startFirstPhase();
kpeter@1060
   351
  }
kpeter@1060
   352
kpeter@1060
   353
  static void startSecondPhase(MF& mf) {
kpeter@1060
   354
    mf.startSecondPhase();
kpeter@1060
   355
  }
kpeter@1060
   356
kpeter@1060
   357
};
kpeter@1060
   358
kpeter@1060
   359
int main() {
kpeter@1060
   360
kpeter@1060
   361
  typedef concepts::Digraph GR;
kpeter@1060
   362
  typedef concepts::ReadMap<GR::Arc, int> CM1;
kpeter@1060
   363
  typedef concepts::ReadMap<GR::Arc, double> CM2;
kpeter@1060
   364
kpeter@1060
   365
  // Check the interface of Preflow
kpeter@1060
   366
  checkConcept< MaxFlowClassConcept<GR, CM1>,
kpeter@1060
   367
                Preflow<GR, CM1> >();
kpeter@1060
   368
  checkConcept< MaxFlowClassConcept<GR, CM2>,
kpeter@1060
   369
                Preflow<GR, CM2> >();
kpeter@1060
   370
kpeter@1060
   371
  // Check the interface of EdmondsKarp
kpeter@1060
   372
  checkConcept< MaxFlowClassConcept<GR, CM1>,
kpeter@1060
   373
                EdmondsKarp<GR, CM1> >();
kpeter@1060
   374
  checkConcept< MaxFlowClassConcept<GR, CM2>,
kpeter@1060
   375
                EdmondsKarp<GR, CM2> >();
kpeter@1060
   376
kpeter@1060
   377
  // Check Preflow
kpeter@1060
   378
  typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<int> > PType1;
kpeter@1060
   379
  typedef Preflow<SmartDigraph, SmartDigraph::ArcMap<float> > PType2;
kpeter@1060
   380
  checkMaxFlowAlg<PType1, PreflowStartFunctions<PType1> >();
kpeter@1060
   381
  checkMaxFlowAlg<PType2, PreflowStartFunctions<PType2> >();
kpeter@1166
   382
kpeter@1166
   383
  checkInitPreflow();
alpar@1092
   384
kpeter@1060
   385
  // Check EdmondsKarp
kpeter@1060
   386
  typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<int> > EKType1;
kpeter@1060
   387
  typedef EdmondsKarp<SmartDigraph, SmartDigraph::ArcMap<float> > EKType2;
kpeter@1060
   388
  checkMaxFlowAlg<EKType1, GeneralStartFunctions<EKType1> >();
kpeter@1060
   389
  checkMaxFlowAlg<EKType2, GeneralStartFunctions<EKType2> >();
alpar@389
   390
alpar@389
   391
  return 0;
alpar@389
   392
}