tools/dim_to_lgf.cc
author deba
Wed, 14 Mar 2007 18:01:04 +0000
changeset 2410 fe46b61da4e3
parent 2404 ec474604075e
child 2413 21eb3ccdc3df
permissions -rw-r--r--
dim_to_lgf
- use the argparser class
arg_parser improvments
- usage of assert in cc
- const char** argv
error
- handling the string parameter
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
alpar@2404
    19
///\ingroup demos
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
///
alpar@2404
    26
/// \include dim_to_lgf.cc
alpar@2404
    27
alpar@2404
    28
#include <iostream>
alpar@2404
    29
#include <fstream>
alpar@2404
    30
#include <cstring>
alpar@2404
    31
alpar@2404
    32
#include <lemon/smart_graph.h>
alpar@2404
    33
#include <lemon/dimacs.h>
alpar@2404
    34
#include <lemon/graph_writer.h>
alpar@2404
    35
deba@2410
    36
#include <lemon/arg_parser.h>
deba@2410
    37
alpar@2404
    38
using namespace std;
alpar@2404
    39
using namespace lemon;
alpar@2404
    40
alpar@2404
    41
alpar@2404
    42
int main(int argc, const char *argv[]) {
alpar@2404
    43
  typedef SmartGraph Graph;
alpar@2404
    44
alpar@2404
    45
  typedef Graph::Edge Edge;
alpar@2404
    46
  typedef Graph::Node Node;
alpar@2404
    47
  typedef Graph::EdgeIt EdgeIt;
alpar@2404
    48
  typedef Graph::NodeIt NodeIt;
alpar@2404
    49
  typedef Graph::EdgeMap<double> DoubleMap;
alpar@2404
    50
alpar@2404
    51
  std::string inputName;
alpar@2404
    52
  std::string outputName;
alpar@2404
    53
  std::string typeName;
alpar@2404
    54
deba@2410
    55
  bool mincostflow;
deba@2410
    56
  bool maxflow;
deba@2410
    57
  bool shortestpath;
deba@2410
    58
  bool capacitated;
deba@2410
    59
  bool plain;
alpar@2404
    60
deba@2410
    61
  bool version;
alpar@2404
    62
deba@2410
    63
  ArgParser ap(argc, argv);
deba@2410
    64
  ap.refOption("-input", 
deba@2410
    65
               "use FILE as input instead of standard input", 
deba@2410
    66
               inputName).synonym("i", "-input")
deba@2410
    67
    .refOption("-output", 
deba@2410
    68
               "use FILE as output instead of standard output", 
deba@2410
    69
               outputName).synonym("o", "-output")
deba@2410
    70
    .refOption("-mincostflow", 
deba@2410
    71
               "set the type of the graph to \"mincostflow\" graph", 
deba@2410
    72
               mincostflow)
deba@2410
    73
    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
deba@2410
    74
    .refOption("-maxflow", 
deba@2410
    75
               "set the type of the graph to \"maxflow\" graph", 
deba@2410
    76
               maxflow)
deba@2410
    77
    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
deba@2410
    78
    .refOption("-shortestpath", 
deba@2410
    79
               "set the type of the graph to \"shortestpath\" graph", 
deba@2410
    80
               shortestpath)
deba@2410
    81
    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
deba@2410
    82
    .refOption("-capacitated", 
deba@2410
    83
               "set the type of the graph to \"capacitated\" graph", 
deba@2410
    84
               capacitated)
deba@2410
    85
    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
deba@2410
    86
    .refOption("-plain", 
deba@2410
    87
               "set the type of the graph to \"plain\" graph", 
deba@2410
    88
               plain)
deba@2410
    89
    .optionGroup("type", "-plain").synonym("pl", "-plain")
deba@2410
    90
    .onlyOneGroup("type")
deba@2410
    91
    .mandatoryGroup("type")
deba@2410
    92
    .refOption("-version", "show version information", version)
deba@2410
    93
    .synonym("v", "-version")
deba@2410
    94
    .run();
alpar@2404
    95
alpar@2404
    96
  ifstream input;
alpar@2404
    97
  if (!inputName.empty()) {
alpar@2404
    98
    input.open(inputName.c_str());
alpar@2404
    99
    if (!input) {
alpar@2404
   100
      cerr << "File open error" << endl;
alpar@2404
   101
      return -1;
alpar@2404
   102
    }
alpar@2404
   103
  }
alpar@2404
   104
  istream& is = (inputName.empty() ? cin : input);
alpar@2404
   105
alpar@2404
   106
  ofstream output;
alpar@2404
   107
  if (!outputName.empty()) {
alpar@2404
   108
    output.open(outputName.c_str());
alpar@2404
   109
    if (!output) {
alpar@2404
   110
      cerr << "File open error" << endl;
alpar@2404
   111
      return -1;
alpar@2404
   112
    }
alpar@2404
   113
  }
alpar@2404
   114
  ostream& os = (outputName.empty() ? cout : output);
alpar@2404
   115
deba@2410
   116
  if (mincostflow) {
alpar@2404
   117
    Graph graph;
alpar@2404
   118
    Node s, t;
alpar@2404
   119
    DoubleMap cost(graph), capacity(graph);
alpar@2404
   120
    readDimacs(is, graph, capacity, s, t, cost);
alpar@2404
   121
    GraphWriter<Graph>(os, graph).
alpar@2404
   122
      writeEdgeMap("capacity", capacity).
alpar@2404
   123
      writeNode("source", s).
alpar@2404
   124
      writeNode("target", t).
alpar@2404
   125
      writeEdgeMap("cost", cost).
alpar@2404
   126
      run();
deba@2410
   127
  } else if (maxflow) {
alpar@2404
   128
    Graph graph;
alpar@2404
   129
    Node s, t;
alpar@2404
   130
    DoubleMap capacity(graph);
alpar@2404
   131
    readDimacs(is, graph, capacity, s, t);
alpar@2404
   132
    GraphWriter<Graph>(os, graph).
alpar@2404
   133
      writeEdgeMap("capacity", capacity).
alpar@2404
   134
      writeNode("source", s).
alpar@2404
   135
      writeNode("target", t).
alpar@2404
   136
      run();
deba@2410
   137
  } else if (shortestpath) {
alpar@2404
   138
    Graph graph;
alpar@2404
   139
    Node s;
alpar@2404
   140
    DoubleMap capacity(graph);
alpar@2404
   141
    readDimacs(is, graph, capacity, s);
alpar@2404
   142
    GraphWriter<Graph>(os, graph).
alpar@2404
   143
      writeEdgeMap("capacity", capacity).
alpar@2404
   144
      writeNode("source", s).
alpar@2404
   145
      run();
deba@2410
   146
  } else if (capacitated) {
alpar@2404
   147
    Graph graph;
alpar@2404
   148
    DoubleMap capacity(graph);
alpar@2404
   149
    readDimacs(is, graph, capacity);
alpar@2404
   150
    GraphWriter<Graph>(os, graph).
alpar@2404
   151
      writeEdgeMap("capacity", capacity).
alpar@2404
   152
      run();
deba@2410
   153
  } else if (plain) {
alpar@2404
   154
    Graph graph;
alpar@2404
   155
    readDimacs(is, graph);
alpar@2404
   156
    GraphWriter<Graph>(os, graph).run();
alpar@2404
   157
  } else {
alpar@2404
   158
    cerr << "Invalid type error" << endl;
alpar@2404
   159
    return -1;
alpar@2404
   160
  }
alpar@2404
   161
  return 0;
alpar@2404
   162
}