tools/lgf-gen.cc
author deba
Tue, 09 Oct 2007 16:22:04 +0000
changeset 2490 31a93dd6f714
parent 2448 ab899ae3505f
child 2491 b63ae56979ef
permissions -rw-r--r--
Minor bug fixes in documentation
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@2453
   284
  int siteindex = 0;
deba@2453
   285
  {
deba@2453
   286
    SiteHeap front;
deba@2453
   287
deba@2453
   288
    while (siteindex < int(siteheap.size()) &&
deba@2453
   289
	   siteheap[0].first == siteheap[siteindex].first) {
deba@2453
   290
      front.push_back(std::make_pair(points[siteheap[siteindex].second].y,
deba@2453
   291
				     siteheap[siteindex].second));
deba@2453
   292
      ++siteindex;
deba@2453
   293
    }
deba@2453
   294
    
deba@2453
   295
    std::sort(front.begin(), front.end());
deba@2453
   296
deba@2453
   297
    for (int i = 0; i < int(front.size()); ++i) {
deba@2453
   298
      int prev = (i == 0 ? -1 : front[i - 1].second);
deba@2453
   299
      int curr = front[i].second;
deba@2453
   300
      int next = (i + 1 == int(front.size()) ? -1 : front[i + 1].second);
deba@2453
   301
deba@2453
   302
      beach.insert(std::make_pair(Part(prev, curr, next), 
deba@2453
   303
				  spikeheap.end()));      
deba@2453
   304
    }
deba@2453
   305
  }
deba@2447
   306
