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