gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
VS compatibility fix (#268)
0 2 0
default
2 files changed with 87 insertions and 81 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_TRAITS_H
20 20
#define LEMON_BITS_TRAITS_H
21 21

	
22 22
//\file
23 23
//\brief Traits for graphs and maps
24 24
//
25 25

	
26 26
#include <lemon/bits/enable_if.h>
27 27

	
28 28
namespace lemon {
29 29

	
30 30
  struct InvalidType {};
31 31

	
32
  template <typename _Graph, typename _Item>
32
  template <typename GR, typename _Item>
33 33
  class ItemSetTraits {};
34 34

	
35 35

	
36
  template <typename Graph, typename Enable = void>
36
  template <typename GR, typename Enable = void>
37 37
  struct NodeNotifierIndicator {
38 38
    typedef InvalidType Type;
39 39
  };
40
  template <typename Graph>
40
  template <typename GR>
41 41
  struct NodeNotifierIndicator<
42
    Graph,
43
    typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
42
    GR,
43
    typename enable_if<typename GR::NodeNotifier::Notifier, void>::type
44 44
  > {
45
    typedef typename Graph::NodeNotifier Type;
45
    typedef typename GR::NodeNotifier Type;
46 46
  };
47 47

	
48
  template <typename _Graph>
49
  class ItemSetTraits<_Graph, typename _Graph::Node> {
48
  template <typename GR>
49
  class ItemSetTraits<GR, typename GR::Node> {
50 50
  public:
51 51

	
52
    typedef _Graph Graph;
52
    typedef GR Graph;
53
    typedef GR Digraph;
53 54

	
54
    typedef typename Graph::Node Item;
55
    typedef typename Graph::NodeIt ItemIt;
55
    typedef typename GR::Node Item;
56
    typedef typename GR::NodeIt ItemIt;
56 57

	
57
    typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
58
    typedef typename NodeNotifierIndicator<GR>::Type ItemNotifier;
58 59

	
59
    template <typename _Value>
60
    class Map : public Graph::template NodeMap<_Value> {
60
    template <typename V>
61
    class Map : public GR::template NodeMap<V> {
62
      typedef typename GR::template NodeMap<V> Parent;
63

	
61 64
    public:
62
      typedef typename Graph::template NodeMap<_Value> Parent;
63
      typedef typename Graph::template NodeMap<_Value> Type;
65
      typedef typename GR::template NodeMap<V> Type;
64 66
      typedef typename Parent::Value Value;
65 67

	
66
      Map(const Graph& _digraph) : Parent(_digraph) {}
67
      Map(const Graph& _digraph, const Value& _value)
68
      Map(const GR& _digraph) : Parent(_digraph) {}
69
      Map(const GR& _digraph, const Value& _value)
68 70
        : Parent(_digraph, _value) {}
69 71

	
70 72
     };
71 73

	
72 74
  };
73 75

	
74
  template <typename Graph, typename Enable = void>
76
  template <typename GR, typename Enable = void>
75 77
  struct ArcNotifierIndicator {
76 78
    typedef InvalidType Type;
77 79
  };
78
  template <typename Graph>
80
  template <typename GR>
79 81
  struct ArcNotifierIndicator<
80
    Graph,
81
    typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type
82
    GR,
83
    typename enable_if<typename GR::ArcNotifier::Notifier, void>::type
82 84
  > {
83
    typedef typename Graph::ArcNotifier Type;
85
    typedef typename GR::ArcNotifier Type;
84 86
  };
85 87

	
86
  template <typename _Graph>
87
  class ItemSetTraits<_Graph, typename _Graph::Arc> {
88
  template <typename GR>
89
  class ItemSetTraits<GR, typename GR::Arc> {
88 90
  public:
89 91

	
90
    typedef _Graph Graph;
92
    typedef GR Graph;
93
    typedef GR Digraph;
91 94

	
92
    typedef typename Graph::Arc Item;
93
    typedef typename Graph::ArcIt ItemIt;
95
    typedef typename GR::Arc Item;
96
    typedef typename GR::ArcIt ItemIt;
94 97

	
95
    typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier;
98
    typedef typename ArcNotifierIndicator<GR>::Type ItemNotifier;
96 99

	
97
    template <typename _Value>
98
    class Map : public Graph::template ArcMap<_Value> {
100
    template <typename V>
101
    class Map : public GR::template ArcMap<V> {
102
      typedef typename GR::template ArcMap<V> Parent;
103

	
99 104
    public:
100
      typedef typename Graph::template ArcMap<_Value> Parent;
101
      typedef typename Graph::template ArcMap<_Value> Type;
105
      typedef typename GR::template ArcMap<V> Type;
102 106
      typedef typename Parent::Value Value;
103 107

	
104
      Map(const Graph& _digraph) : Parent(_digraph) {}
105
      Map(const Graph& _digraph, const Value& _value)
108
      Map(const GR& _digraph) : Parent(_digraph) {}
109
      Map(const GR& _digraph, const Value& _value)
106 110
        : Parent(_digraph, _value) {}
107 111
    };
108 112

	
109 113
  };
110 114

	
111
  template <typename Graph, typename Enable = void>
115
  template <typename GR, typename Enable = void>
112 116
  struct EdgeNotifierIndicator {
113 117
    typedef InvalidType Type;
114 118
  };
115
  template <typename Graph>
119
  template <typename GR>
116 120
  struct EdgeNotifierIndicator<
117
    Graph,
118
    typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
121
    GR,
122
    typename enable_if<typename GR::EdgeNotifier::Notifier, void>::type
119 123
  > {
120
    typedef typename Graph::EdgeNotifier Type;
124
    typedef typename GR::EdgeNotifier Type;
121 125
  };
122 126

	
123
  template <typename _Graph>
124
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
127
  template <typename GR>
128
  class ItemSetTraits<GR, typename GR::Edge> {
125 129
  public:
126 130

	
127
    typedef _Graph Graph;
131
    typedef GR Graph;
132
    typedef GR Digraph;
128 133

	
129
    typedef typename Graph::Edge Item;
130
    typedef typename Graph::EdgeIt ItemIt;
134
    typedef typename GR::Edge Item;
135
    typedef typename GR::EdgeIt ItemIt;
131 136

	
132
    typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
137
    typedef typename EdgeNotifierIndicator<GR>::Type ItemNotifier;
133 138

	
134
    template <typename _Value>
135
    class Map : public Graph::template EdgeMap<_Value> {
139
    template <typename V>
140
    class Map : public GR::template EdgeMap<V> {
141
      typedef typename GR::template EdgeMap<V> Parent;
142

	
136 143
    public:
137
      typedef typename Graph::template EdgeMap<_Value> Parent;
138
      typedef typename Graph::template EdgeMap<_Value> Type;
144
      typedef typename GR::template EdgeMap<V> Type;
139 145
      typedef typename Parent::Value Value;
140 146

	
141
      Map(const Graph& _digraph) : Parent(_digraph) {}
142
      Map(const Graph& _digraph, const Value& _value)
147
      Map(const GR& _digraph) : Parent(_digraph) {}
148
      Map(const GR& _digraph, const Value& _value)
143 149
        : Parent(_digraph, _value) {}
144 150
    };
145 151

	
146 152
  };
147 153

	
148 154
  template <typename Map, typename Enable = void>
149 155
  struct MapTraits {
150 156
    typedef False ReferenceMapTag;
151 157

	
152 158
    typedef typename Map::Key Key;
153 159
    typedef typename Map::Value Value;
154 160

	
155 161
    typedef Value ConstReturnValue;
156 162
    typedef Value ReturnValue;
157 163
  };
158 164

	
159 165
  template <typename Map>
160 166
  struct MapTraits<
161 167
    Map, typename enable_if<typename Map::ReferenceMapTag, void>::type >
162 168
  {
163 169
    typedef True ReferenceMapTag;
164 170

	
165 171
    typedef typename Map::Key Key;
166 172
    typedef typename Map::Value Value;
167 173

	
168 174
    typedef typename Map::ConstReference ConstReturnValue;
169 175
    typedef typename Map::Reference ReturnValue;
170 176

	
171 177
    typedef typename Map::ConstReference ConstReference;
172 178
    typedef typename Map::Reference Reference;
173 179
 };
174 180

	
175 181
  template <typename MatrixMap, typename Enable = void>
176 182
  struct MatrixMapTraits {
177 183
    typedef False ReferenceMapTag;
178 184

	
179 185
    typedef typename MatrixMap::FirstKey FirstKey;
180 186
    typedef typename MatrixMap::SecondKey SecondKey;
181 187
    typedef typename MatrixMap::Value Value;
182 188

	
183 189
    typedef Value ConstReturnValue;
184 190
    typedef Value ReturnValue;
185 191
  };
186 192

	
187 193
  template <typename MatrixMap>
188 194
  struct MatrixMapTraits<
189 195
    MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
190 196
                                  void>::type >
191 197
  {
192 198
    typedef True ReferenceMapTag;
193 199

	
194 200
    typedef typename MatrixMap::FirstKey FirstKey;
195 201
    typedef typename MatrixMap::SecondKey SecondKey;
196 202
    typedef typename MatrixMap::Value Value;
197 203

	
198 204
    typedef typename MatrixMap::ConstReference ConstReturnValue;
199 205
    typedef typename MatrixMap::Reference ReturnValue;
200 206

	
201 207
    typedef typename MatrixMap::ConstReference ConstReference;
202 208
    typedef typename MatrixMap::Reference Reference;
203 209
 };
204 210

	
205 211
  // Indicators for the tags
206 212

	
207
  template <typename Graph, typename Enable = void>
213
  template <typename GR, typename Enable = void>
208 214
  struct NodeNumTagIndicator {
209 215
    static const bool value = false;
210 216
  };
211 217

	
212
  template <typename Graph>
218
  template <typename GR>
213 219
  struct NodeNumTagIndicator<
214
    Graph,
215
    typename enable_if<typename Graph::NodeNumTag, void>::type
220
    GR,
221
    typename enable_if<typename GR::NodeNumTag, void>::type
216 222
  > {
217 223
    static const bool value = true;
218 224
  };
219 225

	
220
  template <typename Graph, typename Enable = void>
226
  template <typename GR, typename Enable = void>
221 227
  struct ArcNumTagIndicator {
222 228
    static const bool value = false;
223 229
  };
224 230

	
225
  template <typename Graph>
231
  template <typename GR>
226 232
  struct ArcNumTagIndicator<
227
    Graph,
228
    typename enable_if<typename Graph::ArcNumTag, void>::type
233
    GR,
234
    typename enable_if<typename GR::ArcNumTag, void>::type
229 235
  > {
230 236
    static const bool value = true;
231 237
  };
232 238

	
233
  template <typename Graph, typename Enable = void>
239
  template <typename GR, typename Enable = void>
234 240
  struct EdgeNumTagIndicator {
235 241
    static const bool value = false;
236 242
  };
237 243

	
238
  template <typename Graph>
244
  template <typename GR>
239 245
  struct EdgeNumTagIndicator<
240
    Graph,
241
    typename enable_if<typename Graph::EdgeNumTag, void>::type
246
    GR,
247
    typename enable_if<typename GR::EdgeNumTag, void>::type
242 248
  > {
243 249
    static const bool value = true;
244 250
  };
245 251

	
246
  template <typename Graph, typename Enable = void>
252
  template <typename GR, typename Enable = void>
247 253
  struct FindArcTagIndicator {
248 254
    static const bool value = false;
249 255
  };
250 256

	
251
  template <typename Graph>
257
  template <typename GR>
252 258
  struct FindArcTagIndicator<
253
    Graph,
254
    typename enable_if<typename Graph::FindArcTag, void>::type
259
    GR,
260
    typename enable_if<typename GR::FindArcTag, void>::type
255 261
  > {
256 262
    static const bool value = true;
257 263
  };
258 264

	
259
  template <typename Graph, typename Enable = void>
265
  template <typename GR, typename Enable = void>
260 266
  struct FindEdgeTagIndicator {
261 267
    static const bool value = false;
262 268
  };
263 269

	
264
  template <typename Graph>
270
  template <typename GR>
265 271
  struct FindEdgeTagIndicator<
266
    Graph,
267
    typename enable_if<typename Graph::FindEdgeTag, void>::type
272
    GR,
273
    typename enable_if<typename GR::FindEdgeTag, void>::type
268 274
  > {
269 275
    static const bool value = true;
270 276
  };
271 277

	
272
  template <typename Graph, typename Enable = void>
278
  template <typename GR, typename Enable = void>
273 279
  struct UndirectedTagIndicator {
274 280
    static const bool value = false;
275 281
  };
276 282

	
277
  template <typename Graph>
283
  template <typename GR>
278 284
  struct UndirectedTagIndicator<
279
    Graph,
280
    typename enable_if<typename Graph::UndirectedTag, void>::type
285
    GR,
286
    typename enable_if<typename GR::UndirectedTag, void>::type
281 287
  > {
282 288
    static const bool value = true;
283 289
  };
284 290

	
285
  template <typename Graph, typename Enable = void>
291
  template <typename GR, typename Enable = void>
286 292
  struct BuildTagIndicator {
287 293
    static const bool value = false;
288 294
  };
289 295

	
290
  template <typename Graph>
296
  template <typename GR>
291 297
  struct BuildTagIndicator<
292
    Graph,
293
    typename enable_if<typename Graph::BuildTag, void>::type
298
    GR,
299
    typename enable_if<typename GR::BuildTag, void>::type
294 300
  > {
295 301
    static const bool value = true;
296 302
  };
297 303

	
298 304
}
299 305

	
300 306
#endif
Ignore white space 768 line context
... ...
@@ -339,496 +339,496 @@
339 339
      beach.erase(bit);
340 340

	
341 341
      SpikeHeap::iterator pit = spikeheap.end();
342 342
      if (prev != -1 &&
343 343
          circle_form(points[prev], points[curr], points[site])) {
344 344
        double x = circle_point(points[prev], points[curr], points[site]);
345 345
        pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
346 346
        pit->second.it =
347 347
          beach.insert(std::make_pair(Part(prev, curr, site), pit));
348 348
      } else {
349 349
        beach.insert(std::make_pair(Part(prev, curr, site), pit));
350 350
      }
351 351

	
352 352
      beach.insert(std::make_pair(Part(curr, site, curr), spikeheap.end()));
353 353

	
354 354
      SpikeHeap::iterator nit = spikeheap.end();
355 355
      if (next != -1 &&
356 356
          circle_form(points[site], points[curr],points[next])) {
357 357
        double x = circle_point(points[site], points[curr], points[next]);
358 358
        nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
359 359
        nit->second.it =
360 360
          beach.insert(std::make_pair(Part(site, curr, next), nit));
361 361
      } else {
362 362
        beach.insert(std::make_pair(Part(site, curr, next), nit));
363 363
      }
364 364

	
365 365
      ++siteindex;
366 366
    } else {
367 367
      sweep = spit->first;
368 368

	
369 369
      Beach::iterator bit = spit->second.it;
370 370

	
371 371
      int prev = bit->first.prev;
372 372
      int curr = bit->first.curr;
373 373
      int next = bit->first.next;
374 374

	
375 375
      {
376 376
        std::pair<int, int> arc;
377 377

	
378 378
        arc = prev < curr ?
379 379
          std::make_pair(prev, curr) : std::make_pair(curr, prev);
380 380

	
381 381
        if (arcs.find(arc) == arcs.end()) {
382 382
          arcs.insert(arc);
383 383
          g.addEdge(nodes[prev], nodes[curr]);
384 384
          ++cnt;
385 385
        }
386 386

	
387 387
        arc = curr < next ?
388 388
          std::make_pair(curr, next) : std::make_pair(next, curr);
389 389

	
390 390
        if (arcs.find(arc) == arcs.end()) {
391 391
          arcs.insert(arc);
392 392
          g.addEdge(nodes[curr], nodes[next]);
393 393
          ++cnt;
394 394
        }
395 395
      }
396 396

	
397 397
      Beach::iterator pbit = bit; --pbit;
398 398
      int ppv = pbit->first.prev;
399 399
      Beach::iterator nbit = bit; ++nbit;
400 400
      int nnt = nbit->first.next;
401 401

	
402 402
      if (bit->second != spikeheap.end()) spikeheap.erase(bit->second);
403 403
      if (pbit->second != spikeheap.end()) spikeheap.erase(pbit->second);
404 404
      if (nbit->second != spikeheap.end()) spikeheap.erase(nbit->second);
405 405

	
406 406
      beach.erase(nbit);
407 407
      beach.erase(bit);
408 408
      beach.erase(pbit);
409 409

	
410 410
      SpikeHeap::iterator pit = spikeheap.end();
411 411
      if (ppv != -1 && ppv != next &&
412 412
          circle_form(points[ppv], points[prev], points[next])) {
413 413
        double x = circle_point(points[ppv], points[prev], points[next]);
414 414
        if (x < sweep) x = sweep;
415 415
        pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
416 416
        pit->second.it =
417 417
          beach.insert(std::make_pair(Part(ppv, prev, next), pit));
418 418
      } else {
419 419
        beach.insert(std::make_pair(Part(ppv, prev, next), pit));
420 420
      }
421 421

	
422 422
      SpikeHeap::iterator nit = spikeheap.end();
423 423
      if (nnt != -1 && prev != nnt &&
424 424
          circle_form(points[prev], points[next], points[nnt])) {
425 425
        double x = circle_point(points[prev], points[next], points[nnt]);
426 426
        if (x < sweep) x = sweep;
427 427
        nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
428 428
        nit->second.it =
429 429
          beach.insert(std::make_pair(Part(prev, next, nnt), nit));
430 430
      } else {
431 431
        beach.insert(std::make_pair(Part(prev, next, nnt), nit));
432 432
      }
433 433

	
434 434
    }
435 435
  }
436 436

	
437 437
  for (Beach::iterator it = beach.begin(); it != beach.end(); ++it) {
438 438
    int curr = it->first.curr;
439 439
    int next = it->first.next;
440 440

	
441 441
    if (next == -1) continue;
442 442

	
443 443
    std::pair<int, int> arc;
444 444

	
445 445
    arc = curr < next ?
446 446
      std::make_pair(curr, next) : std::make_pair(next, curr);
447 447

	
448 448
    if (arcs.find(arc) == arcs.end()) {
449 449
      arcs.insert(arc);
450 450
      g.addEdge(nodes[curr], nodes[next]);
451 451
      ++cnt;
452 452
    }
453 453
  }
454 454
}
455 455

	
456 456
void sparse(int d)
457 457
{
458 458
  Counter cnt("Number of arcs removed: ");
459 459
  Bfs<ListGraph> bfs(g);
460 460
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
461 461
      ei!=arcs.rend();++ei)
462 462
    {
463 463
      Node a=g.u(*ei);
464 464
      Node b=g.v(*ei);
465 465
      g.erase(*ei);
466 466
      bfs.run(a,b);
467 467
      if(bfs.predArc(b)==INVALID || bfs.dist(b)>d)
468 468
        g.addEdge(a,b);
469 469
      else cnt++;
470 470
    }
471 471
}
472 472

	
473 473
void sparse2(int d)
474 474
{
475 475
  Counter cnt("Number of arcs removed: ");
476 476
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
477 477
      ei!=arcs.rend();++ei)
478 478
    {
479 479
      Node a=g.u(*ei);
480 480
      Node b=g.v(*ei);
481 481
      g.erase(*ei);
482 482
      ConstMap<Arc,int> cegy(1);
483 483
      Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy,a,b);
484 484
      int k=sur.run(2);
485 485
      if(k<2 || sur.totalLength()>d)
486 486
        g.addEdge(a,b);
487 487
      else cnt++;
488 488
//       else std::cout << "Remove arc " << g.id(a) << "-" << g.id(b) << '\n';
489 489
    }
490 490
}
491 491

	
492 492
void sparseTriangle(int d)
493 493
{
494 494
  Counter cnt("Number of arcs added: ");
495 495
  std::vector<Parc> pedges;
496 496
  for(NodeIt n(g);n!=INVALID;++n)
497 497
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
498 498
      {
499 499
        Parc p;
500 500
        p.a=n;
501 501
        p.b=m;
502 502
        p.len=(coords[m]-coords[n]).normSquare();
503 503
        pedges.push_back(p);
504 504
      }
505 505
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
506 506
  for(std::vector<Parc>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
507 507
    {
508 508
      Line li(pi->a,pi->b);
509 509
      EdgeIt e(g);
510 510
      for(;e!=INVALID && !cross(e,li);++e) ;
511 511
      Edge ne;
512 512
      if(e==INVALID) {
513 513
        ConstMap<Arc,int> cegy(1);
514 514
        Suurballe<ListGraph,ConstMap<Arc,int> >
515 515
          sur(g,cegy,pi->a,pi->b);
516 516
        int k=sur.run(2);
517 517
        if(k<2 || sur.totalLength()>d)
518 518
          {
519 519
            ne=g.addEdge(pi->a,pi->b);
520 520
            arcs.push_back(ne);
521 521
            cnt++;
522 522
          }
523 523
      }
524 524
    }
525 525
}
526 526

	
527 527
template <typename Graph, typename CoordMap>
528 528
class LengthSquareMap {
529 529
public:
530 530
  typedef typename Graph::Edge Key;
531 531
  typedef typename CoordMap::Value::Value Value;
532 532

	
533 533
  LengthSquareMap(const Graph& graph, const CoordMap& coords)
534 534
    : _graph(graph), _coords(coords) {}
535 535

	
536 536
  Value operator[](const Key& key) const {
537 537
    return (_coords[_graph.v(key)] -
538 538
            _coords[_graph.u(key)]).normSquare();
539 539
  }
540 540

	
541 541
private:
542 542

	
543 543
  const Graph& _graph;
544 544
  const CoordMap& _coords;
545 545
};
546 546

	
547 547
void minTree() {
548 548
  std::vector<Parc> pedges;
549 549
  Timer T;
550 550
  std::cout << T.realTime() << "s: Creating delaunay triangulation...\n";
551 551
  delaunay();
552 552
  std::cout << T.realTime() << "s: Calculating spanning tree...\n";
553 553
  LengthSquareMap<ListGraph, ListGraph::NodeMap<Point> > ls(g, coords);
554 554
  ListGraph::EdgeMap<bool> tree(g);
555 555
  kruskal(g, ls, tree);
556 556
  std::cout << T.realTime() << "s: Removing non tree arcs...\n";
557 557
  std::vector<Edge> remove;
558 558
  for (EdgeIt e(g); e != INVALID; ++e) {
559 559
    if (!tree[e]) remove.push_back(e);
560 560
  }
561 561
  for(int i = 0; i < int(remove.size()); ++i) {
562 562
    g.erase(remove[i]);
563 563
  }
564 564
  std::cout << T.realTime() << "s: Done\n";
565 565
}
566 566

	
567 567
void tsp2()
568 568
{
569 569
  std::cout << "Find a tree..." << std::endl;
570 570

	
571 571
  minTree();
572 572

	
573 573
  std::cout << "Total arc length (tree) : " << totalLen() << std::endl;
574 574

	
575 575
  std::cout << "Make it Euler..." << std::endl;
576 576

	
577 577
  {
578 578
    std::vector<Node> leafs;
579 579
    for(NodeIt n(g);n!=INVALID;++n)
580 580
      if(countIncEdges(g,n)%2==1) leafs.push_back(n);
581 581

	
582 582
//    for(unsigned int i=0;i<leafs.size();i+=2)
583 583
//       g.addArc(leafs[i],leafs[i+1]);
584 584

	
585 585
    std::vector<Parc> pedges;
586 586
    for(unsigned int i=0;i<leafs.size()-1;i++)
587 587
      for(unsigned int j=i+1;j<leafs.size();j++)
588 588
        {
589 589
          Node n=leafs[i];
590 590
          Node m=leafs[j];
591 591
          Parc p;
592 592
          p.a=n;
593 593
          p.b=m;
594 594
          p.len=(coords[m]-coords[n]).normSquare();
595 595
          pedges.push_back(p);
596 596
        }
597 597
    std::sort(pedges.begin(),pedges.end(),pedgeLess);
598 598
    for(unsigned int i=0;i<pedges.size();i++)
599 599
      if(countIncEdges(g,pedges[i].a)%2 &&
600 600
         countIncEdges(g,pedges[i].b)%2)
601 601
        g.addEdge(pedges[i].a,pedges[i].b);
602 602
  }
603 603

	
604 604
  for(NodeIt n(g);n!=INVALID;++n)
605 605
    if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
606 606
      std::cout << "GEBASZ!!!" << std::endl;
607 607

	
608 608
  for(EdgeIt e(g);e!=INVALID;++e)
609 609
    if(g.u(e)==g.v(e))
610 610
      std::cout << "LOOP GEBASZ!!!" << std::endl;
611 611

	
612 612
  std::cout << "Number of arcs : " << countEdges(g) << std::endl;
613 613

	
614 614
  std::cout << "Total arc length (euler) : " << totalLen() << std::endl;
615 615

	
616 616
  ListGraph::EdgeMap<Arc> enext(g);
617 617
  {
618 618
    EulerIt<ListGraph> e(g);
619 619
    Arc eo=e;
620 620
    Arc ef=e;
621 621
//     std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
622 622
    for(++e;e!=INVALID;++e)
623 623
      {
624 624
//         std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
625 625
        enext[eo]=e;
626 626
        eo=e;
627 627
      }
628 628
    enext[eo]=ef;
629 629
  }
630 630

	
631 631
  std::cout << "Creating a tour from that..." << std::endl;
632 632

	
633 633
  int nnum = countNodes(g);
634 634
  int ednum = countEdges(g);
635 635

	
636 636
  for(Arc p=enext[EdgeIt(g)];ednum>nnum;p=enext[p])
637 637
    {
638 638
//       std::cout << "Checking arc " << g.id(p) << std::endl;
639 639
      Arc e=enext[p];
640 640
      Arc f=enext[e];
641 641
      Node n2=g.source(f);
642 642
      Node n1=g.oppositeNode(n2,e);
643 643
      Node n3=g.oppositeNode(n2,f);
644 644
      if(countIncEdges(g,n2)>2)
645 645
        {
646 646
//           std::cout << "Remove an Arc" << std::endl;
647 647
          Arc ff=enext[f];
648 648
          g.erase(e);
649 649
          g.erase(f);
650 650
          if(n1!=n3)
651 651
            {
652 652
              Arc ne=g.direct(g.addEdge(n1,n3),n1);
653 653
              enext[p]=ne;
654 654
              enext[ne]=ff;
655 655
              ednum--;
656 656
            }
657 657
          else {
658 658
            enext[p]=ff;
659 659
            ednum-=2;
660 660
          }
661 661
        }
662 662
    }
663 663

	
664 664
  std::cout << "Total arc length (tour) : " << totalLen() << std::endl;
665 665

	
666 666
  std::cout << "2-opt the tour..." << std::endl;
667 667

	
668 668
  tsp_improve();
669 669

	
670 670
  std::cout << "Total arc length (2-opt tour) : " << totalLen() << std::endl;
671 671
}
672 672

	
673 673

	
674 674
int main(int argc,const char **argv)
675 675
{
676 676
  ArgParser ap(argc,argv);
677 677

	
678 678
//   bool eps;
679 679
  bool disc_d, square_d, gauss_d;
680 680
//   bool tsp_a,two_a,tree_a;
681 681
  int num_of_cities=1;
682 682
  double area=1;
683 683
  N=100;
684 684
//   girth=10;
685 685
  std::string ndist("disc");
686 686
  ap.refOption("n", "Number of nodes (default is 100)", N)
687 687
    .intOption("g", "Girth parameter (default is 10)", 10)
688 688
    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
689 689
    .refOption("area", "Full relative area of the cities (default is 1)", area)
690 690
    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
691 691
    .optionGroup("dist", "disc")
692 692
    .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
693 693
    .optionGroup("dist", "square")
694 694
    .refOption("gauss",
695 695
            "Nodes are located according to a two-dim gauss distribution",
696 696
            gauss_d)
697 697
    .optionGroup("dist", "gauss")
698 698
//     .mandatoryGroup("dist")
699 699
    .onlyOneGroup("dist")
700 700
    .boolOption("eps", "Also generate .eps output (prefix.eps)")
701 701
    .boolOption("nonodes", "Draw the edges only in the generated .eps")
702 702
    .boolOption("dir", "Directed digraph is generated (each arcs are replaced by two directed ones)")
703 703
    .boolOption("2con", "Create a two connected planar digraph")
704 704
    .optionGroup("alg","2con")
705 705
    .boolOption("tree", "Create a min. cost spanning tree")
706 706
    .optionGroup("alg","tree")
707 707
    .boolOption("tsp", "Create a TSP tour")
708 708
    .optionGroup("alg","tsp")
709 709
    .boolOption("tsp2", "Create a TSP tour (tree based)")
710 710
    .optionGroup("alg","tsp2")
711 711
    .boolOption("dela", "Delaunay triangulation digraph")
712 712
    .optionGroup("alg","dela")
713 713
    .onlyOneGroup("alg")
714 714
    .boolOption("rand", "Use time seed for random number generator")
715 715
    .optionGroup("rand", "rand")
716 716
    .intOption("seed", "Random seed", -1)
717 717
    .optionGroup("rand", "seed")
718 718
    .onlyOneGroup("rand")
719 719
    .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
720 720
    .run();
721 721

	
722 722
  if (ap["rand"]) {
723
    int seed = time(0);
723
    int seed = int(time(0));
724 724
    std::cout << "Random number seed: " << seed << std::endl;
725 725
    rnd = Random(seed);
726 726
  }
727 727
  if (ap.given("seed")) {
728 728
    int seed = ap["seed"];
729 729
    std::cout << "Random number seed: " << seed << std::endl;
730 730
    rnd = Random(seed);
731 731
  }
732 732

	
733 733
  std::string prefix;
734 734
  switch(ap.files().size())
735 735
    {
736 736
    case 0:
737 737
      prefix="lgf-gen-out";
738 738
      break;
739 739
    case 1:
740 740
      prefix=ap.files()[0];
741 741
      break;
742 742
    default:
743 743
      std::cerr << "\nAt most one prefix can be given\n\n";
744 744
      exit(1);
745 745
    }
746 746

	
747 747
  double sum_sizes=0;
748 748
  std::vector<double> sizes;
749 749
  std::vector<double> cum_sizes;
750 750
  for(int s=0;s<num_of_cities;s++)
751 751
    {
752 752
      //         sum_sizes+=rnd.exponential();
753 753
      double d=rnd();
754 754
      sum_sizes+=d;
755 755
      sizes.push_back(d);
756 756
      cum_sizes.push_back(sum_sizes);
757 757
    }
758 758
  int i=0;
759 759
  for(int s=0;s<num_of_cities;s++)
760 760
    {
761 761
      Point center=(num_of_cities==1?Point(0,0):rnd.disc());
762 762
      if(gauss_d)
763 763
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
764 764
          Node n=g.addNode();
765 765
          nodes.push_back(n);
766 766
          coords[n]=center+rnd.gauss2()*area*
767 767
            std::sqrt(sizes[s]/sum_sizes);
768 768
        }
769 769
      else if(square_d)
770 770
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
771 771
          Node n=g.addNode();
772 772
          nodes.push_back(n);
773 773
          coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
774 774
            std::sqrt(sizes[s]/sum_sizes);
775 775
        }
776 776
      else if(disc_d || true)
777 777
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
778 778
          Node n=g.addNode();
779 779
          nodes.push_back(n);
780 780
          coords[n]=center+rnd.disc()*area*
781 781
            std::sqrt(sizes[s]/sum_sizes);
782 782
        }
