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