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@463
|
5 |
* Copyright (C) 2003-2009
|
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 |
///\ingroup tools
|
alpar@400
|
20 |
///\file
|
alpar@400
|
21 |
///\brief DIMACS to LGF converter.
|
alpar@400
|
22 |
///
|
alpar@400
|
23 |
/// This program converts various DIMACS formats to the LEMON Digraph Format
|
alpar@400
|
24 |
/// (LGF).
|
alpar@400
|
25 |
///
|
alpar@400
|
26 |
/// See
|
kpeter@631
|
27 |
/// \code
|
kpeter@631
|
28 |
/// dimacs-to-lgf --help
|
kpeter@631
|
29 |
/// \endcode
|
kpeter@631
|
30 |
/// for more info on the usage.
|
alpar@400
|
31 |
|
alpar@400
|
32 |
#include <iostream>
|
alpar@400
|
33 |
#include <fstream>
|
alpar@400
|
34 |
#include <cstring>
|
alpar@400
|
35 |
|
alpar@400
|
36 |
#include <lemon/smart_graph.h>
|
alpar@400
|
37 |
#include <lemon/dimacs.h>
|
alpar@400
|
38 |
#include <lemon/lgf_writer.h>
|
alpar@400
|
39 |
|
alpar@400
|
40 |
#include <lemon/arg_parser.h>
|
alpar@402
|
41 |
#include <lemon/error.h>
|
alpar@400
|
42 |
|
alpar@400
|
43 |
using namespace std;
|
alpar@400
|
44 |
using namespace lemon;
|
alpar@400
|
45 |
|
alpar@400
|
46 |
|
alpar@400
|
47 |
int main(int argc, const char *argv[]) {
|
alpar@400
|
48 |
typedef SmartDigraph Digraph;
|
alpar@400
|
49 |
|
alpar@400
|
50 |
typedef Digraph::Arc Arc;
|
alpar@400
|
51 |
typedef Digraph::Node Node;
|
alpar@400
|
52 |
typedef Digraph::ArcIt ArcIt;
|
alpar@400
|
53 |
typedef Digraph::NodeIt NodeIt;
|
alpar@400
|
54 |
typedef Digraph::ArcMap<double> DoubleArcMap;
|
alpar@400
|
55 |
typedef Digraph::NodeMap<double> DoubleNodeMap;
|
alpar@400
|
56 |
|
alpar@400
|
57 |
std::string inputName;
|
alpar@400
|
58 |
std::string outputName;
|
alpar@400
|
59 |
|
alpar@400
|
60 |
ArgParser ap(argc, argv);
|
alpar@402
|
61 |
ap.other("[INFILE [OUTFILE]]",
|
alpar@402
|
62 |
"If either the INFILE or OUTFILE file is missing the standard\n"
|
alpar@402
|
63 |
" input/output will be used instead.")
|
alpar@400
|
64 |
.run();
|
alpar@400
|
65 |
|
alpar@400
|
66 |
ifstream input;
|
alpar@402
|
67 |
ofstream output;
|
alpar@402
|
68 |
|
alpar@402
|
69 |
switch(ap.files().size())
|
alpar@402
|
70 |
{
|
alpar@402
|
71 |
case 2:
|
alpar@402
|
72 |
output.open(ap.files()[1].c_str());
|
alpar@402
|
73 |
if (!output) {
|
alpar@402
|
74 |
throw IoError("Cannot open the file for writing", ap.files()[1]);
|
alpar@402
|
75 |
}
|
alpar@1397
|
76 |
// fall through
|
alpar@402
|
77 |
case 1:
|
alpar@402
|
78 |
input.open(ap.files()[0].c_str());
|
alpar@402
|
79 |
if (!input) {
|
alpar@402
|
80 |
throw IoError("File cannot be found", ap.files()[0]);
|
alpar@402
|
81 |
}
|
alpar@1397
|
82 |
// fall through
|
alpar@402
|
83 |
case 0:
|
alpar@402
|
84 |
break;
|
alpar@402
|
85 |
default:
|
alpar@402
|
86 |
cerr << ap.commandName() << ": too many arguments\n";
|
alpar@402
|
87 |
return 1;
|
alpar@402
|
88 |
}
|
alpar@402
|
89 |
istream& is = (ap.files().size()<1 ? cin : input);
|
alpar@402
|
90 |
ostream& os = (ap.files().size()<2 ? cout : output);
|
alpar@402
|
91 |
|
alpar@402
|
92 |
DimacsDescriptor desc = dimacsType(is);
|
alpar@402
|
93 |
switch(desc.type)
|
alpar@402
|
94 |
{
|
alpar@402
|
95 |
case DimacsDescriptor::MIN:
|
alpar@402
|
96 |
{
|
alpar@402
|
97 |
Digraph digraph;
|
alpar@402
|
98 |
DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
|
alpar@402
|
99 |
DoubleNodeMap supply(digraph);
|
alpar@608
|
100 |
readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
|
alpar@402
|
101 |
DigraphWriter<Digraph>(digraph, os).
|
alpar@402
|
102 |
nodeMap("supply", supply).
|
alpar@402
|
103 |
arcMap("lower", lower).
|
alpar@402
|
104 |
arcMap("capacity", capacity).
|
alpar@402
|
105 |
arcMap("cost", cost).
|
alpar@402
|
106 |
attribute("problem","min").
|
alpar@402
|
107 |
run();
|
alpar@402
|
108 |
}
|
alpar@402
|
109 |
break;
|
alpar@402
|
110 |
case DimacsDescriptor::MAX:
|
alpar@402
|
111 |
{
|
alpar@402
|
112 |
Digraph digraph;
|
alpar@402
|
113 |
Node s, t;
|
alpar@402
|
114 |
DoubleArcMap capacity(digraph);
|
alpar@608
|
115 |
readDimacsMax(is, digraph, capacity, s, t, 0, desc);
|
alpar@402
|
116 |
DigraphWriter<Digraph>(digraph, os).
|
alpar@402
|
117 |
arcMap("capacity", capacity).
|
alpar@402
|
118 |
node("source", s).
|
alpar@402
|
119 |
node("target", t).
|
alpar@402
|
120 |
attribute("problem","max").
|
alpar@402
|
121 |
run();
|
alpar@402
|
122 |
}
|
alpar@402
|
123 |
break;
|
alpar@402
|
124 |
case DimacsDescriptor::SP:
|
alpar@402
|
125 |
{
|
alpar@402
|
126 |
Digraph digraph;
|
alpar@402
|
127 |
Node s;
|
alpar@402
|
128 |
DoubleArcMap capacity(digraph);
|
alpar@402
|
129 |
readDimacsSp(is, digraph, capacity, s, desc);
|
alpar@402
|
130 |
DigraphWriter<Digraph>(digraph, os).
|
alpar@402
|
131 |
arcMap("capacity", capacity).
|
alpar@402
|
132 |
node("source", s).
|
alpar@402
|
133 |
attribute("problem","sp").
|
alpar@402
|
134 |
run();
|
alpar@402
|
135 |
}
|
alpar@402
|
136 |
break;
|
alpar@402
|
137 |
case DimacsDescriptor::MAT:
|
alpar@402
|
138 |
{
|
alpar@402
|
139 |
Digraph digraph;
|
alpar@402
|
140 |
readDimacsMat(is, digraph,desc);
|
alpar@402
|
141 |
DigraphWriter<Digraph>(digraph, os).
|
alpar@402
|
142 |
attribute("problem","mat").
|
alpar@402
|
143 |
run();
|
alpar@402
|
144 |
}
|
alpar@402
|
145 |
break;
|
alpar@402
|
146 |
default:
|
alpar@402
|
147 |
break;
|
alpar@400
|
148 |
}
|
alpar@400
|
149 |
return 0;
|
alpar@400
|
150 |
}
|