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