deba@2447
   307
  while (siteindex < int(points.size()) || !spikeheap.empty()) {
deba@2447
   308
deba@2447
   309
    SpikeHeap::iterator spit = spikeheap.begin();
deba@2447
   310
deba@2447
   311
    if (siteindex < int(points.size()) && 
deba@2447
   312
	(spit == spikeheap.end() || siteheap[siteindex].first < spit->first)) {
deba@2447
   313
      int site = siteheap[siteindex].second;
deba@2447
   314
      sweep = siteheap[siteindex].first;
deba@2447
   315
          
deba@2447
   316
      Beach::iterator bit = beach.upper_bound(Part(site, site, site));
deba@2447
   317
      
deba@2447
   318
      if (bit->second != spikeheap.end()) {
deba@2447
   319
	spikeheap.erase(bit->second);	
deba@2447
   320
      }
deba@2447
   321
deba@2447
   322
      int prev = bit->first.prev;
deba@2447
   323
      int curr = bit->first.curr;
deba@2447
   324
      int next = bit->first.next;
deba@2447
   325
deba@2447
   326
      beach.erase(bit);
deba@2447
   327
      
deba@2447
   328
      SpikeHeap::iterator pit = spikeheap.end();
deba@2447
   329
      if (prev != -1 && 
deba@2447
   330
	  circle_form(points[prev], points[curr], points[site])) {
deba@2447
   331
	double x = circle_point(points[prev], points[curr], points[site]);
deba@2447
   332
	pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   333
	pit->second.it =
deba@2447
   334
	  beach.insert(std::make_pair(Part(prev, curr, site), pit));
deba@2447
   335
      } else {
deba@2447
   336
	beach.insert(std::make_pair(Part(prev, curr, site), pit));
deba@2447
   337
      }
deba@2447
   338
deba@2447
   339
      beach.insert(std::make_pair(Part(curr, site, curr), spikeheap.end()));
deba@2447
   340
      
deba@2447
   341
      SpikeHeap::iterator nit = spikeheap.end();
deba@2447
   342
      if (next != -1 && 
deba@2447
   343
	  circle_form(points[site], points[curr],points[next])) {
deba@2447
   344
	double x = circle_point(points[site], points[curr], points[next]);
deba@2447
   345
	nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   346
	nit->second.it =
deba@2447
   347
	  beach.insert(std::make_pair(Part(site, curr, next), nit));
deba@2447
   348
      } else {
deba@2447
   349
	beach.insert(std::make_pair(Part(site, curr, next), nit));
deba@2447
   350
      }
deba@2447
   351
      
deba@2447
   352
      ++siteindex;
deba@2447
   353
    } else {
deba@2447
   354
      sweep = spit->first;      
deba@2447
   355
deba@2447
   356
      Beach::iterator bit = spit->second.it;
deba@2447
   357
deba@2447
   358
      int prev = bit->first.prev;
deba@2447
   359
      int curr = bit->first.curr;
deba@2447
   360
      int next = bit->first.next;
deba@2447
   361
alpar@2390
   362
      {
deba@2447
   363
	std::pair<int, int> edge;
deba@2447
   364
deba@2447
   365
	edge = prev < curr ? 
deba@2447
   366
	  std::make_pair(prev, curr) : std::make_pair(curr, prev);
deba@2447
   367
	
deba@2447
   368
	if (edges.find(edge) == edges.end()) {
deba@2447
   369
	  edges.insert(edge);
deba@2447
   370
	  g.addEdge(nodes[prev], nodes[curr]);
deba@2447
   371
	  ++cnt;
deba@2447
   372
	}
deba@2447
   373
deba@2447
   374
	edge = curr < next ? 
deba@2447
   375
	  std::make_pair(curr, next) : std::make_pair(next, curr);
deba@2447
   376
	
deba@2447
   377
	if (edges.find(edge) == edges.end()) {
deba@2447
   378
	  edges.insert(edge);
deba@2447
   379
	  g.addEdge(nodes[curr], nodes[next]);
deba@2447
   380
	  ++cnt;
deba@2447
   381
	}
alpar@2390
   382
      }
deba@2447
   383
      
deba@2447
   384
      Beach::iterator pbit = bit; --pbit;
deba@2447
   385
      int ppv = pbit->first.prev;
deba@2447
   386
      Beach::iterator nbit = bit; ++nbit;
deba@2447
   387
      int nnt = nbit->first.next;
deba@2447
   388
deba@2447
   389
      if (bit->second != spikeheap.end()) spikeheap.erase(bit->second);
deba@2447
   390
      if (pbit->second != spikeheap.end()) spikeheap.erase(pbit->second);
deba@2447
   391
      if (nbit->second != spikeheap.end()) spikeheap.erase(nbit->second);
deba@2447
   392
deba@2447
   393
      beach.erase(nbit);
deba@2447
   394
      beach.erase(bit);
deba@2447
   395
      beach.erase(pbit);
deba@2447
   396
deba@2447
   397
      SpikeHeap::iterator pit = spikeheap.end();
deba@2447
   398
      if (ppv != -1 && ppv != next && 
deba@2447
   399
	  circle_form(points[ppv], points[prev], points[next])) {
deba@2447
   400
	double x = circle_point(points[ppv], points[prev], points[next]);
deba@2447
   401
	if (x < sweep) x = sweep;
deba@2447
   402
	pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   403
	pit->second.it =
deba@2447
   404
	  beach.insert(std::make_pair(Part(ppv, prev, next), pit));
deba@2447
   405
      } else {
deba@2447
   406
	beach.insert(std::make_pair(Part(ppv, prev, next), pit));
alpar@2390
   407
      }
deba@2447
   408
deba@2447
   409
      SpikeHeap::iterator nit = spikeheap.end();
deba@2447
   410
      if (nnt != -1 && prev != nnt &&
deba@2447
   411
	  circle_form(points[prev], points[next], points[nnt])) {
deba@2447
   412
	double x = circle_point(points[prev], points[next], points[nnt]);
deba@2447
   413
	if (x < sweep) x = sweep;
deba@2447
   414
	nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
deba@2447
   415
	nit->second.it =
deba@2447
   416
	  beach.insert(std::make_pair(Part(prev, next, nnt), nit));
deba@2447
   417
      } else {
deba@2447
   418
	beach.insert(std::make_pair(Part(prev, next, nnt), nit));
deba@2447
   419
      }
deba@2447
   420
      
alpar@2390
   421
    }
deba@2447
   422
  }
deba@2447
   423
