alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
* src/test/preflow_test.cc - Part of HUGOlib, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
jacint@833
|
17 |
#include <fstream>
|
klao@858
|
18 |
#include <string>
|
klao@858
|
19 |
|
jacint@833
|
20 |
#include "test_tools.h"
|
jacint@833
|
21 |
#include <hugo/smart_graph.h>
|
jacint@833
|
22 |
#include <hugo/dimacs.h>
|
jacint@833
|
23 |
#include <hugo/preflow.h>
|
jacint@833
|
24 |
#include <hugo/skeletons/graph.h>
|
jacint@833
|
25 |
#include <hugo/skeletons/maps.h>
|
alpar@855
|
26 |
|
jacint@833
|
27 |
using namespace hugo;
|
jacint@833
|
28 |
|
jacint@833
|
29 |
void check_Preflow()
|
jacint@833
|
30 |
{
|
jacint@833
|
31 |
typedef int VType;
|
alpar@880
|
32 |
typedef skeleton::StaticGraph Graph;
|
jacint@833
|
33 |
|
jacint@833
|
34 |
typedef Graph::Node Node;
|
jacint@833
|
35 |
typedef Graph::Edge Edge;
|
jacint@833
|
36 |
typedef skeleton::ReadMap<Edge,VType> CapMap;
|
jacint@833
|
37 |
typedef skeleton::ReadWriteMap<Edge,VType> FlowMap;
|
jacint@833
|
38 |
typedef skeleton::ReadWriteMap<Node,bool> CutMap;
|
jacint@833
|
39 |
|
jacint@833
|
40 |
typedef Preflow<Graph, int, CapMap, FlowMap> PType;
|
jacint@833
|
41 |
|
jacint@833
|
42 |
Graph G;
|
jacint@833
|
43 |
Node n;
|
jacint@833
|
44 |
CapMap cap;
|
jacint@833
|
45 |
FlowMap flow;
|
jacint@833
|
46 |
CutMap cut;
|
jacint@833
|
47 |
|
jacint@833
|
48 |
PType preflow_test(G,n,n,cap,flow);
|
jacint@833
|
49 |
|
jacint@833
|
50 |
preflow_test.run();
|
jacint@833
|
51 |
preflow_test.flowValue();
|
jacint@833
|
52 |
preflow_test.setSource(n);
|
jacint@833
|
53 |
preflow_test.setFlow(flow);
|
jacint@833
|
54 |
|
jacint@833
|
55 |
preflow_test.phase1(PType::NO_FLOW);
|
jacint@833
|
56 |
preflow_test.minCut(cut);
|
jacint@833
|
57 |
|
jacint@833
|
58 |
preflow_test.phase2();
|
jacint@833
|
59 |
preflow_test.setTarget(n);
|
jacint@833
|
60 |
preflow_test.setCap(cap);
|
jacint@833
|
61 |
preflow_test.minMinCut(cut);
|
jacint@833
|
62 |
preflow_test.maxMinCut(cut);
|
jacint@833
|
63 |
}
|
jacint@833
|
64 |
|
jacint@833
|
65 |
int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut,
|
jacint@833
|
66 |
SmartGraph::EdgeMap<int>& cap) {
|
jacint@833
|
67 |
|
jacint@833
|
68 |
int c=0;
|
jacint@833
|
69 |
for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) {
|
jacint@833
|
70 |
if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e];
|
jacint@833
|
71 |
}
|
jacint@833
|
72 |
return c;
|
jacint@833
|
73 |
}
|
jacint@833
|
74 |
|
jacint@833
|
75 |
int main() {
|
jacint@833
|
76 |
|
jacint@833
|
77 |
typedef SmartGraph Graph;
|
jacint@833
|
78 |
|
alpar@842
|
79 |
typedef Graph::Node Node;
|
jacint@833
|
80 |
typedef Graph::NodeIt NodeIt;
|
jacint@833
|
81 |
typedef Graph::EdgeIt EdgeIt;
|
jacint@833
|
82 |
typedef Graph::EdgeMap<int> CapMap;
|
jacint@833
|
83 |
typedef Graph::EdgeMap<int> FlowMap;
|
jacint@833
|
84 |
typedef Graph::NodeMap<bool> CutMap;
|
jacint@833
|
85 |
|
jacint@833
|
86 |
typedef Preflow<Graph, int> PType;
|
jacint@833
|
87 |
|
klao@859
|
88 |
std::string f_name;
|
klao@858
|
89 |
if( getenv("srcdir") ) {
|
jacint@887
|
90 |
f_name = std::string(getenv("srcdir")) + "/preflow_graph.dim";
|
klao@858
|
91 |
}
|
klao@858
|
92 |
else {
|
jacint@887
|
93 |
f_name = "preflow_graph.dim";
|
klao@858
|
94 |
}
|
alpar@855
|
95 |
|
klao@858
|
96 |
std::ifstream file(f_name.c_str());
|
alpar@855
|
97 |
|
klao@858
|
98 |
check(file, "Input file '" << f_name << "' not found.");
|
jacint@833
|
99 |
|
jacint@833
|
100 |
Graph G;
|
alpar@842
|
101 |
Node s, t;
|
jacint@833
|
102 |
CapMap cap(G);
|
jacint@833
|
103 |
readDimacs(file, G, cap, s, t);
|
jacint@833
|
104 |
|
jacint@833
|
105 |
FlowMap flow(G,0);
|
jacint@887
|
106 |
|
jacint@833
|
107 |
|
jacint@887
|
108 |
|
jacint@833
|
109 |
PType preflow_test(G, s, t, cap, flow);
|
jacint@833
|
110 |
preflow_test.run(PType::ZERO_FLOW);
|
jacint@887
|
111 |
|
jacint@833
|
112 |
CutMap mincut(G,false);
|
jacint@833
|
113 |
preflow_test.minCut(mincut);
|
jacint@833
|
114 |
int min_cut_value=cut_value(G,mincut,cap);
|
jacint@833
|
115 |
|
jacint@833
|
116 |
CutMap minmincut(G,false);
|
jacint@833
|
117 |
preflow_test.minMinCut(minmincut);
|
jacint@833
|
118 |
int min_min_cut_value=cut_value(G,minmincut,cap);
|
jacint@833
|
119 |
|
jacint@833
|
120 |
CutMap maxmincut(G,false);
|
jacint@833
|
121 |
preflow_test.maxMinCut(maxmincut);
|
jacint@833
|
122 |
int max_min_cut_value=cut_value(G,maxmincut,cap);
|
jacint@833
|
123 |
|
jacint@833
|
124 |
check(preflow_test.flowValue() == min_cut_value &&
|
jacint@833
|
125 |
min_cut_value == min_min_cut_value &&
|
jacint@833
|
126 |
min_min_cut_value == max_min_cut_value,
|
jacint@833
|
127 |
"The max flow value is not equal to the three min cut values.");
|
jacint@833
|
128 |
|
jacint@833
|
129 |
int flow_value=preflow_test.flowValue();
|
jacint@833
|
130 |
|
jacint@833
|
131 |
|
jacint@887
|
132 |
|
jacint@833
|
133 |
for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e];
|
jacint@833
|
134 |
preflow_test.setCap(cap);
|
alpar@842
|
135 |
|
jacint@833
|
136 |
preflow_test.phase1(PType::PRE_FLOW);
|
jacint@833
|
137 |
|
jacint@833
|
138 |
CutMap mincut1(G,false);
|
jacint@833
|
139 |
preflow_test.minCut(mincut1);
|
jacint@833
|
140 |
min_cut_value=cut_value(G,mincut1,cap);
|
jacint@833
|
141 |
|
jacint@833
|
142 |
check(preflow_test.flowValue() == min_cut_value &&
|
jacint@833
|
143 |
min_cut_value == 2*flow_value,
|
jacint@833
|
144 |
"The max flow value or the min cut value is wrong.");
|
jacint@833
|
145 |
|
jacint@833
|
146 |
preflow_test.phase2();
|
jacint@833
|
147 |
|
jacint@833
|
148 |
CutMap mincut2(G,false);
|
jacint@833
|
149 |
preflow_test.minCut(mincut2);
|
jacint@833
|
150 |
min_cut_value=cut_value(G,mincut2,cap);
|
jacint@833
|
151 |
|
jacint@833
|
152 |
CutMap minmincut2(G,false);
|
jacint@833
|
153 |
preflow_test.minMinCut(minmincut2);
|
jacint@833
|
154 |
min_min_cut_value=cut_value(G,minmincut2,cap);
|
jacint@833
|
155 |
|
jacint@833
|
156 |
preflow_test.maxMinCut(maxmincut);
|
jacint@833
|
157 |
max_min_cut_value=cut_value(G,maxmincut,cap);
|
jacint@833
|
158 |
|
jacint@833
|
159 |
check(preflow_test.flowValue() == min_cut_value &&
|
jacint@833
|
160 |
min_cut_value == min_min_cut_value &&
|
jacint@833
|
161 |
min_min_cut_value == max_min_cut_value &&
|
jacint@833
|
162 |
min_cut_value == 2*flow_value,
|
jacint@833
|
163 |
"The max flow value or the three min cut values were not doubled");
|
jacint@833
|
164 |
|
jacint@887
|
165 |
|
jacint@887
|
166 |
|
jacint@833
|
167 |
EdgeIt e(G);
|
jacint@887
|
168 |
for( int i=1; i==10; ++i ) {
|
jacint@887
|
169 |
flow.set(e,0);
|
jacint@833
|
170 |
++e;
|
jacint@833
|
171 |
}
|
jacint@833
|
172 |
|
jacint@833
|
173 |
preflow_test.setFlow(flow);
|
jacint@887
|
174 |
|
jacint@887
|
175 |
NodeIt tmp1(G,s);
|
jacint@887
|
176 |
++tmp1;
|
jacint@887
|
177 |
if ( tmp1 != INVALID ) s=tmp1;
|
jacint@887
|
178 |
|
jacint@887
|
179 |
NodeIt tmp2(G,t);
|
jacint@887
|
180 |
++tmp2;
|
jacint@887
|
181 |
if ( tmp2 != INVALID ) t=tmp2;
|
jacint@887
|
182 |
|
jacint@833
|
183 |
preflow_test.setSource(s);
|
jacint@887
|
184 |
preflow_test.setTarget(t);
|
jacint@887
|
185 |
|
jacint@833
|
186 |
preflow_test.run();
|
jacint@833
|
187 |
|
jacint@833
|
188 |
CutMap mincut3(G,false);
|
jacint@833
|
189 |
preflow_test.minCut(mincut3);
|
jacint@833
|
190 |
min_cut_value=cut_value(G,mincut3,cap);
|
jacint@833
|
191 |
|
jacint@833
|
192 |
CutMap minmincut3(G,false);
|
jacint@833
|
193 |
preflow_test.minMinCut(minmincut3);
|
jacint@833
|
194 |
min_min_cut_value=cut_value(G,minmincut3,cap);
|
jacint@833
|
195 |
|
jacint@833
|
196 |
preflow_test.maxMinCut(maxmincut);
|
jacint@833
|
197 |
max_min_cut_value=cut_value(G,maxmincut,cap);
|
jacint@833
|
198 |
|
jacint@833
|
199 |
check(preflow_test.flowValue() == min_cut_value &&
|
jacint@833
|
200 |
min_cut_value == min_min_cut_value &&
|
jacint@833
|
201 |
min_min_cut_value == max_min_cut_value,
|
jacint@833
|
202 |
"The max flow value or the three min cut values are incorrect.");
|
jacint@833
|
203 |
}
|