tools/dim_to_lgf.cc
author alpar
Tue, 13 Mar 2007 15:42:06 +0000
changeset 2408 467ca6d16556
child 2410 fe46b61da4e3
permissions -rw-r--r--
Doc improvements contributed by Peter Kovacs.
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
alpar@2404
    21
///\brief DIMACS to LGF converter (demo).
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
alpar@2404
    36
using namespace std;
alpar@2404
    37
using namespace lemon;
alpar@2404
    38
alpar@2404
    39
const char* versionString =
alpar@2404
    40
"dim_to_lgf - part of lemon library\n";
alpar@2404
    41
alpar@2404
    42
const char* helpString =
alpar@2404
    43
"DIMACS to LGF converter\n"
alpar@2404
    44
"Usage: dim_to_lgf [OPTIONS]\n"
alpar@2404
    45
"\n"
alpar@2404
    46
"Examples:\n"
alpar@2404
    47
"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
alpar@2404
    48
"\n"
alpar@2404
    49
"Options:\n"
alpar@2404
    50
"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
alpar@2404
    51
"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
alpar@2404
    52
"  -t TYPE, --type TYPE     set up the type of the graph\n"
alpar@2404
    53
"                             Possible types:\n"
alpar@2404
    54
"                               mincostflow\n"
alpar@2404
    55
"                               maxflow (default)\n"
alpar@2404
    56
"                               shortestpath\n"
alpar@2404
    57
"                               capacitated\n"
alpar@2404
    58
"                               plain\n"
alpar@2404
    59
"  -v, --version            shows the version of the converter\n"
alpar@2404
    60
"  -h, --help               shows the help of the converter\n";
alpar@2404
    61
alpar@2404
    62
alpar@2404
    63
