tools/dimacs-to-lgf.cc
changeset 403 0a3ec097a76c
parent 401 9d1faab5e0f1
child 463 88ed40ad0d4f
equal deleted inserted replaced
1:a37129a87224 2:62468f943a71
    37 #include <lemon/smart_graph.h>
    37 #include <lemon/smart_graph.h>
    38 #include <lemon/dimacs.h>
    38 #include <lemon/dimacs.h>
    39 #include <lemon/lgf_writer.h>
    39 #include <lemon/lgf_writer.h>
    40 
    40 
    41 #include <lemon/arg_parser.h>
    41 #include <lemon/arg_parser.h>
       
    42 #include <lemon/error.h>
    42 
    43 
    43 using namespace std;
    44 using namespace std;
    44 using namespace lemon;
    45 using namespace lemon;
    45 
    46 
    46 
    47 
    54   typedef Digraph::ArcMap<double> DoubleArcMap;
    55   typedef Digraph::ArcMap<double> DoubleArcMap;
    55   typedef Digraph::NodeMap<double> DoubleNodeMap;
    56   typedef Digraph::NodeMap<double> DoubleNodeMap;
    56 
    57 
    57   std::string inputName;
    58   std::string inputName;
    58   std::string outputName;
    59   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;
       
    68 
    60 
    69   ArgParser ap(argc, argv);
    61   ArgParser ap(argc, argv);
    70   ap.refOption("-input",
    62   ap.other("[INFILE [OUTFILE]]",
    71                "use FILE as input instead of standard input",
    63            "If either the INFILE or OUTFILE file is missing the standard\n"
    72                inputName).synonym("i", "-input")
    64            "     input/output will be used instead.")
    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")
       
   100     .run();
    65     .run();
   101 
    66 
   102   ifstream input;
    67   ifstream input;
   103   if (!inputName.empty()) {
    68   ofstream output;
   104     input.open(inputName.c_str());
    69 
   105     if (!input) {
    70   switch(ap.files().size())
   106       cerr << "File open error" << endl;
    71     {
   107       return -1;
    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;
   108     }
   147     }
   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   }
       
   167   return 0;
   148   return 0;
   168 }
   149 }