test/preflow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 12 Dec 2008 21:44:37 +0100
changeset 451 09e416d35896
parent 407 db3251947eba
child 442 ff48c2738fb2
permissions -rw-r--r--
Bug fix in variant.h (#196)
alpar@404
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@404
     2
 *
alpar@404
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@404
     4
 *
alpar@404
     5
 * Copyright (C) 2003-2008
alpar@404
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@404
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@404
     8
 *
alpar@404
     9
 * Permission to use, modify and distribute this software is granted
alpar@404
    10
 * provided that this copyright notice appears in all copies. For
alpar@404
    11
 * precise terms see the accompanying LICENSE file.
alpar@404
    12
 *
alpar@404
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@404
    14
 * express or implied, and with no claim as to its suitability for any
alpar@404
    15
 * purpose.
alpar@404
    16
 *
alpar@404
    17
 */
alpar@404
    18
alpar@404
    19
#include <fstream>
alpar@404
    20
#include <string>
alpar@404
    21
alpar@404
    22
#include "test_tools.h"
alpar@404
    23
#include <lemon/smart_graph.h>
alpar@404
    24
#include <lemon/preflow.h>
alpar@404
    25
#include <lemon/concepts/digraph.h>
alpar@404
    26
#include <lemon/concepts/maps.h>
alpar@404
    27
#include <lemon/lgf_reader.h>
kpeter@409
    28
#include <lemon/elevator.h>
alpar@404
    29
alpar@404
    30
using namespace lemon;
alpar@404
    31
kpeter@409
    32
void checkPreflowCompile()
alpar@404
    33
{
alpar@404
    34
  typedef int VType;
alpar@404
    35
  typedef concepts::Digraph Digraph;
alpar@404
    36
alpar@404
    37
  typedef Digraph::Node Node;
alpar@404
    38
  typedef Digraph::Arc Arc;
alpar@404
    39
  typedef concepts::ReadMap<Arc,VType> CapMap;
alpar@404
    40
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
alpar@404
    41
  typedef concepts::WriteMap<Node,bool> CutMap;
alpar@404
    42
kpeter@409
    43
  typedef Elevator<Digraph, Digraph::Node> Elev;
kpeter@409
    44
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
kpeter@409
    45
alpar@404
    46
  Digraph g;
alpar@404
    47
  Node n;
alpar@404
    48
  Arc e;
alpar@404
    49
  CapMap cap;
alpar@404
    50
  FlowMap flow;
alpar@404
    51
  CutMap cut;
alpar@404
    52
kpeter@409
    53
  Preflow<Digraph, CapMap>
kpeter@409
    54
    ::SetFlowMap<FlowMap>
kpeter@409
    55
    ::SetElevator<Elev>
kpeter@409
    56
    ::SetStandardElevator<LinkedElev>
kpeter@409
    57
    ::Create preflow_test(g,cap,n,n);
alpar@404
    58
alpar@404
    59
  preflow_test.capacityMap(cap);
alpar@404
    60
  flow = preflow_test.flowMap();
alpar@404
    61
  preflow_test.flowMap(flow);
alpar@404
    62
  preflow_test.source(n);
alpar@404
    63
  preflow_test.target(n);
alpar@404
    64
alpar@404
    65
  preflow_test.init();
kpeter@407
    66
  preflow_test.init(cap);
alpar@404
    67
  preflow_test.startFirstPhase();
alpar@404
    68
  preflow_test.startSecondPhase();
alpar@404
    69
  preflow_test.run();
alpar@404
    70
  preflow_test.runMinCut();
alpar@404
    71
alpar@404
    72
  preflow_test.flowValue();
alpar@404
    73
  preflow_test.minCut(n);
alpar@404
    74
  preflow_test.minCutMap(cut);
alpar@404
    75
  preflow_test.flow(e);
alpar@404
    76
alpar@404
    77
}
alpar@404
    78
alpar@404
    79
int cutValue (const SmartDigraph& g,
alpar@404
    80
              const SmartDigraph::NodeMap<bool>& cut,
alpar@404
    81
              const SmartDigraph::ArcMap<int>& cap) {
alpar@404
    82
alpar@404
    83
  int c=0;
alpar@404
    84
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
alpar@404
    85
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
alpar@404
    86
  }
alpar@404
    87
  return c;
alpar@404
    88
}
alpar@404
    89
alpar@404
    90
bool checkFlow(const SmartDigraph& g,
alpar@404
    91
               const SmartDigraph::ArcMap<int>& flow,
alpar@404
    92
               const SmartDigraph::ArcMap<int>& cap,
alpar@404
    93
               SmartDigraph::Node s, SmartDigraph::Node t) {
alpar@404
    94
alpar@404
    95
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
alpar@404
    96
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
alpar@404
    97
  }
alpar@404
    98
alpar@404
    99
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
alpar@404
   100
    if (n == s || n == t) continue;
alpar@404
   101
    int sum = 0;
alpar@404
   102
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
alpar@404
   103
      sum += flow[e];
alpar@404
   104
    }
alpar@404
   105
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
alpar@404
   106
      sum -= flow[e];
alpar@404
   107
    }
alpar@404
   108
    if (sum != 0) return false;
