src/hugo/dimacs.h
author alpar
Thu, 23 Sep 2004 15:09:55 +0000
changeset 907 df8472ab5d4a
parent 903 2e664d4969d7
permissions -rw-r--r--
I forgot to apply
for i in `ls *.h`; do rpl template.h $i $i; done
in src/hugo/attic
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 * src/hugo/dimacs.h - Part of HUGOlib, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
marci@423
    17
#ifndef HUGO_DIMACS_H
marci@423
    18
#define HUGO_DIMACS_H
marci@423
    19
marci@423
    20
#include <iostream>
marci@423
    21
#include <string>
marci@423
    22
#include <vector>
ladanyi@542
    23
#include <hugo/maps.h>
marci@423
    24
jacint@575
    25
/// \ingroup misc
klao@442
    26
/// \file
klao@442
    27
/// \brief Dimacs file format reader.
klao@427
    28
marci@423
    29
namespace hugo {
marci@423
    30
marci@423
    31
jacint@575
    32
  /// \addtogroup misc
jacint@575
    33
  /// @{
jacint@575
    34
jacint@575
    35
  /// Dimacs min cost flow reader function.
jacint@575
    36
jacint@575
    37
  /// This function reads a min cost flow instance from dimacs format,
jacint@575
    38
  /// i.e. from dimacs files having a line starting with \c p \c "min".
jacint@575
    39
  /// At the beginning \c g is cleared by \c g.clear(). The edge
jacint@575
    40
  /// capacities are written to \c capacity, \c s and \c t are set to
jacint@575
    41
  /// the source and the target nodes resp. and the cost of the edges
jacint@575
    42
  /// are written to \c cost.
marci@465
    43
  ///
jacint@575
    44
  /// \author Marton Makai
jacint@528
    45
  template<typename Graph, typename CapacityMap, typename CostMap>
jacint@528
    46
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@528
    47
		  typename Graph::Node &s, typename Graph::Node &t, 
jacint@528
    48
		  CostMap& cost) {
jacint@528
    49
    g.clear();
jacint@528
    50
    typename CapacityMap::ValueType _cap;
jacint@528
    51
    typename CostMap::ValueType _cost;
jacint@528
    52
    char d;
jacint@528
    53
    std::string problem;
jacint@528
    54
    char c;
jacint@528
    55
    int i, j;
jacint@528
    56
    std::string str;
jacint@528
    57
    int n, m; 
jacint@528
    58
    typename Graph::Edge e;
jacint@528
    59
    std::vector<typename Graph::Node> nodes;
jacint@528
    60
    while (is>>c) {
jacint@528
    61
      switch (c) {
jacint@528
    62
      case 'c': //comment
jacint@528
    63
	getline(is, str);
jacint@528
    64
	break;
jacint@528
    65
      case 'p': //problem definition
jacint@528
    66
	is >> problem >> n >> m;
jacint@528
    67
	getline(is, str);
jacint@528
    68
	nodes.resize(n+1);
jacint@528
    69
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
jacint@528
    70
	break;
jacint@528
    71
      case 'n': //node definition
jacint@528
    72
	if (problem=="sp") { //shortest path problem
jacint@528
    73
	  is >> i;
jacint@528
    74
	  getline(is, str);
jacint@528
    75
	  s=nodes[i];
jacint@528
    76
	}
jacint@528
    77
	if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
jacint@528
    78
	  is >> i >> d;
jacint@528
    79
	  getline(is, str);
jacint@528
    80
	  if (d=='s') s=nodes[i];
jacint@528
    81
	  if (d=='t') t=nodes[i];
jacint@528
    82
	}
jacint@528
    83
	break;
jacint@528
    84
      case 'a':
jacint@528
    85
	if ( problem == "max" || problem == "sp") {
jacint@528
    86
	  is >> i >> j >> _cap;
jacint@528
    87
	  getline(is, str);
jacint@528
    88
	  e=g.addEdge(nodes[i], nodes[j]);
marci@784
    89
	  //capacity.update();
jacint@528
    90
	  capacity.set(e, _cap);
jacint@575
    91
	} else {
jacint@575
    92
	  if ( problem == "min" ) {
jacint@575
    93
	    is >> i >> j >> _cap >> _cost;
jacint@575
    94
	    getline(is, str);
jacint@575
    95
	    e=g.addEdge(nodes[i], nodes[j]);
marci@784
    96
	    //capacity.update();
jacint@575
    97
	    capacity.set(e, _cap);
marci@784
    98
	    //cost.update();
jacint@575
    99
	    cost.set(e, _cost);
jacint@575
   100
	  } else {
jacint@575
   101
	    is >> i >> j;
jacint@575
   102
	    getline(is, str);
jacint@575
   103
	    g.addEdge(nodes[i], nodes[j]);
jacint@575
   104
	  }
jacint@528
   105
	}
jacint@528
   106
	break;
jacint@528
   107
      }
jacint@528
   108
    }
jacint@528
   109
  }
jacint@528
   110
jacint@528
   111
