lemon/dimacs.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 01 Jan 2009 00:00:00 +0100
changeset 463 88ed40ad0d4f
parent 403 0a3ec097a76c
child 572 635a8375227d
permissions -rw-r--r--
Happy New Year again
- update the copyright headers + run the source unifier
alpar@400
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@400
     2
 *
alpar@400
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@400
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
alpar@400
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@400
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@400
     8
 *
alpar@400
     9
 * Permission to use, modify and distribute this software is granted
alpar@400
    10
 * provided that this copyright notice appears in all copies. For
alpar@400
    11
 * precise terms see the accompanying LICENSE file.
alpar@400
    12
 *
alpar@400
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@400
    14
 * express or implied, and with no claim as to its suitability for any
alpar@400
    15
 * purpose.
alpar@400
    16
 *
alpar@400
    17
 */
alpar@400
    18
alpar@400
    19
#ifndef LEMON_DIMACS_H
alpar@400
    20
#define LEMON_DIMACS_H
alpar@400
    21
alpar@400
    22
#include <iostream>
alpar@400
    23
#include <string>
alpar@400
    24
#include <vector>
alpar@400
    25
#include <lemon/maps.h>
alpar@402
    26
#include <lemon/error.h>
alpar@400
    27
alpar@400
    28
/// \ingroup dimacs_group
alpar@400
    29
/// \file
alpar@400
    30
/// \brief DIMACS file format reader.
alpar@400
    31
alpar@400
    32
