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