tools/dimacs-to-lgf.cc
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Tue, 26 May 2009 17:49:10 +0100
changeset 673 e4378da8cfcb
parent 561 6e0525ec5355
child 1179 d7e25df22e88
permissions -rw-r--r--
Rename documentation install folder from 'docs' to 'html'
alpar@385
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@385
     2
 *
alpar@385
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@385
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@385
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@385
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@385
     8
 *
alpar@385
     9
 * Permission to use, modify and distribute this software is granted
alpar@385
    10
 * provided that this copyright notice appears in all copies. For
alpar@385
    11
 * precise terms see the accompanying LICENSE file.
alpar@385
    12
 *
alpar@385
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@385
    14
 * express or implied, and with no claim as to its suitability for any
alpar@385
    15
 * purpose.
alpar@385
    16
 *
alpar@385
    17
 */
alpar@385
    18
alpar@385
    19
///\ingroup tools
alpar@385
    20
///\file
alpar@385
    21
///\brief DIMACS to LGF converter.
alpar@385
    22
///
alpar@385
    23
/// This program converts various DIMACS formats to the LEMON Digraph Format
alpar@385
    24
/// (LGF).
alpar@385
    25
///
alpar@385
    26
/// See
kpeter@584
    27
/// \code
kpeter@584
    28
///   dimacs-to-lgf --help
kpeter@584
    29
/// \endcode
kpeter@584
    30
/// for more info on the usage.
alpar@385
    31
alpar@385
    32
#include <iostream>
alpar@385
    33
#include <fstream>
alpar@385
    34
#include <cstring>
alpar@385
    35
alpar@385
    36
#include <lemon/smart_graph.h>
alpar@385
    37
#include <lemon/dimacs.h>
alpar@385
    38
#include <lemon/lgf_writer.h>
alpar@385
    39
alpar@385
    40
#include <lemon/arg_parser.h>
alpar@387
    41
#include <lemon/error.h>
alpar@385
    42
alpar@385
    43
using namespace std;
alpar@385
    44
using namespace lemon;
alpar@385
    45
alpar@385
    46
alpar@385
    47
int main(int argc, const char *argv[]) {
alpar@385
    48
  typedef SmartDigraph Digraph;
alpar@385
    49
alpar@385
    50
  typedef Digraph::Arc Arc;
alpar@385
    51
  typedef Digraph::Node Node;
alpar@385
    52
  typedef Digraph::ArcIt ArcIt;
alpar@385
    53
  typedef Digraph::NodeIt NodeIt;
alpar@385
    54
  typedef Digraph::ArcMap<double> DoubleArcMap;
alpar@385
    55
  typedef Digraph::NodeMap<double> DoubleNodeMap;
alpar@385
    56
alpar@385
    57
  std::string inputName;
alpar@385
    58
  std::string outputName;
alpar@385
    59
alpar@385
    60
  ArgParser ap(argc, argv);
alpar@387
    61
  ap.other("[INFILE [OUTFILE]]",
alpar@387
    62
           "If either the INFILE or OUTFILE file is missing the standard\n"
alpar@387
    63
           "     input/output will be used instead.")
alpar@385
    64
    .run();
alpar@385
    65
alpar@385
    66
  ifstream input;
alpar@387
    67
  ofstream output;
alpar@387
    68
alpar@387
    69
  switch(ap.files().size())
alpar@387
    70
    {
alpar@387
    71
    case 2:
alpar@387
    72
      output.open(ap.files()[1].c_str());
alpar@387
    73
      if (!output) {
alpar@387
    74
        throw IoError("Cannot open the file for writing", ap.files()[1]);
alpar@387
    75
      }
alpar@387
    76
    case 1:
alpar@387
    77
      input.open(ap.files()[0].c_str());
alpar@387
    78
      if (!input) {
alpar@387
    79
        throw IoError("File cannot be found", ap.files()[0]);
alpar@387
    80
      }
alpar@387
    81
    case 0:
alpar@387
    82
      break;
alpar@387
    83
    default:
alpar@387
    84
      cerr << ap.commandName() << ": too many arguments\n";
alpar@387
    85
      return 1;
alpar@387
    86
  }
alpar@387
    87
  istream& is = (ap.files().size()<1 ? cin : input);
alpar@387
    88
  ostream& os = (ap.files().size()<2 ? cout : output);
alpar@387
    89
alpar@387
    90
  DimacsDescriptor desc = dimacsType(is);
alpar@387
    91
  switch(desc.type)
alpar@387
    92
    {
alpar@387
    93
    case DimacsDescriptor::MIN:
alpar@387
    94
      {
alpar@387
    95
        Digraph digraph;
alpar@387
    96
        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
alpar@387
    97
        DoubleNodeMap supply(digraph);
alpar@561
    98
        readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
alpar@387
    99
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   100
          nodeMap("supply", supply).
alpar@387
   101
          arcMap("lower", lower).
alpar@387
   102
          arcMap("capacity", capacity).
alpar@387
   103
          arcMap("cost", cost).
alpar@387
   104
          attribute("problem","min").
alpar@387
   105
          run();
alpar@387
   106
      }
alpar@387
   107
      break;
alpar@387
   108
    case DimacsDescriptor::MAX:
alpar@387
   109
      {
alpar@387
   110
        Digraph digraph;
alpar@387
   111
        Node s, t;
alpar@387
   112
        DoubleArcMap capacity(digraph);
alpar@561
   113
        readDimacsMax(is, digraph, capacity, s, t, 0, desc);
alpar@387
   114
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   115
          arcMap("capacity", capacity).
alpar@387
   116
          node("source", s).
alpar@387
   117
          node("target", t).
alpar@387
   118
          attribute("problem","max").
alpar@387
   119
          run();
alpar@387
   120
      }
alpar@387
   121
      break;
alpar@387
   122
    case DimacsDescriptor::SP:
alpar@387
   123
      {
alpar@387
   124
        Digraph digraph;
alpar@387
   125
        Node s;
alpar@387
   126
        DoubleArcMap capacity(digraph);
alpar@387
   127
        readDimacsSp(is, digraph, capacity, s, desc);
alpar@387
   128
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   129
          arcMap("capacity", capacity).
alpar@387
   130
          node("source", s).
alpar@387
   131
          attribute("problem","sp").
alpar@387
   132
          run();
alpar@387
   133
      }
alpar@387
   134
      break;
alpar@387
   135
    case DimacsDescriptor::MAT:
alpar@387
   136
      {
alpar@387
   137
        Digraph digraph;
alpar@387
   138
        readDimacsMat(is, digraph,desc);
alpar@387
   139
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   140
          attribute("problem","mat").
alpar@387
   141
          run();
alpar@387
   142
      }
alpar@387
   143
      break;
alpar@387
   144
    default:
alpar@387
   145
      break;
alpar@385
   146
    }
alpar@385
   147
  return 0;
alpar@385
   148
}