namespace lemon {
alpar@400
    33
alpar@400
    34
  /// \addtogroup dimacs_group
alpar@400
    35
  /// @{
alpar@400
    36
alpar@402
    37
  /// DIMACS file type descriptor.
alpar@402
    38
  struct DimacsDescriptor
alpar@402
    39
  {
alpar@402
    40
    ///File type enum
alpar@402
    41
    enum Type
alpar@402
    42
      {
alpar@402
    43
        NONE, MIN, MAX, SP, MAT
alpar@402
    44
      };
alpar@402
    45
    ///The file type
alpar@402
    46
    Type type;
kpeter@403
    47
    ///The number of nodes in the graph
alpar@402
    48
    int nodeNum;
kpeter@403
    49
    ///The number of edges in the graph
alpar@402
    50
    int edgeNum;
alpar@402
    51
    int lineShift;
alpar@402
    52
    /// Constructor. Sets the type to NONE.
alpar@402
    53
    DimacsDescriptor() : type(NONE) {}
alpar@402
    54
  };
alpar@402
    55
alpar@402
    56
  ///Discover the type of a DIMACS file
alpar@402
    57
alpar@402
    58
  ///It starts seeking the begining of the file for the problem type
kpeter@403
    59
  ///and size info. The found data is returned in a special struct
alpar@402
    60
  ///that can be evaluated and passed to the appropriate reader
alpar@402
    61
  ///function.
alpar@402
    62
  DimacsDescriptor dimacsType(std::istream& is)
alpar@402
    63
  {
alpar@402
    64
    DimacsDescriptor r;
alpar@402
    65
    std::string problem,str;
alpar@402
    66
    char c;
alpar@402
    67
    r.lineShift=0;
alpar@402
    68
    while (is >> c)
alpar@402
    69
      switch(c)
alpar@402
    70
        {
alpar@402
    71
        case 'p':
alpar@402
    72
          if(is >> problem >> r.nodeNum >> r.edgeNum)
alpar@402
    73
            {
alpar@402
    74
              getline(is, str);
alpar@402
    75
              r.lineShift++;
alpar@402
    76
              if(problem=="min") r.type=DimacsDescriptor::MIN;
alpar@402
    77
              else if(problem=="max") r.type=DimacsDescriptor::MAX;
alpar@402
    78
              else if(problem=="sp") r.type=DimacsDescriptor::SP;
alpar@402
    79
              else if(problem=="mat") r.type=DimacsDescriptor::MAT;
alpar@402
    80
              else throw FormatError("Unknown problem type");
alpar@402
    81
              return r;
alpar@402
    82
            }
alpar@402
    83
          else
alpar@402
    84
            {
alpar@402
    85
              throw FormatError("Missing or wrong problem type declaration.");
alpar@402
    86
            }
alpar@402
    87
          break;
alpar@402
    88
        case 'c':
alpar@402
    89
          getline(is, str);
alpar@402
    90
          r.lineShift++;
alpar@402
    91
          break;
alpar@402
    92
        default:
alpar@402
    93
          throw FormatError("Unknown DIMACS declaration.");
alpar@402
    94
        }
alpar@402
    95
    throw FormatError("Missing problem type declaration.");
alpar@402
    96
  }
alpar@402
    97
alpar@402
    98
alpar@402
    99
kpeter@403
   100
  /// DIMACS minimum cost flow reader function.
alpar@400
   101
  ///
kpeter@403
   102
  /// This function reads a minimum cost flow instance from DIMACS format,
kpeter@403
   103
  /// i.e. from a DIMACS file having a line starting with
alpar@400
   104
  /// \code
alpar@400
   105
  ///   p min
alpar@400
   106
  /// \endcode
alpar@402
   107
  /// At the beginning, \c g is cleared by \c g.clear(). The supply
alpar@400
   108
  /// amount of the nodes are written to \c supply (signed). The
alpar@400
   109
  /// lower bounds, capacities and costs of the arcs are written to
alpar@400
   110
  /// \c lower, \c capacity and \c cost.
alpar@402
   111
  ///
alpar@402
   112
  /// If the file type was previously evaluated by dimacsType(), then
alpar@402
   113
  /// the descriptor struct should be given by the \c dest parameter.
alpar@400
   114
  template <typename Digraph, typename LowerMap,
alpar@402
   115
            typename CapacityMap, typename CostMap,
alpar@402
   116
            typename SupplyMap>
alpar@402
   117
  void readDimacsMin(std::istream& is,
alpar@402
   118
                     Digraph &g,
alpar@402
   119
                     LowerMap& lower,
alpar@402
   120
                     CapacityMap& capacity,
alpar@402
   121
                     CostMap& cost,
alpar@402
   122
                     SupplyMap& supply,
alpar@402
   123
                     DimacsDescriptor desc=DimacsDescriptor())
alpar@400
   124
  {
alpar@400
   125
    g.clear();
alpar@400
   126
    std::vector<typename Digraph::Node> nodes;
alpar@400
   127
    typename Digraph::Arc e;
alpar@400
   128
    std::string problem, str;
alpar@400
   129
    char c;
alpar@400
   130
    int i, j;
alpar@402
   131
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
alpar@402
   132
    if(desc.type!=DimacsDescriptor::MIN)
alpar@402
   133
      throw FormatError("Problem type mismatch");
alpar@402
   134
alpar@402
   135
    nodes.resize(desc.nodeNum + 1);
alpar@402
   136
    for (int k = 1; k <= desc.nodeNum; ++k) {
alpar@402
   137
      nodes[k] = g.addNode();
alpar@402
   138
      supply.set(nodes[k], 0);
alpar@402
   139
    }
alpar@402
   140
alpar@400
   141
    typename SupplyMap::Value sup;
alpar@400
   142
    typename CapacityMap::Value low;
alpar@400
   143
    typename CapacityMap::Value cap;
alpar@400
   144
    typename CostMap::Value co;
alpar@400
   145
    while (is >> c) {
alpar@400
   146
      switch (c) {
alpar@400
   147
      case 'c': // comment line
alpar@400
   148
        getline(is, str);
alpar@400
   149
        break;
alpar@400
   150
      case 'n': // node definition line
alpar@400
   151
        is >> i >> sup;
alpar@400
   152
        getline(is, str);
alpar@400
   153
        supply.set(nodes[i], sup);
alpar@400
   154
        break;
alpar@400
   155
      case 'a': // arc (arc) definition line
alpar@400
   156
        is >> i >> j >> low >> cap >> co;
alpar@400
   157
        getline(is, str);
alpar@400
   158
        e = g.addArc(nodes[i], nodes[j]);
alpar@400
   159
        lower.set(e, low);
alpar@400
   160
        if (cap >= 0)
alpar@400
   161
          capacity.set(e, cap);
alpar@400
   162
        else
alpar@400
   163
          capacity.set(e, -1);
alpar@400
   164
        cost.set(e, co);
alpar@400
   165
        break;
alpar@400
   166
      }
alpar@400
   167
    }
alpar@400
   168
  }
alpar@400
   169
alpar@400
   170
  template<typename Digraph, typename CapacityMap>
alpar@402
   171
  void _readDimacs(std::istream& is,
alpar@402
   172
                   Digraph &g,
alpar@402
   173
                   CapacityMap& capacity,
alpar@402
   174
                   typename Digraph::Node &s,
alpar@402
   175
                   typename Digraph::Node &t,
alpar@402
   176
                   DimacsDescriptor desc=DimacsDescriptor()) {
alpar@400
   177
    g.clear();
alpar@402
   178
    s=t=INVALID;
alpar@400
   179
    std::vector<typename Digraph::Node> nodes;
alpar@400
   180
    typename Digraph::Arc e;
alpar@400
   181
    char c, d;
alpar@400
   182
    int i, j;
alpar@400
   183
    typename CapacityMap::Value _cap;
alpar@400
   184
    std::string str;
alpar@402
   185
    nodes.resize(desc.nodeNum + 1);
alpar@402
   186
    for (int k = 1; k <= desc.nodeNum; ++k) {
alpar@402
   187
      nodes[k] = g.addNode();
alpar@402
   188
    }
alpar@402
   189
alpar@400
   190
    while (is >> c) {
alpar@400
   191
      switch (c) {
alpar@400
   192
      case 'c': // comment line
alpar@400
   193
        getline(is, str);
alpar@400
   194
        break;
alpar@400
   195
      case 'n': // node definition line
alpar@402
   196
        if (desc.type==DimacsDescriptor::SP) { // shortest path problem
alpar@400
   197
          is >> i;
alpar@400
   198
          getline(is, str);
alpar@400
   199
          s = nodes[i];
alpar@400
   200
        }
alpar@402
   201
        if (desc.type==DimacsDescriptor::MAX) { // max flow problem
alpar@400
   202
          is >> i >> d;
alpar@400
   203
          getline(is, str);
alpar@400
   204
          if (d == 's') s = nodes[i];
alpar@400
   205
          if (d == 't') t = nodes[i];
alpar@400
   206
        }
alpar@400
   207
        break;
alpar@400
   208
      case 'a': // arc (arc) definition line
alpar@402
   209
        if (desc.type==DimacsDescriptor::SP ||
alpar@402
   210
            desc.type==DimacsDescriptor::MAX) {
alpar@400
   211
          is >> i >> j >> _cap;
alpar@400
   212
          getline(is, str);
alpar@400
   213
          e = g.addArc(nodes[i], nodes[j]);
alpar@400
   214
          capacity.set(e, _cap);
alpar@400
   215
        } else {
alpar@400
   216
          is >> i >> j;
alpar@400
   217
          getline(is, str);
alpar@400
   218
          g.addArc(nodes[i], nodes[j]);
alpar@400
   219
        }
alpar@400
   220
        break;
alpar@400
   221
      }
alpar@400
   222
    }
alpar@400
   223
  }
alpar@400
   224
kpeter@403
   225
  /// DIMACS maximum flow reader function.
alpar@402
   226
  ///
kpeter@403
   227
  /// This function reads a maximum flow instance from DIMACS format,
kpeter@403
   228
  /// i.e. from a DIMACS file having a line starting with
alpar@402
   229
  /// \code
alpar@402
   230
  ///   p max
alpar@402
   231
  /// \endcode
alpar@402
   232
  /// At the beginning, \c g is cleared by \c g.clear(). The arc
alpar@402
   233
  /// capacities are written to \c capacity and \c s and \c t are
alpar@402
   234
  /// set to the source and the target nodes.
alpar@402
   235
  ///
alpar@402
   236
  /// If the file type was previously evaluated by dimacsType(), then
alpar@402
   237
  /// the descriptor struct should be given by the \c dest parameter.
alpar@402
   238
  template<typename Digraph, typename CapacityMap>
alpar@402
   239
  void readDimacsMax(std::istream& is,
alpar@402
   240
                     Digraph &g,
alpar@402
   241
                     CapacityMap& capacity,
alpar@402
   242
                     typename Digraph::Node &s,
alpar@402
   243
                     typename Digraph::Node &t,
alpar@402
   244
                     DimacsDescriptor desc=DimacsDescriptor()) {
alpar@402
   245
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
alpar@402
   246
    if(desc.type!=DimacsDescriptor::MAX)
alpar@402
   247
      throw FormatError("Problem type mismatch");
alpar@402
   248
    _readDimacs(is,g,capacity,s,t,desc);
alpar@402
   249
  }
alpar@402
   250
alpar@400
   251
  /// DIMACS shortest path reader function.
alpar@400
   252
  ///
alpar@400
   253
  /// This function reads a shortest path instance from DIMACS format,
kpeter@403
   254
  /// i.e. from a DIMACS file having a line starting with
alpar@400
   255
  /// \code
alpar@400
   256
  ///   p sp
alpar@400
   257
  /// \endcode
alpar@402
   258
  /// At the beginning, \c g is cleared by \c g.clear(). The arc
kpeter@403
   259
  /// lengths are written to \c length and \c s is set to the
alpar@400
   260
  /// source node.
alpar@402
   261
  ///
alpar@402
   262
  /// If the file type was previously evaluated by dimacsType(), then
alpar@402
   263
  /// the descriptor struct should be given by the \c dest parameter.
alpar@402
   264
  template<typename Digraph, typename LengthMap>
alpar@402
   265
  void readDimacsSp(std::istream& is,
alpar@402
   266
                    Digraph &g,
alpar@402
   267
                    LengthMap& length,
alpar@402
   268
                    typename Digraph::Node &s,
alpar@402
   269
                    DimacsDescriptor desc=DimacsDescriptor()) {
alpar@401
   270
    typename Digraph::Node t;
alpar@402
   271
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
alpar@402
   272
    if(desc.type!=DimacsDescriptor::SP)
alpar@402
   273
      throw FormatError("Problem type mismatch");
alpar@402
   274
    _readDimacs(is, g, length, s, t,desc);
alpar@400
   275
  }
alpar@400
   276
alpar@400
   277
  /// DIMACS capacitated digraph reader function.
alpar@400
   278
  ///
alpar@400
   279
  /// This function reads an arc capacitated digraph instance from
alpar@402
   280
  /// DIMACS 'mat' or 'sp' format.
alpar@402
   281
  /// At the beginning, \c g is cleared by \c g.clear()
alpar@402
   282
  /// and the arc capacities/lengths are written to \c capacity.
alpar@402
   283
  ///
alpar@402
   284
  /// If the file type was previously evaluated by dimacsType(), then
alpar@402
   285
  /// the descriptor struct should be given by the \c dest parameter.
alpar@400
   286
  template<typename Digraph, typename CapacityMap>
alpar@402
   287
  void readDimacsCap(std::istream& is,
alpar@402
   288
                     Digraph &g,
alpar@402
   289
                     CapacityMap& capacity,
alpar@402
   290
                     DimacsDescriptor desc=DimacsDescriptor()) {
alpar@401
   291
    typename Digraph::Node u,v;
alpar@402
   292
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
alpar@402
   293
    if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP)
alpar@402
   294
      throw FormatError("Problem type mismatch");
alpar@402
   295
    _readDimacs(is, g, capacity, u, v, desc);
alpar@400
   296
  }
