COIN-OR::LEMON - Graph Library

source: lemon-0.x/tools/dim_to_lgf.cc @ 2552:5f711e4668f5

Last change on this file since 2552:5f711e4668f5 was 2491:b63ae56979ef, checked in by Balazs Dezso, 17 years ago

Documentation for lemon tools

File size: 5.4 KB
Line 
1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19///\ingroup tools
20///\file
21///\brief DIMACS to LGF converter.
22///
23/// This program converts various DIMACS formats to the LEMON Graph Format
24/// (LGF).
25///
26///\verbatim
27///Usage:
28///  ./tools/dim_to_lgf
29///     --mincostflow|-mcf|--maxflow|-mf|--shortestpath|-sp|--capacitated|-cap|--plain|-pl
30///     [--help|-h|-help] [--input|-i str] [--output|-o str] [--version|-v]
31///Where:
32///  --capacitated|-cap
33///     set the type of the graph to "capacitated" graph
34///  --help|-h|-help
35///     Print a short help message
36///  --input|-i str
37///     use FILE as input instead of standard input
38///  --maxflow|-mf
39///     set the type of the graph to "maxflow" graph
40///  --mincostflow|-mcf
41///     set the type of the graph to "mincostflow" graph
42///  --output|-o str
43///     use FILE as output instead of standard output
44///  --plain|-pl
45///     set the type of the graph to "plain" graph
46///  --shortestpath|-sp
47///     set the type of the graph to "shortestpath" graph
48///  --version|-v
49///     show version information
50///\endverbatim
51///
52
53#include <iostream>
54#include <fstream>
55#include <cstring>
56
57#include <lemon/smart_graph.h>
58#include <lemon/dimacs.h>
59#include <lemon/graph_writer.h>
60
61#include <lemon/arg_parser.h>
62
63using namespace std;
64using namespace lemon;
65
66
67int main(int argc, const char *argv[]) {
68  typedef SmartGraph Graph;
69
70  typedef Graph::Edge Edge;
71  typedef Graph::Node Node;
72  typedef Graph::EdgeIt EdgeIt;
73  typedef Graph::NodeIt NodeIt;
74  typedef Graph::EdgeMap<double> DoubleEdgeMap;
75  typedef Graph::NodeMap<double> DoubleNodeMap;
76
77  std::string inputName;
78  std::string outputName;
79  std::string typeName;
80
81  bool mincostflow;
82  bool maxflow;
83  bool shortestpath;
84  bool capacitated;
85  bool plain;
86
87  bool version;
88
89  ArgParser ap(argc, argv);
90  ap.refOption("-input",
91               "use FILE as input instead of standard input",
92               inputName).synonym("i", "-input")
93    .refOption("-output",
94               "use FILE as output instead of standard output",
95               outputName).synonym("o", "-output")
96    .refOption("-mincostflow",
97               "set the type of the graph to \"mincostflow\" graph",
98               mincostflow)
99    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
100    .refOption("-maxflow",
101               "set the type of the graph to \"maxflow\" graph",
102               maxflow)
103    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
104    .refOption("-shortestpath",
105               "set the type of the graph to \"shortestpath\" graph",
106               shortestpath)
107    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
108    .refOption("-capacitated",
109               "set the type of the graph to \"capacitated\" graph",
110               capacitated)
111    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
112    .refOption("-plain",
113               "set the type of the graph to \"plain\" graph",
114               plain)
115    .optionGroup("type", "-plain").synonym("pl", "-plain")
116    .onlyOneGroup("type")
117    .mandatoryGroup("type")
118    .refOption("-version", "show version information", version)
119    .synonym("v", "-version")
120    .run();
121
122  ifstream input;
123  if (!inputName.empty()) {
124    input.open(inputName.c_str());
125    if (!input) {
126      cerr << "File open error" << endl;
127      return -1;
128    }
129  }
130  istream& is = (inputName.empty() ? cin : input);
131
132  ofstream output;
133  if (!outputName.empty()) {
134    output.open(outputName.c_str());
135    if (!output) {
136      cerr << "File open error" << endl;
137      return -1;
138    }
139  }
140  ostream& os = (outputName.empty() ? cout : output);
141
142  if (mincostflow) {
143    Graph graph;
144    DoubleEdgeMap lower(graph), capacity(graph), cost(graph);
145    DoubleNodeMap supply(graph);
146    readDimacs(is, graph, lower, capacity, cost, supply);
147    GraphWriter<Graph>(os, graph).
148      writeNodeMap("supply", supply).
149      writeEdgeMap("lower", lower).
150      writeEdgeMap("capacity", capacity).
151      writeEdgeMap("cost", cost).
152      run();
153  } else if (maxflow) {
154    Graph graph;
155    Node s, t;
156    DoubleEdgeMap capacity(graph);
157    readDimacs(is, graph, capacity, s, t);
158    GraphWriter<Graph>(os, graph).
159      writeEdgeMap("capacity", capacity).
160      writeNode("source", s).
161      writeNode("target", t).
162      run();
163  } else if (shortestpath) {
164    Graph graph;
165    Node s;
166    DoubleEdgeMap capacity(graph);
167    readDimacs(is, graph, capacity, s);
168    GraphWriter<Graph>(os, graph).
169      writeEdgeMap("capacity", capacity).
170      writeNode("source", s).
171      run();
172  } else if (capacitated) {
173    Graph graph;
174    DoubleEdgeMap capacity(graph);
175    readDimacs(is, graph, capacity);
176    GraphWriter<Graph>(os, graph).
177      writeEdgeMap("capacity", capacity).
178      run();
179  } else if (plain) {
180    Graph graph;
181    readDimacs(is, graph);
182    GraphWriter<Graph>(os, graph).run();
183  } else {
184    cerr << "Invalid type error" << endl;
185    return -1;
186  }
187  return 0;
188}
Note: See TracBrowser for help on using the repository browser.