tools/dimacs-to-lgf.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 17 Oct 2018 19:22:52 +0200
changeset 1185 c8d0179a32a2
parent 584 33c6b6e755cd
permissions -rw-r--r--
Merge bugfixes #610,#611,#612,#614
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@1179
    76
      // fall through
alpar@387
    77
    case 1:
alpar@387
    78
      input.open(ap.files()[0].c_str());
alpar@387
    79
      if (!input) {
alpar@387
    80
        throw IoError("File cannot be found", ap.files()[0]);
alpar@387
    81
      }
alpar@1179
    82
      // fall through
alpar@387
    83
    case 0:
alpar@387
    84
      break;
alpar@387
    85
    default:
alpar@387
    86
      cerr << ap.commandName() << ": too many arguments\n";
alpar@387
    87
      return 1;
alpar@387
    88
  }
alpar@387
    89
  istream& is = (ap.files().size()<1 ? cin : input);
alpar@387
    90
  ostream& os = (ap.files().size()<2 ? cout : output);
alpar@387
    91
alpar@387
    92
  DimacsDescriptor desc = dimacsType(is);
alpar@387
    93
  switch(desc.type)
alpar@387
    94
    {
alpar@387
    95
    case DimacsDescriptor::MIN:
alpar@387
    96
      {
alpar@387
    97
        Digraph digraph;
alpar@387
    98
        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
alpar@387
    99
        DoubleNodeMap supply(digraph);
alpar@561
   100
        readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
alpar@387
   101
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   102
          nodeMap("supply", supply).
alpar@387
   103
          arcMap("lower", lower).
alpar@387
   104
          arcMap("capacity", capacity).
alpar@387
   105
          arcMap("cost", cost).
alpar@387
   106
          attribute("problem","min").
alpar@387
   107
          run();
alpar@387
   108
      }
alpar@387
   109
      break;
alpar@387
   110
    case DimacsDescriptor::MAX:
alpar@387
   111
      {
alpar@387
   112
        Digraph digraph;
alpar@387
   113
        Node s, t;
alpar@387
   114
        DoubleArcMap capacity(digraph);
alpar@561
   115
        readDimacsMax(is, digraph, capacity, s, t, 0, desc);
alpar@387
   116
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   117
          arcMap("capacity", capacity).
alpar@387
   118
          node("source", s).
alpar@387
   119
          node("target", t).
alpar@387
   120
          attribute("problem","max").
alpar@387
   121
          run();
alpar@387
   122
      }
alpar@387
   123
      break;
alpar@387
   124
    case DimacsDescriptor::SP:
alpar@387
   125
      {
alpar@387
   126
        Digraph digraph;
alpar@387
   127
        Node s;
alpar@387
   128
        DoubleArcMap capacity(digraph);
alpar@387
   129
        readDimacsSp(is, digraph, capacity, s, desc);
alpar@387
   130
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   131
          arcMap("capacity", capacity).
alpar@387
   132
          node("source", s).
alpar@387
   133
          attribute("problem","sp").
alpar@387
   134
          run();
alpar@387
   135
      }
alpar@387
   136
      break;
alpar@387
   137
    case DimacsDescriptor::MAT:
alpar@387
   138
      {
alpar@387
   139
        Digraph digraph;
alpar@387
   140
        readDimacsMat(is, digraph,desc);
alpar@387
   141
        DigraphWriter<Digraph>(digraph, os).
alpar@387
   142
          attribute("problem","mat").
alpar@387
   143
          run();
alpar@387
   144
      }
alpar@387
   145
      break;
alpar@387
   146
    default:
alpar@387
   147
      break;
alpar@385
   148
    }
alpar@385
   149
  return 0;
alpar@385
   150
}