783 783
    }
784 784

	
785 785
//   for (ListGraph::NodeIt n(g); n != INVALID; ++n) {
786 786
//     std::cerr << coords[n] << std::endl;
787 787
//   }
788 788

	
789 789
  if(ap["tsp"]) {
790 790
    tsp();
791 791
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
792 792
  }
793 793
  if(ap["tsp2"]) {
794 794
    tsp2();
795 795
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
796 796
  }
797 797
  else if(ap["2con"]) {
798 798
    std::cout << "Make triangles\n";
799 799
    //   triangle();
800 800
    sparseTriangle(ap["g"]);
801 801
    std::cout << "Make it sparser\n";
802 802
    sparse2(ap["g"]);
803 803
  }
804 804
  else if(ap["tree"]) {
805 805
    minTree();
806 806
  }
807 807
  else if(ap["dela"]) {
808 808
    delaunay();
809 809
  }
810 810

	
811 811

	
812 812
  std::cout << "Number of nodes    : " << countNodes(g) << std::endl;
813 813
  std::cout << "Number of arcs    : " << countEdges(g) << std::endl;
814 814
  double tlen=0;
815 815
  for(EdgeIt e(g);e!=INVALID;++e)
816 816
    tlen+=std::sqrt((coords[g.v(e)]-coords[g.u(e)]).normSquare());
817 817
  std::cout << "Total arc length  : " << tlen << std::endl;
818 818

	
819 819
  if(ap["eps"])
820 820
    graphToEps(g,prefix+".eps").scaleToA4().
821 821
      scale(600).nodeScale(.005).arcWidthScale(.001).preScale(false).
822 822
      coords(coords).hideNodes(ap.given("nonodes")).run();
823 823

	
824 824
  if(ap["dir"])
825 825
    DigraphWriter<ListGraph>(g,prefix+".lgf").
826 826
      nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
827 827
      nodeMap("coordinates_y",scaleMap(yMap(coords),600)).
828 828
      run();
829 829
  else GraphWriter<ListGraph>(g,prefix+".lgf").
830 830
         nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
831 831
         nodeMap("coordinates_y",scaleMap(yMap(coords),600)).
832 832
         run();
833 833
}
834 834

	
0 comments (0 inline)