deba@2447
   424
  for (Beach::iterator it = beach.begin(); it != beach.end(); ++it) {
deba@2447
   425
    int curr = it->first.curr;
deba@2447
   426
    int next = it->first.next;
deba@2447
   427
deba@2447
   428
    if (next == -1) continue;
deba@2447
   429
deba@2447
   430
    std::pair<int, int> edge;
deba@2447
   431
deba@2447
   432
    edge = curr < next ? 
deba@2447
   433
      std::make_pair(curr, next) : std::make_pair(next, curr);
deba@2447
   434
    
deba@2447
   435
    if (edges.find(edge) == edges.end()) {
deba@2447
   436
      edges.insert(edge);
deba@2447
   437
      g.addEdge(nodes[curr], nodes[next]);
deba@2447
   438
      ++cnt;
deba@2447
   439
    }
deba@2447
   440
  }
alpar@2390
   441
}
alpar@2390
   442
alpar@2390
   443
void sparse(int d) 
alpar@2390
   444
{
alpar@2390
   445
  Counter cnt("Number of edges removed: ");
alpar@2390
   446
  Bfs<ListUGraph> bfs(g);
alpar@2390
   447
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   448
      ei!=edges.rend();++ei)
alpar@2390
   449
    {
alpar@2390
   450
      Node a=g.source(*ei);
alpar@2390
   451
      Node b=g.target(*ei);
alpar@2390
   452
      g.erase(*ei);
alpar@2390
   453
      bfs.run(a,b);
alpar@2390
   454
      if(bfs.predEdge(b)==INVALID || bfs.dist(b)>d)
alpar@2390
   455
	g.addEdge(a,b);
alpar@2390
   456
      else cnt++;
alpar@2390
   457
    }
alpar@2390
   458
}
alpar@2390
   459
alpar@2390
   460
void sparse2(int d) 
alpar@2390
   461
{
alpar@2390
   462
  Counter cnt("Number of edges removed: ");
alpar@2390
   463
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   464
      ei!=edges.rend();++ei)
alpar@2390
   465
    {
alpar@2390
   466
      Node a=g.source(*ei);
alpar@2390
   467
      Node b=g.target(*ei);
alpar@2390
   468
      g.erase(*ei);
alpar@2390
   469
      ConstMap<Edge,int> cegy(1);
alpar@2390
   470
      Suurballe<ListUGraph,ConstMap<Edge,int> > sur(g,cegy,a,b);
alpar@2390
   471
      int k=sur.run(2);
alpar@2390
   472
      if(k<2 || sur.totalLength()>d)
alpar@2390
   473
	g.addEdge(a,b);
alpar@2390
   474
      else cnt++;
alpar@2390
   475
//       else std::cout << "Remove edge " << g.id(a) << "-" << g.id(b) << '\n';
alpar@2390
   476
    }
alpar@2390
   477
}
alpar@2390
   478
alpar@2390
   479
void sparseTriangle(int d)
alpar@2390
   480
{
alpar@2390
   481
  Counter cnt("Number of edges added: ");
alpar@2390
   482
  std::vector<Pedge> pedges;
alpar@2390
   483
  for(NodeIt n(g);n!=INVALID;++n) 
alpar@2390
   484
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
alpar@2390
   485
      {
alpar@2390
   486
	Pedge p;
alpar@2390
   487
	p.a=n;
alpar@2390
   488
	p.b=m;
alpar@2390
   489
	p.len=(coords[m]-coords[n]).normSquare();
alpar@2390
   490
	pedges.push_back(p);
alpar@2390
   491
      }
alpar@2390
   492
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2390
   493
  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
alpar@2390
   494
    {
alpar@2390
   495
      Line li(pi->a,pi->b);
alpar@2390
   496
      UEdgeIt e(g);
alpar@2390
   497
      for(;e!=INVALID && !cross(e,li);++e) ;
alpar@2390
   498
      UEdge ne;
alpar@2390
   499
      if(e==INVALID) {
alpar@2390
   500
	ConstMap<Edge,int> cegy(1);
alpar@2390
   501
	Suurballe<ListUGraph,ConstMap<Edge,int> >
alpar@2390
   502
	  sur(g,cegy,pi->a,pi->b);
alpar@2390
   503
	int k=sur.run(2);
alpar@2390
   504
	if(k<2 || sur.totalLength()>d)
alpar@2390
   505
	  {
alpar@2390
   506
	    ne=g.addEdge(pi->a,pi->b);
alpar@2390
   507
	    edges.push_back(ne);
alpar@2390
   508
	    cnt++;
alpar@2390
   509
	  }
alpar@2390
   510
      }
alpar@2390
   511
    }
alpar@2390
   512
}
alpar@2390
   513
