alpar@400: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@400: * alpar@400: * This file is a part of LEMON, a generic C++ optimization library. alpar@400: * alpar@463: * Copyright (C) 2003-2009 alpar@400: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@400: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@400: * alpar@400: * Permission to use, modify and distribute this software is granted alpar@400: * provided that this copyright notice appears in all copies. For alpar@400: * precise terms see the accompanying LICENSE file. alpar@400: * alpar@400: * This software is provided "AS IS" with no warranty of any kind, alpar@400: * express or implied, and with no claim as to its suitability for any alpar@400: * purpose. alpar@400: * alpar@400: */ alpar@400: alpar@400: ///\ingroup tools alpar@400: ///\file alpar@400: ///\brief DIMACS to LGF converter. alpar@400: /// alpar@400: /// This program converts various DIMACS formats to the LEMON Digraph Format alpar@400: /// (LGF). alpar@400: /// alpar@400: /// See kpeter@631: /// \code kpeter@631: /// dimacs-to-lgf --help kpeter@631: /// \endcode kpeter@631: /// for more info on the usage. alpar@400: alpar@400: #include alpar@400: #include alpar@400: #include alpar@400: alpar@400: #include alpar@400: #include alpar@400: #include alpar@400: alpar@400: #include alpar@402: #include alpar@400: alpar@400: using namespace std; alpar@400: using namespace lemon; alpar@400: alpar@400: alpar@400: int main(int argc, const char *argv[]) { alpar@400: typedef SmartDigraph Digraph; alpar@400: alpar@400: typedef Digraph::Arc Arc; alpar@400: typedef Digraph::Node Node; alpar@400: typedef Digraph::ArcIt ArcIt; alpar@400: typedef Digraph::NodeIt NodeIt; alpar@400: typedef Digraph::ArcMap DoubleArcMap; alpar@400: typedef Digraph::NodeMap DoubleNodeMap; alpar@400: alpar@400: std::string inputName; alpar@400: std::string outputName; alpar@400: alpar@400: ArgParser ap(argc, argv); alpar@402: ap.other("[INFILE [OUTFILE]]", alpar@402: "If either the INFILE or OUTFILE file is missing the standard\n" alpar@402: " input/output will be used instead.") alpar@400: .run(); alpar@400: alpar@400: ifstream input; alpar@402: ofstream output; alpar@402: alpar@402: switch(ap.files().size()) alpar@402: { alpar@402: case 2: alpar@402: output.open(ap.files()[1].c_str()); alpar@402: if (!output) { alpar@402: throw IoError("Cannot open the file for writing", ap.files()[1]); alpar@402: } alpar@402: case 1: alpar@402: input.open(ap.files()[0].c_str()); alpar@402: if (!input) { alpar@402: throw IoError("File cannot be found", ap.files()[0]); alpar@402: } alpar@402: case 0: alpar@402: break; alpar@402: default: alpar@402: cerr << ap.commandName() << ": too many arguments\n"; alpar@402: return 1; alpar@402: } alpar@402: istream& is = (ap.files().size()<1 ? cin : input); alpar@402: ostream& os = (ap.files().size()<2 ? cout : output); alpar@402: alpar@402: DimacsDescriptor desc = dimacsType(is); alpar@402: switch(desc.type) alpar@402: { alpar@402: case DimacsDescriptor::MIN: alpar@402: { alpar@402: Digraph digraph; alpar@402: DoubleArcMap lower(digraph), capacity(digraph), cost(digraph); alpar@402: DoubleNodeMap supply(digraph); alpar@608: readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc); alpar@402: DigraphWriter(digraph, os). alpar@402: nodeMap("supply", supply). alpar@402: arcMap("lower", lower). alpar@402: arcMap("capacity", capacity). alpar@402: arcMap("cost", cost). alpar@402: attribute("problem","min"). alpar@402: run(); alpar@402: } alpar@402: break; alpar@402: case DimacsDescriptor::MAX: alpar@402: { alpar@402: Digraph digraph; alpar@402: Node s, t; alpar@402: DoubleArcMap capacity(digraph); alpar@608: readDimacsMax(is, digraph, capacity, s, t, 0, desc); alpar@402: DigraphWriter(digraph, os). alpar@402: arcMap("capacity", capacity). alpar@402: node("source", s). alpar@402: node("target", t). alpar@402: attribute("problem","max"). alpar@402: run(); alpar@402: } alpar@402: break; alpar@402: case DimacsDescriptor::SP: alpar@402: { alpar@402: Digraph digraph; alpar@402: Node s; alpar@402: DoubleArcMap capacity(digraph); alpar@402: readDimacsSp(is, digraph, capacity, s, desc); alpar@402: DigraphWriter(digraph, os). alpar@402: arcMap("capacity", capacity). alpar@402: node("source", s). alpar@402: attribute("problem","sp"). alpar@402: run(); alpar@402: } alpar@402: break; alpar@402: case DimacsDescriptor::MAT: alpar@402: { alpar@402: Digraph digraph; alpar@402: readDimacsMat(is, digraph,desc); alpar@402: DigraphWriter(digraph, os). alpar@402: attribute("problem","mat"). alpar@402: run(); alpar@402: } alpar@402: break; alpar@402: default: alpar@402: break; alpar@400: } alpar@400: return 0; alpar@400: }