tools/lgf-gen.cc
author alpar
Tue, 05 Jun 2007 17:24:35 +0000
changeset 2449 1d685ac667ec
parent 2447 260ce674cc65
child 2453 2800d9efb01d
permissions -rw-r--r--
Minor improvement in Tutorial
alpar@2391
     1
/* -*- C++ -*-
alpar@2391
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2391
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2391
     8
 *
alpar@2391
     9
 * Permission to use, modify and distribute this software is granted
alpar@2391
    10
 * provided that this copyright notice appears in all copies. For
alpar@2391
    11
 * precise terms see the accompanying LICENSE file.
alpar@2391
    12
 *
alpar@2391
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2391
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2391
    15
 * purpose.
alpar@2391
    16
 *
alpar@2391
    17
 */
alpar@2391
    18
alpar@2390
    19
#include <lemon/list_graph.h>
alpar@2390
    20
#include <lemon/graph_utils.h>
alpar@2390
    21
#include <lemon/random.h>
alpar@2390
    22
#include <lemon/dim2.h>
alpar@2390
    23
#include <lemon/bfs.h>
alpar@2390
    24
#include <lemon/counter.h>
alpar@2390
    25
#include <lemon/suurballe.h>
alpar@2390
    26
#include <lemon/graph_to_eps.h>
alpar@2390
    27
#include <lemon/graph_writer.h>
alpar@2390
    28
#include <lemon/arg_parser.h>
alpar@2446
    29
#include <lemon/euler.h>
alpar@2390
    30
#include <cmath>
alpar@2390
    31
#include <algorithm>
deba@2447
    32
#include <lemon/kruskal.h>
alpar@2402
    33
#include <lemon/time_measure.h>
alpar@2390
    34
alpar@2390
    35
using namespace lemon;
alpar@2390
    36
alpar@2390
    37
typedef dim2::Point<double> Point;
alpar@2390
    38
alpar@2390
    39
UGRAPH_TYPEDEFS(ListUGraph);
alpar@2390
    40
alpar@2402
    41
bool progress=true;
alpar@2402
    42
alpar@2390
    43
int N;
alpar@2402
    44
// int girth;
alpar@2390
    45
alpar@2390
    46
ListUGraph g;
alpar@2390
    47
alpar@2390
    48
std::vector<Node> nodes;
alpar@2390
    49
ListUGraph::NodeMap<Point> coords(g);
alpar@2390
    50
alpar@2446
    51
alpar@2446
    52
double totalLen(){
alpar@2446
    53
  double tlen=0;
alpar@2446
    54
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2446
    55
    tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
alpar@2446
    56
  return tlen;
alpar@2446
    57
}
alpar@2446
    58
alpar@2390
    59
int tsp_impr_num=0;
alpar@2390
    60
alpar@2390
    61
const double EPSILON=1e-8; 
alpar@2390
    62
bool tsp_improve(Node u, Node v)
alpar@2390
    63
{
alpar@2390
    64
  double luv=std::sqrt((coords[v]-coords[u]).normSquare());
alpar@2390
    65
  Node u2=u;
alpar@2390
    66
  Node v2=v;
alpar@2390
    67
  do {
alpar@2390
    68
    Node n;
alpar@2390
    69
    for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e);
alpar@2390
    70
    u2=v2;
alpar@2390
    71
    v2=n;
alpar@2390
    72
    if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
alpar@2390
    73
       std::sqrt((coords[u]-coords[u2]).normSquare())+
alpar@2390
    74
       std::sqrt((coords[v]-coords[v2]).normSquare()))
alpar@2390
    75
      {
alpar@2390
    76
 	g.erase(findUEdge(g,u,v));
alpar@2390
    77
 	g.erase(findUEdge(g,u2,v2));
alpar@2390
    78
	g.addEdge(u2,u);
alpar@2390
    79
	g.addEdge(v,v2);
alpar@2390
    80
	tsp_impr_num++;
alpar@2390
    81
	return true;
alpar@2390
    82
      }
alpar@2390
    83
  } while(v2!=u);
alpar@2390
    84
  return false;
alpar@2390
    85
}
alpar@2390
    86
alpar@2390
    87
bool tsp_improve(Node u)
alpar@2390
    88
{
alpar@2390
    89
  for(IncEdgeIt e(g,u);e!=INVALID;++e)
alpar@2390
    90
    if(tsp_improve(u,g.runningNode(e))) return true;
alpar@2390
    91
  return false;
alpar@2390
    92
}
alpar@2390
    93
alpar@2390
    94
void tsp_improve()
alpar@2390
    95
{
alpar@2390
    96
  bool b;
alpar@2390
    97
  do {
alpar@2390
    98
    b=false;
alpar@2390
    99
    for(NodeIt n(g);n!=INVALID;++n)
alpar@2390
   100
      if(tsp_improve(n)) b=true;
alpar@2390
   101
  } while(b);
alpar@2390
   102
}
alpar@2390
   103
alpar@2390
   104
void tsp()
alpar@2390
   105
{
alpar@2390
   106
  for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
alpar@2390
   107
  tsp_improve();
alpar@2390
   108
}
alpar@2390
   109
alpar@2390
   110
