test/circulation_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 02 Aug 2009 13:24:46 +0200
changeset 744 9496ed797f20
parent 632 65fbcf2f978a
parent 657 dacc2cee2b4c
child 736 86c49553fea5
child 1081 f1398882a928
child 1171 7e368d9b67f7
permissions -rw-r--r--
Improvements and unifications for BellmanFord (#51)

- Rework the function type interface to fit to dijkstra().
- Rename named template parameters (Def* -> Set*).
- Rename some private member variables
(to start with an underscore).
- Simplify template parameter names.
- Many unifications and improvements in the doc.
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;
kpeter@418
    76
alpar@658
    77
  typedef Circulation<Digraph, CapMap, CapMap, SupplyMap>
kpeter@632
    78
            ::SetFlowMap<FlowMap>
kpeter@632
    79
            ::SetElevator<Elev>
kpeter@632
    80
            ::SetStandardElevator<LinkedElev>
kpeter@632
    81
            ::Create CirculationType;
alpar@658
    82
  CirculationType circ_test(g, lcap, ucap, supply);
kpeter@632
    83
  const CirculationType& const_circ_test = circ_test;
kpeter@632
    84
   
kpeter@632
    85
  circ_test
alpar@658
    86
    .lowerMap(lcap)
alpar@658
    87
    .upperMap(ucap)
alpar@658
    88
    .supplyMap(supply)
kpeter@632
    89
    .flowMap(flow);
kpeter@418
    90
kpeter@418
    91
  circ_test.init();
kpeter@418
    92
  circ_test.greedyInit();
kpeter@418
    93
  circ_test.start();
kpeter@418
    94
  circ_test.run();
kpeter@418
    95
kpeter@632
    96
  v = const_circ_test.flow(a);
kpeter@632
    97
  const FlowMap& fm = const_circ_test.flowMap();
kpeter@632
    98
  b = const_circ_test.barrier(n);
kpeter@632
    99
  const_circ_test.barrierMap(bar);
kpeter@632
   100
  
kpeter@632
   101
  ignore_unused_variable_warning(fm);
kpeter@418
   102
}
kpeter@418
   103
kpeter@418
   104
template <class G, class LM, class UM, class DM>
kpeter@418
   105
void checkCirculation(const G& g, const LM& lm, const UM& um,
kpeter@418
   106
                      const DM& dm, bool find)
kpeter@418
   107
{
kpeter@418
   108
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
kpeter@418
   109
  bool ret = circ.run();
kpeter@418
   110
  if (find) {
kpeter@418
   111
    check(ret, "A feasible solution should have been found.");
kpeter@418
   112
    check(circ.checkFlow(), "The found flow is corrupt.");
kpeter@418
   113
    check(!circ.checkBarrier(), "A barrier should not have been found.");
kpeter@418
   114
  } else {
kpeter@418
   115
    check(!ret, "A feasible solution should not have been found.");
kpeter@418
   116
    check(circ.checkBarrier(), "The found barrier is corrupt.");
kpeter@418
   117
  }
kpeter@418
   118
}
alpar@415
   119
alpar@415
   120
int main (int, char*[])
alpar@415
   121
{
kpeter@418
   122
  typedef ListDigraph Digraph;
kpeter@418
   123
  DIGRAPH_TYPEDEFS(Digraph);
alpar@415
   124
kpeter@418
   125
  Digraph g;
kpeter@418
   126
  IntArcMap lo(g), up(g);
kpeter@418
   127
  IntNodeMap delta(g, 0);
kpeter@418
   128
  Node s, t;
alpar@415
   129
kpeter@418
   130
  std::istringstream input(test_lgf);
kpeter@418
   131
  DigraphReader<Digraph>(g,input).
kpeter@418
   132
    arcMap("lcap", lo).
kpeter@418
   133
    arcMap("ucap", up).
kpeter@418
   134
    node("source",s).
kpeter@418
   135
    node("sink",t).
kpeter@418
   136
    run();
alpar@415
   137
kpeter@418
   138
  delta[s] = 7; delta[t] = -7;
kpeter@418
   139
  checkCirculation(g, lo, up, delta, true);
alpar@415
   140
kpeter@418
   141
  delta[s] = 13; delta[t] = -13;
kpeter@418
   142
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   143
kpeter@418
   144
  delta[s] = 6; delta[t] = -6;
kpeter@418
   145
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   146
kpeter@418
   147
  delta[s] = 14; delta[t] = -14;
kpeter@418
   148
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   149
kpeter@418
   150
  delta[s] = 7; delta[t] = -13;
kpeter@418
   151
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   152
kpeter@418
   153
  delta[s] = 5; delta[t] = -15;
kpeter@418
   154
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   155
kpeter@418
   156
  delta[s] = 10; delta[t] = -11;
kpeter@418
   157
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   158
kpeter@418
   159
  delta[s] = 11; delta[t] = -10;
kpeter@418
   160
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   161
kpeter@418
   162
  return 0;
alpar@415
   163
}