tools/dim_to_lgf.cc
author alpar
Thu, 25 Oct 2007 16:57:43 +0000
changeset 2503 15b3bf0141c7
parent 2417 113d381c9160
child 2553 bfced05fa852
permissions -rw-r--r--
Fix a typo that caused the failure of 'make dist'
alpar@2404
     1
/* -*- C++ -*-
alpar@2404
     2
 *
alpar@2404
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2404
     4
 *
alpar@2404
     5
 * Copyright (C) 2003-2007
alpar@2404
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2404
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2404
     8
 *
alpar@2404
     9
 * Permission to use, modify and distribute this software is granted
alpar@2404
    10
 * provided that this copyright notice appears in all copies. For
alpar@2404
    11
 * precise terms see the accompanying LICENSE file.
alpar@2404
    12
 *
alpar@2404
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2404
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2404
    15
 * purpose.
alpar@2404
    16
 *
alpar@2404
    17
 */
alpar@2404
    18
deba@2491
    19
///\ingroup tools
alpar@2404
    20
///\file
deba@2410
    21
///\brief DIMACS to LGF converter.
alpar@2404
    22
///
alpar@2404
    23
/// This program converts various DIMACS formats to the LEMON Graph Format
alpar@2404
    24
/// (LGF).
alpar@2404
    25
///
deba@2491
    26
///\verbatim
deba@2491
    27
///Usage:
deba@2491
    28
///  ./tools/dim_to_lgf
deba@2491
    29
///     --mincostflow|-mcf|--maxflow|-mf|--shortestpath|-sp|--capacitated|-cap|--plain|-pl
deba@2491
    30
///     [--help|-h|-help] [--input|-i str] [--output|-o str] [--version|-v]
deba@2491
    31
///Where:
deba@2491
    32
///  --capacitated|-cap
deba@2491
    33
///     set the type of the graph to "capacitated" graph
deba@2491
    34
///  --help|-h|-help
deba@2491
    35
///     Print a short help message
deba@2491
    36
///  --input|-i str
deba@2491
    37
///     use FILE as input instead of standard input
deba@2491
    38
///  --maxflow|-mf
deba@2491
    39
///     set the type of the graph to "maxflow" graph
deba@2491
    40
///  --mincostflow|-mcf
deba@2491
    41
///     set the type of the graph to "mincostflow" graph
deba@2491
    42
///  --output|-o str
deba@2491
    43
///     use FILE as output instead of standard output
deba@2491
    44
///  --plain|-pl
deba@2491
    45
///     set the type of the graph to "plain" graph
deba@2491
    46
///  --shortestpath|-sp
deba@2491
    47
///     set the type of the graph to "shortestpath" graph
deba@2491
    48
///  --version|-v
deba@2491
    49
///     show version information
deba@2491
    50
///\endverbatim
deba@2491
    51
///
alpar@2404
    52
alpar@2404
    53
#include <iostream>
alpar@2404
    54
#include <fstream>
alpar@2404
    55
#include <cstring>
alpar@2404
    56
alpar@2404
    57
#include <lemon/smart_graph.h>
alpar@2404
    58
#include <lemon/dimacs.h>
alpar@2404
    59
#include <lemon/graph_writer.h>
alpar@2404
    60
deba@2410
    61
#include <lemon/arg_parser.h>
deba@2410
    62
alpar@2404
    63
using namespace std;
alpar@2404
    64
using namespace lemon;
alpar@2404
    65
alpar@2404
    66
alpar@2404
    67
