| 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
|---|
| 2 | * |
|---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2003-2008 |
|---|
| 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 tools |
|---|
| 20 | ///\file |
|---|
| 21 | ///\brief DIMACS to LGF converter. |
|---|
| 22 | /// |
|---|
| 23 | /// This program converts various DIMACS formats to the LEMON Digraph Format |
|---|
| 24 | /// (LGF). |
|---|
| 25 | /// |
|---|
| 26 | /// See |
|---|
| 27 | /// \verbatim |
|---|
| 28 | /// dimacs-to-lgf --help |
|---|
| 29 | /// \endverbatim |
|---|
| 30 | /// for more info on usage. |
|---|
| 31 | /// |
|---|
| 32 | |
|---|
| 33 | #include <iostream> |
|---|
| 34 | #include <fstream> |
|---|
| 35 | #include <cstring> |
|---|
| 36 | |
|---|
| 37 | #include <lemon/smart_graph.h> |
|---|
| 38 | #include <lemon/dimacs.h> |
|---|
| 39 | #include <lemon/lgf_writer.h> |
|---|
| 40 | |
|---|
| 41 | #include <lemon/arg_parser.h> |
|---|
| 42 | |
|---|
| 43 | using namespace std; |
|---|
| 44 | using namespace lemon; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | int main(int argc, const char *argv[]) { |
|---|
| 48 | typedef SmartDigraph Digraph; |
|---|
| 49 | |
|---|
| 50 | typedef Digraph::Arc Arc; |
|---|
| 51 | typedef Digraph::Node Node; |
|---|
| 52 | typedef Digraph::ArcIt ArcIt; |
|---|
| 53 | typedef Digraph::NodeIt NodeIt; |
|---|
| 54 | typedef Digraph::ArcMap<double> DoubleArcMap; |
|---|
| 55 | typedef Digraph::NodeMap<double> DoubleNodeMap; |
|---|
| 56 | |
|---|
| 57 | std::string inputName; |
|---|
| 58 | std::string outputName; |
|---|
| 59 | std::string typeName; |
|---|
| 60 | |
|---|
| 61 | bool mincostflow; |
|---|
| 62 | bool maxflow; |
|---|
| 63 | bool shortestpath; |
|---|
| 64 | bool capacitated; |
|---|
| 65 | bool plain; |
|---|
| 66 | |
|---|
| 67 | bool version; |
|---|
| 68 | |
|---|
| 69 | ArgParser ap(argc, argv); |
|---|
| 70 | ap.refOption("-input", |
|---|
| 71 | "use FILE as input instead of standard input", |
|---|
| 72 | inputName).synonym("i", "-input") |
|---|
| 73 | .refOption("-output", |
|---|
| 74 | "use FILE as output instead of standard output", |
|---|
| 75 | outputName).synonym("o", "-output") |
|---|
| 76 | .refOption("-mincostflow", |
|---|
| 77 | "set the type of the digraph to \"mincostflow\" digraph", |
|---|
| 78 | mincostflow) |
|---|
| 79 | .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow") |
|---|
| 80 | .refOption("-maxflow", |
|---|
| 81 | "set the type of the digraph to \"maxflow\" digraph", |
|---|
| 82 | maxflow) |
|---|
| 83 | .optionGroup("type", "-maxflow").synonym("mf", "-maxflow") |
|---|
| 84 | .refOption("-shortestpath", |
|---|
| 85 | "set the type of the digraph to \"shortestpath\" digraph", |
|---|
| 86 | shortestpath) |
|---|
| 87 | .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath") |
|---|
| 88 | .refOption("-capacitated", |
|---|
| 89 | "set the type of the digraph to \"capacitated\" digraph", |
|---|
| 90 | capacitated) |
|---|
| 91 | .optionGroup("type", "-capacitated").synonym("cap", "-capacitated") |
|---|
| 92 | .refOption("-plain", |
|---|
| 93 | "set the type of the digraph to \"plain\" digraph", |
|---|
| 94 | plain) |
|---|
| 95 | .optionGroup("type", "-plain").synonym("pl", "-plain") |
|---|
| 96 | .onlyOneGroup("type") |
|---|
| 97 | .mandatoryGroup("type") |
|---|
| 98 | .refOption("-version", "show version information", version) |
|---|
| 99 | .synonym("v", "-version") |
|---|
| 100 | .run(); |
|---|
| 101 | |
|---|
| 102 | ifstream input; |
|---|
| 103 | if (!inputName.empty()) { |
|---|
| 104 | input.open(inputName.c_str()); |
|---|
| 105 | if (!input) { |
|---|
| 106 | cerr << "File open error" << endl; |
|---|
| 107 | return -1; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | istream& is = (inputName.empty() ? cin : input); |
|---|
| 111 | |
|---|
| 112 | ofstream output; |
|---|
| 113 | if (!outputName.empty()) { |
|---|
| 114 | output.open(outputName.c_str()); |
|---|
| 115 | if (!output) { |
|---|
| 116 | cerr << "File open error" << endl; |
|---|
| 117 | return -1; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | ostream& os = (outputName.empty() ? cout : output); |
|---|
| 121 | |
|---|
| 122 | if (mincostflow) { |
|---|
| 123 | Digraph digraph; |
|---|
| 124 | DoubleArcMap lower(digraph), capacity(digraph), cost(digraph); |
|---|
| 125 | DoubleNodeMap supply(digraph); |
|---|
| 126 | readDimacsMin(is, digraph, lower, capacity, cost, supply); |
|---|
| 127 | DigraphWriter<Digraph>(digraph, os). |
|---|
| 128 | nodeMap("supply", supply). |
|---|
| 129 | arcMap("lower", lower). |
|---|
| 130 | arcMap("capacity", capacity). |
|---|
| 131 | arcMap("cost", cost). |
|---|
| 132 | run(); |
|---|
| 133 | } else if (maxflow) { |
|---|
| 134 | Digraph digraph; |
|---|
| 135 | Node s, t; |
|---|
| 136 | DoubleArcMap capacity(digraph); |
|---|
| 137 | readDimacsMax(is, digraph, capacity, s, t); |
|---|
| 138 | DigraphWriter<Digraph>(digraph, os). |
|---|
| 139 | arcMap("capacity", capacity). |
|---|
| 140 | node("source", s). |
|---|
| 141 | node("target", t). |
|---|
| 142 | run(); |
|---|
| 143 | } else if (shortestpath) { |
|---|
| 144 | Digraph digraph; |
|---|
| 145 | Node s; |
|---|
| 146 | DoubleArcMap capacity(digraph); |
|---|
| 147 | readDimacsSp(is, digraph, capacity, s); |
|---|
| 148 | DigraphWriter<Digraph>(digraph, os). |
|---|
| 149 | arcMap("capacity", capacity). |
|---|
| 150 | node("source", s). |
|---|
| 151 | run(); |
|---|
| 152 | } else if (capacitated) { |
|---|
| 153 | Digraph digraph; |
|---|
| 154 | DoubleArcMap capacity(digraph); |
|---|
| 155 | readDimacsMax(is, digraph, capacity); |
|---|
| 156 | DigraphWriter<Digraph>(digraph, os). |
|---|
| 157 | arcMap("capacity", capacity). |
|---|
| 158 | run(); |
|---|
| 159 | } else if (plain) { |
|---|
| 160 | Digraph digraph; |
|---|
| 161 | readDimacsMat(is, digraph); |
|---|
| 162 | DigraphWriter<Digraph>(digraph, os).run(); |
|---|
| 163 | } else { |
|---|
| 164 | cerr << "Invalid type error" << endl; |
|---|
| 165 | return -1; |
|---|
| 166 | } |
|---|
| 167 | return 0; |
|---|
| 168 | } |
|---|