test/circulation_test.cc
author Balazs Dezso <deba@google.com>
Thu, 08 Aug 2013 22:56:10 +0200
changeset 1265 552e3d1242c6
parent 1171 7e368d9b67f7
child 1258 bdfc038f364c
child 1259 8b2d4e5d96e4
permissions -rw-r--r--
Fix biNodeConnected() function (#439)
alpar@415
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@415
     2
 *
alpar@415
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@415
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
alpar@415
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@415
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@415
     8
 *
alpar@415
     9
 * Permission to use, modify and distribute this software is granted
alpar@415
    10
 * provided that this copyright notice appears in all copies. For
alpar@415
    11
 * precise terms see the accompanying LICENSE file.
alpar@415
    12
 *
alpar@415
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@415
    14
 * express or implied, and with no claim as to its suitability for any
alpar@415
    15
 * purpose.
alpar@415
    16
 *
alpar@415
    17
 */
alpar@415
    18
alpar@415
    19
#include <iostream>
alpar@415
    20
alpar@415
    21
#include "test_tools.h"
alpar@415
    22
#include <lemon/list_graph.h>
alpar@415
    23
#include <lemon/circulation.h>
alpar@415
    24
#include <lemon/lgf_reader.h>
kpeter@418
    25
#include <lemon/concepts/digraph.h>
kpeter@418
    26
#include <lemon/concepts/maps.h>
alpar@415
    27
alpar@415
    28
using namespace lemon;
alpar@415
    29
alpar@415
    30
char test_lgf[] =
alpar@415
    31
  "@nodes\n"
kpeter@418
    32
  "label\n"
kpeter@418
    33
  "0\n"
kpeter@418
    34
  "1\n"
kpeter@418
    35
  "2\n"
kpeter@418
    36
  "3\n"
kpeter@418
    37
  "4\n"
kpeter@418
    38
  "5\n"
kpeter@418
    39
  "@arcs\n"
kpeter@418
    40
  "     lcap  ucap\n"
kpeter@418
    41
  "0 1  2  10\n"
kpeter@418
    42
  "0 2  2  6\n"
kpeter@418
    43
  "1 3  4  7\n"
kpeter@418
    44
  "1 4  0  5\n"
kpeter@418
    45
  "2 4  1  3\n"
kpeter@418
    46
  "3 5  3  8\n"
kpeter@418
    47
  "4 5  3  7\n"
alpar@415
    48
  "@attributes\n"
kpeter@418
    49
  "source 0\n"
kpeter@418
    50
  "sink   5\n";
kpeter@418
    51
kpeter@418
    52
void checkCirculationCompile()
kpeter@418
    53
{
kpeter@418
    54
  typedef int VType;
kpeter@418
    55
  typedef concepts::Digraph Digraph;
kpeter@418
    56
kpeter@418
    57
  typedef Digraph::Node Node;
kpeter@418
    58
  typedef Digraph::Arc Arc;
kpeter@418
    59
  typedef concepts::ReadMap<Arc,VType> CapMap;
kpeter@657
    60
  typedef concepts::ReadMap<Node,VType> SupplyMap;
kpeter@418
    61
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
kpeter@418
    62
  typedef concepts::WriteMap<Node,bool> BarrierMap;
kpeter@418
    63
kpeter@418
    64
  typedef Elevator<Digraph, Digraph::Node> Elev;
kpeter@418
    65
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
kpeter@418
    66
kpeter@418
    67
  Digraph g;
kpeter@418
    68
  Node n;
kpeter@418
    69
  Arc a;
kpeter@418
    70
  CapMap lcap, ucap;
kpeter@657
    71
  SupplyMap supply;
kpeter@418
    72
  FlowMap flow;
kpeter@418
    73
  BarrierMap bar;
kpeter@632
    74
  VType v;
kpeter@632
    75
  bool b;
alpar@1257
    76
  ::lemon::ignore_unused_variable_warning(v,b);
kpeter@418
    77
alpar@658
    78
  typedef Circulation<Digraph, CapMap, CapMap, SupplyMap>
kpeter@632
    79
            ::SetFlowMap<FlowMap>
kpeter@632
    80
            ::SetElevator<Elev>
kpeter@632
    81
            ::SetStandardElevator<LinkedElev>
kpeter@632
    82
            ::Create CirculationType;
alpar@658
    83
  CirculationType circ_test(g, lcap, ucap, supply);
kpeter@632
    84
  const CirculationType& const_circ_test = circ_test;
kpeter@632
    85
   
kpeter@632
    86
  circ_test
alpar@658
    87
    .lowerMap(lcap)
alpar@658
    88
    .upperMap(ucap)
alpar@658
    89
    .supplyMap(supply)
kpeter@632
    90
    .flowMap(flow);
kpeter@418
    91
kpeter@418
    92
  circ_test.init();
kpeter@418
    93
  circ_test.greedyInit();
kpeter@418
    94
  circ_test.start();
kpeter@418
    95
  circ_test.run();
kpeter@418
    96
kpeter@632
    97
  v = const_circ_test.flow(a);
kpeter@632
    98
  const FlowMap& fm = const_circ_test.flowMap();
kpeter@632
    99
  b = const_circ_test.barrier(n);
kpeter@632
   100
  const_circ_test.barrierMap(bar);
kpeter@632
   101
  
alpar@1257
   102
  ::lemon::ignore_unused_variable_warning(fm);
kpeter@418
   103
}
kpeter@418
   104
kpeter@418
   105
template <class G, class LM, class UM, class DM>
kpeter@418
   106
void checkCirculation(const G& g, const LM& lm, const UM& um,
kpeter@418
   107
                      const DM& dm, bool find)
kpeter@418
   108
{
kpeter@418
   109
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
kpeter@418
   110
  bool ret = circ.run();
kpeter@418
   111
  if (find) {
kpeter@418
   112
    check(ret, "A feasible solution should have been found.");
kpeter@418
   113
    check(circ.checkFlow(), "The found flow is corrupt.");
kpeter@418
   114
    check(!circ.checkBarrier(), "A barrier should not have been found.");
kpeter@418
   115
  } else {
kpeter@418
   116
    check(!ret, "A feasible solution should not have been found.");
kpeter@418
   117
    check(circ.checkBarrier(), "The found barrier is corrupt.");
kpeter@418
   118
  }
kpeter@418
   119
}
alpar@415
   120
alpar@415
   121
int main (int, char*[])
alpar@415
   122
{
kpeter@418
   123
  typedef ListDigraph Digraph;
kpeter@418
   124
  DIGRAPH_TYPEDEFS(Digraph);
alpar@415
   125
kpeter@418
   126
  Digraph g;
kpeter@418
   127
  IntArcMap lo(g), up(g);
kpeter@418
   128
  IntNodeMap delta(g, 0);
kpeter@418
   129
  Node s, t;
alpar@415
   130
kpeter@418
   131
  std::istringstream input(test_lgf);
kpeter@418
   132
  DigraphReader<Digraph>(g,input).
kpeter@418
   133
    arcMap("lcap", lo).
kpeter@418
   134
    arcMap("ucap", up).
kpeter@418
   135
    node("source",s).
kpeter@418
   136
    node("sink",t).
kpeter@418
   137
    run();
alpar@415
   138
kpeter@418
   139
  delta[s] = 7; delta[t] = -7;
kpeter@418
   140
  checkCirculation(g, lo, up, delta, true);
alpar@415
   141
kpeter@418
   142
  delta[s] = 13; delta[t] = -13;
kpeter@418
   143
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   144
kpeter@418
   145
  delta[s] = 6; delta[t] = -6;
kpeter@418
   146
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   147
kpeter@418
   148
  delta[s] = 14; delta[t] = -14;
kpeter@418
   149
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   150
kpeter@418
   151
  delta[s] = 7; delta[t] = -13;
kpeter@418
   152
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   153
kpeter@418
   154
  delta[s] = 5; delta[t] = -15;
kpeter@418
   155
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   156
kpeter@418
   157
  delta[s] = 10; delta[t] = -11;
kpeter@418
   158
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   159
kpeter@418
   160
  delta[s] = 11; delta[t] = -10;
kpeter@418
   161
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   162
kpeter@418
   163
  return 0;
alpar@415
   164
}