summaryrefslogtreecommitdiff
path: root/lemon/tools/dimacs-to-lgf.cc
blob: 3968645dd90527746943ba2916724370499033ea (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2009
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

///\ingroup tools
///\file
///\brief DIMACS to LGF converter.
///
/// This program converts various DIMACS formats to the LEMON Digraph Format
/// (LGF).
///
/// See
/// \code
///   dimacs-to-lgf --help
/// \endcode
/// for more info on the usage.

#include <iostream>
#include <fstream>
#include <cstring>

#include <lemon/smart_graph.h>
#include <lemon/dimacs.h>
#include <lemon/lgf_writer.h>

#include <lemon/arg_parser.h>
#include <lemon/error.h>

using namespace std;
using namespace lemon;


int main(int argc, const char *argv[]) {
  typedef SmartDigraph Digraph;

  typedef Digraph::Arc Arc;
  typedef Digraph::Node Node;
  typedef Digraph::ArcIt ArcIt;
  typedef Digraph::NodeIt NodeIt;
  typedef Digraph::ArcMap<double> DoubleArcMap;
  typedef Digraph::NodeMap<double> DoubleNodeMap;

  std::string inputName;
  std::string outputName;

  ArgParser ap(argc, argv);
  ap.other("[INFILE [OUTFILE]]",
           "If either the INFILE or OUTFILE file is missing the standard\n"
           "     input/output will be used instead.")
    .run();

  ifstream input;
  ofstream output;

  switch(ap.files().size())
    {
    case 2:
      output.open(ap.files()[1].c_str());
      if (!output) {
        throw IoError("Cannot open the file for writing", ap.files()[1]);
      }
    case 1:
      input.open(ap.files()[0].c_str());
      if (!input) {
        throw IoError("File cannot be found", ap.files()[0]);
      }
    case 0:
      break;
    default:
      cerr << ap.commandName() << ": too many arguments\n";
      return 1;
  }
  istream& is = (ap.files().size()<1 ? cin : input);
  ostream& os = (ap.files().size()<2 ? cout : output);

  DimacsDescriptor desc = dimacsType(is);
  switch(desc.type)
    {
    case DimacsDescriptor::MIN:
      {
        Digraph digraph;
        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
        DoubleNodeMap supply(digraph);
        readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
        DigraphWriter<Digraph>(digraph, os).
          nodeMap("supply", supply).
          arcMap("lower", lower).
          arcMap("capacity", capacity).
          arcMap("cost", cost).
          attribute("problem","min").
          run();
      }
      break;
    case DimacsDescriptor::MAX:
      {
        Digraph digraph;
        Node s, t;
        DoubleArcMap capacity(digraph);
        readDimacsMax(is, digraph, capacity, s, t, 0, desc);
        DigraphWriter<Digraph>(digraph, os).
          arcMap("capacity", capacity).
          node("source", s).
          node("target", t).
          attribute("problem","max").
          run();
      }
      break;
    case DimacsDescriptor::SP:
      {
        Digraph digraph;
        Node s;
        DoubleArcMap capacity(digraph);
        readDimacsSp(is, digraph, capacity, s, desc);
        DigraphWriter<Digraph>(digraph, os).
          arcMap("capacity", capacity).
          node("source", s).
          attribute("problem","sp").
          run();
      }
      break;
    case DimacsDescriptor::MAT:
      {
        Digraph digraph;
        readDimacsMat(is, digraph,desc);
        DigraphWriter<Digraph>(digraph, os).
          attribute("problem","mat").
          run();
      }
      break;
    default:
      break;
    }
  return 0;
}