lemon/dimacs.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 27 Nov 2008 22:04:46 +0000
changeset 400 50d96f2166d7
child 401 9d1faab5e0f1
permissions -rw-r--r--
Port DIMACS tools from svn -r3516

Namely,
- apply migrate script
- apply unify sources
- break long lines
- Fixes the compilation
- dim_to_lgf -> dimacs-to-lgf
- better .hgignore
- shorten the doc of dimacs-to-lgf
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@400
     5
 * Copyright (C) 2003-2008
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@400
    26
alpar@400
    27
/// \ingroup dimacs_group
alpar@400
    28
/// \file
alpar@400
    29
/// \brief DIMACS file format reader.
alpar@400
    30
alpar@400
    31
namespace lemon {
alpar@400
    32
alpar@400
    33
  ///@defgroup dimacs_group DIMACS format
alpar@400
    34
  ///\brief Read and write files in DIMACS format
alpar@400
    35
  ///
alpar@400
    36
  ///Tools to read a digraph from or write it to a file in DIMACS format
alpar@400
    37
  ///data
alpar@400
    38
  ///\ingroup io_group
alpar@400
    39
alpar@400
    40
  /// \addtogroup dimacs_group
alpar@400
    41
  /// @{
alpar@400
    42
alpar@400
    43
  /// DIMACS min cost flow reader function.
alpar@400
    44
  ///
alpar@400
    45
  /// This function reads a min cost flow instance from DIMACS format,
alpar@400
    46
  /// i.e. from DIMACS files having a line starting with
alpar@400
    47
  /// \code
alpar@400
    48
  ///   p min
alpar@400
    49
  /// \endcode
alpar@400
    50
  /// At the beginning \c g is cleared by \c g.clear(). The supply
alpar@400
    51
  /// amount of the nodes are written to \c supply (signed). The
alpar@400
    52
  /// lower bounds, capacities and costs of the arcs are written to
alpar@400
    53
  /// \c lower, \c capacity and \c cost.
alpar@400
    54
  template <typename Digraph, typename LowerMap,
alpar@400
    55
    typename CapacityMap, typename CostMap,
alpar@400
    56
    typename SupplyMap>
alpar@400
    57
  void readDimacs( std::istream& is,
alpar@400
    58
                   Digraph &g,
alpar@400
    59
                   LowerMap& lower,
alpar@400
    60
                   CapacityMap& capacity,
alpar@400
    61
                   CostMap& cost,
alpar@400
    62
                   SupplyMap& supply )
alpar@400
    63
  {
alpar@400
    64
    g.clear();
alpar@400
    65
    std::vector<typename Digraph::Node> nodes;
alpar@400
    66
    typename Digraph::Arc e;
alpar@400
    67
    std::string problem, str;
alpar@400
    68
    char c;
alpar@400
    69
    int n, m;
alpar@400
    70
    int i, j;
alpar@400
    71
    typename SupplyMap::Value sup;
alpar@400
    72
    typename CapacityMap::Value low;
alpar@400
    73
    typename CapacityMap::Value cap;
alpar@400
    74
    typename CostMap::Value co;
alpar@400
    75
    while (is >> c) {
alpar@400
    76
      switch (c) {
alpar@400
    77
      case 'c': // comment line
alpar@400
    78
        getline(is, str);
alpar@400
    79
        break;
alpar@400
    80
      case 'p': // problem definition line
alpar@400
    81
        is >> problem >> n >> m;
alpar@400
    82
        getline(is, str);
alpar@400
    83
        if (problem != "min") return;
alpar@400
    84
        nodes.resize(n + 1);
alpar@400
    85
        for (int k = 1; k <= n; ++k) {
alpar@400
    86
          nodes[k] = g.addNode();
alpar@400
    87
          supply.set(nodes[k], 0);
alpar@400
    88
        }
alpar@400
    89
        break;
alpar@400
    90
      case 'n': // node definition line
alpar@400
    91
        is >> i >> sup;
alpar@400
    92
        getline(is, str);
alpar@400
    93
        supply.set(nodes[i], sup);
alpar@400
    94
        break;
alpar@400
    95
      case 'a': // arc (arc) definition line
alpar@400
    96
        is >> i >> j >> low >> cap >> co;
alpar@400
    97
        getline(is, str);
alpar@400
    98
        e = g.addArc(nodes[i], nodes[j]);
alpar@400
    99
        lower.set(e, low);
alpar@400
   100
        if (cap >= 0)
alpar@400
   101
          capacity.set(e, cap);
alpar@400
   102
        else
alpar@400
   103
          capacity.set(e, -1);
alpar@400
   104
        cost.set(e, co);
alpar@400
   105
        break;
alpar@400
   106
      }
alpar@400
   107
    }
alpar@400
   108
  }
alpar@400
   109
alpar@400
   110
  /// DIMACS max flow reader function.
alpar@400
   111
  ///
alpar@400
   112
  /// This function reads a max flow instance from DIMACS format,
alpar@400
   113
  /// i.e. from DIMACS files having a line starting with
alpar@400
   114
  /// \code
alpar@400
   115
  ///   p max
alpar@400
   116
  /// \endcode
alpar@400
   117
  /// At the beginning \c g is cleared by \c g.clear(). The arc
alpar@400
   118
  /// capacities are written to \c capacity and \c s and \c t are
alpar@400
   119
  /// set to the source and the target nodes.
alpar@400
   120
  template<typename Digraph, typename CapacityMap>
alpar@400
   121
  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
alpar@400
   122
                  typename Digraph::Node &s, typename Digraph::Node &t) {
alpar@400
   123
    g.clear();
alpar@400
   124
    std::vector<typename Digraph::Node> nodes;
alpar@400
   125
    typename Digraph::Arc e;
alpar@400
   126
    std::string problem;
alpar@400
   127
    char c, d;
alpar@400
   128
    int n, m;
alpar@400
   129
    int i, j;
alpar@400
   130
    typename CapacityMap::Value _cap;
alpar@400
   131
    std::string str;
alpar@400
   132
    while (is >> c) {
alpar@400
   133
      switch (c) {
alpar@400
   134
      case 'c': // comment line
alpar@400
   135
        getline(is, str);
alpar@400
   136
        break;
alpar@400
   137
      case 'p': // problem definition line
alpar@400
   138
        is >> problem >> n >> m;
alpar@400
   139
        getline(is, str);
alpar@400
   140
        nodes.resize(n + 1);
alpar@400
   141
        for (int k = 1; k <= n; ++k)
alpar@400
   142
          nodes[k] = g.addNode();
alpar@400
   143
        break;
alpar@400
   144
      case 'n': // node definition line
alpar@400
   145
        if (problem == "sp") { // shortest path problem
alpar@400
   146
          is >> i;
alpar@400
   147
          getline(is, str);
alpar@400
   148
          s = nodes[i];
alpar@400
   149
        }
alpar@400
   150
        if (problem == "max") { // max flow problem
alpar@400
   151
          is >> i >> d;
alpar@400
   152
          getline(is, str);
alpar@400
   153
          if (d == 's') s = nodes[i];
alpar@400
   154
          if (d == 't') t = nodes[i];
alpar@400
   155
        }
alpar@400
   156
        break;
alpar@400
   157
      case 'a': // arc (arc) definition line
alpar@400
   158
        if (problem == "max" || problem == "sp") {
alpar@400
   159
          is >> i >> j >> _cap;
alpar@400
   160
          getline(is, str);
alpar@400
   161
          e = g.addArc(nodes[i], nodes[j]);
alpar@400
   162
          capacity.set(e, _cap);
alpar@400
   163
        } else {
alpar@400
   164
          is >> i >> j;
alpar@400
   165
          getline(is, str);
alpar@400
   166
          g.addArc(nodes[i], nodes[j]);
alpar@400
   167
        }
alpar@400
   168
        break;
alpar@400
   169
      }
alpar@400
   170
    }
alpar@400
   171
  }
