tools/dimacs-to-lgf.cc
changeset 385 50d96f2166d7
child 386 9d1faab5e0f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/dimacs-to-lgf.cc	Thu Nov 27 22:04:46 2008 +0000
     1.3 @@ -0,0 +1,168 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     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 tools
    1.23 +///\file
    1.24 +///\brief DIMACS to LGF converter.
    1.25 +///
    1.26 +/// This program converts various DIMACS formats to the LEMON Digraph Format
    1.27 +/// (LGF).
    1.28 +///
    1.29 +/// See
    1.30 +/// \verbatim
    1.31 +///  dimacs-to-lgf --help
    1.32 +/// \endverbatim
    1.33 +/// for more info on usage.
    1.34 +///
    1.35 +
    1.36 +#include <iostream>
    1.37 +#include <fstream>
    1.38 +#include <cstring>
    1.39 +
    1.40 +#include <lemon/smart_graph.h>
    1.41 +#include <lemon/dimacs.h>
    1.42 +#include <lemon/lgf_writer.h>
    1.43 +
    1.44 +#include <lemon/arg_parser.h>
    1.45 +
    1.46 +using namespace std;
    1.47 +using namespace lemon;
    1.48 +
    1.49 +
    1.50 +int main(int argc, const char *argv[]) {
    1.51 +  typedef SmartDigraph Digraph;
    1.52 +
    1.53 +  typedef Digraph::Arc Arc;
    1.54 +  typedef Digraph::Node Node;
    1.55 +  typedef Digraph::ArcIt ArcIt;
    1.56 +  typedef Digraph::NodeIt NodeIt;
    1.57 +  typedef Digraph::ArcMap<double> DoubleArcMap;
    1.58 +  typedef Digraph::NodeMap<double> DoubleNodeMap;
    1.59 +
    1.60 +  std::string inputName;
    1.61 +  std::string outputName;
    1.62 +  std::string typeName;
    1.63 +
    1.64 +  bool mincostflow;
    1.65 +  bool maxflow;
    1.66 +  bool shortestpath;
    1.67 +  bool capacitated;
    1.68 +  bool plain;
    1.69 +
    1.70 +  bool version;
    1.71 +
    1.72 +  ArgParser ap(argc, argv);
    1.73 +  ap.refOption("-input",
    1.74 +               "use FILE as input instead of standard input",
    1.75 +               inputName).synonym("i", "-input")
    1.76 +    .refOption("-output",
    1.77 +               "use FILE as output instead of standard output",
    1.78 +               outputName).synonym("o", "-output")
    1.79 +    .refOption("-mincostflow",
    1.80 +               "set the type of the digraph to \"mincostflow\" digraph",
    1.81 +               mincostflow)
    1.82 +    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
    1.83 +    .refOption("-maxflow",
    1.84 +               "set the type of the digraph to \"maxflow\" digraph",
    1.85 +               maxflow)
    1.86 +    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
    1.87 +    .refOption("-shortestpath",
    1.88 +               "set the type of the digraph to \"shortestpath\" digraph",
    1.89 +               shortestpath)
    1.90 +    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
    1.91 +    .refOption("-capacitated",
    1.92 +               "set the type of the digraph to \"capacitated\" digraph",
    1.93 +               capacitated)
    1.94 +    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
    1.95 +    .refOption("-plain",
    1.96 +               "set the type of the digraph to \"plain\" digraph",
    1.97 +               plain)
    1.98 +    .optionGroup("type", "-plain").synonym("pl", "-plain")
    1.99 +    .onlyOneGroup("type")
   1.100 +    .mandatoryGroup("type")
   1.101 +    .refOption("-version", "show version information", version)
   1.102 +    .synonym("v", "-version")
   1.103 +    .run();
   1.104 +
   1.105 +  ifstream input;
   1.106 +  if (!inputName.empty()) {
   1.107 +    input.open(inputName.c_str());
   1.108 +    if (!input) {
   1.109 +      cerr << "File open error" << endl;
   1.110 +      return -1;
   1.111 +    }
   1.112 +  }
   1.113 +  istream& is = (inputName.empty() ? cin : input);
   1.114 +
   1.115 +  ofstream output;
   1.116 +  if (!outputName.empty()) {
   1.117 +    output.open(outputName.c_str());
   1.118 +    if (!output) {
   1.119 +      cerr << "File open error" << endl;
   1.120 +      return -1;
   1.121 +    }
   1.122 +  }
   1.123 +  ostream& os = (outputName.empty() ? cout : output);
   1.124 +
   1.125 +  if (mincostflow) {
   1.126 +    Digraph digraph;
   1.127 +    DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
   1.128 +    DoubleNodeMap supply(digraph);
   1.129 +    readDimacs(is, digraph, lower, capacity, cost, supply);
   1.130 +    DigraphWriter<Digraph>(digraph, os).
   1.131 +      nodeMap("supply", supply).
   1.132 +      arcMap("lower", lower).
   1.133 +      arcMap("capacity", capacity).
   1.134 +      arcMap("cost", cost).
   1.135 +      run();
   1.136 +  } else if (maxflow) {
   1.137 +    Digraph digraph;
   1.138 +    Node s, t;
   1.139 +    DoubleArcMap capacity(digraph);
   1.140 +    readDimacs(is, digraph, capacity, s, t);
   1.141 +    DigraphWriter<Digraph>(digraph, os).
   1.142 +      arcMap("capacity", capacity).
   1.143 +      node("source", s).
   1.144 +      node("target", t).
   1.145 +      run();
   1.146 +  } else if (shortestpath) {
   1.147 +    Digraph digraph;
   1.148 +    Node s;
   1.149 +    DoubleArcMap capacity(digraph);
   1.150 +    readDimacs(is, digraph, capacity, s);
   1.151 +    DigraphWriter<Digraph>(digraph, os).
   1.152 +      arcMap("capacity", capacity).
   1.153 +      node("source", s).
   1.154 +      run();
   1.155 +  } else if (capacitated) {
   1.156 +    Digraph digraph;
   1.157 +    DoubleArcMap capacity(digraph);
   1.158 +    readDimacs(is, digraph, capacity);
   1.159 +    DigraphWriter<Digraph>(digraph, os).
   1.160 +      arcMap("capacity", capacity).
   1.161 +      run();
   1.162 +  } else if (plain) {
   1.163 +    Digraph digraph;
   1.164 +    readDimacs(is, digraph);
   1.165 +    DigraphWriter<Digraph>(digraph, os).run();
   1.166 +  } else {
   1.167 +    cerr << "Invalid type error" << endl;
   1.168 +    return -1;
   1.169 +  }
   1.170 +  return 0;
   1.171 +}