demo/dim_to_lgf.cc
author klao
Wed, 16 Nov 2005 04:22:49 +0000
changeset 1797 91b8b9cea2f7
parent 1744 51d5d41e15b1
child 1875 98698b69a902
permissions -rw-r--r--
lp_test.cc:

* bugfix in cplex part: check compiling and running on two different instances
     1 /* -*- C++ -*-
     2  * demo/dim_to_lgf.cc - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 ///\ingroup demos
    18 ///\file
    19 ///\brief DIMACS to LGF converter (demo).
    20 ///
    21 /// This program converts various DIMACS formats to the LEMON Graph Format
    22 /// (LGF).
    23 ///
    24 /// \include dim_to_lgf.cc
    25 
    26 #include <iostream>
    27 #include <fstream>
    28 #include <cstring>
    29 
    30 #include <lemon/smart_graph.h>
    31 #include <lemon/dimacs.h>
    32 #include <lemon/graph_writer.h>
    33 
    34 using namespace std;
    35 using namespace lemon;
    36 
    37 const char* versionString =
    38 "dim_to_lgf - part of lemon library\n";
    39 
    40 const char* helpString =
    41 "DIMACS to LGF converter\n"
    42 "Usage: dim_to_lgf [OPTIONS]\n"
    43 "\n"
    44 "Examples:\n"
    45 "  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    46 "\n"
    47 "Options:\n"
    48 "  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    49 "  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    50 "  -t TYPE, --type TYPE     set up the type of the graph\n"
    51 "                             Possible types:\n"
    52 "                               mincostflow\n"
    53 "                               maxflow (default)\n"
    54 "                               shortestpath\n"
    55 "                               capacitated\n"
    56 "                               plain\n"
    57 "  -v, --version            shows the version of the converter\n"
    58 "  -h, --help               shows the help of the converter\n";
    59 
    60 
    61 int main(int argc, const char *argv[]) {
    62   typedef SmartGraph Graph;
    63 
    64   typedef Graph::Edge Edge;
    65   typedef Graph::Node Node;
    66   typedef Graph::EdgeIt EdgeIt;
    67   typedef Graph::NodeIt NodeIt;
    68   typedef Graph::EdgeMap<double> DoubleMap;
    69 
    70   std::string inputName;
    71   std::string outputName;
    72   std::string typeName;
    73 
    74   bool help = false;
    75   bool version = false;
    76 
    77   for (int arg = 1; arg < argc; ++arg) {
    78     if (strcmp(argv[arg], "--type") == 0 || 
    79 	strcmp(argv[arg], "-t") == 0) {
    80       if (!typeName.empty()) {
    81 	cerr << "Multiple type description" << endl;
    82 	return -1;
    83       }
    84       if (arg + 1 == argc) {
    85 	cerr << "Parameter without value" << endl;
    86 	return -1;
    87       }
    88       typeName = argv[++arg];
    89     }
    90     else if (strcmp(argv[arg], "--input") == 0 || 
    91 	     strcmp(argv[arg], "-i") == 0) {
    92       if (!inputName.empty()) {
    93 	cerr << "Multiple input description" << endl;
    94 	return -1;
    95       }
    96       if (arg + 1 == argc) {
    97 	cerr << "Parameter without value" << endl;
    98 	return -1;
    99       }
   100       inputName = argv[++arg];
   101     }
   102     else if (strcmp(argv[arg], "--output") == 0 || 
   103 	     strcmp(argv[arg], "-o") == 0) {
   104       if (!outputName.empty()) {
   105 	cerr << "Multiple input description" << endl;
   106 	return -1;
   107       }
   108       if (arg + 1 == argc) {
   109 	cerr << "Parameter without value" << endl;
   110 	return -1;
   111       }
   112       outputName = argv[++arg];
   113     } else if (strcmp(argv[arg], "--help") == 0 ||
   114 	       strcmp(argv[arg], "-h") == 0) {
   115       help = true;
   116     } else if (strcmp(argv[arg], "--version") == 0 ||
   117 	       strcmp(argv[arg], "-v") == 0) {
   118       version = true;
   119     } else {
   120       cerr << "Invalid option: " << argv[arg] << endl;
   121       return -1;
   122     }
   123   }
   124 
   125   if (version) {
   126     cout << versionString;
   127   }
   128   if (help) {
   129     cout << helpString;
   130   }
   131   if (help || version) {
   132     return 0;
   133   }
   134 
   135   ifstream input;
   136   if (!inputName.empty()) {
   137     input.open(inputName.c_str());
   138     if (!input) {
   139       cerr << "File open error" << endl;
   140       return -1;
   141     }
   142   }
   143   istream& is = (inputName.empty() ? cin : input);
   144 
   145   ofstream output;
   146   if (!outputName.empty()) {
   147     output.open(outputName.c_str());
   148     if (!output) {
   149       cerr << "File open error" << endl;
   150       return -1;
   151     }
   152   }
   153   ostream& os = (outputName.empty() ? cout : output);
   154 
   155   if (typeName.empty()) {
   156     typeName = "maxflow";
   157   }
   158 
   159   if (typeName == "mincostflow") {
   160     Graph graph;
   161     Node s, t;
   162     DoubleMap cost(graph), capacity(graph);
   163     readDimacs(is, graph, capacity, s, t, cost);
   164     GraphWriter<Graph>(os, graph).
   165       writeEdgeMap("capacity", capacity).
   166       writeNode("source", s).
   167       writeNode("target", t).
   168       writeEdgeMap("cost", cost).
   169       run();
   170   } else if (typeName == "maxflow") {
   171     Graph graph;
   172     Node s, t;
   173     DoubleMap capacity(graph);
   174     readDimacs(is, graph, capacity, s, t);
   175     GraphWriter<Graph>(os, graph).
   176       writeEdgeMap("capacity", capacity).
   177       writeNode("source", s).
   178       writeNode("target", t).
   179       run();
   180   } else if (typeName == "shortestpath") {
   181     Graph graph;
   182     Node s;
   183     DoubleMap capacity(graph);
   184     readDimacs(is, graph, capacity, s);
   185     GraphWriter<Graph>(os, graph).
   186       writeEdgeMap("capacity", capacity).
   187       writeNode("source", s).
   188       run();
   189   } else if (typeName == "capacitated") {
   190     Graph graph;
   191     DoubleMap capacity(graph);
   192     readDimacs(is, graph, capacity);
   193     GraphWriter<Graph>(os, graph).
   194       writeEdgeMap("capacity", capacity).
   195       run();
   196   } else if (typeName == "plain") {
   197     Graph graph;
   198     readDimacs(is, graph);
   199     GraphWriter<Graph>(os, graph).run();
   200   } else {
   201     cerr << "Invalid type error" << endl;
   202     return -1;
   203   }
   204   return 0;
   205 }