int main(int argc, const char *argv[]) {
alpar@2404
    64
  typedef SmartGraph Graph;
alpar@2404
    65
alpar@2404
    66
  typedef Graph::Edge Edge;
alpar@2404
    67
  typedef Graph::Node Node;
alpar@2404
    68
  typedef Graph::EdgeIt EdgeIt;
alpar@2404
    69
  typedef Graph::NodeIt NodeIt;
alpar@2404
    70
  typedef Graph::EdgeMap<double> DoubleMap;
alpar@2404
    71
alpar@2404
    72
  std::string inputName;
alpar@2404
    73
  std::string outputName;
alpar@2404
    74
  std::string typeName;
alpar@2404
    75
alpar@2404
    76
  bool help = false;
alpar@2404
    77
  bool version = false;
alpar@2404
    78
alpar@2404
    79
  for (int arg = 1; arg < argc; ++arg) {
alpar@2404
    80
    if (strcmp(argv[arg], "--type") == 0 || 
alpar@2404
    81
	strcmp(argv[arg], "-t") == 0) {
alpar@2404
    82
      if (!typeName.empty()) {
alpar@2404
    83
	cerr << "Multiple type description" << endl;
alpar@2404
    84
	return -1;
alpar@2404
    85
      }
alpar@2404
    86
      if (arg + 1 == argc) {
alpar@2404
    87
	cerr << "Parameter without value" << endl;
alpar@2404
    88
	return -1;
alpar@2404
    89
      }
alpar@2404
    90
      typeName = argv[++arg];
alpar@2404
    91
    }
alpar@2404
    92
    else if (strcmp(argv[arg], "--input") == 0 || 
alpar@2404
    93
	     strcmp(argv[arg], "-i") == 0) {
alpar@2404
    94
      if (!inputName.empty()) {
alpar@2404
    95
	cerr << "Multiple input description" << endl;
alpar@2404
    96
	return -1;
alpar@2404
    97
      }
alpar@2404
    98
      if (arg + 1 == argc) {
alpar@2404
    99
	cerr << "Parameter without value" << endl;
alpar@2404
   100
	return -1;
alpar@2404
   101
      }
alpar@2404
   102
      inputName = argv[++arg];
alpar@2404
   103
    }
alpar@2404
   104
    else if (strcmp(argv[arg], "--output") == 0 || 
alpar@2404
   105
	     strcmp(argv[arg], "-o") == 0) {
alpar@2404
   106
      if (!outputName.empty()) {
alpar@2404
   107
	cerr << "Multiple input description" << endl;
alpar@2404
   108
	return -1;
alpar@2404
   109
      }
alpar@2404
   110
      if (arg + 1 == argc) {
alpar@2404
   111
	cerr << "Parameter without value" << endl;
alpar@2404
   112
	return -1;
alpar@2404
   113
      }
alpar@2404
   114
      outputName = argv[++arg];
alpar@2404
   115
    } else if (strcmp(argv[arg], "--help") == 0 ||
alpar@2404
   116
	       strcmp(argv[arg], "-h") == 0) {
alpar@2404
   117
      help = true;
alpar@2404
   118
    } else if (strcmp(argv[arg], "--version") == 0 ||
alpar@2404
   119
	       strcmp(argv[arg], "-v") == 0) {
alpar@2404
   120
      version = true;
alpar@2404
   121
    } else {
alpar@2404
   122
      cerr << "Invalid option: " << argv[arg] << endl;
alpar@2404
   123
      return -1;
alpar@2404
   124
    }
alpar@2404
   125
  }
alpar@2404
   126
alpar@2404
   127
  if (version) {
alpar@2404
   128
    cout << versionString;
alpar@2404
   129
  }
alpar@2404
   130
  if (help) {
alpar@2404
   131
    cout << helpString;
alpar@2404
   132
  }
alpar@2404
   133
  if (help || version) {
alpar@2404
   134
    return 0;
alpar@2404
   135
  }
alpar@2404
   136
alpar@2404
   137
  ifstream input;
alpar@2404
   138
  if (!inputName.empty()) {
alpar@2404
   139
    input.open(inputName.c_str());
alpar@2404
   140
    if (!input) {
alpar@2404
   141
      cerr << "File open error" << endl;
alpar@2404
   142
      return -1;
alpar@2404
   143
    }
alpar@2404
   144
  }
alpar@2404
   145
  istream& is = (inputName.empty() ? cin : input);
alpar@2404
   146
alpar@2404
   147
  ofstream output;
alpar@2404
   148
  if (!outputName.empty()) {
alpar@2404
   149
    output.open(outputName.c_str());
alpar@2404
   150
    if (!output) {
alpar@2404
   151
      cerr << "File open error" << endl;
alpar@2404
   152
      return -1;
alpar@2404
   153
    }
alpar@2404
   154
  }
alpar@2404
   155
  ostream& os = (outputName.empty() ? cout : output);
alpar@2404
   156
alpar@2404
   157
  if (typeName.empty()) {
alpar@2404
   158
    typeName = "maxflow";
alpar@2404
   159
  }
alpar@2404
   160
alpar@2404
   161
  if (typeName == "mincostflow") {
alpar@2404
   162
    Graph graph;
alpar@2404
   163
    Node s, t;
alpar@2404
   164
    DoubleMap cost(graph), capacity(graph);
alpar@2404
   165
    readDimacs(is, graph, capacity, s, t, cost);
alpar@2404
   166
    GraphWriter<Graph>(os, graph).
alpar@2404
   167
      writeEdgeMap("capacity", capacity).
alpar@2404
   168
      writeNode("source", s).
alpar@2404
   169
      writeNode("target", t).
alpar@2404
   170
      writeEdgeMap("cost", cost).
alpar@2404
   171
      run();
alpar@2404
   172
  } else if (typeName == "maxflow") {
alpar@2404
   173
    Graph graph;
alpar@2404
   174
    Node s, t;
alpar@2404
   175
    DoubleMap capacity(graph);
alpar@2404
   176
    readDimacs(is, graph, capacity, s, t);
alpar@2404
   177
    GraphWriter<Graph>(os, graph).
alpar@2404
   178
      writeEdgeMap("capacity", capacity).
alpar@2404
   179
      writeNode("source", s).
alpar@2404
   180
      writeNode("target", t).
alpar@2404
   181
      run();
alpar@2404
   182
  } else if (typeName == "shortestpath") {
alpar@2404
   183
    Graph graph;
alpar@2404
   184
    Node s;
alpar@2404
   185
    DoubleMap capacity(graph);
alpar@2404
   186
    readDimacs(is, graph, capacity, s);
alpar@2404
   187
    GraphWriter<Graph>(os, graph).
alpar@2404
   188
      writeEdgeMap("capacity", capacity).
alpar@2404
   189
      writeNode("source", s).
alpar@2404
   190
      run();
alpar@2404
   191
  } else if (typeName == "capacitated") {
alpar@2404
   192
    Graph graph;
alpar@2404
   193
    DoubleMap capacity(graph);
alpar@2404
   194
    readDimacs(is, graph, capacity);
alpar@2404
   195
    GraphWriter<Graph>(os, graph).
alpar@2404
   196
      writeEdgeMap("capacity", capacity).
alpar@2404
   197
      run();
alpar@2404
   198
  } else if (typeName == "plain") {
alpar@2404
   199
    Graph graph;
alpar@2404
   200
    readDimacs(is, graph);
alpar@2404
   201
    GraphWriter<Graph>(os, graph).run();
alpar@2404
   202
  } else {
alpar@2404
   203
    cerr << "Invalid type error" << endl;
alpar@2404
   204
    return -1;
alpar@2404
   205
  }
alpar@2404
   206
  return 0;
alpar@2404
   207
}