tools/dimacs-to-lgf.cc
changeset 708 994c7df296c9
parent 552 6e0525ec5355
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/dimacs-to-lgf.cc	Thu Dec 10 17:05:35 2009 +0100
     1.3 @@ -0,0 +1,148 @@
     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-2009
     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 +/// \code
    1.31 +///   dimacs-to-lgf --help
    1.32 +/// \endcode
    1.33 +/// for more info on the usage.
    1.34 +
    1.35 +#include <iostream>
    1.36 +#include <fstream>
    1.37 +#include <cstring>
    1.38 +
    1.39 +#include <lemon/smart_graph.h>
    1.40 +#include <lemon/dimacs.h>
    1.41 +#include <lemon/lgf_writer.h>
    1.42 +
    1.43 +#include <lemon/arg_parser.h>
    1.44 +#include <lemon/error.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 +
    1.63 +  ArgParser ap(argc, argv);
    1.64 +  ap.other("[INFILE [OUTFILE]]",
    1.65 +           "If either the INFILE or OUTFILE file is missing the standard\n"
    1.66 +           "     input/output will be used instead.")
    1.67 +    .run();
    1.68 +
    1.69 +  ifstream input;
    1.70 +  ofstream output;
    1.71 +
    1.72 +  switch(ap.files().size())
    1.73 +    {
    1.74 +    case 2:
    1.75 +      output.open(ap.files()[1].c_str());
    1.76 +      if (!output) {
    1.77 +        throw IoError("Cannot open the file for writing", ap.files()[1]);
    1.78 +      }
    1.79 +    case 1:
    1.80 +      input.open(ap.files()[0].c_str());
    1.81 +      if (!input) {
    1.82 +        throw IoError("File cannot be found", ap.files()[0]);
    1.83 +      }
    1.84 +    case 0:
    1.85 +      break;
    1.86 +    default:
    1.87 +      cerr << ap.commandName() << ": too many arguments\n";
    1.88 +      return 1;
    1.89 +  }
    1.90 +  istream& is = (ap.files().size()<1 ? cin : input);
    1.91 +  ostream& os = (ap.files().size()<2 ? cout : output);
    1.92 +
    1.93 +  DimacsDescriptor desc = dimacsType(is);
    1.94 +  switch(desc.type)
    1.95 +    {
    1.96 +    case DimacsDescriptor::MIN:
    1.97 +      {
    1.98 +        Digraph digraph;
    1.99 +        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
   1.100 +        DoubleNodeMap supply(digraph);
   1.101 +        readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
   1.102 +        DigraphWriter<Digraph>(digraph, os).
   1.103 +          nodeMap("supply", supply).
   1.104 +          arcMap("lower", lower).
   1.105 +          arcMap("capacity", capacity).
   1.106 +          arcMap("cost", cost).
   1.107 +          attribute("problem","min").
   1.108 +          run();
   1.109 +      }
   1.110 +      break;
   1.111 +    case DimacsDescriptor::MAX:
   1.112 +      {
   1.113 +        Digraph digraph;
   1.114 +        Node s, t;
   1.115 +        DoubleArcMap capacity(digraph);
   1.116 +        readDimacsMax(is, digraph, capacity, s, t, 0, desc);
   1.117 +        DigraphWriter<Digraph>(digraph, os).
   1.118 +          arcMap("capacity", capacity).
   1.119 +          node("source", s).
   1.120 +          node("target", t).
   1.121 +          attribute("problem","max").
   1.122 +          run();
   1.123 +      }
   1.124 +      break;
   1.125 +    case DimacsDescriptor::SP:
   1.126 +      {
   1.127 +        Digraph digraph;
   1.128 +        Node s;
   1.129 +        DoubleArcMap capacity(digraph);
   1.130 +        readDimacsSp(is, digraph, capacity, s, desc);
   1.131 +        DigraphWriter<Digraph>(digraph, os).
   1.132 +          arcMap("capacity", capacity).
   1.133 +          node("source", s).
   1.134 +          attribute("problem","sp").
   1.135 +          run();
   1.136 +      }
   1.137 +      break;
   1.138 +    case DimacsDescriptor::MAT:
   1.139 +      {
   1.140 +        Digraph digraph;
   1.141 +        readDimacsMat(is, digraph,desc);
   1.142 +        DigraphWriter<Digraph>(digraph, os).
   1.143 +          attribute("problem","mat").
   1.144 +          run();
   1.145 +      }
   1.146 +      break;
   1.147 +    default:
   1.148 +      break;
   1.149 +    }
   1.150 +  return 0;
   1.151 +}