gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge #347
0 1 0
merge default
0 files changed with 11 insertions and 8 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -46,232 +46,235 @@
46 46
#include <lemon/network_simplex.h>
47 47

	
48 48
using namespace lemon;
49 49
typedef SmartDigraph Digraph;
50 50
DIGRAPH_TYPEDEFS(Digraph);
51 51
typedef SmartGraph Graph;
52 52

	
53 53
template<class Value>
54 54
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &,
55 55
              DimacsDescriptor &desc)
56 56
{
57 57
  bool report = !ap.given("q");
58 58
  Digraph g;
59 59
  Node s;
60 60
  Digraph::ArcMap<Value> len(g);
61 61
  Timer t;
62 62
  t.restart();
63 63
  readDimacsSp(is, g, len, s, desc);
64 64
  if(report) std::cerr << "Read the file: " << t << '\n';
65 65
  t.restart();
66 66
  Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len);
67 67
  if(report) std::cerr << "Setup Dijkstra class: " << t << '\n';
68 68
  t.restart();
69 69
  dij.run(s);
70 70
  if(report) std::cerr << "Run Dijkstra: " << t << '\n';
71 71
}
72 72

	
73 73
template<class Value>
74 74
void solve_max(ArgParser &ap, std::istream &is, std::ostream &,
75 75
               Value infty, DimacsDescriptor &desc)
76 76
{
77 77
  bool report = !ap.given("q");
78 78
  Digraph g;
79 79
  Node s,t;
80 80
  Digraph::ArcMap<Value> cap(g);
81 81
  Timer ti;
82 82
  ti.restart();
83 83
  readDimacsMax(is, g, cap, s, t, infty, desc);
84 84
  if(report) std::cerr << "Read the file: " << ti << '\n';
85 85
  ti.restart();
86 86
  Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t);
87 87
  if(report) std::cerr << "Setup Preflow class: " << ti << '\n';
88 88
  ti.restart();
89 89
  pre.run();
90 90
  if(report) std::cerr << "Run Preflow: " << ti << '\n';
91 91
  if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n';  
92 92
}
93 93

	
94
template<class Value>
94
template<class Value, class LargeValue>
95 95
void solve_min(ArgParser &ap, std::istream &is, std::ostream &,
96 96
               Value infty, DimacsDescriptor &desc)
97 97
{
98 98
  bool report = !ap.given("q");
99 99
  Digraph g;
100 100
  Digraph::ArcMap<Value> lower(g), cap(g), cost(g);
101 101
  Digraph::NodeMap<Value> sup(g);
102 102
  Timer ti;
103 103

	
104 104
  ti.restart();
105 105
  readDimacsMin(is, g, lower, cap, cost, sup, infty, desc);
106 106
  ti.stop();
107 107
  Value sum_sup = 0;
108 108
  for (Digraph::NodeIt n(g); n != INVALID; ++n) {
109 109
    sum_sup += sup[n];
110 110
  }
111 111
  if (report) {
112 112
    std::cerr << "Sum of supply values: " << sum_sup << "\n";
113 113
    if (sum_sup <= 0)
114 114
      std::cerr << "GEQ supply contraints are used for NetworkSimplex\n\n";
115 115
    else
116 116
      std::cerr << "LEQ supply contraints are used for NetworkSimplex\n\n";
117 117
  }
118 118
  if (report) std::cerr << "Read the file: " << ti << '\n';
119 119

	
120 120
  ti.restart();
121 121
  NetworkSimplex<Digraph, Value> ns(g);
122 122
  ns.lowerMap(lower).upperMap(cap).costMap(cost).supplyMap(sup);
123 123
  if (sum_sup > 0) ns.supplyType(ns.LEQ);
124 124
  if (report) std::cerr << "Setup NetworkSimplex class: " << ti << '\n';
125 125
  ti.restart();
126 126
  bool res = ns.run();
127 127
  if (report) {
128 128
    std::cerr << "Run NetworkSimplex: " << ti << "\n\n";
129 129
    std::cerr << "Feasible flow: " << (res ? "found" : "not found") << '\n';
130
    if (res) std::cerr << "Min flow cost: " << ns.totalCost() << '\n';
130
    if (res) std::cerr << "Min flow cost: "
131
                       << ns.template totalCost<LargeValue>() << '\n';
131 132
  }
132 133
}
133 134

	
134 135
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &,
135 136
              DimacsDescriptor &desc)
136 137
{
137 138
  bool report = !ap.given("q");
138 139
  Graph g;
139 140
  Timer ti;
140 141
  ti.restart();
141 142
  readDimacsMat(is, g, desc);
142 143
  if(report) std::cerr << "Read the file: " << ti << '\n';
143 144
  ti.restart();
144 145
  MaxMatching<Graph> mat(g);
145 146
  if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n';
146 147
  ti.restart();
147 148
  mat.run();
148 149
  if(report) std::cerr << "Run MaxMatching: " << ti << '\n';
149 150
  if(report) std::cerr << "\nCardinality of max matching: "
150 151
                       << mat.matchingSize() << '\n';  
151 152
}
152 153

	
153 154

	
154
template<class Value>
155
template<class Value, class LargeValue>
155 156
void solve(ArgParser &ap, std::istream &is, std::ostream &os,
156 157
           DimacsDescriptor &desc)
