Location: LEMON/LEMON-official/tools/dimacs-solver.cc - annotation
Load file history
Merge and extend the fix of #275
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r579:997a75bac45a r573:28b154307c0d r579:997a75bac45a r573:28b154307c0d r573:28b154307c0d r631:33c6b6e755cd r631:33c6b6e755cd r631:33c6b6e755cd r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r641:d657c71db7db r649:a79ef774fae1 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r608:6e0525ec5355 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r608:6e0525ec5355 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r649:a79ef774fae1 r649:a79ef774fae1 r661:19b6f20e0ea2 r649:a79ef774fae1 r649:a79ef774fae1 r649:a79ef774fae1 r649:a79ef774fae1 r649:a79ef774fae1 r649:a79ef774fae1 r661:19b6f20e0ea2 r649:a79ef774fae1 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r649:a79ef774fae1 r661:19b6f20e0ea2 r649:a79ef774fae1 r652:5232721b3f14 r652:5232721b3f14 r661:19b6f20e0ea2 r649:a79ef774fae1 r649:a79ef774fae1 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r661:19b6f20e0ea2 r649:a79ef774fae1 r649:a79ef774fae1 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r616:24682336c38e r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r608:6e0525ec5355 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r661:19b6f20e0ea2 r573:28b154307c0d r573:28b154307c0d r608:6e0525ec5355 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r674:20dac2104519 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r608:6e0525ec5355 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r579:997a75bac45a r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r579:997a75bac45a r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r674:20dac2104519 r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d r573:28b154307c0d | /* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2009
* 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.
*
*/
///\ingroup tools
///\file
///\brief DIMACS problem solver.
///
/// This program solves various problems given in DIMACS format.
///
/// See
/// \code
/// dimacs-solver --help
/// \endcode
/// for more info on usage.
#include <iostream>
#include <fstream>
#include <cstring>
#include <lemon/smart_graph.h>
#include <lemon/dimacs.h>
#include <lemon/lgf_writer.h>
#include <lemon/time_measure.h>
#include <lemon/arg_parser.h>
#include <lemon/error.h>
#include <lemon/dijkstra.h>
#include <lemon/preflow.h>
#include <lemon/matching.h>
#include <lemon/network_simplex.h>
using namespace lemon;
typedef SmartDigraph Digraph;
DIGRAPH_TYPEDEFS(Digraph);
typedef SmartGraph Graph;
template<class Value>
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &,
DimacsDescriptor &desc)
{
bool report = !ap.given("q");
Digraph g;
Node s;
Digraph::ArcMap<Value> len(g);
Timer t;
t.restart();
readDimacsSp(is, g, len, s, desc);
if(report) std::cerr << "Read the file: " << t << '\n';
t.restart();
Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len);
if(report) std::cerr << "Setup Dijkstra class: " << t << '\n';
t.restart();
dij.run(s);
if(report) std::cerr << "Run Dijkstra: " << t << '\n';
}
template<class Value>
void solve_max(ArgParser &ap, std::istream &is, std::ostream &,
Value infty, DimacsDescriptor &desc)
{
bool report = !ap.given("q");
Digraph g;
Node s,t;
Digraph::ArcMap<Value> cap(g);
Timer ti;
ti.restart();
readDimacsMax(is, g, cap, s, t, infty, desc);
if(report) std::cerr << "Read the file: " << ti << '\n';
ti.restart();
Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t);
if(report) std::cerr << "Setup Preflow class: " << ti << '\n';
ti.restart();
pre.run();
if(report) std::cerr << "Run Preflow: " << ti << '\n';
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n';
}
template<class Value>
void solve_min(ArgParser &ap, std::istream &is, std::ostream &,
Value infty, DimacsDescriptor &desc)
{
bool report = !ap.given("q");
Digraph g;
Digraph::ArcMap<Value> lower(g), cap(g), cost(g);
Digraph::NodeMap<Value> sup(g);
Timer ti;
ti.restart();
readDimacsMin(is, g, lower, cap, cost, sup, infty, desc);
ti.stop();
Value sum_sup = 0;
for (Digraph::NodeIt n(g); n != INVALID; ++n) {
sum_sup += sup[n];
}
if (report) {
std::cerr << "Sum of supply values: " << sum_sup << "\n";
if (sum_sup <= 0)
std::cerr << "GEQ supply contraints are used for NetworkSimplex\n\n";
else
std::cerr << "LEQ supply contraints are used for NetworkSimplex\n\n";
}
if (report) std::cerr << "Read the file: " << ti << '\n';
ti.restart();
NetworkSimplex<Digraph, Value> ns(g);
ns.lowerMap(lower).capacityMap(cap).costMap(cost).supplyMap(sup);
if (sum_sup > 0) ns.problemType(ns.LEQ);
if (report) std::cerr << "Setup NetworkSimplex class: " << ti << '\n';
ti.restart();
bool res = ns.run();
if (report) {
std::cerr << "Run NetworkSimplex: " << ti << "\n\n";
std::cerr << "Feasible flow: " << (res ? "found" : "not found") << '\n';
if (res) std::cerr << "Min flow cost: " << ns.totalCost() << '\n';
}
}
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &,
DimacsDescriptor &desc)
{
bool report = !ap.given("q");
Graph g;
Timer ti;
ti.restart();
readDimacsMat(is, g, desc);
if(report) std::cerr << "Read the file: " << ti << '\n';
ti.restart();
MaxMatching<Graph> mat(g);
if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n';
ti.restart();
mat.run();
if(report) std::cerr << "Run MaxMatching: " << ti << '\n';
if(report) std::cerr << "\nCardinality of max matching: "
<< mat.matchingSize() << '\n';
}
template<class Value>
void solve(ArgParser &ap, std::istream &is, std::ostream &os,
DimacsDescriptor &desc)
{
std::stringstream iss(static_cast<std::string>(ap["infcap"]));
Value infty;
iss >> infty;
if(iss.fail())
{
std::cerr << "Cannot interpret '"
<< static_cast<std::string>(ap["infcap"]) << "' as infinite"
<< std::endl;
exit(1);
}
switch(desc.type)
{
case DimacsDescriptor::MIN:
solve_min<Value>(ap,is,os,infty,desc);
break;
case DimacsDescriptor::MAX:
solve_max<Value>(ap,is,os,infty,desc);
break;
case DimacsDescriptor::SP:
solve_sp<Value>(ap,is,os,desc);
break;
case DimacsDescriptor::MAT:
solve_mat(ap,is,os,desc);
break;
default:
break;
}
}
int main(int argc, const char *argv[]) {
typedef SmartDigraph Digraph;
typedef Digraph::Arc Arc;
std::string inputName;
std::string outputName;
ArgParser ap(argc, argv);
ap.other("[INFILE [OUTFILE]]",
"If either the INFILE or OUTFILE file is missing the standard\n"
" input/output will be used instead.")
.boolOption("q", "Do not print any report")
.boolOption("int","Use 'int' for capacities, costs etc. (default)")
.optionGroup("datatype","int")
#ifdef LEMON_HAVE_LONG_LONG
.boolOption("long","Use 'long long' for capacities, costs etc.")
.optionGroup("datatype","long")
#endif
.boolOption("double","Use 'double' for capacities, costs etc.")
.optionGroup("datatype","double")
.boolOption("ldouble","Use 'long double' for capacities, costs etc.")
.optionGroup("datatype","ldouble")
.onlyOneGroup("datatype")
.stringOption("infcap","Value used for 'very high' capacities","0")
.run();
std::ifstream input;
std::ofstream output;
switch(ap.files().size())
{
case 2:
output.open(ap.files()[1].c_str());
if (!output) {
throw IoError("Cannot open the file for writing", ap.files()[1]);
}
case 1:
input.open(ap.files()[0].c_str());
if (!input) {
throw IoError("File cannot be found", ap.files()[0]);
}
case 0:
break;
default:
std::cerr << ap.commandName() << ": too many arguments\n";
return 1;
}
std::istream& is = (ap.files().size()<1 ? std::cin : input);
std::ostream& os = (ap.files().size()<2 ? std::cout : output);
DimacsDescriptor desc = dimacsType(is);
if(!ap.given("q"))
{
std::cout << "Problem type: ";
switch(desc.type)
{
case DimacsDescriptor::MIN:
std::cout << "min";
break;
case DimacsDescriptor::MAX:
std::cout << "max";
break;
case DimacsDescriptor::SP:
std::cout << "sp";
case DimacsDescriptor::MAT:
std::cout << "mat";
break;
default:
exit(1);
break;
}
std::cout << "\nNum of nodes: " << desc.nodeNum;
std::cout << "\nNum of arcs: " << desc.edgeNum;
std::cout << "\n\n";
}
if(ap.given("double"))
solve<double>(ap,is,os,desc);
else if(ap.given("ldouble"))
solve<long double>(ap,is,os,desc);
#ifdef LEMON_HAVE_LONG_LONG
else if(ap.given("long"))
solve<long long>(ap,is,os,desc);
#endif
else solve<int>(ap,is,os,desc);
return 0;
}
|