deba@512
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@512
|
2 |
*
|
deba@512
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@512
|
4 |
*
|
alpar@761
|
5 |
* Copyright (C) 2003-2011
|
deba@512
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@512
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@512
|
8 |
*
|
deba@512
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@512
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@512
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@512
|
12 |
*
|
deba@512
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@512
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@512
|
15 |
* purpose.
|
deba@512
|
16 |
*
|
deba@512
|
17 |
*/
|
deba@512
|
18 |
|
deba@512
|
19 |
#include <iostream>
|
deba@512
|
20 |
#include <set>
|
deba@512
|
21 |
#include <vector>
|
deba@512
|
22 |
#include <iterator>
|
deba@512
|
23 |
|
deba@512
|
24 |
#include <lemon/smart_graph.h>
|
deba@512
|
25 |
#include <lemon/min_cost_arborescence.h>
|
deba@512
|
26 |
#include <lemon/lgf_reader.h>
|
kpeter@617
|
27 |
#include <lemon/concepts/digraph.h>
|
deba@512
|
28 |
|
deba@512
|
29 |
#include "test_tools.h"
|
deba@512
|
30 |
|
deba@512
|
31 |
using namespace lemon;
|
deba@512
|
32 |
using namespace std;
|
deba@512
|
33 |
|
deba@512
|
34 |
const char test_lgf[] =
|
deba@512
|
35 |
"@nodes\n"
|
deba@512
|
36 |
"label\n"
|
deba@512
|
37 |
"0\n"
|
deba@512
|
38 |
"1\n"
|
deba@512
|
39 |
"2\n"
|
deba@512
|
40 |
"3\n"
|
deba@512
|
41 |
"4\n"
|
deba@512
|
42 |
"5\n"
|
deba@512
|
43 |
"6\n"
|
deba@512
|
44 |
"7\n"
|
deba@512
|
45 |
"8\n"
|
deba@512
|
46 |
"9\n"
|
deba@512
|
47 |
"@arcs\n"
|
deba@512
|
48 |
" label cost\n"
|
deba@512
|
49 |
"1 8 0 107\n"
|
deba@512
|
50 |
"0 3 1 70\n"
|
deba@512
|
51 |
"2 1 2 46\n"
|
deba@512
|
52 |
"4 1 3 28\n"
|
deba@512
|
53 |
"4 4 4 91\n"
|
deba@512
|
54 |
"3 9 5 76\n"
|
deba@512
|
55 |
"9 8 6 61\n"
|
deba@512
|
56 |
"8 1 7 39\n"
|
deba@512
|
57 |
"9 8 8 74\n"
|
deba@512
|
58 |
"8 0 9 39\n"
|
deba@512
|
59 |
"4 3 10 45\n"
|
deba@512
|
60 |
"2 2 11 34\n"
|
deba@512
|
61 |
"0 1 12 100\n"
|
deba@512
|
62 |
"6 3 13 95\n"
|
deba@512
|
63 |
"4 1 14 22\n"
|
deba@512
|
64 |
"1 1 15 31\n"
|
deba@512
|
65 |
"7 2 16 51\n"
|
deba@512
|
66 |
"2 6 17 29\n"
|
deba@512
|
67 |
"8 3 18 115\n"
|
deba@512
|
68 |
"6 9 19 32\n"
|
deba@512
|
69 |
"1 1 20 60\n"
|
deba@512
|
70 |
"0 3 21 40\n"
|
deba@512
|
71 |
"@attributes\n"
|
deba@512
|
72 |
"source 0\n";
|
deba@512
|
73 |
|
kpeter@617
|
74 |
|
kpeter@617
|
75 |
void checkMinCostArborescenceCompile()
|
kpeter@617
|
76 |
{
|
kpeter@617
|
77 |
typedef double VType;
|
kpeter@617
|
78 |
typedef concepts::Digraph Digraph;
|
kpeter@617
|
79 |
typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
|
kpeter@617
|
80 |
typedef Digraph::Node Node;
|
kpeter@617
|
81 |
typedef Digraph::Arc Arc;
|
kpeter@617
|
82 |
typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
|
kpeter@617
|
83 |
typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
|
kpeter@617
|
84 |
|
kpeter@617
|
85 |
typedef MinCostArborescence<Digraph, CostMap>::
|
kpeter@617
|
86 |
SetArborescenceMap<ArbMap>::
|
kpeter@617
|
87 |
SetPredMap<PredMap>::Create MinCostArbType;
|
kpeter@617
|
88 |
|
kpeter@617
|
89 |
Digraph g;
|
kpeter@617
|
90 |
Node s, n;
|
kpeter@617
|
91 |
Arc e;
|
kpeter@617
|
92 |
VType c;
|
kpeter@617
|
93 |
bool b;
|
alpar@796
|
94 |
ignore_unused_variable_warning(c,b);
|
kpeter@617
|
95 |
int i;
|
kpeter@617
|
96 |
CostMap cost;
|
kpeter@617
|
97 |
ArbMap arb;
|
kpeter@617
|
98 |
PredMap pred;
|
kpeter@617
|
99 |
|
kpeter@617
|
100 |
MinCostArbType mcarb_test(g, cost);
|
kpeter@617
|
101 |
const MinCostArbType& const_mcarb_test = mcarb_test;
|
kpeter@617
|
102 |
|
kpeter@617
|
103 |
mcarb_test
|
kpeter@617
|
104 |
.arborescenceMap(arb)
|
kpeter@617
|
105 |
.predMap(pred)
|
kpeter@617
|
106 |
.run(s);
|
kpeter@617
|
107 |
|
kpeter@617
|
108 |
mcarb_test.init();
|
kpeter@617
|
109 |
mcarb_test.addSource(s);
|
kpeter@617
|
110 |
mcarb_test.start();
|
kpeter@617
|
111 |
n = mcarb_test.processNextNode();
|
kpeter@617
|
112 |
b = const_mcarb_test.emptyQueue();
|
kpeter@617
|
113 |
i = const_mcarb_test.queueSize();
|
alpar@761
|
114 |
|
kpeter@617
|
115 |
c = const_mcarb_test.arborescenceCost();
|
kpeter@617
|
116 |
b = const_mcarb_test.arborescence(e);
|
kpeter@617
|
117 |
e = const_mcarb_test.pred(n);
|
kpeter@617
|
118 |
const MinCostArbType::ArborescenceMap &am =
|
kpeter@617
|
119 |
const_mcarb_test.arborescenceMap();
|
kpeter@617
|
120 |
const MinCostArbType::PredMap &pm =
|
kpeter@617
|
121 |
const_mcarb_test.predMap();
|
kpeter@617
|
122 |
b = const_mcarb_test.reached(n);
|
kpeter@617
|
123 |
b = const_mcarb_test.processed(n);
|
alpar@761
|
124 |
|
kpeter@617
|
125 |
i = const_mcarb_test.dualNum();
|
kpeter@617
|
126 |
c = const_mcarb_test.dualValue();
|
kpeter@617
|
127 |
i = const_mcarb_test.dualSize(i);
|
kpeter@617
|
128 |
c = const_mcarb_test.dualValue(i);
|
alpar@761
|
129 |
|
kpeter@617
|
130 |
ignore_unused_variable_warning(am);
|
kpeter@617
|
131 |
ignore_unused_variable_warning(pm);
|
kpeter@617
|
132 |
}
|
kpeter@617
|
133 |
|
deba@512
|
134 |
int main() {
|
deba@512
|
135 |
typedef SmartDigraph Digraph;
|
deba@512
|
136 |
DIGRAPH_TYPEDEFS(Digraph);
|
deba@512
|
137 |
|
deba@512
|
138 |
typedef Digraph::ArcMap<double> CostMap;
|
deba@512
|
139 |
|
deba@512
|
140 |
Digraph digraph;
|
deba@512
|
141 |
CostMap cost(digraph);
|
deba@512
|
142 |
Node source;
|
deba@512
|
143 |
|
deba@512
|
144 |
std::istringstream is(test_lgf);
|
deba@512
|
145 |
digraphReader(digraph, is).
|
deba@512
|
146 |
arcMap("cost", cost).
|
deba@512
|
147 |
node("source", source).run();
|
deba@512
|
148 |
|
deba@512
|
149 |
MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
|
deba@512
|
150 |
mca.run(source);
|
deba@512
|
151 |
|
deba@512
|
152 |
vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
|
deba@512
|
153 |
|
deba@512
|
154 |
for (int i = 0; i < mca.dualNum(); ++i) {
|
deba@512
|
155 |
dualSolution[i].first = mca.dualValue(i);
|
deba@512
|
156 |
for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
|
deba@512
|
157 |
it != INVALID; ++it) {
|
deba@512
|
158 |
dualSolution[i].second.insert(it);
|
deba@512
|
159 |
}
|
deba@512
|
160 |
}
|
deba@512
|
161 |
|
deba@512
|
162 |
for (ArcIt it(digraph); it != INVALID; ++it) {
|
deba@512
|
163 |
if (mca.reached(digraph.source(it))) {
|
deba@512
|
164 |
double sum = 0.0;
|
deba@512
|
165 |
for (int i = 0; i < int(dualSolution.size()); ++i) {
|
deba@512
|
166 |
if (dualSolution[i].second.find(digraph.target(it))
|
deba@512
|
167 |
!= dualSolution[i].second.end() &&
|
deba@512
|
168 |
dualSolution[i].second.find(digraph.source(it))
|
deba@512
|
169 |
== dualSolution[i].second.end()) {
|
deba@512
|
170 |
sum += dualSolution[i].first;
|
deba@512
|
171 |
}
|
deba@512
|
172 |
}
|
deba@512
|
173 |
if (mca.arborescence(it)) {
|
kpeter@617
|
174 |
check(sum == cost[it], "Invalid dual solution");
|
deba@512
|
175 |
}
|
kpeter@617
|
176 |
check(sum <= cost[it], "Invalid dual solution");
|
deba@512
|
177 |
}
|
deba@512
|
178 |
}
|
deba@512
|
179 |
|
deba@512
|
180 |
|
kpeter@617
|
181 |
check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
|
deba@512
|
182 |
|
kpeter@617
|
183 |
check(mca.reached(source), "Invalid arborescence");
|
deba@512
|
184 |
for (ArcIt a(digraph); a != INVALID; ++a) {
|
deba@512
|
185 |
check(!mca.reached(digraph.source(a)) ||
|
kpeter@617
|
186 |
mca.reached(digraph.target(a)), "Invalid arborescence");
|
deba@512
|
187 |
}
|
deba@512
|
188 |
|
deba@512
|
189 |
for (NodeIt n(digraph); n != INVALID; ++n) {
|
deba@512
|
190 |
if (!mca.reached(n)) continue;
|
deba@512
|
191 |
int cnt = 0;
|
deba@512
|
192 |
for (InArcIt a(digraph, n); a != INVALID; ++a) {
|
deba@512
|
193 |
if (mca.arborescence(a)) {
|
kpeter@617
|
194 |
check(mca.pred(n) == a, "Invalid arborescence");
|
deba@512
|
195 |
++cnt;
|
deba@512
|
196 |
}
|
deba@512
|
197 |
}
|
kpeter@617
|
198 |
check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
|
deba@512
|
199 |
}
|
deba@512
|
200 |
|
deba@512
|
201 |
Digraph::ArcMap<bool> arborescence(digraph);
|
kpeter@617
|
202 |
check(mca.arborescenceCost() ==
|
deba@512
|
203 |
minCostArborescence(digraph, cost, source, arborescence),
|
kpeter@617
|
204 |
"Wrong result of the function interface");
|
deba@512
|
205 |
|
deba@512
|
206 |
return 0;
|
deba@512
|
207 |
}
|