alpar@400
   297
alpar@400
   298
  /// DIMACS plain digraph reader function.
alpar@400
   299
  ///
alpar@400
   300
  /// This function reads a digraph without any designated nodes and
alpar@400
   301
  /// maps from DIMACS format, i.e. from DIMACS files having a line
alpar@400
   302
  /// starting with
alpar@400
   303
  /// \code
alpar@400
   304
  ///   p mat
alpar@400
   305
  /// \endcode
alpar@402
   306
  /// At the beginning, \c g is cleared by \c g.clear().
alpar@402
   307
  ///
alpar@402
   308
  /// If the file type was previously evaluated by dimacsType(), then
alpar@402
   309
  /// the descriptor struct should be given by the \c dest parameter.
alpar@400
   310
  template<typename Digraph>
alpar@402
   311
  void readDimacsMat(std::istream& is, Digraph &g,
alpar@402
   312
                     DimacsDescriptor desc=DimacsDescriptor()) {
alpar@401
   313
    typename Digraph::Node u,v;
alpar@400
   314
    NullMap<typename Digraph::Arc, int> n;
alpar@402
   315
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
alpar@402
   316
    if(desc.type!=DimacsDescriptor::MAT)
alpar@402
   317
      throw FormatError("Problem type mismatch");
alpar@402
   318
    _readDimacs(is, g, n, u, v, desc);
alpar@400
   319
  }
