ladanyi@1626: /* -*- C++ -*- ladanyi@1626: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport ladanyi@1626: * (Egervary Research Group on Combinatorial Optimization, EGRES). ladanyi@1626: * ladanyi@1626: * Permission to use, modify and distribute this software is granted ladanyi@1626: * provided that this copyright notice appears in all copies. For ladanyi@1626: * precise terms see the accompanying LICENSE file. ladanyi@1626: * ladanyi@1626: * This software is provided "AS IS" with no warranty of any kind, ladanyi@1626: * express or implied, and with no claim as to its suitability for any ladanyi@1626: * purpose. ladanyi@1626: * ladanyi@1626: */ ladanyi@1626: ladanyi@1626: ///\ingroup demos ladanyi@1626: ///\file ladanyi@1626: ///\brief DIMACS to LGF converter (demo). ladanyi@1626: /// ladanyi@1626: /// This program converts various DIMACS formats to the LEMON Graph Format ladanyi@1626: /// (LGF). alpar@1641: /// alpar@1641: /// \include dim_to_lgf.cc ladanyi@1626: deba@1296: #include deba@1296: #include deba@1296: #include deba@1296: deba@1296: #include deba@1296: #include deba@1296: #include deba@1296: deba@1296: using namespace std; deba@1296: using namespace lemon; deba@1296: deba@1296: const char* versionString = deba@1296: "dim_to_lgf - part of lemon library\n"; deba@1296: deba@1296: const char* helpString = ladanyi@1626: "DIMACS to LGF converter\n" deba@1296: "Usage: dim_to_lgf [OPTIONS]\n" deba@1296: "\n" deba@1296: "Examples:\n" deba@1296: " dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n" deba@1296: "\n" deba@1296: "Options:\n" deba@1296: " -i FILE, --input FILE use FILE as input instead of standard input\n" deba@1296: " -o FILE, --output FILE use FILE as output instead of standard output\n" deba@1296: " -t TYPE, --type TYPE set up the type of the graph\n" deba@1296: " Possible types:\n" deba@1296: " mincostflow\n" deba@1296: " maxflow (default)\n" deba@1296: " shortestpath\n" deba@1296: " capacitated\n" deba@1296: " plain\n" deba@1296: " -v, --version shows the version of the converter\n" deba@1296: " -h, --help shows the help of the converter\n"; deba@1296: deba@1296: deba@1296: int main(int argc, const char *argv[]) { deba@1296: typedef SmartGraph Graph; deba@1296: deba@1296: typedef Graph::Edge Edge; deba@1296: typedef Graph::Node Node; deba@1296: typedef Graph::EdgeIt EdgeIt; deba@1296: typedef Graph::NodeIt NodeIt; deba@1794: typedef Graph::EdgeMap DoubleMap; deba@1296: deba@1296: std::string inputName; deba@1296: std::string outputName; deba@1296: std::string typeName; deba@1296: deba@1296: bool help = false; deba@1296: bool version = false; deba@1296: deba@1296: for (int arg = 1; arg < argc; ++arg) { deba@1296: if (strcmp(argv[arg], "--type") == 0 || deba@1296: strcmp(argv[arg], "-t") == 0) { deba@1296: if (!typeName.empty()) { deba@1296: cerr << "Multiple type description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: typeName = argv[++arg]; deba@1296: } deba@1296: else if (strcmp(argv[arg], "--input") == 0 || deba@1296: strcmp(argv[arg], "-i") == 0) { deba@1296: if (!inputName.empty()) { deba@1296: cerr << "Multiple input description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: inputName = argv[++arg]; deba@1296: } deba@1296: else if (strcmp(argv[arg], "--output") == 0 || deba@1296: strcmp(argv[arg], "-o") == 0) { deba@1296: if (!outputName.empty()) { deba@1296: cerr << "Multiple input description" << endl; deba@1296: return -1; deba@1296: } deba@1296: if (arg + 1 == argc) { deba@1296: cerr << "Parameter without value" << endl; deba@1296: return -1; deba@1296: } deba@1296: outputName = argv[++arg]; deba@1296: } else if (strcmp(argv[arg], "--help") == 0 || deba@1296: strcmp(argv[arg], "-h") == 0) { deba@1296: help = true; deba@1296: } else if (strcmp(argv[arg], "--version") == 0 || deba@1296: strcmp(argv[arg], "-v") == 0) { deba@1296: version = true; deba@1296: } else { deba@1296: cerr << "Invalid option: " << argv[arg] << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: deba@1296: if (version) { deba@1296: cout << versionString; deba@1296: } deba@1296: if (help) { deba@1296: cout << helpString; deba@1296: } deba@1296: if (help || version) { deba@1296: return 0; deba@1296: } deba@1296: deba@1296: ifstream input; deba@1296: if (!inputName.empty()) { deba@1296: input.open(inputName.c_str()); deba@1296: if (!input) { deba@1296: cerr << "File open error" << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: istream& is = (inputName.empty() ? cin : input); deba@1296: deba@1296: ofstream output; deba@1296: if (!outputName.empty()) { deba@1296: output.open(outputName.c_str()); deba@1296: if (!output) { deba@1296: cerr << "File open error" << endl; deba@1296: return -1; deba@1296: } deba@1296: } deba@1296: ostream& os = (outputName.empty() ? cout : output); deba@1296: deba@1296: if (typeName.empty()) { deba@1296: typeName = "maxflow"; deba@1296: } deba@1296: deba@1296: if (typeName == "mincostflow") { deba@1296: Graph graph; deba@1296: Node s, t; deba@1794: DoubleMap cost(graph), capacity(graph); deba@1296: readDimacs(is, graph, capacity, s, t, cost); deba@1744: GraphWriter(os, graph). deba@1744: writeEdgeMap("capacity", capacity). deba@1744: writeNode("source", s). deba@1744: writeNode("target", t). deba@1744: writeEdgeMap("cost", cost). deba@1744: run(); deba@1296: } else if (typeName == "maxflow") { deba@1296: Graph graph; deba@1296: Node s, t; deba@1794: DoubleMap capacity(graph); deba@1296: readDimacs(is, graph, capacity, s, t); deba@1744: GraphWriter(os, graph). deba@1744: writeEdgeMap("capacity", capacity). deba@1744: writeNode("source", s). deba@1744: writeNode("target", t). deba@1744: run(); deba@1296: } else if (typeName == "shortestpath") { deba@1296: Graph graph; deba@1296: Node s; deba@1794: DoubleMap capacity(graph); deba@1296: readDimacs(is, graph, capacity, s); deba@1744: GraphWriter(os, graph). deba@1744: writeEdgeMap("capacity", capacity). deba@1744: writeNode("source", s). deba@1744: run(); deba@1296: } else if (typeName == "capacitated") { deba@1296: Graph graph; deba@1794: DoubleMap capacity(graph); deba@1296: readDimacs(is, graph, capacity); deba@1744: GraphWriter(os, graph). deba@1744: writeEdgeMap("capacity", capacity). deba@1744: run(); deba@1296: } else if (typeName == "plain") { deba@1296: Graph graph; deba@1296: readDimacs(is, graph); deba@1744: GraphWriter(os, graph).run(); deba@1296: } else { deba@1296: cerr << "Invalid type error" << endl; deba@1296: return -1; deba@1296: } deba@1296: return 0; deba@1296: }