test/circulation_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 24 Mar 2009 00:18:25 +0100
changeset 651 8c3112a66878
parent 418 940587667b47
child 632 65fbcf2f978a
child 657 dacc2cee2b4c
permissions -rw-r--r--
Use XTI implementation instead of ATI in NetworkSimplex (#234)

XTI (eXtended Threaded Index) is an imporved version of the widely
known ATI (Augmented Threaded Index) method for storing and updating
the spanning tree structure in Network Simplex algorithms.

In the ATI data structure three indices are stored for each node:
predecessor, thread and depth. In the XTI data structure depth is
replaced by the number of successors and the last successor
(according to the thread index).
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@418
    60
  typedef concepts::ReadMap<Node,VType> DeltaMap;
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@418
    71
  DeltaMap delta;
kpeter@418
    72
  FlowMap flow;
kpeter@418
    73
  BarrierMap bar;
kpeter@418
    74
kpeter@418
    75
  Circulation<Digraph, CapMap, CapMap, DeltaMap>
kpeter@418
    76
    ::SetFlowMap<FlowMap>
kpeter@418
    77
    ::SetElevator<Elev>
kpeter@418
    78
    ::SetStandardElevator<LinkedElev>
kpeter@418
    79
    ::Create circ_test(g,lcap,ucap,delta);
kpeter@418
    80
kpeter@418
    81
  circ_test.lowerCapMap(lcap);
kpeter@418
    82
  circ_test.upperCapMap(ucap);
kpeter@418
    83
  circ_test.deltaMap(delta);
kpeter@418
    84
  flow = circ_test.flowMap();
kpeter@418
    85
  circ_test.flowMap(flow);
kpeter@418
    86
kpeter@418
    87
  circ_test.init();
kpeter@418
    88
  circ_test.greedyInit();
kpeter@418
    89
  circ_test.start();
kpeter@418
    90
  circ_test.run();
kpeter@418
    91
kpeter@418
    92
  circ_test.barrier(n);
kpeter@418
    93
  circ_test.barrierMap(bar);
kpeter@418
    94
  circ_test.flow(a);
kpeter@418
    95
}
kpeter@418
    96
kpeter@418
    97
template <class G, class LM, class UM, class DM>
kpeter@418
    98
void checkCirculation(const G& g, const LM& lm, const UM& um,
kpeter@418
    99
                      const DM& dm, bool find)
kpeter@418
   100
{
kpeter@418
   101
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
kpeter@418
   102
  bool ret = circ.run();
kpeter@418
   103
  if (find) {
kpeter@418
   104
    check(ret, "A feasible solution should have been found.");
kpeter@418
   105
    check(circ.checkFlow(), "The found flow is corrupt.");
kpeter@418
   106
    check(!circ.checkBarrier(), "A barrier should not have been found.");
kpeter@418
   107
  } else {
kpeter@418
   108
    check(!ret, "A feasible solution should not have been found.");
kpeter@418
   109
    check(circ.checkBarrier(), "The found barrier is corrupt.");
kpeter@418
   110
  }
kpeter@418
   111
}
alpar@415
   112
alpar@415
   113
int main (int, char*[])
alpar@415
   114
{
kpeter@418
   115
  typedef ListDigraph Digraph;
kpeter@418
   116
  DIGRAPH_TYPEDEFS(Digraph);
alpar@415
   117
kpeter@418
   118
  Digraph g;
kpeter@418
   119
  IntArcMap lo(g), up(g);
kpeter@418
   120
  IntNodeMap delta(g, 0);
kpeter@418
   121
  Node s, t;
alpar@415
   122
kpeter@418
   123
  std::istringstream input(test_lgf);
kpeter@418
   124
  DigraphReader<Digraph>(g,input).
kpeter@418
   125
    arcMap("lcap", lo).
kpeter@418
   126
    arcMap("ucap", up).
kpeter@418
   127
    node("source",s).
kpeter@418
   128
    node("sink",t).
kpeter@418
   129
    run();
alpar@415
   130
kpeter@418
   131
  delta[s] = 7; delta[t] = -7;
kpeter@418
   132
  checkCirculation(g, lo, up, delta, true);
alpar@415
   133
kpeter@418
   134
  delta[s] = 13; delta[t] = -13;
kpeter@418
   135
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   136
kpeter@418
   137
  delta[s] = 6; delta[t] = -6;
kpeter@418
   138
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   139
kpeter@418
   140
  delta[s] = 14; delta[t] = -14;
kpeter@418
   141
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   142
kpeter@418
   143
  delta[s] = 7; delta[t] = -13;
kpeter@418
   144
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   145
kpeter@418
   146
  delta[s] = 5; delta[t] = -15;
kpeter@418
   147
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   148
kpeter@418
   149
  delta[s] = 10; delta[t] = -11;
kpeter@418
   150
  checkCirculation(g, lo, up, delta, true);
kpeter@418
   151
kpeter@418
   152
  delta[s] = 11; delta[t] = -10;
kpeter@418
   153
  checkCirculation(g, lo, up, delta, false);
kpeter@418
   154
kpeter@418
   155
  return 0;
alpar@415
   156
}