alpar@404
   109
  }
alpar@404
   110
  return true;
alpar@404
   111
}
alpar@404
   112
alpar@404
   113
int main() {
alpar@404
   114
alpar@404
   115
  typedef SmartDigraph Digraph;
alpar@404
   116
alpar@404
   117
  typedef Digraph::Node Node;
alpar@404
   118
  typedef Digraph::NodeIt NodeIt;
alpar@404
   119
  typedef Digraph::ArcIt ArcIt;
alpar@404
   120
  typedef Digraph::ArcMap<int> CapMap;
alpar@404
   121
  typedef Digraph::ArcMap<int> FlowMap;
alpar@404
   122
  typedef Digraph::NodeMap<bool> CutMap;
alpar@404
   123
alpar@404
   124
  typedef Preflow<Digraph, CapMap> PType;
alpar@404
   125
alpar@404
   126
  std::string f_name;
alpar@404
   127
  if( getenv("srcdir") )
alpar@404
   128
    f_name = std::string(getenv("srcdir"));
alpar@404
   129
  else f_name = ".";
alpar@404
   130
  f_name += "/test/preflow_graph.lgf";
alpar@404
   131
alpar@404
   132
  std::ifstream file(f_name.c_str());
alpar@404
   133
alpar@404
   134
  check(file, "Input file '" << f_name << "' not found.");
alpar@404
   135
alpar@404
   136
  Digraph g;
alpar@404
   137
  Node s, t;
alpar@404
   138
  CapMap cap(g);
alpar@404
   139
  DigraphReader<Digraph>(g,file).
alpar@404
   140
    arcMap("capacity", cap).
alpar@404
   141
    node("source",s).
alpar@404
   142
    node("target",t).
alpar@404
   143
    run();
alpar@404
   144
alpar@404
   145
  PType preflow_test(g, cap, s, t);
alpar@404
   146
  preflow_test.run();
alpar@404
   147
alpar@404
   148
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@404
   149
        "The flow is not feasible.");
alpar@404
   150
alpar@404
   151
  CutMap min_cut(g);
alpar@404
   152
  preflow_test.minCutMap(min_cut);
alpar@404
   153
  int min_cut_value=cutValue(g,min_cut,cap);
alpar@404
   154
alpar@404
   155
  check(preflow_test.flowValue() == min_cut_value,
alpar@404
   156
        "The max flow value is not equal to the three min cut values.");
alpar@404
   157
alpar@404
   158
  FlowMap flow(g);
alpar@404
   159
  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
alpar@404
   160
alpar@404
   161
  int flow_value=preflow_test.flowValue();
alpar@404
   162
alpar@404
   163
  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
kpeter@407
   164
  preflow_test.init(flow);
alpar@404
   165
  preflow_test.startFirstPhase();
alpar@404
   166
alpar@404
   167
  CutMap min_cut1(g);
alpar@404
   168
  preflow_test.minCutMap(min_cut1);
alpar@404
   169
  min_cut_value=cutValue(g,min_cut1,cap);
alpar@404
   170
alpar@404
   171
  check(preflow_test.flowValue() == min_cut_value &&
alpar@404
   172
        min_cut_value == 2*flow_value,
alpar@404
   173
        "The max flow value or the min cut value is wrong.");
alpar@404
   174
alpar@404
   175
  preflow_test.startSecondPhase();
alpar@404
   176
alpar@404
   177
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@404
   178
        "The flow is not feasible.");
alpar@404
   179
alpar@404
   180
  CutMap min_cut2(g);
alpar@404
   181
  preflow_test.minCutMap(min_cut2);
alpar@404
   182
  min_cut_value=cutValue(g,min_cut2,cap);
alpar@404
   183
alpar@404
   184
  check(preflow_test.flowValue() == min_cut_value &&
alpar@404
   185
        min_cut_value == 2*flow_value,
alpar@404
   186
        "The max flow value or the three min cut values were not doubled");
alpar@404
   187
alpar@404
   188
alpar@404
   189
  preflow_test.flowMap(flow);
alpar@404
   190
alpar@404
   191
  NodeIt tmp1(g,s);
alpar@404
   192
  ++tmp1;
alpar@404
   193
  if ( tmp1 != INVALID ) s=tmp1;
alpar@404
   194
alpar@404
   195
  NodeIt tmp2(g,t);
alpar@404
   196
  ++tmp2;
alpar@404
   197
  if ( tmp2 != INVALID ) t=tmp2;
alpar@404
   198
alpar@404
   199
  preflow_test.source(s);
alpar@404
   200
  preflow_test.target(t);
alpar@404
   201
alpar@404
   202
  preflow_test.run();
alpar@404
   203
alpar@404
   204
  CutMap min_cut3(g);
alpar@404
   205
  preflow_test.minCutMap(min_cut3);
alpar@404
   206
  min_cut_value=cutValue(g,min_cut3,cap);
alpar@404
   207
alpar@404
   208
alpar@404
   209
  check(preflow_test.flowValue() == min_cut_value,
alpar@404
   210
        "The max flow value or the three min cut values are incorrect.");
alpar@404
   211
alpar@404
   212
  return 0;
alpar@404
   213
}