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