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