alpar@385
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@385
|
2 |
*
|
alpar@385
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@385
|
4 |
*
|
alpar@440
|
5 |
* Copyright (C) 2003-2009
|
alpar@385
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@385
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@385
|
8 |
*
|
alpar@385
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@385
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@385
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@385
|
12 |
*
|
alpar@385
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@385
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@385
|
15 |
* purpose.
|
alpar@385
|
16 |
*
|
alpar@385
|
17 |
*/
|
alpar@385
|
18 |
|
alpar@385
|
19 |
///\ingroup tools
|
alpar@385
|
20 |
///\file
|
kpeter@532
|
21 |
///\brief DIMACS problem solver.
|
alpar@385
|
22 |
///
|
kpeter@532
|
23 |
/// This program solves various problems given in DIMACS format.
|
alpar@385
|
24 |
///
|
alpar@385
|
25 |
/// See
|
kpeter@584
|
26 |
/// \code
|
kpeter@584
|
27 |
/// dimacs-solver --help
|
kpeter@584
|
28 |
/// \endcode
|
alpar@385
|
29 |
/// for more info on usage.
|
alpar@385
|
30 |
|
alpar@385
|
31 |
#include <iostream>
|
alpar@385
|
32 |
#include <fstream>
|
alpar@385
|
33 |
#include <cstring>
|
alpar@385
|
34 |
|
alpar@385
|
35 |
#include <lemon/smart_graph.h>
|
alpar@385
|
36 |
#include <lemon/dimacs.h>
|
alpar@385
|
37 |
#include <lemon/lgf_writer.h>
|
alpar@526
|
38 |
#include <lemon/time_measure.h>
|
alpar@385
|
39 |
|
alpar@385
|
40 |
#include <lemon/arg_parser.h>
|
alpar@387
|
41 |
#include <lemon/error.h>
|
alpar@385
|
42 |
|
alpar@526
|
43 |
#include <lemon/dijkstra.h>
|
alpar@526
|
44 |
#include <lemon/preflow.h>
|
kpeter@594
|
45 |
#include <lemon/matching.h>
|
alpar@526
|
46 |
|
alpar@385
|
47 |
using namespace lemon;
|
alpar@526
|
48 |
typedef SmartDigraph Digraph;
|
alpar@526
|
49 |
DIGRAPH_TYPEDEFS(Digraph);
|
alpar@526
|
50 |
typedef SmartGraph Graph;
|
alpar@385
|
51 |
|
alpar@526
|
52 |
template<class Value>
|
alpar@526
|
53 |
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &,
|
alpar@526
|
54 |
DimacsDescriptor &desc)
|
alpar@526
|
55 |
{
|
alpar@526
|
56 |
bool report = !ap.given("q");
|
alpar@526
|
57 |
Digraph g;
|
alpar@526
|
58 |
Node s;
|
alpar@526
|
59 |
Digraph::ArcMap<Value> len(g);
|
alpar@526
|
60 |
Timer t;
|
alpar@526
|
61 |
t.restart();
|
alpar@526
|
62 |
readDimacsSp(is, g, len, s, desc);
|
alpar@526
|
63 |
if(report) std::cerr << "Read the file: " << t << '\n';
|
alpar@526
|
64 |
t.restart();
|
alpar@526
|
65 |
Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len);
|
alpar@526
|
66 |
if(report) std::cerr << "Setup Dijkstra class: " << t << '\n';
|
alpar@526
|
67 |
t.restart();
|
alpar@526
|
68 |
dij.run(s);
|
alpar@526
|
69 |
if(report) std::cerr << "Run Dijkstra: " << t << '\n';
|
alpar@526
|
70 |
}
|
alpar@526
|
71 |
|
alpar@526
|
72 |
template<class Value>
|
alpar@526
|
73 |
void solve_max(ArgParser &ap, std::istream &is, std::ostream &,
|
alpar@561
|
74 |
Value infty, DimacsDescriptor &desc)
|
alpar@526
|
75 |
{
|
alpar@526
|
76 |
bool report = !ap.given("q");
|
alpar@526
|
77 |
Digraph g;
|
alpar@526
|
78 |
Node s,t;
|
alpar@526
|
79 |
Digraph::ArcMap<Value> cap(g);
|
alpar@526
|
80 |
Timer ti;
|
alpar@526
|
81 |
ti.restart();
|
alpar@561
|
82 |
readDimacsMax(is, g, cap, s, t, infty, desc);
|
alpar@526
|
83 |
if(report) std::cerr << "Read the file: " << ti << '\n';
|
alpar@526
|
84 |
ti.restart();
|
alpar@526
|
85 |
Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t);
|
alpar@526
|
86 |
if(report) std::cerr << "Setup Preflow class: " << ti << '\n';
|
alpar@526
|
87 |
ti.restart();
|
alpar@526
|
88 |
pre.run();
|
alpar@526
|
89 |
if(report) std::cerr << "Run Preflow: " << ti << '\n';
|
alpar@526
|
90 |
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n';
|
alpar@526
|
91 |
}
|
alpar@526
|
92 |
|
alpar@526
|
93 |
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &,
|
alpar@526
|
94 |
DimacsDescriptor &desc)
|
alpar@526
|
95 |
{
|
alpar@526
|
96 |
bool report = !ap.given("q");
|
alpar@526
|
97 |
Graph g;
|
alpar@526
|
98 |
Timer ti;
|
alpar@526
|
99 |
ti.restart();
|
alpar@526
|
100 |
readDimacsMat(is, g, desc);
|
alpar@526
|
101 |
if(report) std::cerr << "Read the file: " << ti << '\n';
|
alpar@526
|
102 |
ti.restart();
|
alpar@526
|
103 |
MaxMatching<Graph> mat(g);
|
alpar@526
|
104 |
if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n';
|
alpar@526
|
105 |
ti.restart();
|
alpar@526
|
106 |
mat.run();
|
alpar@526
|
107 |
if(report) std::cerr << "Run MaxMatching: " << ti << '\n';
|
alpar@526
|
108 |
if(report) std::cerr << "\nCardinality of max matching: "
|
alpar@526
|
109 |
<< mat.matchingSize() << '\n';
|
alpar@526
|
110 |
}
|
alpar@526
|
111 |
|
alpar@526
|
112 |
|
alpar@526
|
113 |
template<class Value>
|
alpar@526
|
114 |
void solve(ArgParser &ap, std::istream &is, std::ostream &os,
|
alpar@526
|
115 |
DimacsDescriptor &desc)
|
alpar@526
|
116 |
{
|
ladanyi@569
|
117 |
std::stringstream iss(static_cast<std::string>(ap["infcap"]));
|
alpar@561
|
118 |
Value infty;
|
alpar@561
|
119 |
iss >> infty;
|
alpar@561
|
120 |
if(iss.fail())
|
alpar@561
|
121 |
{
|
alpar@561
|
122 |
std::cerr << "Cannot interpret '"
|
alpar@561
|
123 |
<< static_cast<std::string>(ap["infcap"]) << "' as infinite"
|
alpar@561
|
124 |
<< std::endl;
|
alpar@561
|
125 |
exit(1);
|
alpar@561
|
126 |
}
|
alpar@561
|
127 |
|
alpar@526
|
128 |
switch(desc.type)
|
alpar@526
|
129 |
{
|
alpar@526
|
130 |
case DimacsDescriptor::MIN:
|
alpar@526
|
131 |
std::cerr <<
|
kpeter@532
|
132 |
"\n\n Sorry, the min. cost flow solver is not yet available.\n";
|
alpar@526
|
133 |
break;
|
alpar@526
|
134 |
case DimacsDescriptor::MAX:
|
alpar@561
|
135 |
solve_max<Value>(ap,is,os,infty,desc);
|
alpar@526
|
136 |
break;
|
alpar@526
|
137 |
case DimacsDescriptor::SP:
|
alpar@526
|
138 |
solve_sp<Value>(ap,is,os,desc);
|
alpar@526
|
139 |
break;
|
alpar@526
|
140 |
case DimacsDescriptor::MAT:
|
alpar@526
|
141 |
solve_mat(ap,is,os,desc);
|
alpar@526
|
142 |
break;
|
alpar@526
|
143 |
default:
|
alpar@526
|
144 |
break;
|
alpar@526
|
145 |
}
|
alpar@526
|
146 |
}
|
alpar@385
|
147 |
|
alpar@385
|
148 |
int main(int argc, const char *argv[]) {
|
alpar@385
|
149 |
typedef SmartDigraph Digraph;
|
alpar@385
|
150 |
|
alpar@385
|
151 |
typedef Digraph::Arc Arc;
|
alpar@385
|
152 |
|
alpar@385
|
153 |
std::string inputName;
|
alpar@385
|
154 |
std::string outputName;
|
alpar@385
|
155 |
|
alpar@385
|
156 |
ArgParser ap(argc, argv);
|
alpar@387
|
157 |
ap.other("[INFILE [OUTFILE]]",
|
alpar@387
|
158 |
"If either the INFILE or OUTFILE file is missing the standard\n"
|
alpar@387
|
159 |
" input/output will be used instead.")
|
alpar@526
|
160 |
.boolOption("q", "Do not print any report")
|
alpar@526
|
161 |
.boolOption("int","Use 'int' for capacities, costs etc. (default)")
|
alpar@526
|
162 |
.optionGroup("datatype","int")
|
alpar@526
|
163 |
#ifdef HAVE_LONG_LONG
|
alpar@526
|
164 |
.boolOption("long","Use 'long long' for capacities, costs etc.")
|
alpar@526
|
165 |
.optionGroup("datatype","long")
|
alpar@526
|
166 |
#endif
|
alpar@526
|
167 |
.boolOption("double","Use 'double' for capacities, costs etc.")
|
alpar@526
|
168 |
.optionGroup("datatype","double")
|
alpar@526
|
169 |
.boolOption("ldouble","Use 'long double' for capacities, costs etc.")
|
alpar@526
|
170 |
.optionGroup("datatype","ldouble")
|
alpar@526
|
171 |
.onlyOneGroup("datatype")
|
alpar@561
|
172 |
.stringOption("infcap","Value used for 'very high' capacities","0")
|
alpar@385
|
173 |
.run();
|
alpar@385
|
174 |
|
alpar@526
|
175 |
std::ifstream input;
|
alpar@526
|
176 |
std::ofstream output;
|
alpar@387
|
177 |
|
alpar@387
|
178 |
switch(ap.files().size())
|
alpar@387
|
179 |
{
|
alpar@387
|
180 |
case 2:
|
alpar@387
|
181 |
output.open(ap.files()[1].c_str());
|
alpar@387
|
182 |
if (!output) {
|
alpar@387
|
183 |
throw IoError("Cannot open the file for writing", ap.files()[1]);
|
alpar@387
|
184 |
}
|
alpar@387
|
185 |
case 1:
|
alpar@387
|
186 |
input.open(ap.files()[0].c_str());
|
alpar@387
|
187 |
if (!input) {
|
alpar@387
|
188 |
throw IoError("File cannot be found", ap.files()[0]);
|
alpar@387
|
189 |
}
|
alpar@387
|
190 |
case 0:
|
alpar@387
|
191 |
break;
|
alpar@387
|
192 |
default:
|
alpar@526
|
193 |
std::cerr << ap.commandName() << ": too many arguments\n";
|
alpar@387
|
194 |
return 1;
|
kpeter@532
|
195 |
}
|
alpar@526
|
196 |
std::istream& is = (ap.files().size()<1 ? std::cin : input);
|
alpar@526
|
197 |
std::ostream& os = (ap.files().size()<2 ? std::cout : output);
|
alpar@387
|
198 |
|
alpar@387
|
199 |
DimacsDescriptor desc = dimacsType(is);
|
alpar@526
|
200 |
|
alpar@526
|
201 |
if(!ap.given("q"))
|
alpar@387
|
202 |
{
|
alpar@526
|
203 |
std::cout << "Problem type: ";
|
alpar@526
|
204 |
switch(desc.type)
|
alpar@526
|
205 |
{
|
alpar@526
|
206 |
case DimacsDescriptor::MIN:
|
alpar@526
|
207 |
std::cout << "min";
|
alpar@526
|
208 |
break;
|
alpar@526
|
209 |
case DimacsDescriptor::MAX:
|
alpar@526
|
210 |
std::cout << "max";
|
alpar@526
|
211 |
break;
|
alpar@526
|
212 |
case DimacsDescriptor::SP:
|
alpar@526
|
213 |
std::cout << "sp";
|
alpar@526
|
214 |
case DimacsDescriptor::MAT:
|
alpar@526
|
215 |
std::cout << "mat";
|
alpar@526
|
216 |
break;
|
alpar@526
|
217 |
default:
|
alpar@526
|
218 |
exit(1);
|
alpar@526
|
219 |
break;
|
alpar@526
|
220 |
}
|
alpar@526
|
221 |
std::cout << "\nNum of nodes: " << desc.nodeNum;
|
alpar@526
|
222 |
std::cout << "\nNum of arcs: " << desc.edgeNum;
|
kpeter@532
|
223 |
std::cout << "\n\n";
|
alpar@385
|
224 |
}
|
alpar@526
|
225 |
|
alpar@526
|
226 |
if(ap.given("double"))
|
alpar@526
|
227 |
solve<double>(ap,is,os,desc);
|
alpar@526
|
228 |
else if(ap.given("ldouble"))
|
alpar@526
|
229 |
solve<long double>(ap,is,os,desc);
|
alpar@526
|
230 |
#ifdef HAVE_LONG_LONG
|
alpar@526
|
231 |
else if(ap.given("long"))
|
alpar@526
|
232 |
solve<long long>(ap,is,os,desc);
|
alpar@526
|
233 |
#endif
|
alpar@526
|
234 |
else solve<int>(ap,is,os,desc);
|
alpar@526
|
235 |
|
alpar@385
|
236 |
return 0;
|
alpar@385
|
237 |
}
|