class Line
alpar@2390
   111
{
alpar@2390
   112
public:
alpar@2390
   113
  Point a;
alpar@2390
   114
  Point b;
alpar@2390
   115
  Line(Point _a,Point _b) :a(_a),b(_b) {}
alpar@2390
   116
  Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
alpar@2390
   117
  Line(const Edge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
alpar@2390
   118
  Line(const UEdge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
alpar@2390
   119
};
alpar@2390
   120
  
alpar@2390
   121
inline std::ostream& operator<<(std::ostream &os, const Line &l)
alpar@2390
   122
{
alpar@2390
   123
  os << l.a << "->" << l.b;
alpar@2390
   124
  return os;
alpar@2390
   125
}
alpar@2390
   126
alpar@2390
   127
bool cross(Line a, Line b) 
alpar@2390
   128
{
alpar@2390
   129
  Point ao=rot90(a.b-a.a);
alpar@2390
   130
  Point bo=rot90(b.b-b.a);
alpar@2390
   131
  return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
alpar@2390
   132
    (bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
alpar@2390
   133
}
alpar@2390
   134
alpar@2390
   135
struct Pedge
alpar@2390
   136
{
alpar@2390
   137
  Node a;
alpar@2390
   138
  Node b;
alpar@2390
   139
  double len;
alpar@2390
   140
};
alpar@2390
   141
alpar@2390
   142
bool pedgeLess(Pedge a,Pedge b)
alpar@2390
   143
{
alpar@2390
   144
  return a.len<b.len;
alpar@2390
   145
}
alpar@2390
   146
alpar@2390
   147
std::vector<UEdge> edges;
alpar@2390
   148
deba@2447
   149
namespace _delaunay_bits {
deba@2447
   150
deba@2447
   151
  struct Part {
deba@2447
   152
    int prev, curr, next;
deba@2447
   153
deba@2447
   154
    Part(int p, int c, int n) : prev(p), curr(c), next(n) {} 
deba@2447
   155
  };
deba@2447
   156
deba@2447
   157
  inline std::ostream& operator<<(std::ostream& os, const Part& part) {
deba@2447
   158
    os << '(' << part.prev << ',' << part.curr << ',' << part.next << ')';
deba@2447
   159
    return os;
deba@2447
   160
  }
deba@2447
   161
deba@2447
   162
  inline double circle_point(const Point& p, const Point& q, const Point& r) {
deba@2447
   163
    double a = p.x * (q.y - r.y) + q.x * (r.y - p.y) + r.x * (p.y - q.y);
deba@2447
   164
    if (a == 0) return std::numeric_limits<double>::quiet_NaN();
deba@2447
   165
deba@2447
   166
    double d = (p.x * p.x + p.y * p.y) * (q.y - r.y) +
deba@2447
   167
      (q.x * q.x + q.y * q.y) * (r.y - p.y) +
deba@2447
   168
      (r.x * r.x + r.y * r.y) * (p.y - q.y);
deba@2447
   169
deba@2447
   170
    double e = (p.x * p.x + p.y * p.y) * (q.x - r.x) +
deba@2447
   171
      (q.x * q.x + q.y * q.y) * (r.x - p.x) +
deba@2447
   172
      (r.x * r.x + r.y * r.y) * (p.x - q.x);
deba@2447
   173
deba@2447
   174
    double f = (p.x * p.x + p.y * p.y) * (q.x * r.y - r.x * q.y) +
deba@2447
   175
      (q.x * q.x + q.y * q.y) * (r.x * p.y - p.x * r.y) +
deba@2447
   176
      (r.x * r.x + r.y * r.y) * (p.x * q.y - q.x * p.y);
deba@2447
   177
deba@2447
   178
    return d / (2 * a) + sqrt((d * d + e * e) / (4 * a * a) + f / a);
deba@2447
   179
  }
deba@2447
   180
deba@2447
   181
  inline bool circle_form(const Point& p, const Point& q, const Point& r) {
deba@2447
   182
    return rot90(q - p) * (r - q) < 0.0;
deba@2447
   183
  }
deba@2447
   184
deba@2447
   185
  inline double intersection(const Point& p, const Point& q, double sx) {
deba@2447
   186
    const double epsilon = 1e-8;
deba@2447
   187
deba@2447
   188
    if (p.x == q.x) return (p.y + q.y) / 2.0;
deba@2447
   189
deba@2447
   190
    if (sx < p.x + epsilon) return p.y;
deba@2447
   191
    if (sx < q.x + epsilon) return q.y;
deba@2447
   192
    
deba@2447
   193
    double a = q.x - p.x;
deba@2447
   194
    double b = (q.x - sx) * p.y - (p.x - sx) * q.y;    
deba@2447
   195
    double d = (q.x - sx) * (p.x - sx) * (p - q).normSquare();
deba@2447
   196
    return (b - sqrt(d)) / a;
deba@2447
   197
  }
deba@2447
   198
deba@2447
   199
  struct YLess {
deba@2447
   200
deba@2447
   201
deba@2447
   202
    YLess(const std::vector<Point>& points, double& sweep) 
deba@2447
   203
      : _points(points), _sweep(sweep) {}
deba@2447
   204
deba@2447
   205
    bool operator()(const Part& l, const Part& r) const {
deba@2447
   206
      const double epsilon = 1e-8;
deba@2447
   207
deba@2447
   208
      //      std::cerr << l << " vs " << r << std::endl;
deba@2447
   209
      double lbx = l.prev != -1 ?
deba@2447
   210
	intersection(_points[l.prev], _points[l.curr], _sweep) :
deba@2447
   211
	- std::numeric_limits<double>::infinity();
deba@2447
   212
      double rbx = r.prev != -1 ?
deba@2447
   213
	intersection(_points[r.prev], _points[r.curr], _sweep) :
deba@2447
   214
	- std::numeric_limits<double>::infinity();
deba@2447
   215
      double lex = l.next != -1 ?
deba@2447
   216
	intersection(_points[l.curr], _points[l.next], _sweep) :
deba@2447
   217
	std::numeric_limits<double>::infinity();
deba@2447
   218
      double rex = r.next != -1 ?
deba@2447
   219
	intersection(_points[r.curr], _points[r.next], _sweep) :
deba@2447
   220
	std::numeric_limits<double>::infinity();
deba@2447
   221
deba@2447
   222
      if (lbx > lex) std::swap(lbx, lex);
deba@2447
   223
      if (rbx > rex) std::swap(rbx, rex);
deba@2447
   224
deba@2447
   225
      if (lex < epsilon + rex && lbx + epsilon < rex) return true;
deba@2447
   226
      if (rex < epsilon + lex && rbx + epsilon < lex) return false;
deba@2447
   227
      return lex < rex;
deba@2447
   228
    }
deba@2447
   229
    
deba@2447
   230
    const std::vector<Point>& _points;
deba@2447
   231
    double& _sweep;
deba@2447
   232
  };
deba@2447
   233
  
deba@2447
   234
  struct BeachIt;
deba@2447
   235
  
deba@2447
   236
  typedef std::multimap<double, BeachIt> SpikeHeap;
deba@2447
   237
deba@2447
   238
  typedef std::multimap<Part, SpikeHeap::iterator, YLess> Beach;
deba@2447
   239
deba@2447
   240
  struct BeachIt {
deba@2447
   241
    Beach::iterator it;
deba@2447
   242
deba@2447
   243
    BeachIt(Beach::iterator iter) : it(iter) {}
deba@2447
   244
  };
deba@2447
   245
deba@2447
   246
}
deba@2447
   247
deba@2447
   248
inline void delaunay() {
alpar@2390
   249
  Counter cnt("Number of edges added: ");
deba@2447
   250
  
deba@2447
   251
  using namespace _delaunay_bits;
deba@2447
   252
deba@2447
   253
  typedef _delaunay_bits::Part Part;
deba@2447
   254
  typedef std::vector<std::pair<double, int> > SiteHeap;
deba@2447
   255
deba@2447
   256
deba@2447
   257
  std::vector<Point> points;
deba@2447
   258
  std::vector<Node> nodes;
deba@2447
   259
deba@2447
   260
  for (NodeIt it(g); it != INVALID; ++it) {
deba@2447
   261
    nodes.push_back(it);
deba@2447
   262
    points.push_back(coords[it]);
deba@2447
   263
  }
deba@2447
   264
deba@2447
   265
  SiteHeap siteheap(points.size());
deba@2447
   266
deba@2447
   267
  double sweep;
deba@2447
   268
deba@2447
   269
deba@2447
   270
  for (int i = 0; i < int(siteheap.size()); ++i) {
deba@2447
   271
    siteheap[i] = std::make_pair(points[i].x, i);
deba@2447
   272
  }
deba@2447
   273
  
deba@2447
   274
  std::sort(siteheap.begin(), siteheap.end());
deba@2447
   275
  sweep = siteheap.front().first;
deba@2447
   276
  
deba@2447
   277
  YLess yless(points, sweep);
deba@2447
   278
  Beach beach(yless);
deba@2447
   279
deba@2447
   280
  SpikeHeap spikeheap;
deba@2447
   281
deba@2447
   282
  std::set<std::pair<int, int> > edges;
deba@2447
   283
deba@2447
   284
  beach.insert(std::make_pair(Part(-1, siteheap[0].second, -1), 
deba@2447
   285
			      spikeheap.end()));
deba@2447
   286
  int siteindex = 1;
deba@2447
   287
deba@2447
   288
  while (siteindex < int(points.size()) || !spikeheap.empty()) {
deba@2447
   289
deba@2447
   290
    SpikeHeap::iterator spit = spikeheap.begin();
deba@2447
   291
deba@2447
   292
    if (siteindex < int(points.size()) && 
deba@2447
   293
	(spit == spikeheap.end() || siteheap[siteindex].first < spit->first)) {
deba@2447
   294
      int site = siteheap[siteindex].second;
deba@2447
   295
      sweep = siteheap[siteindex].first;
deba@2447
   296
          
deba@2447
   297
      Beach::iterator bit = beach.upper_bound(Part(site, site, site));
deba@2447
   298
      
deba@2447
   299
      if (bit->second != spikeheap.end()) {
deba@2447
   300
	spikeheap.erase(bit->second);	
deba@2447
   301
      }
deba@2447
   302
deba@2447
   303
      int prev = bit->first.prev;
deba@2447
   304
      int curr = bit->first.curr;
deba@2447
   305
      int next = bit->first.next;
deba@2447
   306
deba@2447
   307
      beach.erase(bit);
deba@2447
   308
      
deba@2447
   309
      SpikeHeap::iterator pit = spikeheap.end();
deba@2447
   310
      if (prev != -1 && 
deba@2447
   311
	  circle_form(points[prev], points[curr], points[site])) {
deba@2447
   312
	double x = circle_point(points[prev], points[curr], points[site]);
deba@2447
   313
	pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   314
	pit->second.it =
deba@2447
   315
	  beach.insert(std::make_pair(Part(prev, curr, site), pit));
deba@2447
   316
      } else {
deba@2447
   317
	beach.insert(std::make_pair(Part(prev, curr, site), pit));
deba@2447
   318
      }
deba@2447
   319
deba@2447
   320
      beach.insert(std::make_pair(Part(curr, site, curr), spikeheap.end()));
deba@2447
   321
      
deba@2447
   322
      SpikeHeap::iterator nit = spikeheap.end();
deba@2447
   323
      if (next != -1 && 
deba@2447
   324
	  circle_form(points[site], points[curr],points[next])) {
deba@2447
   325
	double x = circle_point(points[site], points[curr], points[next]);
deba@2447
   326
	nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   327
	nit->second.it =
deba@2447
   328
	  beach.insert(std::make_pair(Part(site, curr, next), nit));
deba@2447
   329
      } else {
deba@2447
   330
	beach.insert(std::make_pair(Part(site, curr, next), nit));
deba@2447
   331
      }
deba@2447
   332
      
deba@2447
   333
      ++siteindex;
deba@2447
   334
    } else {
deba@2447
   335
      sweep = spit->first;      
deba@2447
   336
deba@2447
   337
      Beach::iterator bit = spit->second.it;
deba@2447
   338
deba@2447
   339
      int prev = bit->first.prev;
deba@2447
   340
      int curr = bit->first.curr;
deba@2447
   341
      int next = bit->first.next;
deba@2447
   342
alpar@2390
   343
      {
deba@2447
   344
	std::pair<int, int> edge;
deba@2447
   345
deba@2447
   346
	edge = prev < curr ? 
deba@2447
   347
	  std::make_pair(prev, curr) : std::make_pair(curr, prev);
deba@2447
   348
	
deba@2447
   349
	if (edges.find(edge) == edges.end()) {
deba@2447
   350
	  edges.insert(edge);
deba@2447
   351
	  g.addEdge(nodes[prev], nodes[curr]);
deba@2447
   352
	  ++cnt;
deba@2447
   353
	}
deba@2447
   354
deba@2447
   355
	edge = curr < next ? 
deba@2447
   356
	  std::make_pair(curr, next) : std::make_pair(next, curr);
deba@2447
   357
	
deba@2447
   358
	if (edges.find(edge) == edges.end()) {
deba@2447
   359
	  edges.insert(edge);
deba@2447
   360
	  g.addEdge(nodes[curr], nodes[next]);
deba@2447
   361
	  ++cnt;
deba@2447
   362
	}
alpar@2390
   363
      }
deba@2447
   364
      
deba@2447
   365
      Beach::iterator pbit = bit; --pbit;
deba@2447
   366
      int ppv = pbit->first.prev;
deba@2447
   367
      Beach::iterator nbit = bit; ++nbit;
deba@2447
   368
      int nnt = nbit->first.next;
deba@2447
   369
deba@2447
   370
      if (bit->second != spikeheap.end()) spikeheap.erase(bit->second);
deba@2447
   371
      if (pbit->second != spikeheap.end()) spikeheap.erase(pbit->second);
deba@2447
   372
      if (nbit->second != spikeheap.end()) spikeheap.erase(nbit->second);
deba@2447
   373
deba@2447
   374
      beach.erase(nbit);
deba@2447
   375
      beach.erase(bit);
deba@2447
   376
      beach.erase(pbit);
deba@2447
   377
deba@2447
   378
      SpikeHeap::iterator pit = spikeheap.end();
deba@2447
   379
      if (ppv != -1 && ppv != next && 
deba@2447
   380
	  circle_form(points[ppv], points[prev], points[next])) {
deba@2447
   381
	double x = circle_point(points[ppv], points[prev], points[next]);
deba@2447
   382
	if (x < sweep) x = sweep;
deba@2447
   383
	pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   384
	pit->second.it =
deba@2447
   385
	  beach.insert(std::make_pair(Part(ppv, prev, next), pit));
deba@2447
   386
      } else {
deba@2447
   387
	beach.insert(std::make_pair(Part(ppv, prev, next), pit));
alpar@2390
   388
      }
deba@2447
   389
deba@2447
   390
      SpikeHeap::iterator nit = spikeheap.end();
deba@2447
   391
      if (nnt != -1 && prev != nnt &&
deba@2447
   392
	  circle_form(points[prev], points[next], points[nnt])) {
deba@2447
   393
	double x = circle_point(points[prev], points[next], points[nnt]);
deba@2447
   394
	if (x < sweep) x = sweep;
deba@2447
   395
	nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   396
	nit->second.it =
deba@2447
   397
	  beach.insert(std::make_pair(Part(prev, next, nnt), nit));
deba@2447
   398
      } else {
deba@2447
   399
	beach.insert(std::make_pair(Part(prev, next, nnt), nit));
deba@2447
   400
      }
deba@2447
   401
      
alpar@2390
   402
    }
deba@2447
   403
  }
deba@2447
   404
deba@2447
   405
  for (Beach::iterator it = beach.begin(); it != beach.end(); ++it) {
deba@2447
   406
    int curr = it->first.curr;
deba@2447
   407
    int next = it->first.next;
deba@2447
   408
deba@2447
   409
    if (next == -1) continue;
deba@2447
   410
deba@2447
   411
    std::pair<int, int> edge;
deba@2447
   412
deba@2447
   413
    edge = curr < next ? 
deba@2447
   414
      std::make_pair(curr, next) : std::make_pair(next, curr);
deba@2447
   415
    
deba@2447
   416
    if (edges.find(edge) == edges.end()) {
deba@2447
   417
      edges.insert(edge);
deba@2447
   418
      g.addEdge(nodes[curr], nodes[next]);
deba@2447
   419
      ++cnt;
deba@2447
   420
    }
deba@2447
   421
  }
alpar@2390
   422
}
alpar@2390
   423
alpar@2390
   424
void sparse(int d) 
alpar@2390
   425
{
alpar@2390
   426
  Counter cnt("Number of edges removed: ");
alpar@2390
   427
  Bfs<ListUGraph> bfs(g);
alpar@2390
   428
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   429
      ei!=edges.rend();++ei)
alpar@2390
   430
    {
alpar@2390
   431
      Node a=g.source(*ei);
alpar@2390
   432
      Node b=g.target(*ei);
alpar@2390
   433
      g.erase(*ei);
alpar@2390
   434
      bfs.run(a,b);
alpar@2390
   435
      if(bfs.predEdge(b)==INVALID || bfs.dist(b)>d)
alpar@2390
   436
	g.addEdge(a,b);
alpar@2390
   437
      else cnt++;
alpar@2390
   438
    }
alpar@2390
   439
}
alpar@2390
   440
alpar@2390
   441
void sparse2(int d) 
alpar@2390
   442
{
alpar@2390
   443
  Counter cnt("Number of edges removed: ");
alpar@2390
   444
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   445
      ei!=edges.rend();++ei)
alpar@2390
   446
    {
alpar@2390
   447
      Node a=g.source(*ei);
alpar@2390
   448
      Node b=g.target(*ei);
alpar@2390
   449
      g.erase(*ei);
alpar@2390
   450
      ConstMap<Edge,int> cegy(1);
alpar@2390
   451
      Suurballe<ListUGraph,ConstMap<Edge,int> > sur(g,cegy,a,b);
alpar@2390
   452
      int k=sur.run(2);
alpar@2390
   453
      if(k<2 || sur.totalLength()>d)
alpar@2390
   454
	g.addEdge(a,b);
alpar@2390
   455
      else cnt++;
alpar@2390
   456
//       else std::cout << "Remove edge " << g.id(a) << "-" << g.id(b) << '\n';
alpar@2390
   457
    }
alpar@2390
   458
}
alpar@2390
   459
alpar@2390
   460
void sparseTriangle(int d)
alpar@2390
   461
{
alpar@2390
   462
  Counter cnt("Number of edges added: ");
alpar@2390
   463
  std::vector<Pedge> pedges;
alpar@2390
   464
  for(NodeIt n(g);n!=INVALID;++n) 
alpar@2390
   465
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
alpar@2390
   466
      {
alpar@2390
   467
	Pedge p;
alpar@2390
   468
	p.a=n;
alpar@2390
   469
	p.b=m;
alpar@2390
   470
	p.len=(coords[m]-coords[n]).normSquare();
alpar@2390
   471
	pedges.push_back(p);
alpar@2390
   472
      }
alpar@2390
   473
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2390
   474
  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
alpar@2390
   475
    {
alpar@2390
   476
      Line li(pi->a,pi->b);
alpar@2390
   477
      UEdgeIt e(g);
alpar@2390
   478
      for(;e!=INVALID && !cross(e,li);++e) ;
alpar@2390
   479
      UEdge ne;
alpar@2390
   480
      if(e==INVALID) {
alpar@2390
   481
	ConstMap<Edge,int> cegy(1);
alpar@2390
   482
	Suurballe<ListUGraph,ConstMap<Edge,int> >
alpar@2390
   483
	  sur(g,cegy,pi->a,pi->b);
alpar@2390
   484
	int k=sur.run(2);
alpar@2390
   485
	if(k<2 || sur.totalLength()>d)
alpar@2390
   486
	  {
alpar@2390
   487
	    ne=g.addEdge(pi->a,pi->b);
alpar@2390
   488
	    edges.push_back(ne);
alpar@2390
   489
	    cnt++;
alpar@2390
   490
	  }
alpar@2390
   491
      }
alpar@2390
   492
    }
alpar@2390
   493
}
alpar@2390
   494
deba@2447
   495
template <typename UGraph, typename CoordMap>
deba@2447
   496
class LengthSquareMap {
deba@2447
   497
public:
deba@2447
   498
  typedef typename UGraph::UEdge Key;
deba@2447
   499
  typedef typename CoordMap::Value::Value Value;
deba@2447
   500
deba@2447
   501
  LengthSquareMap(const UGraph& ugraph, const CoordMap& coords)
deba@2447
   502
    : _ugraph(ugraph), _coords(coords) {}
deba@2447
   503
deba@2447
   504
  Value operator[](const Key& key) const {
deba@2447
   505
    return (_coords[_ugraph.target(key)] -
deba@2447
   506
	    _coords[_ugraph.source(key)]).normSquare();
deba@2447
   507
  }
deba@2447
   508
deba@2447
   509
private:
deba@2447
   510
deba@2447
   511
  const UGraph& _ugraph;
deba@2447
   512
  const CoordMap& _coords;
deba@2447
   513
};
deba@2447
   514
alpar@2390
   515
void minTree() {
alpar@2390
   516
  std::vector<Pedge> pedges;
alpar@2402
   517
  Timer T;
deba@2447
   518
  std::cout << T.realTime() << "s: Creating delaunay triangulation...\n";
deba@2447
   519
  delaunay();
deba@2447
   520
  std::cout << T.realTime() << "s: Calculating spanning tree...\n";
deba@2447
   521
  LengthSquareMap<ListUGraph, ListUGraph::NodeMap<Point> > ls(g, coords);
deba@2447
   522
  ListUGraph::UEdgeMap<bool> tree(g);
deba@2447
   523
  kruskal(g, ls, tree);
deba@2447
   524
  std::cout << T.realTime() << "s: Removing non tree edges...\n";
deba@2447
   525
  std::vector<UEdge> remove;
deba@2447
   526
  for (UEdgeIt e(g); e != INVALID; ++e) {
deba@2447
   527
    if (!tree[e]) remove.push_back(e);
deba@2447
   528
  }
deba@2447
   529
  for(int i = 0; i < int(remove.size()); ++i) {
deba@2447
   530
    g.erase(remove[i]);
deba@2447
   531
  }
alpar@2402
   532
  std::cout << T.realTime() << "s: Done\n";
alpar@2390
   533
}
alpar@2390
   534
alpar@2446
   535
void tsp2() 
alpar@2446
   536
{
alpar@2446
   537
  std::cout << "Find a tree..." << std::endl;
alpar@2446
   538
alpar@2446
   539
  minTree();
alpar@2446
   540
alpar@2446
   541
  std::cout << "Total edge length (tree) : " << totalLen() << std::endl;
alpar@2446
   542
alpar@2446
   543
  std::cout << "Make it Euler..." << std::endl;
alpar@2446
   544
alpar@2446
   545
  {
alpar@2446
   546
    std::vector<Node> leafs;
alpar@2446
   547
    for(NodeIt n(g);n!=INVALID;++n)
alpar@2446
   548
      if(countIncEdges(g,n)%2==1) leafs.push_back(n);
alpar@2448
   549
alpar@2448
   550
//    for(unsigned int i=0;i<leafs.size();i+=2)
alpar@2448
   551
//       g.addEdge(leafs[i],leafs[i+1]);
alpar@2448
   552
alpar@2448
   553
    std::vector<Pedge> pedges;
alpar@2448
   554
    for(unsigned int i=0;i<leafs.size()-1;i++)
alpar@2448
   555
      for(unsigned int j=i+1;j<leafs.size();j++)
alpar@2448
   556
	{
alpar@2448
   557
	  Node n=leafs[i];
alpar@2448
   558
	  Node m=leafs[j];
alpar@2448
   559
	  Pedge p;
alpar@2448
   560
	  p.a=n;
alpar@2448
   561
	  p.b=m;
alpar@2448
   562
	  p.len=(coords[m]-coords[n]).normSquare();
alpar@2448
   563
	  pedges.push_back(p);
alpar@2448
   564
	}
alpar@2448
   565
    std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2448
   566
    for(unsigned int i=0;i<pedges.size();i++)
alpar@2448
   567
      if(countIncEdges(g,pedges[i].a)%2 &&
alpar@2448
   568
	 countIncEdges(g,pedges[i].b)%2)
alpar@2448
   569
	g.addEdge(pedges[i].a,pedges[i].b);
alpar@2446
   570
  }
alpar@2446
   571
alpar@2446
   572
  for(NodeIt n(g);n!=INVALID;++n)
alpar@2448
   573
    if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
alpar@2446
   574
      std::cout << "GEBASZ!!!" << std::endl;
alpar@2446
   575
  
alpar@2448
   576
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2448
   577
    if(g.source(e)==g.target(e))
alpar@2448
   578
      std::cout << "LOOP GEBASZ!!!" << std::endl;
alpar@2448
   579
  
alpar@2446
   580
  std::cout << "Number of edges : " << countUEdges(g) << std::endl;
alpar@2446
   581
  
alpar@2446
   582
  std::cout << "Total edge length (euler) : " << totalLen() << std::endl;
alpar@2446
   583
alpar@2448
   584
  ListUGraph::UEdgeMap<Edge> enext(g);
alpar@2446
   585
  {
alpar@2446
   586
    UEulerIt<ListUGraph> e(g);
alpar@2448
   587
    Edge eo=e;
alpar@2448
   588
    Edge ef=e;
alpar@2446
   589
//     std::cout << "Tour edge: " << g.id(UEdge(e)) << std::endl;      
alpar@2446
   590
    for(++e;e!=INVALID;++e)
alpar@2446
   591
      {
alpar@2446
   592
// 	std::cout << "Tour edge: " << g.id(UEdge(e)) << std::endl;      
alpar@2446
   593
	enext[eo]=e;
alpar@2446
   594
	eo=e;
alpar@2446
   595
      }
alpar@2446
   596
    enext[eo]=ef;
alpar@2446
   597
  }
alpar@2448
   598
    
alpar@2446
   599
  std::cout << "Creating a tour from that..." << std::endl;
alpar@2446
   600
  
alpar@2446
   601
  int nnum = countNodes(g);
alpar@2446
   602
  int ednum = countUEdges(g);
alpar@2446
   603
  
alpar@2448
   604
  for(Edge p=enext[UEdgeIt(g)];ednum>nnum;p=enext[p]) 
alpar@2446
   605
    {
alpar@2446
   606
//       std::cout << "Checking edge " << g.id(p) << std::endl;      
alpar@2448
   607
      Edge e=enext[p];
alpar@2448
   608
      Edge f=enext[e];
alpar@2448
   609
      Node n2=g.source(f);
alpar@2446
   610
      Node n1=g.oppositeNode(n2,e);
alpar@2446
   611
      Node n3=g.oppositeNode(n2,f);
alpar@2446
   612
      if(countIncEdges(g,n2)>2)
alpar@2446
   613
	{
alpar@2446
   614
// 	  std::cout << "Remove an Edge" << std::endl;
alpar@2448
   615
	  Edge ff=enext[f];
alpar@2446
   616
	  g.erase(e);
alpar@2446
   617
	  g.erase(f);
alpar@2448
   618
	  if(n1!=n3)
alpar@2448
   619
	    {
alpar@2448
   620
	      Edge ne=g.direct(g.addEdge(n1,n3),n1);
alpar@2448
   621
	      enext[p]=ne;
alpar@2448
   622
	      enext[ne]=ff;
alpar@2448
   623
	      ednum--;
alpar@2448
   624
	    }
alpar@2448
   625
	  else {
alpar@2448
   626
	    enext[p]=ff;
alpar@2448
   627
	    ednum-=2;
alpar@2448
   628
	  }
alpar@2446
   629
	}
alpar@2446
   630
    }
alpar@2446
   631
alpar@2446
   632
  std::cout << "Total edge length (tour) : " << totalLen() << std::endl;
alpar@2446
   633
alpar@2448
   634
  std::cout << "2-opt the tour..." << std::endl;
alpar@2448
   635
  
alpar@2446
   636
  tsp_improve();
alpar@2446
   637
  
alpar@2446
   638
  std::cout << "Total edge length (2-opt tour) : " << totalLen() << std::endl;
alpar@2446
   639
}
alpar@2390
   640
alpar@2390
   641
deba@2410
   642
int main(int argc,const char **argv) 
alpar@2390
   643
{
alpar@2390
   644
  ArgParser ap(argc,argv);
alpar@2390
   645
alpar@2402
   646
//   bool eps;
alpar@2390
   647
  bool disc_d, square_d, gauss_d;
alpar@2402
   648
//   bool tsp_a,two_a,tree_a;
alpar@2390
   649
  int num_of_cities=1;
alpar@2390
   650
  double area=1;
alpar@2390
   651
  N=100;
alpar@2402
   652
//   girth=10;
alpar@2390
   653
  std::string ndist("disc");
alpar@2402
   654
  ap.refOption("n", "Number of nodes (default is 100)", N)
alpar@2402
   655
    .intOption("g", "Girth parameter (default is 10)", 10)
alpar@2402
   656
    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
alpar@2402
   657
    .refOption("area", "Full relative area of the cities (default is 1)", area)
alpar@2402
   658
    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
alpar@2390
   659
    .optionGroup("dist", "disc")
alpar@2402
   660
    .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
alpar@2390
   661
    .optionGroup("dist", "square")
alpar@2402
   662
    .refOption("gauss",
alpar@2390
   663
	    "Nodes are located according to a two-dim gauss distribution",
alpar@2390
   664
	    gauss_d)
alpar@2390
   665
    .optionGroup("dist", "gauss")
alpar@2390
   666
//     .mandatoryGroup("dist")
alpar@2390
   667
    .onlyOneGroup("dist")
alpar@2402
   668
    .boolOption("eps", "Also generate .eps output (prefix.eps)")
alpar@2446
   669
    .boolOption("dir", "Directed graph is generated (each edges are replaced by two directed ones)")
alpar@2402
   670
    .boolOption("2con", "Create a two connected planar graph")
alpar@2390
   671
    .optionGroup("alg","2con")
alpar@2402
   672
    .boolOption("tree", "Create a min. cost spanning tree")
alpar@2390
   673
    .optionGroup("alg","tree")
alpar@2402
   674
    .boolOption("tsp", "Create a TSP tour")
alpar@2390
   675
    .optionGroup("alg","tsp")
alpar@2446
   676
    .boolOption("tsp2", "Create a TSP tour (tree based)")
alpar@2446
   677
    .optionGroup("alg","tsp2")
deba@2447
   678
    .boolOption("dela", "Delaunay triangulation graph")
deba@2447
   679
    .optionGroup("alg","dela")
alpar@2390
   680
    .onlyOneGroup("alg")
deba@2447
   681
    .boolOption("rand", "Use time seed for random number generator")
deba@2447
   682
    .optionGroup("rand", "rand")
deba@2447
   683
    .intOption("seed", "Random seed", -1)
deba@2447
   684
    .optionGroup("rand", "seed")
deba@2447
   685
    .onlyOneGroup("rand")
alpar@2390
   686
    .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
alpar@2390
   687
    .run();
deba@2447
   688
deba@2447
   689
  if (ap["rand"]) {
deba@2447
   690
    int seed = time(0);
deba@2447
   691
    std::cout << "Random number seed: " << seed << std::endl;
deba@2447
   692
    rnd = Random(seed);
deba@2447
   693
  }
deba@2447
   694
  if (ap.given("seed")) {
deba@2447
   695
    int seed = ap["seed"];
deba@2447
   696
    std::cout << "Random number seed: " << seed << std::endl;
deba@2447
   697
    rnd = Random(seed);
deba@2447
   698
  }
alpar@2390
   699
  
alpar@2390
   700
  std::string prefix;
alpar@2390
   701
  switch(ap.files().size()) 
alpar@2390
   702
    {
alpar@2390
   703
    case 0:
alpar@2390
   704
      prefix="lgf-gen-out";
alpar@2390
   705
      break;
alpar@2390
   706
    case 1:
alpar@2390
   707
      prefix=ap.files()[0];
alpar@2390
   708
      break;
alpar@2390
   709
    default:
alpar@2390
   710
      std::cerr << "\nAt most one prefix can be given\n\n";
alpar@2390
   711
      exit(1);
alpar@2390
   712
    }
alpar@2390
   713
  
alpar@2390
   714
  double sum_sizes=0;
alpar@2390
   715
  std::vector<double> sizes;
alpar@2390
   716
  std::vector<double> cum_sizes;
alpar@2390
   717
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   718
    {
alpar@2390
   719
      // 	sum_sizes+=rnd.exponential();
alpar@2390
   720
      double d=rnd();
alpar@2390
   721
      sum_sizes+=d;
alpar@2390
   722
      sizes.push_back(d);
alpar@2390
   723
      cum_sizes.push_back(sum_sizes);
alpar@2390
   724
    }
alpar@2390
   725
  int i=0;
alpar@2390
   726
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   727
    {
alpar@2390
   728
      Point center=(num_of_cities==1?Point(0,0):rnd.disc());
alpar@2390
   729
      if(gauss_d)
alpar@2390
   730
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   731
	  Node n=g.addNode();
alpar@2390
   732
	  nodes.push_back(n);
alpar@2390
   733
	  coords[n]=center+rnd.gauss2()*area*
alpar@2390
   734
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   735
	}
alpar@2390
   736
      else if(square_d)
alpar@2390
   737
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   738
	  Node n=g.addNode();
alpar@2390
   739
	  nodes.push_back(n);
alpar@2390
   740
	  coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
alpar@2390
   741
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   742
	}
alpar@2390
   743
      else if(disc_d || true)
alpar@2390
   744
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   745
	  Node n=g.addNode();
alpar@2390
   746
	  nodes.push_back(n);
alpar@2390
   747
	  coords[n]=center+rnd.disc()*area*
alpar@2390
   748
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   749
	}
