tools/dimacs-solver.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 23 Feb 2009 11:52:45 +0000
changeset 527 c458e02723b1
parent 440 88ed40ad0d4f
child 532 997a75bac45a
permissions -rw-r--r--
CMAKE builds the tools/ dir, too (#228)
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
alpar@385
    27
/// \verbatim
alpar@526
    28
///  dimacs-solver --help
alpar@385
    29
/// \endverbatim
alpar@385
    30
/// for more info on usage.
alpar@385
    31
///
alpar@385
    32
alpar@385
    33
#include <iostream>
alpar@385
    34
#include <fstream>
alpar@385
    35
#include <cstring>
alpar@385
    36
alpar@385
    37
#include <lemon/smart_graph.h>
alpar@385
    38
#include <lemon/dimacs.h>
alpar@385
    39
#include <lemon/lgf_writer.h>
alpar@526
    40
#include <lemon/time_measure.h>
alpar@385
    41
alpar@385
    42
#include <lemon/arg_parser.h>
alpar@387
    43
#include <lemon/error.h>
alpar@385
    44
alpar@526
    45
#include <lemon/dijkstra.h>
alpar@526
    46
#include <lemon/preflow.h>
alpar@526
    47
#include <lemon/max_matching.h>
alpar@526
    48
alpar@385
    49
using namespace lemon;
alpar@526
    50
typedef SmartDigraph Digraph;
alpar@526
    51
DIGRAPH_TYPEDEFS(Digraph);
alpar@526
    52
typedef SmartGraph Graph;
alpar@385
    53
alpar@526
    54
template<class Value>
alpar@526
    55
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &,
alpar@526
    56
              DimacsDescriptor &desc)
alpar@526
    57
{
alpar@526
    58
  bool report = !ap.given("q");
alpar@526
    59
  Digraph g;
alpar@526
    60
  Node s;
alpar@526
    61
  Digraph::ArcMap<Value> len(g);
alpar@526
    62
  Timer t;
alpar@526
    63
  t.restart();
alpar@526
    64
  readDimacsSp(is, g, len, s, desc);
alpar@526
    65
  if(report) std::cerr << "Read the file: " << t << '\n';
alpar@526
    66
  t.restart();
alpar@526
    67
  Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len);
alpar@526
    68
  if(report) std::cerr << "Setup Dijkstra class: " << t << '\n';
alpar@526
    69
  t.restart();
alpar@526
    70
  dij.run(s);
alpar@526
    71
  if(report) std::cerr << "Run Dijkstra: " << t << '\n';
alpar@526
    72
}
alpar@526
    73
alpar@526
    74
template<class Value>
alpar@526
    75
void solve_max(ArgParser &ap, std::istream &is, std::ostream &,
alpar@526
    76
              DimacsDescriptor &desc)
alpar@526
    77
{
alpar@526
    78
  bool report = !ap.given("q");
alpar@526
    79
  Digraph g;
alpar@526
    80
  Node s,t;
alpar@526
    81
  Digraph::ArcMap<Value> cap(g);
alpar@526
    82
  Timer ti;
alpar@526
    83
  ti.restart();
alpar@526
    84
  readDimacsMax(is, g, cap, s, t, desc);
alpar@526
    85
  if(report) std::cerr << "Read the file: " << ti << '\n';
alpar@526
    86
  ti.restart();
alpar@526
    87
  Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t);
alpar@526
    88
  if(report) std::cerr << "Setup Preflow class: " << ti << '\n';
alpar@526
    89
  ti.restart();
alpar@526
    90
  pre.run();
alpar@526
    91
  if(report) std::cerr << "Run Preflow: " << ti << '\n';
alpar@526
    92
  if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n';  
alpar@526
    93
}
alpar@526
    94
alpar@526
    95
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &,
alpar@526
    96
              DimacsDescriptor &desc)
alpar@526
    97
{
alpar@526
    98
  bool report = !ap.given("q");
alpar@526
    99
  Graph g;
alpar@526
   100
  Timer ti;
alpar@526
   101
  ti.restart();
alpar@526
   102
  readDimacsMat(is, g, desc);
alpar@526
   103
  if(report) std::cerr << "Read the file: " << ti << '\n';
alpar@526
   104
  ti.restart();
alpar@526
   105
  MaxMatching<Graph> mat(g);
alpar@526
   106
  if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n';
alpar@526
   107
  ti.restart();
alpar@526
   108
  mat.run();
alpar@526
   109
  if(report) std::cerr << "Run MaxMatching: " << ti << '\n';
alpar@526
   110
  if(report) std::cerr << "\nCardinality of max matching: "
alpar@526
   111
                       << mat.matchingSize() << '\n';  
alpar@526
   112
}
alpar@526
   113
alpar@526
   114
alpar@526
   115
template<class Value>
alpar@526
   116
void solve(ArgParser &ap, std::istream &is, std::ostream &os,
alpar@526
   117
           DimacsDescriptor &desc)
alpar@526
   118
{
alpar@526
   119
  switch(desc.type)
alpar@526
   120
    {
alpar@526
   121
    case DimacsDescriptor::MIN:
alpar@526
   122
      std::cerr <<
alpar@526
   123
        "\n\n Sorry, the min. cost flow solver is not yet available.\n"
alpar@526
   124
                << std::endl;
alpar@526
   125
      break;
alpar@526
   126
    case DimacsDescriptor::MAX:
alpar@526
   127
      solve_max<Value>(ap,is,os,desc);
alpar@526
   128
      break;
alpar@526
   129
    case DimacsDescriptor::SP:
alpar@526
   130
      solve_sp<Value>(ap,is,os,desc);
alpar@526
   131
      break;
alpar@526
   132
    case DimacsDescriptor::MAT:
alpar@526
   133
      solve_mat(ap,is,os,desc);
alpar@526
   134
      break;
alpar@526
   135
    default:
alpar@526
   136
      break;
alpar@526
   137
    }
alpar@526
   138
}
alpar@385
   139
