tools/dimacs-to-lgf.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 27 Nov 2008 22:04:46 +0000
changeset 385 50d96f2166d7
child 386 9d1faab5e0f1
permissions -rw-r--r--
Port DIMACS tools from svn -r3516

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