deba@2447
   514
template <typename UGraph, typename CoordMap>
deba@2447
   515
class LengthSquareMap {
deba@2447
   516
public:
deba@2447
   517
  typedef typename UGraph::UEdge Key;
deba@2447
   518
  typedef typename CoordMap::Value::Value Value;
deba@2447
   519
deba@2447
   520
  LengthSquareMap(const UGraph& ugraph, const CoordMap& coords)
deba@2447
   521
    : _ugraph(ugraph), _coords(coords) {}
deba@2447
   522
deba@2447
   523
  Value operator[](const Key& key) const {
deba@2447
   524
    return (_coords[_ugraph.target(key)] -
deba@2447
   525
	    _coords[_ugraph.source(key)]).normSquare();
deba@2447
   526
  }
deba@2447
   527
deba@2447
   528
private:
deba@2447
   529
deba@2447
   530
  const UGraph& _ugraph;
deba@2447
   531
  const CoordMap& _coords;
deba@2447
   532
};
deba@2447
   533
alpar@2390
   534
void minTree() {
alpar@2390
   535
  std::vector<Pedge> pedges;
alpar@2402
   536
  Timer T;
deba@2447
   537
  std::cout << T.realTime() << "s: Creating delaunay triangulation...\n";
deba@2447
   538
  delaunay();
deba@2447
   539
  std::cout << T.realTime() << "s: Calculating spanning tree...\n";
deba@2447
   540
  LengthSquareMap<ListUGraph, ListUGraph::NodeMap<Point> > ls(g, coords);
deba@2447
   541
  ListUGraph::UEdgeMap<bool> tree(g);
deba@2447
   542
  kruskal(g, ls, tree);
deba@2447
   543
  std::cout << T.realTime() << "s: Removing non tree edges...\n";
deba@2447
   544
  std::vector<UEdge> remove;
deba@2447
   545
  for (UEdgeIt e(g); e != INVALID; ++e) {
deba@2447
   546
    if (!tree[e]) remove.push_back(e);
deba@2447
   547
  }
deba@2447
   548
  for(int i = 0; i < int(remove.size()); ++i) {
deba@2447
   549
    g.erase(remove[i]);
deba@2447
   550
  }
alpar@2402
   551
  std::cout << T.realTime() << "s: Done\n";
alpar@2390
   552
}
alpar@2390
   553
alpar@2446
   554
