0
2
0
12
11
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIMACS_H |
20 | 20 |
#define LEMON_DIMACS_H |
21 | 21 |
|
22 | 22 |
#include <iostream> |
23 | 23 |
#include <string> |
24 | 24 |
#include <vector> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
|
27 | 27 |
/// \ingroup dimacs_group |
28 | 28 |
/// \file |
29 | 29 |
/// \brief DIMACS file format reader. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
///@defgroup dimacs_group DIMACS format |
34 | 34 |
///\brief Read and write files in DIMACS format |
35 | 35 |
/// |
36 | 36 |
///Tools to read a digraph from or write it to a file in DIMACS format |
37 | 37 |
///data |
38 | 38 |
///\ingroup io_group |
39 | 39 |
|
40 | 40 |
/// \addtogroup dimacs_group |
41 | 41 |
/// @{ |
42 | 42 |
|
43 | 43 |
/// DIMACS min cost flow reader function. |
44 | 44 |
/// |
45 | 45 |
/// This function reads a min cost flow instance from DIMACS format, |
46 | 46 |
/// i.e. from DIMACS files having a line starting with |
47 | 47 |
/// \code |
48 | 48 |
/// p min |
49 | 49 |
/// \endcode |
50 | 50 |
/// At the beginning \c g is cleared by \c g.clear(). The supply |
51 | 51 |
/// amount of the nodes are written to \c supply (signed). The |
52 | 52 |
/// lower bounds, capacities and costs of the arcs are written to |
53 | 53 |
/// \c lower, \c capacity and \c cost. |
54 | 54 |
template <typename Digraph, typename LowerMap, |
55 | 55 |
typename CapacityMap, typename CostMap, |
56 | 56 |
typename SupplyMap> |
57 |
void |
|
57 |
void readDimacsMin( std::istream& is, |
|
58 | 58 |
Digraph &g, |
59 | 59 |
LowerMap& lower, |
60 | 60 |
CapacityMap& capacity, |
61 | 61 |
CostMap& cost, |
62 | 62 |
SupplyMap& supply ) |
63 | 63 |
{ |
64 | 64 |
g.clear(); |
65 | 65 |
std::vector<typename Digraph::Node> nodes; |
66 | 66 |
typename Digraph::Arc e; |
67 | 67 |
std::string problem, str; |
68 | 68 |
char c; |
69 | 69 |
int n, m; |
70 | 70 |
int i, j; |
71 | 71 |
typename SupplyMap::Value sup; |
72 | 72 |
typename CapacityMap::Value low; |
73 | 73 |
typename CapacityMap::Value cap; |
74 | 74 |
typename CostMap::Value co; |
75 | 75 |
while (is >> c) { |
76 | 76 |
switch (c) { |
77 | 77 |
case 'c': // comment line |
78 | 78 |
getline(is, str); |
79 | 79 |
break; |
80 | 80 |
case 'p': // problem definition line |
81 | 81 |
is >> problem >> n >> m; |
82 | 82 |
getline(is, str); |
83 | 83 |
if (problem != "min") return; |
84 | 84 |
nodes.resize(n + 1); |
85 | 85 |
for (int k = 1; k <= n; ++k) { |
86 | 86 |
nodes[k] = g.addNode(); |
87 | 87 |
supply.set(nodes[k], 0); |
88 | 88 |
} |
89 | 89 |
break; |
90 | 90 |
case 'n': // node definition line |
91 | 91 |
is >> i >> sup; |
92 | 92 |
getline(is, str); |
93 | 93 |
supply.set(nodes[i], sup); |
94 | 94 |
break; |
95 | 95 |
case 'a': // arc (arc) definition line |
96 | 96 |
is >> i >> j >> low >> cap >> co; |
97 | 97 |
getline(is, str); |
98 | 98 |
e = g.addArc(nodes[i], nodes[j]); |
99 | 99 |
lower.set(e, low); |
100 | 100 |
if (cap >= 0) |
101 | 101 |
capacity.set(e, cap); |
102 | 102 |
else |
103 | 103 |
capacity.set(e, -1); |
104 | 104 |
cost.set(e, co); |
105 | 105 |
break; |
106 | 106 |
} |
107 | 107 |
} |
108 | 108 |
} |
109 | 109 |
|
110 | 110 |
/// DIMACS max flow reader function. |
111 | 111 |
/// |
112 | 112 |
/// This function reads a max flow instance from DIMACS format, |
113 | 113 |
/// i.e. from DIMACS files having a line starting with |
114 | 114 |
/// \code |
115 | 115 |
/// p max |
116 | 116 |
/// \endcode |
117 | 117 |
/// At the beginning \c g is cleared by \c g.clear(). The arc |
118 | 118 |
/// capacities are written to \c capacity and \c s and \c t are |
119 | 119 |
/// set to the source and the target nodes. |
120 | 120 |
template<typename Digraph, typename CapacityMap> |
121 |
void |
|
121 |
void readDimacsMax(std::istream& is, Digraph &g, CapacityMap& capacity, |
|
122 | 122 |
typename Digraph::Node &s, typename Digraph::Node &t) { |
123 | 123 |
g.clear(); |
124 | 124 |
std::vector<typename Digraph::Node> nodes; |
125 | 125 |
typename Digraph::Arc e; |
126 | 126 |
std::string problem; |
127 | 127 |
char c, d; |
128 | 128 |
int n, m; |
129 | 129 |
int i, j; |
130 | 130 |
typename CapacityMap::Value _cap; |
131 | 131 |
std::string str; |
132 | 132 |
while (is >> c) { |
133 | 133 |
switch (c) { |
134 | 134 |
case 'c': // comment line |
135 | 135 |
getline(is, str); |
136 | 136 |
break; |
137 | 137 |
case 'p': // problem definition line |
138 | 138 |
is >> problem >> n >> m; |
139 | 139 |
getline(is, str); |
140 | 140 |
nodes.resize(n + 1); |
141 | 141 |
for (int k = 1; k <= n; ++k) |
142 | 142 |
nodes[k] = g.addNode(); |
143 | 143 |
break; |
144 | 144 |
case 'n': // node definition line |
145 | 145 |
if (problem == "sp") { // shortest path problem |
146 | 146 |
is >> i; |
147 | 147 |
getline(is, str); |
148 | 148 |
s = nodes[i]; |
149 | 149 |
} |
150 | 150 |
if (problem == "max") { // max flow problem |
151 | 151 |
is >> i >> d; |
152 | 152 |
getline(is, str); |
153 | 153 |
if (d == 's') s = nodes[i]; |
154 | 154 |
if (d == 't') t = nodes[i]; |
155 | 155 |
} |
156 | 156 |
break; |
157 | 157 |
case 'a': // arc (arc) definition line |
158 | 158 |
if (problem == "max" || problem == "sp") { |
159 | 159 |
is >> i >> j >> _cap; |
160 | 160 |
getline(is, str); |
161 | 161 |
e = g.addArc(nodes[i], nodes[j]); |
162 | 162 |
capacity.set(e, _cap); |
163 | 163 |
} else { |
164 | 164 |
is >> i >> j; |
165 | 165 |
getline(is, str); |
166 | 166 |
g.addArc(nodes[i], nodes[j]); |
167 | 167 |
} |
168 | 168 |
break; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
/// DIMACS shortest path reader function. |
174 | 174 |
/// |
175 | 175 |
/// This function reads a shortest path instance from DIMACS format, |
176 | 176 |
/// i.e. from DIMACS files having a line starting with |
177 | 177 |
/// \code |
178 | 178 |
/// p sp |
179 | 179 |
/// \endcode |
180 | 180 |
/// At the beginning \c g is cleared by \c g.clear(). The arc |
181 | 181 |
/// capacities are written to \c capacity and \c s is set to the |
182 | 182 |
/// source node. |
183 | 183 |
template<typename Digraph, typename CapacityMap> |
184 |
void |
|
184 |
void readDimacsSp(std::istream& is, Digraph &g, CapacityMap& capacity, |
|
185 | 185 |
typename Digraph::Node &s) { |
186 |
|
|
186 |
typename Digraph::Node t; |
|
187 |
readDimacsMax(is, g, capacity, s, t); |
|
187 | 188 |
} |
188 | 189 |
|
189 | 190 |
/// DIMACS capacitated digraph reader function. |
190 | 191 |
/// |
191 | 192 |
/// This function reads an arc capacitated digraph instance from |
192 | 193 |
/// DIMACS format. At the beginning \c g is cleared by \c g.clear() |
193 | 194 |
/// and the arc capacities are written to \c capacity. |
194 | 195 |
template<typename Digraph, typename CapacityMap> |
195 |
void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity) { |
|
196 |
typename Digraph::Node u; |
|
197 |
|
|
196 |
void readDimacsMax(std::istream& is, Digraph &g, CapacityMap& capacity) { |
|
197 |
typename Digraph::Node u,v; |
|
198 |
readDimacsMax(is, g, capacity, u, v); |
|
198 | 199 |
} |
199 | 200 |
|
200 | 201 |
/// DIMACS plain digraph reader function. |
201 | 202 |
/// |
202 | 203 |
/// This function reads a digraph without any designated nodes and |
203 | 204 |
/// maps from DIMACS format, i.e. from DIMACS files having a line |
204 | 205 |
/// starting with |
205 | 206 |
/// \code |
206 | 207 |
/// p mat |
207 | 208 |
/// \endcode |
208 | 209 |
/// At the beginning \c g is cleared by \c g.clear(). |
209 | 210 |
template<typename Digraph> |
210 |
void readDimacs(std::istream& is, Digraph &g) { |
|
211 |
typename Digraph::Node u; |
|
211 |
void readDimacsMat(std::istream& is, Digraph &g) { |
|
212 |
typename Digraph::Node u,v; |
|
212 | 213 |
NullMap<typename Digraph::Arc, int> n; |
213 |
|
|
214 |
readDimacsMax(is, g, n, u, v); |
|
214 | 215 |
} |
215 | 216 |
|
216 | 217 |
/// DIMACS plain digraph writer function. |
217 | 218 |
/// |
218 | 219 |
/// This function writes a digraph without any designated nodes and |
219 | 220 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
220 | 221 |
/// starting with |
221 | 222 |
/// \code |
222 | 223 |
/// p mat |
223 | 224 |
/// \endcode |
224 | 225 |
template<typename Digraph> |
225 |
void |
|
226 |
void writeDimacsMat(std::ostream& os, const Digraph &g) { |
|
226 | 227 |
typedef typename Digraph::NodeIt NodeIt; |
227 | 228 |
typedef typename Digraph::ArcIt ArcIt; |
228 | 229 |
|
229 | 230 |
os << "c matching problem" << std::endl; |
230 | 231 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl; |
231 | 232 |
|
232 | 233 |
typename Digraph::template NodeMap<int> nodes(g); |
233 | 234 |
int i = 1; |
234 | 235 |
for(NodeIt v(g); v != INVALID; ++v) { |
235 | 236 |
nodes.set(v, i); |
236 | 237 |
++i; |
237 | 238 |
} |
238 | 239 |
for(ArcIt e(g); e != INVALID; ++e) { |
239 | 240 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] |
240 | 241 |
<< std::endl; |
241 | 242 |
} |
242 | 243 |
} |
243 | 244 |
|
244 | 245 |
/// @} |
245 | 246 |
|
246 | 247 |
} //namespace lemon |
247 | 248 |
|
248 | 249 |
#endif //LEMON_DIMACS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup tools |
20 | 20 |
///\file |
21 | 21 |
///\brief DIMACS to LGF converter. |
22 | 22 |
/// |
23 | 23 |
/// This program converts various DIMACS formats to the LEMON Digraph Format |
24 | 24 |
/// (LGF). |
25 | 25 |
/// |
26 | 26 |
/// See |
27 | 27 |
/// \verbatim |
28 | 28 |
/// dimacs-to-lgf --help |
29 | 29 |
/// \endverbatim |
30 | 30 |
/// for more info on usage. |
31 | 31 |
/// |
32 | 32 |
|
33 | 33 |
#include <iostream> |
34 | 34 |
#include <fstream> |
35 | 35 |
#include <cstring> |
36 | 36 |
|
37 | 37 |
#include <lemon/smart_graph.h> |
38 | 38 |
#include <lemon/dimacs.h> |
39 | 39 |
#include <lemon/lgf_writer.h> |
40 | 40 |
|
41 | 41 |
#include <lemon/arg_parser.h> |
42 | 42 |
|
43 | 43 |
using namespace std; |
44 | 44 |
using namespace lemon; |
45 | 45 |
|
46 | 46 |
|
47 | 47 |
int main(int argc, const char *argv[]) { |
48 | 48 |
typedef SmartDigraph Digraph; |
49 | 49 |
|
50 | 50 |
typedef Digraph::Arc Arc; |
51 | 51 |
typedef Digraph::Node Node; |
52 | 52 |
typedef Digraph::ArcIt ArcIt; |
53 | 53 |
typedef Digraph::NodeIt NodeIt; |
54 | 54 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
55 | 55 |
typedef Digraph::NodeMap<double> DoubleNodeMap; |
56 | 56 |
|
57 | 57 |
std::string inputName; |
58 | 58 |
std::string outputName; |
59 | 59 |
std::string typeName; |
60 | 60 |
|
61 | 61 |
bool mincostflow; |
62 | 62 |
bool maxflow; |
63 | 63 |
bool shortestpath; |
64 | 64 |
bool capacitated; |
65 | 65 |
bool plain; |
66 | 66 |
|
67 | 67 |
bool version; |
68 | 68 |
|
69 | 69 |
ArgParser ap(argc, argv); |
70 | 70 |
ap.refOption("-input", |
71 | 71 |
"use FILE as input instead of standard input", |
72 | 72 |
inputName).synonym("i", "-input") |
73 | 73 |
.refOption("-output", |
74 | 74 |
"use FILE as output instead of standard output", |
75 | 75 |
outputName).synonym("o", "-output") |
76 | 76 |
.refOption("-mincostflow", |
77 | 77 |
"set the type of the digraph to \"mincostflow\" digraph", |
78 | 78 |
mincostflow) |
79 | 79 |
.optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow") |
80 | 80 |
.refOption("-maxflow", |
81 | 81 |
"set the type of the digraph to \"maxflow\" digraph", |
82 | 82 |
maxflow) |
83 | 83 |
.optionGroup("type", "-maxflow").synonym("mf", "-maxflow") |
84 | 84 |
.refOption("-shortestpath", |
85 | 85 |
"set the type of the digraph to \"shortestpath\" digraph", |
86 | 86 |
shortestpath) |
87 | 87 |
.optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath") |
88 | 88 |
.refOption("-capacitated", |
89 | 89 |
"set the type of the digraph to \"capacitated\" digraph", |
90 | 90 |
capacitated) |
91 | 91 |
.optionGroup("type", "-capacitated").synonym("cap", "-capacitated") |
92 | 92 |
.refOption("-plain", |
93 | 93 |
"set the type of the digraph to \"plain\" digraph", |
94 | 94 |
plain) |
95 | 95 |
.optionGroup("type", "-plain").synonym("pl", "-plain") |
96 | 96 |
.onlyOneGroup("type") |
97 | 97 |
.mandatoryGroup("type") |
98 | 98 |
.refOption("-version", "show version information", version) |
99 | 99 |
.synonym("v", "-version") |
100 | 100 |
.run(); |
101 | 101 |
|
102 | 102 |
ifstream input; |
103 | 103 |
if (!inputName.empty()) { |
104 | 104 |
input.open(inputName.c_str()); |
105 | 105 |
if (!input) { |
106 | 106 |
cerr << "File open error" << endl; |
107 | 107 |
return -1; |
108 | 108 |
} |
109 | 109 |
} |
110 | 110 |
istream& is = (inputName.empty() ? cin : input); |
111 | 111 |
|
112 | 112 |
ofstream output; |
113 | 113 |
if (!outputName.empty()) { |
114 | 114 |
output.open(outputName.c_str()); |
115 | 115 |
if (!output) { |
116 | 116 |
cerr << "File open error" << endl; |
117 | 117 |
return -1; |
118 | 118 |
} |
119 | 119 |
} |
120 | 120 |
ostream& os = (outputName.empty() ? cout : output); |
121 | 121 |
|
122 | 122 |
if (mincostflow) { |
123 | 123 |
Digraph digraph; |
124 | 124 |
DoubleArcMap lower(digraph), capacity(digraph), cost(digraph); |
125 | 125 |
DoubleNodeMap supply(digraph); |
126 |
|
|
126 |
readDimacsMin(is, digraph, lower, capacity, cost, supply); |
|
127 | 127 |
DigraphWriter<Digraph>(digraph, os). |
128 | 128 |
nodeMap("supply", supply). |
129 | 129 |
arcMap("lower", lower). |
130 | 130 |
arcMap("capacity", capacity). |
131 | 131 |
arcMap("cost", cost). |
132 | 132 |
run(); |
133 | 133 |
} else if (maxflow) { |
134 | 134 |
Digraph digraph; |
135 | 135 |
Node s, t; |
136 | 136 |
DoubleArcMap capacity(digraph); |
137 |
|
|
137 |
readDimacsMax(is, digraph, capacity, s, t); |
|
138 | 138 |
DigraphWriter<Digraph>(digraph, os). |
139 | 139 |
arcMap("capacity", capacity). |
140 | 140 |
node("source", s). |
141 | 141 |
node("target", t). |
142 | 142 |
run(); |
143 | 143 |
} else if (shortestpath) { |
144 | 144 |
Digraph digraph; |
145 | 145 |
Node s; |
146 | 146 |
DoubleArcMap capacity(digraph); |
147 |
|
|
147 |
readDimacsSp(is, digraph, capacity, s); |
|
148 | 148 |
DigraphWriter<Digraph>(digraph, os). |
149 | 149 |
arcMap("capacity", capacity). |
150 | 150 |
node("source", s). |
151 | 151 |
run(); |
152 | 152 |
} else if (capacitated) { |
153 | 153 |
Digraph digraph; |
154 | 154 |
DoubleArcMap capacity(digraph); |
155 |
|
|
155 |
readDimacsMax(is, digraph, capacity); |
|
156 | 156 |
DigraphWriter<Digraph>(digraph, os). |
157 | 157 |
arcMap("capacity", capacity). |
158 | 158 |
run(); |
159 | 159 |
} else if (plain) { |
160 | 160 |
Digraph digraph; |
161 |
|
|
161 |
readDimacsMat(is, digraph); |
|
162 | 162 |
DigraphWriter<Digraph>(digraph, os).run(); |
163 | 163 |
} else { |
164 | 164 |
cerr << "Invalid type error" << endl; |
165 | 165 |
return -1; |
166 | 166 |
} |
167 | 167 |
return 0; |
168 | 168 |
} |
0 comments (0 inline)