summaryrefslogtreecommitdiff
path: root/lemon/test/circulation_test.cc
blob: 99dc76477fe1d2fb78d94b3894753af53f00255d (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2010
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <iostream>

#include "test_tools.h"
#include <lemon/list_graph.h>
#include <lemon/circulation.h>
#include <lemon/lgf_reader.h>
#include <lemon/concepts/digraph.h>
#include <lemon/concepts/maps.h>

using namespace lemon;

char test_lgf[] =
  "@nodes\n"
  "label\n"
  "0\n"
  "1\n"
  "2\n"
  "3\n"
  "4\n"
  "5\n"
  "@arcs\n"
  "     lcap  ucap\n"
  "0 1  2  10\n"
  "0 2  2  6\n"
  "1 3  4  7\n"
  "1 4  0  5\n"
  "2 4  1  3\n"
  "3 5  3  8\n"
  "4 5  3  7\n"
  "@attributes\n"
  "source 0\n"
  "sink   5\n";

void checkCirculationCompile()
{
  typedef int VType;
  typedef concepts::Digraph Digraph;

  typedef Digraph::Node Node;
  typedef Digraph::Arc Arc;
  typedef concepts::ReadMap<Arc,VType> CapMap;
  typedef concepts::ReadMap<Node,VType> SupplyMap;
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
  typedef concepts::WriteMap<Node,bool> BarrierMap;

  typedef Elevator<Digraph, Digraph::Node> Elev;
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;

  Digraph g;
  Node n;
  Arc a;
  CapMap lcap, ucap;
  SupplyMap supply;
  FlowMap flow;
  BarrierMap bar;
  VType v;
  bool b;

  typedef Circulation<Digraph, CapMap, CapMap, SupplyMap>
            ::SetFlowMap<FlowMap>
            ::SetElevator<Elev>
            ::SetStandardElevator<LinkedElev>
            ::Create CirculationType;
  CirculationType circ_test(g, lcap, ucap, supply);
  const CirculationType& const_circ_test = circ_test;

  circ_test
    .lowerMap(lcap)
    .upperMap(ucap)
    .supplyMap(supply)
    .flowMap(flow);

  const CirculationType::Elevator& elev = const_circ_test.elevator();
  circ_test.elevator(const_cast<CirculationType::Elevator&>(elev));
  CirculationType::Tolerance tol = const_circ_test.tolerance();
  circ_test.tolerance(tol);

  circ_test.init();
  circ_test.greedyInit();
  circ_test.start();
  circ_test.run();

  v = const_circ_test.flow(a);
  const FlowMap& fm = const_circ_test.flowMap();
  b = const_circ_test.barrier(n);
  const_circ_test.barrierMap(bar);

  ignore_unused_variable_warning(fm);
}

template <class G, class LM, class UM, class DM>
void checkCirculation(const G& g, const LM& lm, const UM& um,
                      const DM& dm, bool find)
{
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
  bool ret = circ.run();
  if (find) {
    check(ret, "A feasible solution should have been found.");
    check(circ.checkFlow(), "The found flow is corrupt.");
    check(!circ.checkBarrier(), "A barrier should not have been found.");
  } else {
    check(!ret, "A feasible solution should not have been found.");
    check(circ.checkBarrier(), "The found barrier is corrupt.");
  }
}

int main (int, char*[])
{
  typedef ListDigraph Digraph;
  DIGRAPH_TYPEDEFS(Digraph);

  Digraph g;
  IntArcMap lo(g), up(g);
  IntNodeMap delta(g, 0);
  Node s, t;

  std::istringstream input(test_lgf);
  DigraphReader<Digraph>(g,input).
    arcMap("lcap", lo).
    arcMap("ucap", up).
    node("source",s).
    node("sink",t).
    run();

  delta[s] = 7; delta[t] = -7;
  checkCirculation(g, lo, up, delta, true);

  delta[s] = 13; delta[t] = -13;
  checkCirculation(g, lo, up, delta, true);

  delta[s] = 6; delta[t] = -6;
  checkCirculation(g, lo, up, delta, false);

  delta[s] = 14; delta[t] = -14;
  checkCirculation(g, lo, up, delta, false);

  delta[s] = 7; delta[t] = -13;
  checkCirculation(g, lo, up, delta, true);

  delta[s] = 5; delta[t] = -15;
  checkCirculation(g, lo, up, delta, true);

  delta[s] = 10; delta[t] = -11;
  checkCirculation(g, lo, up, delta, true);

  delta[s] = 11; delta[t] = -10;
  checkCirculation(g, lo, up, delta, false);

  return 0;
}