void tsp2() 
alpar@2446
   555
{
alpar@2446
   556
  std::cout << "Find a tree..." << std::endl;
alpar@2446
   557
alpar@2446
   558
  minTree();
alpar@2446
   559
alpar@2446
   560
  std::cout << "Total edge length (tree) : " << totalLen() << std::endl;
alpar@2446
   561
alpar@2446
   562
  std::cout << "Make it Euler..." << std::endl;
alpar@2446
   563
alpar@2446
   564
  {
alpar@2446
   565
    std::vector<Node> leafs;
alpar@2446
   566
    for(NodeIt n(g);n!=INVALID;++n)
alpar@2446
   567
      if(countIncEdges(g,n)%2==1) leafs.push_back(n);
alpar@2448
   568
alpar@2448
   569
//    for(unsigned int i=0;i<leafs.size();i+=2)
alpar@2448
   570
//       g.addEdge(leafs[i],leafs[i+1]);
alpar@2448
   571
alpar@2448
   572
    std::vector<Pedge> pedges;
alpar@2448
   573
    for(unsigned int i=0;i<leafs.size()-1;i++)
alpar@2448
   574
      for(unsigned int j=i+1;j<leafs.size();j++)
alpar@2448
   575
	{
alpar@2448
   576
	  Node n=leafs[i];
alpar@2448
   577
	  Node m=leafs[j];
alpar@2448
   578
	  Pedge p;
alpar@2448
   579
	  p.a=n;
alpar@2448
   580
	  p.b=m;
alpar@2448
   581
	  p.len=(coords[m]-coords[n]).normSquare();
alpar@2448
   582
	  pedges.push_back(p);
alpar@2448
   583
	}
alpar@2448
   584
    std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2448
   585
    for(unsigned int i=0;i<pedges.size();i++)
alpar@2448
   586
      if(countIncEdges(g,pedges[i].a)%2 &&
alpar@2448
   587
	 countIncEdges(g,pedges[i].b)%2)
alpar@2448
   588
	g.addEdge(pedges[i].a,pedges[i].b);
alpar@2446
   589
  }
alpar@2446
   590
alpar@2446
   591
  for(NodeIt n(g);n!=INVALID;++n)
alpar@2448
   592
    if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
alpar@2446
   593
      std::cout << "GEBASZ!!!" << std::endl;
alpar@2446
   594
  
alpar@2448
   595
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2448
   596
    if(g.source(e)==g.target(e))
alpar@2448
   597
      std::cout << "LOOP GEBASZ!!!" << std::endl;
alpar@2448
   598
  
alpar@2446
   599
  std::cout << "Number of edges : " << countUEdges(g) << std::endl;
alpar@2446
   600
  
alpar@2446
   601
  std::cout << "Total edge length (euler) : " << totalLen() << std::endl;
alpar@2446
   602
alpar@2448
   603
  ListUGraph::UEdgeMap<Edge> enext(g);
alpar@2446
   604
  {
alpar@2446
   605
    UEulerIt<ListUGraph> e(g);
alpar@2448
   606
    Edge eo=e;
alpar@2448
   607
    Edge ef=e;
alpar@2446
   608
//     std::cout << "Tour edge: " << g.id(UEdge(e)) << std::endl;      
alpar@2446
   609
    for(++e;e!=INVALID;++e)
alpar@2446
   610
      {
alpar@2446
   611
// 	std::cout << "Tour edge: " << g.id(UEdge(e)) << std::endl;      
alpar@2446
   612
	enext[eo]=e;
alpar@2446
   613
	eo=e;
alpar@2446
   614
      }
alpar@2446
   615
    enext[eo]=ef;
alpar@2446
   616
  }
alpar@2448
   617
    
alpar@2446
   618
  std::cout << "Creating a tour from that..." << std::endl;
alpar@2446
   619
  
alpar@2446
   620
  int nnum = countNodes(g);
alpar@2446
   621
  int ednum = countUEdges(g);
alpar@2446
   622
  
alpar@2448
   623
  for(Edge p=enext[UEdgeIt(g)];ednum>nnum;p=enext[p]) 
alpar@2446
   624
    {
alpar@2446
   625
//       std::cout << "Checking edge " << g.id(p) << std::endl;      
alpar@2448
   626
      Edge e=enext[p];
alpar@2448
   627
      Edge f=enext[e];
alpar@2448
   628
      Node n2=g.source(f);
alpar@2446
   629
      Node n1=g.oppositeNode(n2,e);
alpar@2446
   630
      Node n3=g.oppositeNode(n2,f);
alpar@2446
   631
      if(countIncEdges(g,n2)>2)
alpar@2446
   632
	{
alpar@2446
   633
// 	  std::cout << "Remove an Edge" << std::endl;
alpar@2448
   634
	  Edge ff=enext[f];
alpar@2446
   635
	  g.erase(e);
alpar@2446
   636
	  g.erase(f);
alpar@2448
   637
	  if(n1!=n3)
alpar@2448
   638
	    {
alpar@2448
   639
	      Edge ne=g.direct(g.addEdge(n1,n3),n1);
alpar@2448
   640
	      enext[p]=ne;
alpar@2448
   641
	      enext[ne]=ff;
alpar@2448
   642
	      ednum--;
alpar@2448
   643
	    }
alpar@2448
   644
	  else {
alpar@2448
   645
	    enext[p]=ff;
alpar@2448
   646
	    ednum-=2;
alpar@2448
   647
	  }
alpar@2446
   648
	}
alpar@2446
   649
    }
alpar@2446
   650
alpar@2446
   651
  std::cout << "Total edge length (tour) : " << totalLen() << std::endl;
alpar@2446
   652
alpar@2448
   653
  std::cout << "2-opt the tour..." << std::endl;
alpar@2448
   654
  
alpar@2446
   655
  tsp_improve();
alpar@2446
   656
  
alpar@2446
   657
  std::cout << "Total edge length (2-opt tour) : " << totalLen() << std::endl;
alpar@2446
   658
}
alpar@2390
   659
