tools/dim_to_lgf.cc
changeset 2404 ec474604075e
child 2410 fe46b61da4e3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/dim_to_lgf.cc	Mon Mar 12 13:57:53 2007 +0000
     1.3 @@ -0,0 +1,207 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2007
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\ingroup demos
    1.23 +///\file
    1.24 +///\brief DIMACS to LGF converter (demo).
    1.25 +///
    1.26 +/// This program converts various DIMACS formats to the LEMON Graph Format
    1.27 +/// (LGF).
    1.28 +///
    1.29 +/// \include dim_to_lgf.cc
    1.30 +
    1.31 +#include <iostream>
    1.32 +#include <fstream>
    1.33 +#include <cstring>
    1.34 +
    1.35 +#include <lemon/smart_graph.h>
    1.36 +#include <lemon/dimacs.h>
    1.37 +#include <lemon/graph_writer.h>
    1.38 +
    1.39 +using namespace std;
    1.40 +using namespace lemon;
    1.41 +
    1.42 +const char* versionString =
    1.43 +"dim_to_lgf - part of lemon library\n";
    1.44 +
    1.45 +const char* helpString =
    1.46 +"DIMACS to LGF converter\n"
    1.47 +"Usage: dim_to_lgf [OPTIONS]\n"
    1.48 +"\n"
    1.49 +"Examples:\n"
    1.50 +"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    1.51 +"\n"
    1.52 +"Options:\n"
    1.53 +"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    1.54 +"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    1.55 +"  -t TYPE, --type TYPE     set up the type of the graph\n"
    1.56 +"                             Possible types:\n"
    1.57 +"                               mincostflow\n"
    1.58 +"                               maxflow (default)\n"
    1.59 +"                               shortestpath\n"
    1.60 +"                               capacitated\n"
    1.61 +"                               plain\n"
    1.62 +"  -v, --version            shows the version of the converter\n"
    1.63 +"  -h, --help               shows the help of the converter\n";
    1.64 +
    1.65 +
    1.66 +int main(int argc, const char *argv[]) {
    1.67 +  typedef SmartGraph Graph;
    1.68 +
    1.69 +  typedef Graph::Edge Edge;
    1.70 +  typedef Graph::Node Node;
    1.71 +  typedef Graph::EdgeIt EdgeIt;
    1.72 +  typedef Graph::NodeIt NodeIt;
    1.73 +  typedef Graph::EdgeMap<double> DoubleMap;
    1.74 +
    1.75 +  std::string inputName;
    1.76 +  std::string outputName;
    1.77 +  std::string typeName;
    1.78 +
    1.79 +  bool help = false;
    1.80 +  bool version = false;
    1.81 +
    1.82 +  for (int arg = 1; arg < argc; ++arg) {
    1.83 +    if (strcmp(argv[arg], "--type") == 0 || 
    1.84 +	strcmp(argv[arg], "-t") == 0) {
    1.85 +      if (!typeName.empty()) {
    1.86 +	cerr << "Multiple type description" << endl;
    1.87 +	return -1;
    1.88 +      }
    1.89 +      if (arg + 1 == argc) {
    1.90 +	cerr << "Parameter without value" << endl;
    1.91 +	return -1;
    1.92 +      }
    1.93 +      typeName = argv[++arg];
    1.94 +    }
    1.95 +    else if (strcmp(argv[arg], "--input") == 0 || 
    1.96 +	     strcmp(argv[arg], "-i") == 0) {
    1.97 +      if (!inputName.empty()) {
    1.98 +	cerr << "Multiple input description" << endl;
    1.99 +	return -1;
   1.100 +      }
   1.101 +      if (arg + 1 == argc) {
   1.102 +	cerr << "Parameter without value" << endl;
   1.103 +	return -1;
   1.104 +      }
   1.105 +      inputName = argv[++arg];
   1.106 +    }
   1.107 +    else if (strcmp(argv[arg], "--output") == 0 || 
   1.108 +	     strcmp(argv[arg], "-o") == 0) {
   1.109 +      if (!outputName.empty()) {
   1.110 +	cerr << "Multiple input description" << endl;
   1.111 +	return -1;
   1.112 +      }
   1.113 +      if (arg + 1 == argc) {
   1.114 +	cerr << "Parameter without value" << endl;
   1.115 +	return -1;
   1.116 +      }
   1.117 +      outputName = argv[++arg];
   1.118 +    } else if (strcmp(argv[arg], "--help") == 0 ||
   1.119 +	       strcmp(argv[arg], "-h") == 0) {
   1.120 +      help = true;
   1.121 +    } else if (strcmp(argv[arg], "--version") == 0 ||
   1.122 +	       strcmp(argv[arg], "-v") == 0) {
   1.123 +      version = true;
   1.124 +    } else {
   1.125 +      cerr << "Invalid option: " << argv[arg] << endl;
   1.126 +      return -1;
   1.127 +    }
   1.128 +  }
   1.129 +
   1.130 +  if (version) {
   1.131 +    cout << versionString;
   1.132 +  }
   1.133 +  if (help) {
   1.134 +    cout << helpString;
   1.135 +  }
   1.136 +  if (help || version) {
   1.137 +    return 0;
   1.138 +  }
   1.139 +
   1.140 +  ifstream input;
   1.141 +  if (!inputName.empty()) {
   1.142 +    input.open(inputName.c_str());
   1.143 +    if (!input) {
   1.144 +      cerr << "File open error" << endl;
   1.145 +      return -1;
   1.146 +    }
   1.147 +  }
   1.148 +  istream& is = (inputName.empty() ? cin : input);
   1.149 +
   1.150 +  ofstream output;
   1.151 +  if (!outputName.empty()) {
   1.152 +    output.open(outputName.c_str());
   1.153 +    if (!output) {
   1.154 +      cerr << "File open error" << endl;
   1.155 +      return -1;
   1.156 +    }
   1.157 +  }
   1.158 +  ostream& os = (outputName.empty() ? cout : output);
   1.159 +
   1.160 +  if (typeName.empty()) {
   1.161 +    typeName = "maxflow";
   1.162 +  }
   1.163 +
   1.164 +  if (typeName == "mincostflow") {
   1.165 +    Graph graph;
   1.166 +    Node s, t;
   1.167 +    DoubleMap cost(graph), capacity(graph);
   1.168 +    readDimacs(is, graph, capacity, s, t, cost);
   1.169 +    GraphWriter<Graph>(os, graph).
   1.170 +      writeEdgeMap("capacity", capacity).
   1.171 +      writeNode("source", s).
   1.172 +      writeNode("target", t).
   1.173 +      writeEdgeMap("cost", cost).
   1.174 +      run();
   1.175 +  } else if (typeName == "maxflow") {
   1.176 +    Graph graph;
   1.177 +    Node s, t;
   1.178 +    DoubleMap capacity(graph);
   1.179 +    readDimacs(is, graph, capacity, s, t);
   1.180 +    GraphWriter<Graph>(os, graph).
   1.181 +      writeEdgeMap("capacity", capacity).
   1.182 +      writeNode("source", s).
   1.183 +      writeNode("target", t).
   1.184 +      run();
   1.185 +  } else if (typeName == "shortestpath") {
   1.186 +    Graph graph;
   1.187 +    Node s;
   1.188 +    DoubleMap capacity(graph);
   1.189 +    readDimacs(is, graph, capacity, s);
   1.190 +    GraphWriter<Graph>(os, graph).
   1.191 +      writeEdgeMap("capacity", capacity).
   1.192 +      writeNode("source", s).
   1.193 +      run();
   1.194 +  } else if (typeName == "capacitated") {
   1.195 +    Graph graph;
   1.196 +    DoubleMap capacity(graph);
   1.197 +    readDimacs(is, graph, capacity);
   1.198 +    GraphWriter<Graph>(os, graph).
   1.199 +      writeEdgeMap("capacity", capacity).
   1.200 +      run();
   1.201 +  } else if (typeName == "plain") {
   1.202 +    Graph graph;
   1.203 +    readDimacs(is, graph);
   1.204 +    GraphWriter<Graph>(os, graph).run();
   1.205 +  } else {
   1.206 +    cerr << "Invalid type error" << endl;
   1.207 +    return -1;
   1.208 +  }
   1.209 +  return 0;
   1.210 +}