COIN-OR::LEMON - Graph Library

Ticket #167: c052e6718a95.patch

File c052e6718a95.patch, 14.0 KB (added by Alpar Juttner, 16 years ago)

Rename the DIMACS reader functions

  • .hgignore

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1227619276 0
    # Node ID c052e6718a958fb724f783c90f55f551973204f7
    # Parent  fc6954b4fce49fa09a571eb41b2d16ab7c463569
    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
    
    diff --git a/.hgignore b/.hgignore
    a b  
    3636^build-aux/.*
    3737^objs.*/.*
    3838^test/[a-z_]*$
     39^tools/[a-z-_]*$
    3940^demo/.*_demo$
    4041^build/.*
    4142^doc/gen-images/.*
  • lemon/Makefile.am

    diff --git a/lemon/Makefile.am b/lemon/Makefile.am
    a b  
    2727        lemon/dfs.h \
    2828        lemon/dijkstra.h \
    2929        lemon/dim2.h \
     30        lemon/dimacs.h \
    3031        lemon/elevator.h \
    3132        lemon/error.h \
    3233        lemon/full_graph.h \
  • new file lemon/dimacs.h

    diff --git a/lemon/dimacs.h b/lemon/dimacs.h
    new file mode 100644
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2008
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19#ifndef LEMON_DIMACS_H
     20#define LEMON_DIMACS_H
     21
     22#include <iostream>
     23#include <string>
     24#include <vector>
     25#include <lemon/maps.h>
     26
     27/// \ingroup dimacs_group
     28/// \file
     29/// \brief DIMACS file format reader.
     30
     31namespace lemon {
     32
     33  ///@defgroup dimacs_group DIMACS format
     34  ///\brief Read and write files in DIMACS format
     35  ///
     36  ///Tools to read a digraph from or write it to a file in DIMACS format
     37  ///data
     38  ///\ingroup io_group
     39
     40  /// \addtogroup dimacs_group
     41  /// @{
     42
     43  /// DIMACS min cost flow reader function.
     44  ///
     45  /// This function reads a min cost flow instance from DIMACS format,
     46  /// i.e. from DIMACS files having a line starting with
     47  /// \code
     48  ///   p min
     49  /// \endcode
     50  /// At the beginning \c g is cleared by \c g.clear(). The supply
     51  /// amount of the nodes are written to \c supply (signed). The
     52  /// lower bounds, capacities and costs of the arcs are written to
     53  /// \c lower, \c capacity and \c cost.
     54  ///
     55  /// \author Marton Makai and Peter Kovacs
     56  template <typename Digraph, typename LowerMap,
     57    typename CapacityMap, typename CostMap,
     58    typename SupplyMap>
     59  void readDimacs( std::istream& is,
     60                   Digraph &g,
     61                   LowerMap& lower,
     62                   CapacityMap& capacity,
     63                   CostMap& cost,
     64                   SupplyMap& supply )
     65  {
     66    g.clear();
     67    std::vector<typename Digraph::Node> nodes;
     68    typename Digraph::Arc e;
     69    std::string problem, str;
     70    char c;
     71    int n, m;
     72    int i, j;
     73    typename SupplyMap::Value sup;
     74    typename CapacityMap::Value low;
     75    typename CapacityMap::Value cap;
     76    typename CostMap::Value co;
     77    while (is >> c) {
     78      switch (c) {
     79      case 'c': // comment line
     80        getline(is, str);
     81        break;
     82      case 'p': // problem definition line
     83        is >> problem >> n >> m;
     84        getline(is, str);
     85        if (problem != "min") return;
     86        nodes.resize(n + 1);
     87        for (int k = 1; k <= n; ++k) {
     88          nodes[k] = g.addNode();
     89          supply.set(nodes[k], 0);
     90        }
     91        break;
     92      case 'n': // node definition line
     93        is >> i >> sup;
     94        getline(is, str);
     95        supply.set(nodes[i], sup);
     96        break;
     97      case 'a': // arc (arc) definition line
     98        is >> i >> j >> low >> cap >> co;
     99        getline(is, str);
     100        e = g.addArc(nodes[i], nodes[j]);
     101        lower.set(e, low);
     102        if (cap >= 0)
     103          capacity.set(e, cap);
     104        else
     105          capacity.set(e, -1);
     106        cost.set(e, co);
     107        break;
     108      }
     109    }
     110  }
     111
     112  /// DIMACS max flow reader function.
     113  ///
     114  /// This function reads a max flow instance from DIMACS format,
     115  /// i.e. from DIMACS files having a line starting with
     116  /// \code
     117  ///   p max
     118  /// \endcode
     119  /// At the beginning \c g is cleared by \c g.clear(). The arc
     120  /// capacities are written to \c capacity and \c s and \c t are
     121  /// set to the source and the target nodes.
     122  ///
     123  /// \author Marton Makai
     124  template<typename Digraph, typename CapacityMap>
     125  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
     126                  typename Digraph::Node &s, typename Digraph::Node &t) {
     127    g.clear();
     128    std::vector<typename Digraph::Node> nodes;
     129    typename Digraph::Arc e;
     130    std::string problem;
     131    char c, d;
     132    int n, m;
     133    int i, j;
     134    typename CapacityMap::Value _cap;
     135    std::string str;
     136    while (is >> c) {
     137      switch (c) {
     138      case 'c': // comment line
     139        getline(is, str);
     140        break;
     141      case 'p': // problem definition line
     142        is >> problem >> n >> m;
     143        getline(is, str);
     144        nodes.resize(n + 1);
     145        for (int k = 1; k <= n; ++k)
     146          nodes[k] = g.addNode();
     147        break;
     148      case 'n': // node definition line
     149        if (problem == "sp") { // shortest path problem
     150          is >> i;
     151          getline(is, str);
     152          s = nodes[i];
     153        }
     154        if (problem == "max") { // max flow problem
     155          is >> i >> d;
     156          getline(is, str);
     157          if (d == 's') s = nodes[i];
     158          if (d == 't') t = nodes[i];
     159        }
     160        break;
     161      case 'a': // arc (arc) definition line
     162        if (problem == "max" || problem == "sp") {
     163          is >> i >> j >> _cap;
     164          getline(is, str);
     165          e = g.addArc(nodes[i], nodes[j]);
     166          capacity.set(e, _cap);
     167        } else {
     168          is >> i >> j;
     169          getline(is, str);
     170          g.addArc(nodes[i], nodes[j]);
     171        }
     172        break;
     173      }
     174    }
     175  }
     176
     177  /// DIMACS shortest path reader function.
     178  ///
     179  /// This function reads a shortest path instance from DIMACS format,
     180  /// i.e. from DIMACS files having a line starting with
     181  /// \code
     182  ///   p sp
     183  /// \endcode
     184  /// At the beginning \c g is cleared by \c g.clear(). The arc
     185  /// capacities are written to \c capacity and \c s is set to the
     186  /// source node.
     187  ///
     188  /// \author Marton Makai
     189  template<typename Digraph, typename CapacityMap>
     190  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
     191                  typename Digraph::Node &s) {
     192    readDimacs(is, g, capacity, s, s);
     193  }
     194
     195  /// DIMACS capacitated digraph reader function.
     196  ///
     197  /// This function reads an arc capacitated digraph instance from
     198  /// DIMACS format. At the beginning \c g is cleared by \c g.clear()
     199  /// and the arc capacities are written to \c capacity.
     200  ///
     201  /// \author Marton Makai
     202  template<typename Digraph, typename CapacityMap>
     203  void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity) {
     204    typename Digraph::Node u;
     205    readDimacs(is, g, capacity, u, u);
     206  }
     207
     208  /// DIMACS plain digraph reader function.
     209  ///
     210  /// This function reads a digraph without any designated nodes and
     211  /// maps from DIMACS format, i.e. from DIMACS files having a line
     212  /// starting with
     213  /// \code
     214  ///   p mat
     215  /// \endcode
     216  /// At the beginning \c g is cleared by \c g.clear().
     217  ///
     218  /// \author Marton Makai
     219  template<typename Digraph>
     220  void readDimacs(std::istream& is, Digraph &g) {
     221    typename Digraph::Node u;
     222    NullMap<typename Digraph::Arc, int> n;
     223    readDimacs(is, g, n, u, u);
     224  }
     225
     226  /// DIMACS plain digraph writer function.
     227  ///
     228  /// This function writes a digraph without any designated nodes and
     229  /// maps into DIMACS format, i.e. into DIMACS file having a line
     230  /// starting with
     231  /// \code
     232  ///   p mat
     233  /// \endcode
     234  ///
     235  /// \author Marton Makai
     236  template<typename Digraph>
     237  void writeDimacs(std::ostream& os, const Digraph &g) {
     238    typedef typename Digraph::NodeIt NodeIt;
     239    typedef typename Digraph::ArcIt ArcIt;
     240
     241    os << "c matching problem" << std::endl;
     242    os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
     243
     244    typename Digraph::template NodeMap<int> nodes(g);
     245    int i = 1;
     246    for(NodeIt v(g); v != INVALID; ++v) {
     247      nodes.set(v, i);
     248      ++i;
     249    }
     250    for(ArcIt e(g); e != INVALID; ++e) {
     251      os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]
     252         << std::endl;
     253    }
     254  }
     255
     256  /// @}
     257
     258} //namespace lemon
     259
     260#endif //LEMON_DIMACS_H
  • tools/Makefile.am

    diff --git a/tools/Makefile.am b/tools/Makefile.am
    a b  
    11if WANT_TOOLS
    22
    3 bin_PROGRAMS +=
     3bin_PROGRAMS += \
     4        tools/dimacs-to-lgf
     5
    46dist_bin_SCRIPTS += tools/lemon-0.x-to-1.x.sh
    57
    68endif WANT_TOOLS
     9
     10tools_dimacs_to_lgf_SOURCES = tools/dimacs-to-lgf.cc
  • new file tools/dimacs-to-lgf.cc

    diff --git a/tools/dimacs-to-lgf.cc b/tools/dimacs-to-lgf.cc
    new file mode 100644
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2008
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19///\ingroup tools
     20///\file
     21///\brief DIMACS to LGF converter.
     22///
     23/// This program converts various DIMACS formats to the LEMON Digraph Format
     24/// (LGF).
     25///
     26/// See
     27/// \verbatim
     28///  dimacs-to-lgf --help
     29/// \endverbatim
     30/// for more info on usage.
     31///
     32
     33#include <iostream>
     34#include <fstream>
     35#include <cstring>
     36
     37#include <lemon/smart_graph.h>
     38#include <lemon/dimacs.h>
     39#include <lemon/lgf_writer.h>
     40
     41#include <lemon/arg_parser.h>
     42
     43using namespace std;
     44using namespace lemon;
     45
     46
     47int main(int argc, const char *argv[]) {
     48  typedef SmartDigraph Digraph;
     49
     50  typedef Digraph::Arc Arc;
     51  typedef Digraph::Node Node;
     52  typedef Digraph::ArcIt ArcIt;
     53  typedef Digraph::NodeIt NodeIt;
     54  typedef Digraph::ArcMap<double> DoubleArcMap;
     55  typedef Digraph::NodeMap<double> DoubleNodeMap;
     56
     57  std::string inputName;
     58  std::string outputName;
     59  std::string typeName;
     60
     61  bool mincostflow;
     62  bool maxflow;
     63  bool shortestpath;
     64  bool capacitated;
     65  bool plain;
     66
     67  bool version;
     68
     69  ArgParser ap(argc, argv);
     70  ap.refOption("-input",
     71               "use FILE as input instead of standard input",
     72               inputName).synonym("i", "-input")
     73    .refOption("-output",
     74               "use FILE as output instead of standard output",
     75               outputName).synonym("o", "-output")
     76    .refOption("-mincostflow",
     77               "set the type of the digraph to \"mincostflow\" digraph",
     78               mincostflow)
     79    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
     80    .refOption("-maxflow",
     81               "set the type of the digraph to \"maxflow\" digraph",
     82               maxflow)
     83    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
     84    .refOption("-shortestpath",
     85               "set the type of the digraph to \"shortestpath\" digraph",
     86               shortestpath)
     87    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
     88    .refOption("-capacitated",
     89               "set the type of the digraph to \"capacitated\" digraph",
     90               capacitated)
     91    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
     92    .refOption("-plain",
     93               "set the type of the digraph to \"plain\" digraph",
     94               plain)
     95    .optionGroup("type", "-plain").synonym("pl", "-plain")
     96    .onlyOneGroup("type")
     97    .mandatoryGroup("type")
     98    .refOption("-version", "show version information", version)
     99    .synonym("v", "-version")
     100    .run();
     101
     102  ifstream input;
     103  if (!inputName.empty()) {
     104    input.open(inputName.c_str());
     105    if (!input) {
     106      cerr << "File open error" << endl;
     107      return -1;
     108    }
     109  }
     110  istream& is = (inputName.empty() ? cin : input);
     111
     112  ofstream output;
     113  if (!outputName.empty()) {
     114    output.open(outputName.c_str());
     115    if (!output) {
     116      cerr << "File open error" << endl;
     117      return -1;
     118    }
     119  }
     120  ostream& os = (outputName.empty() ? cout : output);
     121
     122  if (mincostflow) {
     123    Digraph digraph;
     124    DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
     125    DoubleNodeMap supply(digraph);
     126    readDimacs(is, digraph, lower, capacity, cost, supply);
     127    DigraphWriter<Digraph>(digraph, os).
     128      nodeMap("supply", supply).
     129      arcMap("lower", lower).
     130      arcMap("capacity", capacity).
     131      arcMap("cost", cost).
     132      run();
     133  } else if (maxflow) {
     134    Digraph digraph;
     135    Node s, t;
     136    DoubleArcMap capacity(digraph);
     137    readDimacs(is, digraph, capacity, s, t);
     138    DigraphWriter<Digraph>(digraph, os).
     139      arcMap("capacity", capacity).
     140      node("source", s).
     141      node("target", t).
     142      run();
     143  } else if (shortestpath) {
     144    Digraph digraph;
     145    Node s;
     146    DoubleArcMap capacity(digraph);
     147    readDimacs(is, digraph, capacity, s);
     148    DigraphWriter<Digraph>(digraph, os).
     149      arcMap("capacity", capacity).
     150      node("source", s).
     151      run();
     152  } else if (capacitated) {
     153    Digraph digraph;
     154    DoubleArcMap capacity(digraph);
     155    readDimacs(is, digraph, capacity);
     156    DigraphWriter<Digraph>(digraph, os).
     157      arcMap("capacity", capacity).
     158      run();
     159  } else if (plain) {
     160    Digraph digraph;
     161    readDimacs(is, digraph);
     162    DigraphWriter<Digraph>(digraph, os).run();
     163  } else {
     164    cerr << "Invalid type error" << endl;
     165    return -1;
     166  }
     167  return 0;
     168}