COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/dim_to_lgf.cc @ 2390:8450951a8e2d

Last change on this file since 2390:8450951a8e2d was 1956:a055123339d5, checked in by Alpar Juttner, 19 years ago

Unified copyright notices

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-2006
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 demos
20///\file
21///\brief DIMACS to LGF converter (demo).
22///
23/// This program converts various DIMACS formats to the LEMON Graph Format
24/// (LGF).
25///
26/// \include dim_to_lgf.cc
27
28#include <iostream>
29#include <fstream>
30#include <cstring>
31
32#include <lemon/smart_graph.h>
33#include <lemon/dimacs.h>
34#include <lemon/graph_writer.h>
35
36using namespace std;
37using namespace lemon;
38
39const char* versionString =
40"dim_to_lgf - part of lemon library\n";
41
42const char* helpString =
43"DIMACS to LGF converter\n"
44"Usage: dim_to_lgf [OPTIONS]\n"
45"\n"
46"Examples:\n"
47"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
48"\n"
49"Options:\n"
50"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
51"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
52"  -t TYPE, --type TYPE     set up the type of the graph\n"
53"                             Possible types:\n"
54"                               mincostflow\n"
55"                               maxflow (default)\n"
56"                               shortestpath\n"
57"                               capacitated\n"
58"                               plain\n"
59"  -v, --version            shows the version of the converter\n"
60"  -h, --help               shows the help of the converter\n";
61
62
63int main(int argc, const char *argv[]) {
64  typedef SmartGraph Graph;
65
66  typedef Graph::Edge Edge;
67  typedef Graph::Node Node;
68  typedef Graph::EdgeIt EdgeIt;
69  typedef Graph::NodeIt NodeIt;
70  typedef Graph::EdgeMap<double> DoubleMap;
71
72  std::string inputName;
73  std::string outputName;
74  std::string typeName;
75
76  bool help = false;
77  bool version = false;
78
79  for (int arg = 1; arg < argc; ++arg) {
80    if (strcmp(argv[arg], "--type") == 0 ||
81        strcmp(argv[arg], "-t") == 0) {
82      if (!typeName.empty()) {
83        cerr << "Multiple type description" << endl;
84        return -1;
85      }
86      if (arg + 1 == argc) {
87        cerr << "Parameter without value" << endl;
88        return -1;
89      }
90      typeName = argv[++arg];
91    }
92    else if (strcmp(argv[arg], "--input") == 0 ||
93             strcmp(argv[arg], "-i") == 0) {
94      if (!inputName.empty()) {
95        cerr << "Multiple input description" << endl;
96        return -1;
97      }
98      if (arg + 1 == argc) {
99        cerr << "Parameter without value" << endl;
100        return -1;
101      }
102      inputName = argv[++arg];
103    }
104    else if (strcmp(argv[arg], "--output") == 0 ||
105             strcmp(argv[arg], "-o") == 0) {
106      if (!outputName.empty()) {
107        cerr << "Multiple input description" << endl;
108        return -1;
109      }
110      if (arg + 1 == argc) {
111        cerr << "Parameter without value" << endl;
112        return -1;
113      }
114      outputName = argv[++arg];
115    } else if (strcmp(argv[arg], "--help") == 0 ||
116               strcmp(argv[arg], "-h") == 0) {
117      help = true;
118    } else if (strcmp(argv[arg], "--version") == 0 ||
119               strcmp(argv[arg], "-v") == 0) {
120      version = true;
121    } else {
122      cerr << "Invalid option: " << argv[arg] << endl;
123      return -1;
124    }
125  }
126
127  if (version) {
128    cout << versionString;
129  }
130  if (help) {
131    cout << helpString;
132  }
133  if (help || version) {
134    return 0;
135  }
136
137  ifstream input;
138  if (!inputName.empty()) {
139    input.open(inputName.c_str());
140    if (!input) {
141      cerr << "File open error" << endl;
142      return -1;
143    }
144  }
145  istream& is = (inputName.empty() ? cin : input);
146
147  ofstream output;
148  if (!outputName.empty()) {
149    output.open(outputName.c_str());
150    if (!output) {
151      cerr << "File open error" << endl;
152      return -1;
153    }
154  }
155  ostream& os = (outputName.empty() ? cout : output);
156
157  if (typeName.empty()) {
158    typeName = "maxflow";
159  }
160
161  if (typeName == "mincostflow") {
162    Graph graph;
163    Node s, t;
164    DoubleMap cost(graph), capacity(graph);
165    readDimacs(is, graph, capacity, s, t, cost);
166    GraphWriter<Graph>(os, graph).
167      writeEdgeMap("capacity", capacity).
168      writeNode("source", s).
169      writeNode("target", t).
170      writeEdgeMap("cost", cost).
171      run();
172  } else if (typeName == "maxflow") {
173    Graph graph;
174    Node s, t;
175    DoubleMap capacity(graph);
176    readDimacs(is, graph, capacity, s, t);
177    GraphWriter<Graph>(os, graph).
178      writeEdgeMap("capacity", capacity).
179      writeNode("source", s).
180      writeNode("target", t).
181      run();
182  } else if (typeName == "shortestpath") {
183    Graph graph;
184    Node s;
185    DoubleMap capacity(graph);
186    readDimacs(is, graph, capacity, s);
187    GraphWriter<Graph>(os, graph).
188      writeEdgeMap("capacity", capacity).
189      writeNode("source", s).
190      run();
191  } else if (typeName == "capacitated") {
192    Graph graph;
193    DoubleMap capacity(graph);
194    readDimacs(is, graph, capacity);
195    GraphWriter<Graph>(os, graph).
196      writeEdgeMap("capacity", capacity).
197      run();
198  } else if (typeName == "plain") {
199    Graph graph;
200    readDimacs(is, graph);
201    GraphWriter<Graph>(os, graph).run();
202  } else {
203    cerr << "Invalid type error" << endl;
204    return -1;
205  }
206  return 0;
207}
Note: See TracBrowser for help on using the repository browser.