int main(int argc, const char *argv[]) {
alpar@2404
    68
  typedef SmartGraph Graph;
alpar@2404
    69
alpar@2404
    70
  typedef Graph::Edge Edge;
alpar@2404
    71
  typedef Graph::Node Node;
alpar@2404
    72
  typedef Graph::EdgeIt EdgeIt;
alpar@2404
    73
  typedef Graph::NodeIt NodeIt;
deba@2413
    74
  typedef Graph::EdgeMap<double> DoubleEdgeMap;
deba@2413
    75
  typedef Graph::NodeMap<double> DoubleNodeMap;
alpar@2404
    76
alpar@2404
    77
  std::string inputName;
alpar@2404
    78
  std::string outputName;
alpar@2404
    79
  std::string typeName;
alpar@2404
    80
deba@2410
    81
  bool mincostflow;
deba@2410
    82
  bool maxflow;
deba@2410
    83
  bool shortestpath;
deba@2410
    84
  bool capacitated;
deba@2410
    85
  bool plain;
alpar@2404
    86
deba@2410
    87
  bool version;
alpar@2404
    88
deba@2410
    89
  ArgParser ap(argc, argv);
deba@2410
    90
  ap.refOption("-input", 
deba@2410
    91
               "use FILE as input instead of standard input", 
deba@2410
    92
               inputName).synonym("i", "-input")
deba@2410
    93
    .refOption("-output", 
deba@2410
    94
               "use FILE as output instead of standard output", 
deba@2410
    95
               outputName).synonym("o", "-output")
deba@2410
    96
    .refOption("-mincostflow", 
deba@2410
    97
               "set the type of the graph to \"mincostflow\" graph", 
deba@2410
    98
               mincostflow)
deba@2410
    99
    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
deba@2410
   100
    .refOption("-maxflow", 
deba@2410
   101
               "set the type of the graph to \"maxflow\" graph", 
deba@2410
   102
               maxflow)
deba@2410
   103
    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
deba@2410
   104
    .refOption("-shortestpath", 
deba@2410
   105
               "set the type of the graph to \"shortestpath\" graph", 
deba@2410
   106
               shortestpath)
deba@2410
   107
    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
deba@2410
   108
    .refOption("-capacitated", 
deba@2410
   109
               "set the type of the graph to \"capacitated\" graph", 
deba@2410
   110
               capacitated)
deba@2410
   111
    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
deba@2410
   112
    .refOption("-plain", 
deba@2410
   113
               "set the type of the graph to \"plain\" graph", 
deba@2410
   114
               plain)
deba@2410
   115
    .optionGroup("type", "-plain").synonym("pl", "-plain")
deba@2410
   116
    .onlyOneGroup("type")
deba@2410
   117
    .mandatoryGroup("type")
deba@2410
   118
    .refOption("-version", "show version information", version)
deba@2410
   119
    .synonym("v", "-version")
deba@2410
   120
    .run();
alpar@2404
   121
alpar@2404
   122
  ifstream input;
alpar@2404
   123
  if (!inputName.empty()) {
alpar@2404
   124
    input.open(inputName.c_str());
alpar@2404
   125
    if (!input) {
alpar@2404
   126
      cerr << "File open error" << endl;
alpar@2404
   127
      return -1;
alpar@2404
   128
    }
alpar@2404
   129
  }
alpar@2404
   130
  istream& is = (inputName.empty() ? cin : input);
alpar@2404
   131
alpar@2404
   132
  ofstream output;
alpar@2404
   133
  if (!outputName.empty()) {
alpar@2404
   134
    output.open(outputName.c_str());
alpar@2404
   135
    if (!output) {
alpar@2404
   136
      cerr << "File open error" << endl;
alpar@2404
   137
      return -1;
alpar@2404
   138
    }
alpar@2404
   139
  }
alpar@2404
   140
  ostream& os = (outputName.empty() ? cout : output);
alpar@2404
   141
deba@2410
   142
  if (mincostflow) {
alpar@2404
   143
    Graph graph;
deba@2417
   144
    DoubleEdgeMap lower(graph), capacity(graph), cost(graph);
deba@2413
   145
    DoubleNodeMap supply(graph);
deba@2417
   146
    readDimacs(is, graph, lower, capacity, cost, supply);
alpar@2404
   147
    GraphWriter<Graph>(os, graph).
deba@2413
   148
      writeNodeMap("supply", supply).
deba@2413
   149
      writeEdgeMap("lower", lower).
alpar@2404
   150
      writeEdgeMap("capacity", capacity).
alpar@2404
   151
      writeEdgeMap("cost", cost).
alpar@2404
   152
      run();
deba@2410
   153
  } else if (maxflow) {
alpar@2404
   154
    Graph graph;
alpar@2404
   155
    Node s, t;
deba@2413
   156
    DoubleEdgeMap capacity(graph);
alpar@2404
   157
    readDimacs(is, graph, capacity, s, t);
alpar@2404
   158
    GraphWriter<Graph>(os, graph).
alpar@2404
   159
      writeEdgeMap("capacity", capacity).
alpar@2404
   160
      writeNode("source", s).
alpar@2404
   161
      writeNode("target", t).
alpar@2404
   162
      run();
deba@2410
   163
  } else if (shortestpath) {
alpar@2404
   164
    Graph graph;
alpar@2404
   165
    Node s;
deba@2413
   166
    DoubleEdgeMap capacity(graph);
alpar@2404
   167
    readDimacs(is, graph, capacity, s);
alpar@2404
   168
    GraphWriter<Graph>(os, graph).
alpar@2404
   169
      writeEdgeMap("capacity", capacity).
alpar@2404
   170
      writeNode("source", s).
alpar@2404
   171
      run();
deba@2410
   172
  } else if (capacitated) {
alpar@2404
   173
    Graph graph;
deba@2413
   174
    DoubleEdgeMap capacity(graph);
alpar@2404
   175
    readDimacs(is, graph, capacity);
alpar@2404
   176
    GraphWriter<Graph>(os, graph).
alpar@2404
   177
      writeEdgeMap("capacity", capacity).
alpar@2404
   178
      run();
deba@2410
   179
  } else if (plain) {
alpar@2404
   180
    Graph graph;
alpar@2404
   181
    readDimacs(is, graph);
alpar@2404
   182
    GraphWriter<Graph>(os, graph).run();
alpar@2404
   183
  } else {
alpar@2404
   184
    cerr << "Invalid type error" << endl;
alpar@2404
   185
    return -1;
alpar@2404
   186
  }
alpar@2404
   187
  return 0;
alpar@2404
   188
}