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 alpar@400: /// \verbatim alpar@573: /// dimacs-solver --help alpar@400: /// \endverbatim alpar@400: /// for more info on usage. alpar@400: /// alpar@400: alpar@400: #include alpar@400: #include alpar@400: #include alpar@400: alpar@400: #include alpar@400: #include alpar@400: #include alpar@573: #include alpar@400: alpar@400: #include alpar@402: #include alpar@400: alpar@573: #include alpar@573: #include alpar@573: #include alpar@573: alpar@400: using namespace lemon; alpar@573: typedef SmartDigraph Digraph; alpar@573: DIGRAPH_TYPEDEFS(Digraph); alpar@573: typedef SmartGraph Graph; alpar@400: alpar@573: template alpar@573: void solve_sp(ArgParser &ap, std::istream &is, std::ostream &, alpar@573: DimacsDescriptor &desc) alpar@573: { alpar@573: bool report = !ap.given("q"); alpar@573: Digraph g; alpar@573: Node s; alpar@573: Digraph::ArcMap len(g); alpar@573: Timer t; alpar@573: t.restart(); alpar@573: readDimacsSp(is, g, len, s, desc); alpar@573: if(report) std::cerr << "Read the file: " << t << '\n'; alpar@573: t.restart(); alpar@573: Dijkstra > dij(g,len); alpar@573: if(report) std::cerr << "Setup Dijkstra class: " << t << '\n'; alpar@573: t.restart(); alpar@573: dij.run(s); alpar@573: if(report) std::cerr << "Run Dijkstra: " << t << '\n'; alpar@573: } alpar@573: alpar@573: template alpar@573: void solve_max(ArgParser &ap, std::istream &is, std::ostream &, alpar@573: DimacsDescriptor &desc) alpar@573: { alpar@573: bool report = !ap.given("q"); alpar@573: Digraph g; alpar@573: Node s,t; alpar@573: Digraph::ArcMap cap(g); alpar@573: Timer ti; alpar@573: ti.restart(); alpar@573: readDimacsMax(is, g, cap, s, t, desc); alpar@573: if(report) std::cerr << "Read the file: " << ti << '\n'; alpar@573: ti.restart(); alpar@573: Preflow > pre(g,cap,s,t); alpar@573: if(report) std::cerr << "Setup Preflow class: " << ti << '\n'; alpar@573: ti.restart(); alpar@573: pre.run(); alpar@573: if(report) std::cerr << "Run Preflow: " << ti << '\n'; alpar@573: if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n'; alpar@573: } alpar@573: alpar@573: void solve_mat(ArgParser &ap, std::istream &is, std::ostream &, alpar@573: DimacsDescriptor &desc) alpar@573: { alpar@573: bool report = !ap.given("q"); alpar@573: Graph g; alpar@573: Timer ti; alpar@573: ti.restart(); alpar@573: readDimacsMat(is, g, desc); alpar@573: if(report) std::cerr << "Read the file: " << ti << '\n'; alpar@573: ti.restart(); alpar@573: MaxMatching mat(g); alpar@573: if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n'; alpar@573: ti.restart(); alpar@573: mat.run(); alpar@573: if(report) std::cerr << "Run MaxMatching: " << ti << '\n'; alpar@573: if(report) std::cerr << "\nCardinality of max matching: " alpar@573: << mat.matchingSize() << '\n'; alpar@573: } alpar@573: alpar@573: alpar@573: template alpar@573: void solve(ArgParser &ap, std::istream &is, std::ostream &os, alpar@573: DimacsDescriptor &desc) alpar@573: { alpar@573: switch(desc.type) alpar@573: { alpar@573: case DimacsDescriptor::MIN: alpar@573: std::cerr << alpar@573: "\n\n Sorry, the min. cost flow solver is not yet available.\n" alpar@573: << std::endl; alpar@573: break; alpar@573: case DimacsDescriptor::MAX: alpar@573: solve_max(ap,is,os,desc); alpar@573: break; alpar@573: case DimacsDescriptor::SP: alpar@573: solve_sp(ap,is,os,desc); alpar@573: break; alpar@573: case DimacsDescriptor::MAT: alpar@573: solve_mat(ap,is,os,desc); alpar@573: break; alpar@573: default: alpar@573: break; alpar@573: } alpar@573: } 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@573: .boolOption("q", "Do not print any report") alpar@573: .boolOption("int","Use 'int' for capacities, costs etc. (default)") alpar@573: .optionGroup("datatype","int") alpar@573: #ifdef HAVE_LONG_LONG alpar@573: .boolOption("long","Use 'long long' for capacities, costs etc.") alpar@573: .optionGroup("datatype","long") alpar@573: #endif alpar@573: .boolOption("double","Use 'double' for capacities, costs etc.") alpar@573: .optionGroup("datatype","double") alpar@573: .boolOption("ldouble","Use 'long double' for capacities, costs etc.") alpar@573: .optionGroup("datatype","ldouble") alpar@573: .onlyOneGroup("datatype") alpar@400: .run(); alpar@400: alpar@573: std::ifstream input; alpar@573: std::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@573: std::cerr << ap.commandName() << ": too many arguments\n"; alpar@402: return 1; alpar@402: } alpar@573: std::istream& is = (ap.files().size()<1 ? std::cin : input); alpar@573: std::ostream& os = (ap.files().size()<2 ? std::cout : output); alpar@402: alpar@402: DimacsDescriptor desc = dimacsType(is); alpar@573: alpar@573: if(!ap.given("q")) alpar@402: { alpar@573: std::cout << "Problem type: "; alpar@573: switch(desc.type) alpar@573: { alpar@573: case DimacsDescriptor::MIN: alpar@573: std::cout << "min"; alpar@573: break; alpar@573: case DimacsDescriptor::MAX: alpar@573: std::cout << "max"; alpar@573: break; alpar@573: case DimacsDescriptor::SP: alpar@573: std::cout << "sp"; alpar@573: case DimacsDescriptor::MAT: alpar@573: std::cout << "mat"; alpar@573: break; alpar@573: default: alpar@573: exit(1); alpar@573: break; alpar@573: } alpar@573: std::cout << "\nNum of nodes: " << desc.nodeNum; alpar@573: std::cout << "\nNum of arcs: " << desc.edgeNum; alpar@573: std::cout << '\n' << std::endl; alpar@400: } alpar@573: alpar@573: if(ap.given("double")) alpar@573: solve(ap,is,os,desc); alpar@573: else if(ap.given("ldouble")) alpar@573: solve(ap,is,os,desc); alpar@573: #ifdef HAVE_LONG_LONG alpar@573: else if(ap.given("long")) alpar@573: solve(ap,is,os,desc); alpar@573: #endif alpar@573: else solve(ap,is,os,desc); alpar@573: alpar@400: return 0; alpar@400: }