157 158
{
158 159
  std::stringstream iss(static_cast<std::string>(ap["infcap"]));
159 160
  Value infty;
160 161
  iss >> infty;
161 162
  if(iss.fail())
162 163
    {
163 164
      std::cerr << "Cannot interpret '"
164 165
                << static_cast<std::string>(ap["infcap"]) << "' as infinite"
165 166
                << std::endl;
166 167
      exit(1);
167 168
    }
168 169
  
169 170
  switch(desc.type)
170 171
    {
171 172
    case DimacsDescriptor::MIN:
172
      solve_min<Value>(ap,is,os,infty,desc);
173
      solve_min<Value, LargeValue>(ap,is,os,infty,desc);
173 174
      break;
174 175
    case DimacsDescriptor::MAX:
175 176
      solve_max<Value>(ap,is,os,infty,desc);
176 177
      break;
177 178
    case DimacsDescriptor::SP:
178 179
      solve_sp<Value>(ap,is,os,desc);
179 180
      break;
180 181
    case DimacsDescriptor::MAT:
181 182
      solve_mat(ap,is,os,desc);
182 183
      break;
183 184
    default:
184 185
      break;
185 186
    }
186 187
}
187 188

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

	
191 192
  typedef Digraph::Arc Arc;
192 193

	
193 194
  std::string inputName;
194 195
  std::string outputName;
195 196

	
196 197
  ArgParser ap(argc, argv);
197 198
  ap.other("[INFILE [OUTFILE]]",
198 199
           "If either the INFILE or OUTFILE file is missing the standard\n"
199 200
           "     input/output will be used instead.")
200 201
    .boolOption("q", "Do not print any report")
201 202
    .boolOption("int","Use 'int' for capacities, costs etc. (default)")
202 203
    .optionGroup("datatype","int")
203 204
#ifdef LEMON_HAVE_LONG_LONG
204 205
    .boolOption("long","Use 'long long' for capacities, costs etc.")
205 206
    .optionGroup("datatype","long")
206 207
#endif
207 208
    .boolOption("double","Use 'double' for capacities, costs etc.")
208 209
    .optionGroup("datatype","double")
209 210
    .boolOption("ldouble","Use 'long double' for capacities, costs etc.")
210 211
    .optionGroup("datatype","ldouble")
211 212
    .onlyOneGroup("datatype")
212 213
    .stringOption("infcap","Value used for 'very high' capacities","0")
213 214
    .run();
214 215

	
215 216
  std::ifstream input;
216 217
  std::ofstream output;
217 218

	
218 219
  switch(ap.files().size())
219 220
    {
220 221
    case 2:
221 222
      output.open(ap.files()[1].c_str());
222 223
      if (!output) {
223 224
        throw IoError("Cannot open the file for writing", ap.files()[1]);
224 225
      }
225 226
    case 1:
226 227
      input.open(ap.files()[0].c_str());
227 228
      if (!input) {
228 229
        throw IoError("File cannot be found", ap.files()[0]);
229 230
      }
230 231
    case 0:
231 232
      break;
232 233
    default:
233 234
      std::cerr << ap.commandName() << ": too many arguments\n";
234 235
      return 1;
235 236
    }
236 237
  std::istream& is = (ap.files().size()<1 ? std::cin : input);
237 238
  std::ostream& os = (ap.files().size()<2 ? std::cout : output);
238 239

	
239 240
  DimacsDescriptor desc = dimacsType(is);
240 241
  
241 242
  if(!ap.given("q"))
242 243
    {
243 244
      std::cout << "Problem type: ";
244 245
      switch(desc.type)
245 246
        {
246 247
        case DimacsDescriptor::MIN:
247 248
          std::cout << "min";
248 249
          break;
249 250
        case DimacsDescriptor::MAX:
250 251
          std::cout << "max";
251 252
          break;
252 253
        case DimacsDescriptor::SP:
253 254
          std::cout << "sp";
254 255
        case DimacsDescriptor::MAT:
255 256
          std::cout << "mat";
256 257
          break;
257 258
        default:
258 259
          exit(1);
259 260
          break;
260 261
        }
261 262
      std::cout << "\nNum of nodes: " << desc.nodeNum;
262 263
      std::cout << "\nNum of arcs:  " << desc.edgeNum;
263 264
      std::cout << "\n\n";
264 265
    }
265 266
    
266 267
  if(ap.given("double"))
267
    solve<double>(ap,is,os,desc);
268
    solve<double, double>(ap,is,os,desc);
268 269
  else if(ap.given("ldouble"))
269
    solve<long double>(ap,is,os,desc);
270
    solve<long double, long double>(ap,is,os,desc);
270 271
#ifdef LEMON_HAVE_LONG_LONG
271 272
  else if(ap.given("long"))
272
    solve<long long>(ap,is,os,desc);
273
    solve<long long, long long>(ap,is,os,desc);
274
  else solve<int, long long>(ap,is,os,desc);
275
#else
276
  else solve<int, long>(ap,is,os,desc);
273 277
#endif
274
  else solve<int>(ap,is,os,desc);
275 278

	
276 279
  return 0;
277 280
}
0 comments (0 inline)