alpar@400
   172
alpar@400
   173
  /// DIMACS shortest path reader function.
alpar@400
   174
  ///
alpar@400
   175
  /// This function reads a shortest path instance from DIMACS format,
alpar@400
   176
  /// i.e. from DIMACS files having a line starting with
alpar@400
   177
  /// \code
alpar@400
   178
  ///   p sp
alpar@400
   179
  /// \endcode
alpar@400
   180
  /// At the beginning \c g is cleared by \c g.clear(). The arc
alpar@400
   181
  /// capacities are written to \c capacity and \c s is set to the
alpar@400
   182
  /// source node.
alpar@400
   183
  template<typename Digraph, typename CapacityMap>
alpar@400
   184
  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
alpar@400
   185
                  typename Digraph::Node &s) {
alpar@400
   186
    readDimacs(is, g, capacity, s, s);
alpar@400
   187
  }
alpar@400
   188
alpar@400
   189
  /// DIMACS capacitated digraph reader function.
alpar@400
   190
  ///
alpar@400
   191
  /// This function reads an arc capacitated digraph instance from
alpar@400
   192
  /// DIMACS format. At the beginning \c g is cleared by \c g.clear()
alpar@400
   193
  /// and the arc capacities are written to \c capacity.
alpar@400
   194
  template<typename Digraph, typename CapacityMap>
