tools/dim_to_lgf.cc
changeset 2410 fe46b61da4e3
parent 2404 ec474604075e
child 2413 21eb3ccdc3df
equal deleted inserted replaced
0:0d0f9671208c 1:091db9361e4b
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 ///\ingroup demos
    19 ///\ingroup demos
    20 ///\file
    20 ///\file
    21 ///\brief DIMACS to LGF converter (demo).
    21 ///\brief DIMACS to LGF converter.
    22 ///
    22 ///
    23 /// This program converts various DIMACS formats to the LEMON Graph Format
    23 /// This program converts various DIMACS formats to the LEMON Graph Format
    24 /// (LGF).
    24 /// (LGF).
    25 ///
    25 ///
    26 /// \include dim_to_lgf.cc
    26 /// \include dim_to_lgf.cc
    31 
    31 
    32 #include <lemon/smart_graph.h>
    32 #include <lemon/smart_graph.h>
    33 #include <lemon/dimacs.h>
    33 #include <lemon/dimacs.h>
    34 #include <lemon/graph_writer.h>
    34 #include <lemon/graph_writer.h>
    35 
    35 
       
    36 #include <lemon/arg_parser.h>
       
    37 
    36 using namespace std;
    38 using namespace std;
    37 using namespace lemon;
    39 using namespace lemon;
    38 
       
    39 const char* versionString =
       
    40 "dim_to_lgf - part of lemon library\n";
       
    41 
       
    42 const char* helpString =
       
    43 "DIMACS to LGF converter\n"
       
    44 "Usage: dim_to_lgf [OPTIONS]\n"
       
    45 "\n"
       
    46 "Examples:\n"
       
    47 "  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
       
    48 "\n"
       
    49 "Options:\n"
       
    50 "  -i FILE, --input FILE    use FILE as input instead of standard input\n"
       
    51 "  -o FILE, --output FILE   use FILE as output instead of standard output\n"
       
    52 "  -t TYPE, --type TYPE     set up the type of the graph\n"
       
    53 "                             Possible types:\n"
       
    54 "                               mincostflow\n"
       
    55 "                               maxflow (default)\n"
       
    56 "                               shortestpath\n"
       
    57 "                               capacitated\n"
       
    58 "                               plain\n"
       
    59 "  -v, --version            shows the version of the converter\n"
       
    60 "  -h, --help               shows the help of the converter\n";
       
    61 
    40 
    62 
    41 
    63 int main(int argc, const char *argv[]) {
    42 int main(int argc, const char *argv[]) {
    64   typedef SmartGraph Graph;
    43   typedef SmartGraph Graph;
    65 
    44 
    71 
    50 
    72   std::string inputName;
    51   std::string inputName;
    73   std::string outputName;
    52   std::string outputName;
    74   std::string typeName;
    53   std::string typeName;
    75 
    54 
    76   bool help = false;
    55   bool mincostflow;
    77   bool version = false;
    56   bool maxflow;
       
    57   bool shortestpath;
       
    58   bool capacitated;
       
    59   bool plain;
    78 
    60 
    79   for (int arg = 1; arg < argc; ++arg) {
    61   bool version;
    80     if (strcmp(argv[arg], "--type") == 0 || 
       
    81 	strcmp(argv[arg], "-t") == 0) {
       
    82       if (!typeName.empty()) {
       
    83 	cerr << "Multiple type description" << endl;
       
    84 	return -1;
       
    85       }
       
    86       if (arg + 1 == argc) {
       
    87 	cerr << "Parameter without value" << endl;
       
    88 	return -1;
       
    89       }
       
    90       typeName = argv[++arg];
       
    91     }
       
    92     else if (strcmp(argv[arg], "--input") == 0 || 
       
    93 	     strcmp(argv[arg], "-i") == 0) {
       
    94       if (!inputName.empty()) {
       
    95 	cerr << "Multiple input description" << endl;
       
    96 	return -1;
       
    97       }
       
    98       if (arg + 1 == argc) {
       
    99 	cerr << "Parameter without value" << endl;
       
   100 	return -1;
       
   101       }
       
   102       inputName = argv[++arg];
       
   103     }
       
   104     else if (strcmp(argv[arg], "--output") == 0 || 
       
   105 	     strcmp(argv[arg], "-o") == 0) {
       
   106       if (!outputName.empty()) {
       
   107 	cerr << "Multiple input description" << endl;
       
   108 	return -1;
       
   109       }
       
   110       if (arg + 1 == argc) {
       
   111 	cerr << "Parameter without value" << endl;
       
   112 	return -1;
       
   113       }
       
   114       outputName = argv[++arg];
       
   115     } else if (strcmp(argv[arg], "--help") == 0 ||
       
   116 	       strcmp(argv[arg], "-h") == 0) {
       
   117       help = true;
       
   118     } else if (strcmp(argv[arg], "--version") == 0 ||
       
   119 	       strcmp(argv[arg], "-v") == 0) {
       
   120       version = true;
       
   121     } else {
       
   122       cerr << "Invalid option: " << argv[arg] << endl;
       
   123       return -1;
       
   124     }
       
   125   }
       
   126 
    62 
   127   if (version) {
    63   ArgParser ap(argc, argv);
   128     cout << versionString;
    64   ap.refOption("-input", 
   129   }
    65                "use FILE as input instead of standard input", 
   130   if (help) {
    66                inputName).synonym("i", "-input")
   131     cout << helpString;
    67     .refOption("-output", 
   132   }
    68                "use FILE as output instead of standard output", 
   133   if (help || version) {
    69                outputName).synonym("o", "-output")
   134     return 0;
    70     .refOption("-mincostflow", 
   135   }
    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();
   136 
    95 
   137   ifstream input;
    96   ifstream input;
   138   if (!inputName.empty()) {
    97   if (!inputName.empty()) {
   139     input.open(inputName.c_str());
    98     input.open(inputName.c_str());
   140     if (!input) {
    99     if (!input) {
   152       return -1;
   111       return -1;
   153     }
   112     }
   154   }
   113   }
   155   ostream& os = (outputName.empty() ? cout : output);
   114   ostream& os = (outputName.empty() ? cout : output);
   156 
   115 
   157   if (typeName.empty()) {
   116   if (mincostflow) {
   158     typeName = "maxflow";
       
   159   }
       
   160 
       
   161   if (typeName == "mincostflow") {
       
   162     Graph graph;
   117     Graph graph;
   163     Node s, t;
   118     Node s, t;
   164     DoubleMap cost(graph), capacity(graph);
   119     DoubleMap cost(graph), capacity(graph);
   165     readDimacs(is, graph, capacity, s, t, cost);
   120     readDimacs(is, graph, capacity, s, t, cost);
   166     GraphWriter<Graph>(os, graph).
   121     GraphWriter<Graph>(os, graph).
   167       writeEdgeMap("capacity", capacity).
   122       writeEdgeMap("capacity", capacity).
   168       writeNode("source", s).
   123       writeNode("source", s).
   169       writeNode("target", t).
   124       writeNode("target", t).
   170       writeEdgeMap("cost", cost).
   125       writeEdgeMap("cost", cost).
   171       run();
   126       run();
   172   } else if (typeName == "maxflow") {
   127   } else if (maxflow) {
   173     Graph graph;
   128     Graph graph;
   174     Node s, t;
   129     Node s, t;
   175     DoubleMap capacity(graph);
   130     DoubleMap capacity(graph);
   176     readDimacs(is, graph, capacity, s, t);
   131     readDimacs(is, graph, capacity, s, t);
   177     GraphWriter<Graph>(os, graph).
   132     GraphWriter<Graph>(os, graph).
   178       writeEdgeMap("capacity", capacity).
   133       writeEdgeMap("capacity", capacity).
   179       writeNode("source", s).
   134       writeNode("source", s).
   180       writeNode("target", t).
   135       writeNode("target", t).
   181       run();
   136       run();
   182   } else if (typeName == "shortestpath") {
   137   } else if (shortestpath) {
   183     Graph graph;
   138     Graph graph;
   184     Node s;
   139     Node s;
   185     DoubleMap capacity(graph);
   140     DoubleMap capacity(graph);
   186     readDimacs(is, graph, capacity, s);
   141     readDimacs(is, graph, capacity, s);
   187     GraphWriter<Graph>(os, graph).
   142     GraphWriter<Graph>(os, graph).
   188       writeEdgeMap("capacity", capacity).
   143       writeEdgeMap("capacity", capacity).
   189       writeNode("source", s).
   144       writeNode("source", s).
   190       run();
   145       run();
   191   } else if (typeName == "capacitated") {
   146   } else if (capacitated) {
   192     Graph graph;
   147     Graph graph;
   193     DoubleMap capacity(graph);
   148     DoubleMap capacity(graph);
   194     readDimacs(is, graph, capacity);
   149     readDimacs(is, graph, capacity);
   195     GraphWriter<Graph>(os, graph).
   150     GraphWriter<Graph>(os, graph).
   196       writeEdgeMap("capacity", capacity).
   151       writeEdgeMap("capacity", capacity).
   197       run();
   152       run();
   198   } else if (typeName == "plain") {
   153   } else if (plain) {
   199     Graph graph;
   154     Graph graph;
   200     readDimacs(is, graph);
   155     readDimacs(is, graph);
   201     GraphWriter<Graph>(os, graph).run();
   156     GraphWriter<Graph>(os, graph).run();
   202   } else {
   157   } else {
   203     cerr << "Invalid type error" << endl;
   158     cerr << "Invalid type error" << endl;