COIN-OR::LEMON - Graph Library

Changeset 387:24a2c6ee6cb0 in lemon-1.2 for tools/dimacs-to-lgf.cc


Ignore:
Timestamp:
11/28/08 07:38:20 (16 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Phase:
public
Message:

Refactoring of DIMACS tools

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/dimacs-to-lgf.cc

    r386 r387  
    4040
    4141#include <lemon/arg_parser.h>
     42#include <lemon/error.h>
    4243
    4344using namespace std;
     
    5758  std::string inputName;
    5859  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;
    6860
    6961  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")
     62  ap.other("[INFILE [OUTFILE]]",
     63           "If either the INFILE or OUTFILE file is missing the standard\n"
     64           "     input/output will be used instead.")
    10065    .run();
    10166
    10267  ifstream input;
    103   if (!inputName.empty()) {
    104     input.open(inputName.c_str());
    105     if (!input) {
    106       cerr << "File open error" << endl;
    107       return -1;
     68  ofstream output;
     69
     70  switch(ap.files().size())
     71    {
     72    case 2:
     73      output.open(ap.files()[1].c_str());
     74      if (!output) {
     75        throw IoError("Cannot open the file for writing", ap.files()[1]);
     76      }
     77    case 1:
     78      input.open(ap.files()[0].c_str());
     79      if (!input) {
     80        throw IoError("File cannot be found", ap.files()[0]);
     81      }
     82    case 0:
     83      break;
     84    default:
     85      cerr << ap.commandName() << ": too many arguments\n";
     86      return 1;
     87  }
     88  istream& is = (ap.files().size()<1 ? cin : input);
     89  ostream& os = (ap.files().size()<2 ? cout : output);
     90
     91  DimacsDescriptor desc = dimacsType(is);
     92  switch(desc.type)
     93    {
     94    case DimacsDescriptor::MIN:
     95      {
     96        Digraph digraph;
     97        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
     98        DoubleNodeMap supply(digraph);
     99        readDimacsMin(is, digraph, lower, capacity, cost, supply, desc);
     100        DigraphWriter<Digraph>(digraph, os).
     101          nodeMap("supply", supply).
     102          arcMap("lower", lower).
     103          arcMap("capacity", capacity).
     104          arcMap("cost", cost).
     105          attribute("problem","min").
     106          run();
     107      }
     108      break;
     109    case DimacsDescriptor::MAX:
     110      {
     111        Digraph digraph;
     112        Node s, t;
     113        DoubleArcMap capacity(digraph);
     114        readDimacsMax(is, digraph, capacity, s, t, desc);
     115        DigraphWriter<Digraph>(digraph, os).
     116          arcMap("capacity", capacity).
     117          node("source", s).
     118          node("target", t).
     119          attribute("problem","max").
     120          run();
     121      }
     122      break;
     123    case DimacsDescriptor::SP:
     124      {
     125        Digraph digraph;
     126        Node s;
     127        DoubleArcMap capacity(digraph);
     128        readDimacsSp(is, digraph, capacity, s, desc);
     129        DigraphWriter<Digraph>(digraph, os).
     130          arcMap("capacity", capacity).
     131          node("source", s).
     132          attribute("problem","sp").
     133          run();
     134      }
     135      break;
     136    case DimacsDescriptor::MAT:
     137      {
     138        Digraph digraph;
     139        readDimacsMat(is, digraph,desc);
     140        DigraphWriter<Digraph>(digraph, os).
     141          attribute("problem","mat").
     142          run();
     143      }
     144      break;
     145    default:
     146      break;
    108147    }
    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     readDimacsMin(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     readDimacsMax(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     readDimacsSp(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     readDimacsMax(is, digraph, capacity);
    156     DigraphWriter<Digraph>(digraph, os).
    157       arcMap("capacity", capacity).
    158       run();
    159   } else if (plain) {
    160     Digraph digraph;
    161     readDimacsMat(is, digraph);
    162     DigraphWriter<Digraph>(digraph, os).run();
    163   } else {
    164     cerr << "Invalid type error" << endl;
    165     return -1;
    166   }
    167148  return 0;
    168149}
Note: See TracChangeset for help on using the changeset viewer.