alpar@385
   140
int main(int argc, const char *argv[]) {
alpar@385
   141
  typedef SmartDigraph Digraph;
alpar@385
   142
alpar@385
   143
  typedef Digraph::Arc Arc;
alpar@385
   144
  typedef Digraph::Node Node;
alpar@385
   145
  typedef Digraph::ArcIt ArcIt;
alpar@385
   146
  typedef Digraph::NodeIt NodeIt;
alpar@385
   147
  typedef Digraph::ArcMap<double> DoubleArcMap;
alpar@385
   148
  typedef Digraph::NodeMap<double> DoubleNodeMap;
alpar@385
   149
alpar@385
   150
  std::string inputName;
alpar@385
   151
  std::string outputName;
alpar@385
   152
alpar@385
   153
  ArgParser ap(argc, argv);
alpar@387
   154
  ap.other("[INFILE [OUTFILE]]",
alpar@387
   155
           "If either the INFILE or OUTFILE file is missing the standard\n"
alpar@387
   156
           "     input/output will be used instead.")
alpar@526
   157
    .boolOption("q", "Do not print any report")
alpar@526
   158
    .boolOption("int","Use 'int' for capacities, costs etc. (default)")
alpar@526
   159
    .optionGroup("datatype","int")
alpar@526
   160
#ifdef HAVE_LONG_LONG
alpar@526
   161
    .boolOption("long","Use 'long long' for capacities, costs etc.")
alpar@526
   162
    .optionGroup("datatype","long")
alpar@526
   163
#endif
alpar@526
   164
    .boolOption("double","Use 'double' for capacities, costs etc.")
alpar@526
   165
    .optionGroup("datatype","double")
alpar@526
   166
    .boolOption("ldouble","Use 'long double' for capacities, costs etc.")
alpar@526
   167
    .optionGroup("datatype","ldouble")
alpar@526
   168
    .onlyOneGroup("datatype")
alpar@385
   169
    .run();
alpar@385
   170
alpar@526
   171
  std::ifstream input;
alpar@526
   172
  std::ofstream output;
alpar@387
   173
alpar@387
   174
  switch(ap.files().size())
alpar@387
   175
    {
alpar@387
   176
    case 2:
alpar@387
   177
      output.open(ap.files()[1].c_str());
alpar@387
   178
      if (!output) {
alpar@387
   179
        throw IoError("Cannot open the file for writing", ap.files()[1]);
alpar@387
   180
      }
alpar@387
   181
    case 1:
alpar@387
   182
      input.open(ap.files()[0].c_str());
alpar@387
   183
      if (!input) {
alpar@387
   184
        throw IoError("File cannot be found", ap.files()[0]);
alpar@387
   185
      }
alpar@387
   186
    case 0:
alpar@387
   187
      break;
alpar@387
   188
    default:
alpar@526
   189
      std::cerr << ap.commandName() << ": too many arguments\n";
alpar@387
   190
      return 1;
alpar@387
   191
  }
alpar@526
   192
  std::istream& is = (ap.files().size()<1 ? std::cin : input);
alpar@526
   193
  std::ostream& os = (ap.files().size()<2 ? std::cout : output);
alpar@387
   194
alpar@387
   195
  DimacsDescriptor desc = dimacsType(is);
alpar@526
   196
  
alpar@526
   197
  if(!ap.given("q"))
alpar@387
   198
    {
alpar@526
   199
      std::cout << "Problem type: ";
alpar@526
   200
      switch(desc.type)
alpar@526
   201
        {
alpar@526
   202
        case DimacsDescriptor::MIN:
alpar@526
   203
          std::cout << "min";
alpar@526
   204
          break;
alpar@526
   205
        case DimacsDescriptor::MAX:
alpar@526
   206
          std::cout << "max";
alpar@526
   207
          break;
alpar@526
   208
        case DimacsDescriptor::SP:
alpar@526
   209
          std::cout << "sp";
alpar@526
   210
        case DimacsDescriptor::MAT:
alpar@526
   211
          std::cout << "mat";
alpar@526
   212
          break;
alpar@526
   213
        default:
alpar@526
   214
          exit(1);
alpar@526
   215
          break;
alpar@526
   216
        }
alpar@526
   217
      std::cout << "\nNum of nodes: " << desc.nodeNum;
alpar@526
   218
      std::cout << "\nNum of arcs:  " << desc.edgeNum;
alpar@526
   219
      std::cout << '\n' << std::endl;
alpar@385
   220
    }
alpar@526
   221
    
alpar@526
   222
  if(ap.given("double"))
alpar@526
   223
    solve<double>(ap,is,os,desc);
alpar@526
   224
  else if(ap.given("ldouble"))
alpar@526
   225
    solve<long double>(ap,is,os,desc);
alpar@526
   226
#ifdef HAVE_LONG_LONG
alpar@526
   227
  else if(ap.given("long"))
alpar@526
   228
    solve<long long>(ap,is,os,desc);
alpar@526
   229
#endif
alpar@526
   230
  else solve<int>(ap,is,os,desc);
alpar@526
   231
alpar@385
   232
  return 0;
alpar@385
   233
}