alpar@400
   195
  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity) {
alpar@400
   196
    typename Digraph::Node u;
alpar@400
   197
    readDimacs(is, g, capacity, u, u);
alpar@400
   198
  }
alpar@400
   199
alpar@400
   200
  /// DIMACS plain digraph reader function.
alpar@400
   201
  ///
alpar@400
   202
  /// This function reads a digraph without any designated nodes and
alpar@400
   203
  /// maps from DIMACS format, i.e. from DIMACS files having a line
alpar@400
   204
  /// starting with
alpar@400
   205
  /// \code
alpar@400
   206
  ///   p mat
alpar@400
   207
  /// \endcode
alpar@400
   208
  /// At the beginning \c g is cleared by \c g.clear().
alpar@400
   209
  template<typename Digraph>
alpar@400
   210
  void readDimacs(std::istream& is, Digraph &g) {
alpar@400
   211
    typename Digraph::Node u;
alpar@400
   212
    NullMap<typename Digraph::Arc, int> n;
alpar@400
   213
    readDimacs(is, g, n, u, u);
alpar@400
   214
  }
alpar@400
   215
alpar@400
   216
  /// DIMACS plain digraph writer function.
alpar@400
   217
  ///
alpar@400
   218
  /// This function writes a digraph without any designated nodes and
alpar@400
   219
  /// maps into DIMACS format, i.e. into DIMACS file having a line
alpar@400
   220
  /// starting with
alpar@400
   221
  /// \code
alpar@400
   222
  ///   p mat
alpar@400
   223
  /// \endcode
alpar@400
   224
  template<typename Digraph>
alpar@400
   225
  void writeDimacs(std::ostream& os, const Digraph &g) {
alpar@400
   226
    typedef typename Digraph::NodeIt NodeIt;
alpar@400
   227
    typedef typename Digraph::ArcIt ArcIt;
alpar@400
   228
alpar@400
   229
    os << "c matching problem" << std::endl;
alpar@400
   230
    os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
alpar@400
   231
alpar@400
   232
    typename Digraph::template NodeMap<int> nodes(g);
alpar@400
   233
    int i = 1;
alpar@400
   234
    for(NodeIt v(g); v != INVALID; ++v) {
alpar@400
   235
      nodes.set(v, i);
alpar@400
   236
      ++i;
alpar@400
   237
    }
alpar@400
   238
    for(ArcIt e(g); e != INVALID; ++e) {
alpar@400
   239
      os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]
alpar@400
   240
         << std::endl;
alpar@400
   241
    }
alpar@400
   242
  }
alpar@400
   243
alpar@400
   244
  /// @}
alpar@400
   245
alpar@400
   246
} //namespace lemon
alpar@400
   247
alpar@400
   248
#endif //LEMON_DIMACS_H