alpar@2390
   660
deba@2410
   661
int main(int argc,const char **argv) 
alpar@2390
   662
{
alpar@2390
   663
  ArgParser ap(argc,argv);
alpar@2390
   664
alpar@2402
   665
//   bool eps;
alpar@2390
   666
  bool disc_d, square_d, gauss_d;
alpar@2402
   667
//   bool tsp_a,two_a,tree_a;
alpar@2390
   668
  int num_of_cities=1;
alpar@2390
   669
  double area=1;
alpar@2390
   670
  N=100;
alpar@2402
   671
//   girth=10;
alpar@2390
   672
  std::string ndist("disc");
alpar@2402
   673
  ap.refOption("n", "Number of nodes (default is 100)", N)
alpar@2402
   674
    .intOption("g", "Girth parameter (default is 10)", 10)
alpar@2402
   675
    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
alpar@2402
   676
    .refOption("area", "Full relative area of the cities (default is 1)", area)
alpar@2402
   677
    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
alpar@2390
   678
    .optionGroup("dist", "disc")
alpar@2402
   679
    .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
alpar@2390
   680
    .optionGroup("dist", "square")
alpar@2402
   681
    .refOption("gauss",
alpar@2390
   682
	    "Nodes are located according to a two-dim gauss distribution",
alpar@2390
   683
	    gauss_d)
alpar@2390
   684
    .optionGroup("dist", "gauss")
alpar@2390
   685
//     .mandatoryGroup("dist")
alpar@2390
   686
    .onlyOneGroup("dist")
alpar@2402
   687
    .boolOption("eps", "Also generate .eps output (prefix.eps)")
alpar@2446
   688
    .boolOption("dir", "Directed graph is generated (each edges are replaced by two directed ones)")
alpar@2402
   689
    .boolOption("2con", "Create a two connected planar graph")
alpar@2390
   690
    .optionGroup("alg","2con")
alpar@2402
   691
    .boolOption("tree", "Create a min. cost spanning tree")
alpar@2390
   692
    .optionGroup("alg","tree")
alpar@2402
   693
    .boolOption("tsp", "Create a TSP tour")
alpar@2390
   694
    .optionGroup("alg","tsp")
alpar@2446
   695
    .boolOption("tsp2", "Create a TSP tour (tree based)")
alpar@2446
   696
    .optionGroup("alg","tsp2")
deba@2447
   697
    .boolOption("dela", "Delaunay triangulation graph")
deba@2447
   698
    .optionGroup("alg","dela")
alpar@2390
   699
    .onlyOneGroup("alg")
deba@2447
   700
    .boolOption("rand", "Use time seed for random number generator")
deba@2447
   701
    .optionGroup("rand", "rand")
deba@2447
   702
    .intOption("seed", "Random seed", -1)
deba@2447
   703
    .optionGroup("rand", "seed")
deba@2447
   704
    .onlyOneGroup("rand")
alpar@2390
   705
    .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
alpar@2390
   706
    .run();
deba@2447
   707
deba@2447
   708
  if (ap["rand"]) {
deba@2447
   709
    int seed = time(0);
deba@2447
   710
    std::cout << "Random number seed: " << seed << std::endl;
deba@2447
   711
    rnd = Random(seed);
deba@2447
   712
  }
deba@2447
   713
  if (ap.given("seed")) {
deba@2447
   714
    int seed = ap["seed"];
deba@2447
   715
    std::cout << "Random number seed: " << seed << std::endl;
deba@2447
   716
    rnd = Random(seed);
deba@2447
   717
  }
alpar@2390
   718
  
alpar@2390
   719
  std::string prefix;
alpar@2390
   720
  switch(ap.files().size()) 
alpar@2390
   721
    {
alpar@2390
   722
    case 0:
alpar@2390
   723
      prefix="lgf-gen-out";
alpar@2390
   724
      break;
alpar@2390
   725
    case 1:
alpar@2390
   726
      prefix=ap.files()[0];
alpar@2390
   727
      break;
alpar@2390
   728
    default:
alpar@2390
   729
      std::cerr << "\nAt most one prefix can be given\n\n";
alpar@2390
   730
      exit(1);
alpar@2390
   731
    }
alpar@2390
   732
  
alpar@2390
   733
  double sum_sizes=0;
alpar@2390
   734
  std::vector<double> sizes;
alpar@2390
   735
  std::vector<double> cum_sizes;
alpar@2390
   736
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   737
    {
alpar@2390
   738
      // 	sum_sizes+=rnd.exponential();
alpar@2390
   739
      double d=rnd();
alpar@2390
   740
      sum_sizes+=d;
alpar@2390
   741
      sizes.push_back(d);
alpar@2390
   742
      cum_sizes.push_back(sum_sizes);
alpar@2390
   743
    }
alpar@2390
   744
  int i=0;
alpar@2390
   745
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   746
    {
alpar@2390
   747
      Point center=(num_of_cities==1?Point(0,0):rnd.disc());
alpar@2390
   748
      if(gauss_d)
alpar@2390
   749
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   750
	  Node n=g.addNode();
alpar@2390
   751
	  nodes.push_back(n);
alpar@2390
   752
	  coords[n]=center+rnd.gauss2()*area*
alpar@2390
   753
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   754
	}
alpar@2390
   755
      else if(square_d)
alpar@2390
   756
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   757
	  Node n=g.addNode();
alpar@2390
   758
	  nodes.push_back(n);
alpar@2390
   759
	  coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
alpar@2390
   760
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   761
	}