jacint@575
   112
  /// Dimacs max flow reader function.
jacint@575
   113
jacint@575
   114
  /// This function reads a max flow instance from dimacs format,
jacint@575
   115
  /// i.e. from dimacs files having a line starting with \c p \c
jacint@575
   116
  /// "max".  At the beginning \c g is cleared by \c g.clear(). The
jacint@575
   117
  /// edge capacities are written to \c capacity and \c s and \c t are
jacint@575
   118
  /// set to the source and the target nodes.
jacint@575
   119
  ///
jacint@575
   120
  /// \author Marton Makai
jacint@575
   121
  template<typename Graph, typename CapacityMap>
jacint@575
   122
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@575
   123
		  typename Graph::Node &s, typename Graph::Node &t) {
jacint@575
   124
    NullMap<typename Graph::Edge, int> n;
jacint@575
   125
    readDimacs(is, g, capacity, s, t, n);
jacint@575
   126
  }
jacint@575
   127
jacint@575
   128
jacint@575
   129
  /// Dimacs shortest path reader function.
jacint@575
   130
jacint@575
   131
  /// This function reads a shortest path instance from dimacs format,
jacint@575
   132
  /// i.e. from dimacs files having a line starting with \c p \c "sp".
jacint@575
   133
  /// At the beginning \c g is cleared by \c g.clear(). The edge
jacint@575
   134
  /// capacities are written to \c capacity and \c s is set to the
jacint@575
   135
  /// source node.
jacint@575
   136
  ///
jacint@575
   137
  /// \author Marton Makai
jacint@575
   138
  template<typename Graph, typename CapacityMap>
jacint@575
   139
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 
jacint@575
   140
		  typename Graph::Node &s) {
jacint@575
   141
    NullMap<typename Graph::Edge, int> n;
jacint@575
   142
    readDimacs(is, g, capacity, s, s, n);
jacint@575
   143
  }
jacint@575
   144
jacint@575
   145
jacint@575
   146
  /// Dimacs capacitated graph reader function.
jacint@575
   147
jacint@575
   148
  /// This function reads an edge capacitated graph instance from
jacint@575
   149
  /// dimacs format.  At the beginning \c g is cleared by \c g.clear()
jacint@575
   150
  /// and the edge capacities are written to \c capacity.
jacint@575
   151
  ///
jacint@575
   152
  /// \author Marton Makai
jacint@575
   153
  template<typename Graph, typename CapacityMap>
jacint@575
   154
  void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
jacint@575
   155
    typename Graph::Node u;
jacint@575
   156
    NullMap<typename Graph::Edge, int> n;
jacint@575
   157
    readDimacs(is, g, capacity, u, u, n);
jacint@575
   158
  }
jacint@575
   159
jacint@575
   160
jacint@575
   161
  /// Dimacs plain graph reader function.
jacint@575
   162
jacint@575
   163
  /// This function reads a graph without any designated nodes and
jacint@575
   164
  /// maps from dimacs format, i.e. from dimacs files having a line
jacint@575
   165
  /// starting with \c p \c "mat".  At the beginning \c g is cleared
jacint@575
   166
  /// by \c g.clear().
jacint@575
   167
  ///
jacint@575
   168
  /// \author Marton Makai
jacint@575
   169
  template<typename Graph>
jacint@575
   170
  void readDimacs(std::istream& is, Graph &g) {
jacint@575
   171
    typename Graph::Node u;
jacint@575
   172
    NullMap<typename Graph::Edge, int> n;
jacint@575
   173
    readDimacs(is, g, n, u, u, n);
jacint@575
   174
  }
jacint@575
   175
  
jacint@575
   176
jacint@575
   177
marci@423
   178
  
jacint@528
   179
  /// write matching problem
jacint@528
   180
  template<typename Graph>
jacint@528
   181
  void writeDimacs(std::ostream& os, const Graph &g) {
jacint@528
   182
    typedef typename Graph::NodeIt NodeIt;
jacint@528
   183
    typedef typename Graph::EdgeIt EdgeIt;  
jacint@528
   184
    
jacint@528
   185
    typename Graph::template NodeMap<int> nodes(g);
jacint@528
   186
jacint@528
   187
    os << "c matching problem" << std::endl;
jacint@528
   188
jacint@528
   189
    int i=1;
marci@778
   190
    for(NodeIt v(g); v!=INVALID; ++v) {
jacint@528
   191
      nodes.set(v, i);
jacint@528
   192
      ++i;
jacint@528
   193
    }    
jacint@528
   194
    
jacint@528
   195
    os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
jacint@528
   196
marci@778
   197
    for(EdgeIt e(g); e!=INVALID; ++e) {
jacint@528
   198
      os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl; 
jacint@528
   199
    }
jacint@528
   200
jacint@528
   201
  }
jacint@528
   202
jacint@575
   203
  /// @}
jacint@528
   204
marci@423
   205
} //namespace hugo
marci@423
   206
marci@423
   207
#endif //HUGO_DIMACS_H