alpar@2390
   750
    }
deba@2447
   751
deba@2447
   752
//   for (ListUGraph::NodeIt n(g); n != INVALID; ++n) {
deba@2447
   753
//     std::cerr << coords[n] << std::endl;
deba@2447
   754
//   }
alpar@2390
   755
  
alpar@2402
   756
  if(ap["tsp"]) {
alpar@2390
   757
    tsp();
alpar@2390
   758
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
alpar@2390
   759
  }
alpar@2446
   760
  if(ap["tsp2"]) {
alpar@2446
   761
    tsp2();
alpar@2446
   762
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
alpar@2446
   763
  }
alpar@2402
   764
  else if(ap["2con"]) {
alpar@2390
   765
    std::cout << "Make triangles\n";
alpar@2390
   766
    //   triangle();
alpar@2402
   767
    sparseTriangle(ap["g"]);
alpar@2390
   768
    std::cout << "Make it sparser\n";
alpar@2402
   769
    sparse2(ap["g"]);
alpar@2390
   770
  }
alpar@2402
   771
  else if(ap["tree"]) {
alpar@2390
   772
    minTree();
alpar@2390
   773
  }
deba@2447
   774
  else if(ap["dela"]) {
deba@2447
   775
    delaunay();
deba@2447
   776
  }
alpar@2390
   777
  
alpar@2390
   778
alpar@2390
   779
  std::cout << "Number of nodes    : " << countNodes(g) << std::endl;
alpar@2390
   780
  std::cout << "Number of edges    : " << countUEdges(g) << std::endl;
alpar@2390
   781
  double tlen=0;
alpar@2390
   782
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2390
   783
    tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
alpar@2390
   784
  std::cout << "Total edge length  : " << tlen << std::endl;
alpar@2448
   785
alpar@2402
   786
  if(ap["eps"])
alpar@2390
   787
    graphToEps(g,prefix+".eps").
alpar@2390
   788
      scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
alpar@2390
   789
      coords(coords).run();
alpar@2448
   790
  
alpar@2446
   791
  if(ap["dir"])
alpar@2446
   792
    GraphWriter<ListUGraph>(prefix+".lgf",g).
alpar@2446
   793
      writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
alpar@2446
   794
      writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).
alpar@2446
   795
      run();
alpar@2446
   796
  else UGraphWriter<ListUGraph>(prefix+".lgf",g).
alpar@2446
   797
	 writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
alpar@2446
   798
	 writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).
alpar@2446
   799
	 run();
alpar@2390
   800
}
alpar@2390
   801