alpar@400
   320
alpar@400
   321
  /// DIMACS plain digraph writer function.
alpar@400
   322
  ///
alpar@400
   323
  /// This function writes a digraph without any designated nodes and
alpar@400
   324
  /// maps into DIMACS format, i.e. into DIMACS file having a line
alpar@400
   325
  /// starting with
alpar@400
   326
  /// \code
alpar@400
   327
  ///   p mat
alpar@400
   328
  /// \endcode
alpar@402
   329
  /// If \c comment is not empty, then it will be printed in the first line
alpar@402
   330
  /// prefixed by 'c'.
alpar@400
   331
  template<typename Digraph>
alpar@402
   332
  void writeDimacsMat(std::ostream& os, const Digraph &g,
alpar@402
   333
                      std::string comment="") {
alpar@400
   334
    typedef typename Digraph::NodeIt NodeIt;
alpar@400
   335
    typedef typename Digraph::ArcIt ArcIt;
alpar@400
   336
alpar@463
   337
    if(!comment.empty())
alpar@402
   338
      os << "c " << comment << std::endl;
alpar@400
   339
    os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
alpar@400
   340
alpar@400
   341
    typename Digraph::template NodeMap<int> nodes(g);
alpar@400
   342
    int i = 1;
alpar@400
   343
    for(NodeIt v(g); v != INVALID; ++v) {
alpar@400
   344
      nodes.set(v, i);
alpar@400
   345
      ++i;
alpar@400
   346
    }
alpar@400
   347
    for(ArcIt e(g); e != INVALID; ++e) {
alpar@400
   348
      os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]
alpar@400
   349
         << std::endl;
alpar@400
   350
    }
alpar@400
   351
  }
alpar@400
   352
alpar@400
   353
  /// @}
alpar@400
   354
alpar@400
   355
} //namespace lemon
alpar@400
   356
alpar@400
   357
#endif //LEMON_DIMACS_H