alpar@2390
   762
      else if(disc_d || true)
alpar@2390
   763
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   764
	  Node n=g.addNode();
alpar@2390
   765
	  nodes.push_back(n);
alpar@2390
   766
	  coords[n]=center+rnd.disc()*area*
alpar@2390
   767
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   768
	}
alpar@2390
   769
    }
deba@2447
   770
deba@2447
   771
//   for (ListUGraph::NodeIt n(g); n != INVALID; ++n) {
deba@2447
   772
//     std::cerr << coords[n] << std::endl;
deba@2447
   773
//   }
alpar@2390
   774
  
alpar@2402
   775
  if(ap["tsp"]) {
alpar@2390
   776
    tsp();
alpar@2390
   777
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
alpar@2390
   778
  }
alpar@2446
   779
  if(ap["tsp2"]) {
alpar@2446
   780
    tsp2();
alpar@2446
   781
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
alpar@2446
   782
  }
alpar@2402
   783
  else if(ap["2con"]) {
alpar@2390
   784
    std::cout << "Make triangles\n";
alpar@2390
   785
    //   triangle();
alpar@2402
   786
    sparseTriangle(ap["g"]);
alpar@2390
   787
    std::cout << "Make it sparser\n";
alpar@2402
   788
    sparse2(ap["g"]);
alpar@2390
   789
  }
alpar@2402
   790
  else if(ap["tree"]) {
alpar@2390
   791
    minTree();
alpar@2390
   792
  }
deba@2447
   793
  else if(ap["dela"]) {
deba@2447
   794
    delaunay();
deba@2447
   795
  }
alpar@2390
   796
  
alpar@2390
   797
alpar@2390
   798
  std::cout << "Number of nodes    : " << countNodes(g) << std::endl;
alpar@2390
   799
  std::cout << "Number of edges    : " << countUEdges(g) << std::endl;
alpar@2390
   800
  double tlen=0;
alpar@2390
   801
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2390
   802
    tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
alpar@2390
   803
  std::cout << "Total edge length  : " << tlen << std::endl;
alpar@2448
   804
alpar@2402
   805
  if(ap["eps"])
deba@2453
   806
    graphToEps(g,prefix+".eps").scaleToA4().
alpar@2390
   807
      scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
alpar@2390
   808
      coords(coords).run();
alpar@2448
   809
  
alpar@2446
   810
  if(ap["dir"])
alpar@2446
   811
    GraphWriter<ListUGraph>(prefix+".lgf",g).
alpar@2446
   812
      writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
alpar@2446
   813
      writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).
alpar@2446
   814
      run();
alpar@2446
   815
  else UGraphWriter<ListUGraph>(prefix+".lgf",g).
alpar@2446
   816
	 writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
alpar@2446
   817
	 writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).
alpar@2446
   818
	 run();
alpar@2390
   819
}
alpar@2390
   820