1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/dimacs-to-lgf.cc Sun Nov 30 09:39:34 2008 +0000
1.3 @@ -0,0 +1,149 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2008
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +///\ingroup tools
1.23 +///\file
1.24 +///\brief DIMACS to LGF converter.
1.25 +///
1.26 +/// This program converts various DIMACS formats to the LEMON Digraph Format
1.27 +/// (LGF).
1.28 +///
1.29 +/// See
1.30 +/// \verbatim
1.31 +/// dimacs-to-lgf --help
1.32 +/// \endverbatim
1.33 +/// for more info on usage.
1.34 +///
1.35 +
1.36 +#include <iostream>
1.37 +#include <fstream>
1.38 +#include <cstring>
1.39 +
1.40 +#include <lemon/smart_graph.h>
1.41 +#include <lemon/dimacs.h>
1.42 +#include <lemon/lgf_writer.h>
1.43 +
1.44 +#include <lemon/arg_parser.h>
1.45 +#include <lemon/error.h>
1.46 +
1.47 +using namespace std;
1.48 +using namespace lemon;
1.49 +
1.50 +
1.51 +int main(int argc, const char *argv[]) {
1.52 + typedef SmartDigraph Digraph;
1.53 +
1.54 + typedef Digraph::Arc Arc;
1.55 + typedef Digraph::Node Node;
1.56 + typedef Digraph::ArcIt ArcIt;
1.57 + typedef Digraph::NodeIt NodeIt;
1.58 + typedef Digraph::ArcMap<double> DoubleArcMap;
1.59 + typedef Digraph::NodeMap<double> DoubleNodeMap;
1.60 +
1.61 + std::string inputName;
1.62 + std::string outputName;
1.63 +
1.64 + ArgParser ap(argc, argv);
1.65 + ap.other("[INFILE [OUTFILE]]",
1.66 + "If either the INFILE or OUTFILE file is missing the standard\n"
1.67 + " input/output will be used instead.")
1.68 + .run();
1.69 +
1.70 + ifstream input;
1.71 + ofstream output;
1.72 +
1.73 + switch(ap.files().size())
1.74 + {
1.75 + case 2:
1.76 + output.open(ap.files()[1].c_str());
1.77 + if (!output) {
1.78 + throw IoError("Cannot open the file for writing", ap.files()[1]);
1.79 + }
1.80 + case 1:
1.81 + input.open(ap.files()[0].c_str());
1.82 + if (!input) {
1.83 + throw IoError("File cannot be found", ap.files()[0]);
1.84 + }
1.85 + case 0:
1.86 + break;
1.87 + default:
1.88 + cerr << ap.commandName() << ": too many arguments\n";
1.89 + return 1;
1.90 + }
1.91 + istream& is = (ap.files().size()<1 ? cin : input);
1.92 + ostream& os = (ap.files().size()<2 ? cout : output);
1.93 +
1.94 + DimacsDescriptor desc = dimacsType(is);
1.95 + switch(desc.type)
1.96 + {
1.97 + case DimacsDescriptor::MIN:
1.98 + {
1.99 + Digraph digraph;
1.100 + DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
1.101 + DoubleNodeMap supply(digraph);
1.102 + readDimacsMin(is, digraph, lower, capacity, cost, supply, desc);
1.103 + DigraphWriter<Digraph>(digraph, os).
1.104 + nodeMap("supply", supply).
1.105 + arcMap("lower", lower).
1.106 + arcMap("capacity", capacity).
1.107 + arcMap("cost", cost).
1.108 + attribute("problem","min").
1.109 + run();
1.110 + }
1.111 + break;
1.112 + case DimacsDescriptor::MAX:
1.113 + {
1.114 + Digraph digraph;
1.115 + Node s, t;
1.116 + DoubleArcMap capacity(digraph);
1.117 + readDimacsMax(is, digraph, capacity, s, t, desc);
1.118 + DigraphWriter<Digraph>(digraph, os).
1.119 + arcMap("capacity", capacity).
1.120 + node("source", s).
1.121 + node("target", t).
1.122 + attribute("problem","max").
1.123 + run();
1.124 + }
1.125 + break;
1.126 + case DimacsDescriptor::SP:
1.127 + {
1.128 + Digraph digraph;
1.129 + Node s;
1.130 + DoubleArcMap capacity(digraph);
1.131 + readDimacsSp(is, digraph, capacity, s, desc);
1.132 + DigraphWriter<Digraph>(digraph, os).
1.133 + arcMap("capacity", capacity).
1.134 + node("source", s).
1.135 + attribute("problem","sp").
1.136 + run();
1.137 + }
1.138 + break;
1.139 + case DimacsDescriptor::MAT:
1.140 + {
1.141 + Digraph digraph;
1.142 + readDimacsMat(is, digraph,desc);
1.143 + DigraphWriter<Digraph>(digraph, os).
1.144 + attribute("problem","mat").
1.145 + run();
1.146 + }
1.147 + break;
1.148 + default:
1.149 + break;
1.150 + }
1.151 + return 0;
1.152 +}