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