tools/lgf-gen.cc
author athos
Tue, 27 Mar 2007 09:23:33 +0000
changeset 2415 ef13597d249a
parent 2402 da8eb8f4ea41
child 2446 dd20d76eed13
permissions -rw-r--r--
I only corrected bugs to make things compile: some featured not implemented here yet.
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@2390
    29
#include <cmath>
alpar@2390
    30
#include <algorithm>
alpar@2390
    31
#include <lemon/unionfind.h>
alpar@2402
    32
#include <lemon/time_measure.h>
alpar@2390
    33
alpar@2390
    34
using namespace lemon;
alpar@2390
    35
alpar@2390
    36
typedef dim2::Point<double> Point;
alpar@2390
    37
alpar@2390
    38
UGRAPH_TYPEDEFS(ListUGraph);
alpar@2390
    39
alpar@2402
    40
bool progress=true;
alpar@2402
    41
alpar@2390
    42
int N;
alpar@2402
    43
// int girth;
alpar@2390
    44
alpar@2390
    45
ListUGraph g;
alpar@2390
    46
alpar@2390
    47
std::vector<Node> nodes;
alpar@2390
    48
ListUGraph::NodeMap<Point> coords(g);
alpar@2390
    49
alpar@2390
    50
int tsp_impr_num=0;
alpar@2390
    51
alpar@2390
    52
const double EPSILON=1e-8; 
alpar@2390
    53
bool tsp_improve(Node u, Node v)
alpar@2390
    54
{
alpar@2390
    55
  double luv=std::sqrt((coords[v]-coords[u]).normSquare());
alpar@2390
    56
  Node u2=u;
alpar@2390
    57
  Node v2=v;
alpar@2390
    58
  do {
alpar@2390
    59
    Node n;
alpar@2390
    60
    for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e);
alpar@2390
    61
    u2=v2;
alpar@2390
    62
    v2=n;
alpar@2390
    63
    if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
alpar@2390
    64
       std::sqrt((coords[u]-coords[u2]).normSquare())+
alpar@2390
    65
       std::sqrt((coords[v]-coords[v2]).normSquare()))
alpar@2390
    66
      {
alpar@2390
    67
 	g.erase(findUEdge(g,u,v));
alpar@2390
    68
 	g.erase(findUEdge(g,u2,v2));
alpar@2390
    69
	g.addEdge(u2,u);
alpar@2390
    70
	g.addEdge(v,v2);
alpar@2390
    71
	tsp_impr_num++;
alpar@2390
    72
	return true;
alpar@2390
    73
      }
alpar@2390
    74
  } while(v2!=u);
alpar@2390
    75
  return false;
alpar@2390
    76
}
alpar@2390
    77
alpar@2390
    78
bool tsp_improve(Node u)
alpar@2390
    79
{
alpar@2390
    80
  for(IncEdgeIt e(g,u);e!=INVALID;++e)
alpar@2390
    81
    if(tsp_improve(u,g.runningNode(e))) return true;
alpar@2390
    82
  return false;
alpar@2390
    83
}
alpar@2390
    84
alpar@2390
    85
void tsp_improve()
alpar@2390
    86
{
alpar@2390
    87
  bool b;
alpar@2390
    88
  do {
alpar@2390
    89
    b=false;
alpar@2390
    90
    for(NodeIt n(g);n!=INVALID;++n)
alpar@2390
    91
      if(tsp_improve(n)) b=true;
alpar@2390
    92
  } while(b);
alpar@2390
    93
}
alpar@2390
    94
alpar@2390
    95
void tsp()
alpar@2390
    96
{
alpar@2390
    97
  for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
alpar@2390
    98
  tsp_improve();
alpar@2390
    99
}
alpar@2390
   100
alpar@2390
   101
class Line
alpar@2390
   102
{
alpar@2390
   103
public:
alpar@2390
   104
  Point a;
alpar@2390
   105
  Point b;
alpar@2390
   106
  Line(Point _a,Point _b) :a(_a),b(_b) {}
alpar@2390
   107
  Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
alpar@2390
   108
  Line(const Edge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
alpar@2390
   109
  Line(const UEdge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
alpar@2390
   110
};
alpar@2390
   111
  
alpar@2390
   112
inline std::ostream& operator<<(std::ostream &os, const Line &l)
alpar@2390
   113
{
alpar@2390
   114
  os << l.a << "->" << l.b;
alpar@2390
   115
  return os;
alpar@2390
   116
}
alpar@2390
   117
alpar@2390
   118
bool cross(Line a, Line b) 
alpar@2390
   119
{
alpar@2390
   120
  Point ao=rot90(a.b-a.a);
alpar@2390
   121
  Point bo=rot90(b.b-b.a);
alpar@2390
   122
  return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
alpar@2390
   123
    (bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
alpar@2390
   124
}
alpar@2390
   125
alpar@2390
   126
struct Pedge
alpar@2390
   127
{
alpar@2390
   128
  Node a;
alpar@2390
   129
  Node b;
alpar@2390
   130
  double len;
alpar@2390
   131
};
alpar@2390
   132
alpar@2390
   133
bool pedgeLess(Pedge a,Pedge b)
alpar@2390
   134
{
alpar@2390
   135
  return a.len<b.len;
alpar@2390
   136
}
alpar@2390
   137
alpar@2390
   138
std::vector<UEdge> edges;
alpar@2390
   139
alpar@2390
   140
void triangle()
alpar@2390
   141
{
alpar@2390
   142
  Counter cnt("Number of edges added: ");
alpar@2390
   143
  std::vector<Pedge> pedges;
alpar@2390
   144
  for(NodeIt n(g);n!=INVALID;++n) 
alpar@2390
   145
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
alpar@2390
   146
      {
alpar@2390
   147
	Pedge p;
alpar@2390
   148
	p.a=n;
alpar@2390
   149
	p.b=m;
alpar@2390
   150
	p.len=(coords[m]-coords[n]).normSquare();
alpar@2390
   151
	pedges.push_back(p);
alpar@2390
   152
      }
alpar@2390
   153
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2390
   154
  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
alpar@2390
   155
    {
alpar@2390
   156
      Line li(pi->a,pi->b);
alpar@2390
   157
      UEdgeIt e(g);
alpar@2390
   158
      for(;e!=INVALID && !cross(e,li);++e) ;
alpar@2390
   159
      UEdge ne;
alpar@2390
   160
      if(e==INVALID) {
alpar@2390
   161
	ne=g.addEdge(pi->a,pi->b);
alpar@2390
   162
	edges.push_back(ne);
alpar@2390
   163
	cnt++;
alpar@2390
   164
      }
alpar@2390
   165
    }
alpar@2390
   166
}
alpar@2390
   167
alpar@2390
   168
void sparse(int d) 
alpar@2390
   169
{
alpar@2390
   170
  Counter cnt("Number of edges removed: ");
alpar@2390
   171
  Bfs<ListUGraph> bfs(g);
alpar@2390
   172
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   173
      ei!=edges.rend();++ei)
alpar@2390
   174
    {
alpar@2390
   175
      Node a=g.source(*ei);
alpar@2390
   176
      Node b=g.target(*ei);
alpar@2390
   177
      g.erase(*ei);
alpar@2390
   178
      bfs.run(a,b);
alpar@2390
   179
      if(bfs.predEdge(b)==INVALID || bfs.dist(b)>d)
alpar@2390
   180
	g.addEdge(a,b);
alpar@2390
   181
      else cnt++;
alpar@2390
   182
    }
alpar@2390
   183
}
alpar@2390
   184
alpar@2390
   185
void sparse2(int d) 
alpar@2390
   186
{
alpar@2390
   187
  Counter cnt("Number of edges removed: ");
alpar@2390
   188
  for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
alpar@2390
   189
      ei!=edges.rend();++ei)
alpar@2390
   190
    {
alpar@2390
   191
      Node a=g.source(*ei);
alpar@2390
   192
      Node b=g.target(*ei);
alpar@2390
   193
      g.erase(*ei);
alpar@2390
   194
      ConstMap<Edge,int> cegy(1);
alpar@2390
   195
      Suurballe<ListUGraph,ConstMap<Edge,int> > sur(g,cegy,a,b);
alpar@2390
   196
      int k=sur.run(2);
alpar@2390
   197
      if(k<2 || sur.totalLength()>d)
alpar@2390
   198
	g.addEdge(a,b);
alpar@2390
   199
      else cnt++;
alpar@2390
   200
//       else std::cout << "Remove edge " << g.id(a) << "-" << g.id(b) << '\n';
alpar@2390
   201
    }
alpar@2390
   202
}
alpar@2390
   203
alpar@2390
   204
void sparseTriangle(int d)
alpar@2390
   205
{
alpar@2390
   206
  Counter cnt("Number of edges added: ");
alpar@2390
   207
  std::vector<Pedge> pedges;
alpar@2390
   208
  for(NodeIt n(g);n!=INVALID;++n) 
alpar@2390
   209
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
alpar@2390
   210
      {
alpar@2390
   211
	Pedge p;
alpar@2390
   212
	p.a=n;
alpar@2390
   213
	p.b=m;
alpar@2390
   214
	p.len=(coords[m]-coords[n]).normSquare();
alpar@2390
   215
	pedges.push_back(p);
alpar@2390
   216
      }
alpar@2390
   217
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2390
   218
  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
alpar@2390
   219
    {
alpar@2390
   220
      Line li(pi->a,pi->b);
alpar@2390
   221
      UEdgeIt e(g);
alpar@2390
   222
      for(;e!=INVALID && !cross(e,li);++e) ;
alpar@2390
   223
      UEdge ne;
alpar@2390
   224
      if(e==INVALID) {
alpar@2390
   225
	ConstMap<Edge,int> cegy(1);
alpar@2390
   226
	Suurballe<ListUGraph,ConstMap<Edge,int> >
alpar@2390
   227
	  sur(g,cegy,pi->a,pi->b);
alpar@2390
   228
	int k=sur.run(2);
alpar@2390
   229
	if(k<2 || sur.totalLength()>d)
alpar@2390
   230
	  {
alpar@2390
   231
	    ne=g.addEdge(pi->a,pi->b);
alpar@2390
   232
	    edges.push_back(ne);
alpar@2390
   233
	    cnt++;
alpar@2390
   234
	  }
alpar@2390
   235
      }
alpar@2390
   236
    }
alpar@2390
   237
}
alpar@2390
   238
alpar@2390
   239
void minTree() {
alpar@2402
   240
  int en=0;
alpar@2402
   241
  int pr=0;
alpar@2390
   242
  std::vector<Pedge> pedges;
alpar@2402
   243
  Timer T;
alpar@2402
   244
  std::cout << T.realTime() << "s: Setting up the edges...\n";
alpar@2390
   245
  for(NodeIt n(g);n!=INVALID;++n) 
alpar@2402
   246
    {
alpar@2402
   247
      for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
alpar@2402
   248
	{
alpar@2402
   249
	  Pedge p;
alpar@2402
   250
	  p.a=n;
alpar@2402
   251
	  p.b=m;
alpar@2402
   252
	  p.len=(coords[m]-coords[n]).normSquare();
alpar@2402
   253
	  pedges.push_back(p);
alpar@2402
   254
	}
alpar@2402
   255
      en++;
alpar@2402
   256
      if(progress && en>=pr*double(N)/100) 
alpar@2402
   257
	{
alpar@2402
   258
	  std::cout << pr << "%  \r" << std::flush;
alpar@2402
   259
	  pr++;
alpar@2402
   260
	}
alpar@2402
   261
    }
alpar@2402
   262
  std::cout << T.realTime() << "s: Sorting the edges...\n";
alpar@2390
   263
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
alpar@2402
   264
  std::cout << T.realTime() << "s: Creating the tree...\n";
alpar@2390
   265
  ListUGraph::NodeMap<int> comp(g);
alpar@2390
   266
  UnionFind<ListUGraph::NodeMap<int> > uf(comp);
alpar@2390
   267
  for (NodeIt it(g); it != INVALID; ++it)
alpar@2402
   268
    uf.insert(it);  
alpar@2390
   269
  for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
alpar@2390
   270
    {
alpar@2390
   271
      if ( uf.join(pi->a,pi->b) ) {
alpar@2390
   272
	g.addEdge(pi->a,pi->b);
alpar@2402
   273
	if(en>=N-1)
alpar@2402
   274
	  break;
alpar@2390
   275
      }
alpar@2390
   276
    }
alpar@2402
   277
  std::cout << T.realTime() << "s: Done\n";
alpar@2390
   278
}
alpar@2390
   279
alpar@2390
   280
alpar@2390
   281
deba@2410
   282
int main(int argc,const char **argv) 
alpar@2390
   283
{
alpar@2390
   284
  ArgParser ap(argc,argv);
alpar@2390
   285
alpar@2402
   286
//   bool eps;
alpar@2390
   287
  bool disc_d, square_d, gauss_d;
alpar@2402
   288
//   bool tsp_a,two_a,tree_a;
alpar@2390
   289
  int num_of_cities=1;
alpar@2390
   290
  double area=1;
alpar@2390
   291
  N=100;
alpar@2402
   292
//   girth=10;
alpar@2390
   293
  std::string ndist("disc");
alpar@2402
   294
  ap.refOption("n", "Number of nodes (default is 100)", N)
alpar@2402
   295
    .intOption("g", "Girth parameter (default is 10)", 10)
alpar@2402
   296
    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
alpar@2402
   297
    .refOption("area", "Full relative area of the cities (default is 1)", area)
alpar@2402
   298
    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
alpar@2390
   299
    .optionGroup("dist", "disc")
alpar@2402
   300
    .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
alpar@2390
   301
    .optionGroup("dist", "square")
alpar@2402
   302
    .refOption("gauss",
alpar@2390
   303
	    "Nodes are located according to a two-dim gauss distribution",
alpar@2390
   304
	    gauss_d)
alpar@2390
   305
    .optionGroup("dist", "gauss")
alpar@2390
   306
//     .mandatoryGroup("dist")
alpar@2390
   307
    .onlyOneGroup("dist")
alpar@2402
   308
    .boolOption("eps", "Also generate .eps output (prefix.eps)")
alpar@2402
   309
    .boolOption("2con", "Create a two connected planar graph")
alpar@2390
   310
    .optionGroup("alg","2con")
alpar@2402
   311
    .boolOption("tree", "Create a min. cost spanning tree")
alpar@2390
   312
    .optionGroup("alg","tree")
alpar@2402
   313
    .boolOption("tsp", "Create a TSP tour")
alpar@2390
   314
    .optionGroup("alg","tsp")
alpar@2390
   315
    .onlyOneGroup("alg")
alpar@2390
   316
    .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
alpar@2390
   317
    .run();
alpar@2390
   318
  
alpar@2390
   319
  std::string prefix;
alpar@2390
   320
  switch(ap.files().size()) 
alpar@2390
   321
    {
alpar@2390
   322
    case 0:
alpar@2390
   323
      prefix="lgf-gen-out";
alpar@2390
   324
      break;
alpar@2390
   325
    case 1:
alpar@2390
   326
      prefix=ap.files()[0];
alpar@2390
   327
      break;
alpar@2390
   328
    default:
alpar@2390
   329
      std::cerr << "\nAt most one prefix can be given\n\n";
alpar@2390
   330
      exit(1);
alpar@2390
   331
    }
alpar@2390
   332
  
alpar@2390
   333
  double sum_sizes=0;
alpar@2390
   334
  std::vector<double> sizes;
alpar@2390
   335
  std::vector<double> cum_sizes;
alpar@2390
   336
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   337
    {
alpar@2390
   338
      // 	sum_sizes+=rnd.exponential();
alpar@2390
   339
      double d=rnd();
alpar@2390
   340
      sum_sizes+=d;
alpar@2390
   341
      sizes.push_back(d);
alpar@2390
   342
      cum_sizes.push_back(sum_sizes);
alpar@2390
   343
    }
alpar@2390
   344
  int i=0;
alpar@2390
   345
  for(int s=0;s<num_of_cities;s++) 
alpar@2390
   346
    {
alpar@2390
   347
      Point center=(num_of_cities==1?Point(0,0):rnd.disc());
alpar@2390
   348
      if(gauss_d)
alpar@2390
   349
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   350
	  Node n=g.addNode();
alpar@2390
   351
	  nodes.push_back(n);
alpar@2390
   352
	  coords[n]=center+rnd.gauss2()*area*
alpar@2390
   353
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   354
	}
alpar@2390
   355
      else if(square_d)
alpar@2390
   356
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   357
	  Node n=g.addNode();
alpar@2390
   358
	  nodes.push_back(n);
alpar@2390
   359
	  coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
alpar@2390
   360
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   361
	}
alpar@2390
   362
      else if(disc_d || true)
alpar@2390
   363
	for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
alpar@2390
   364
	  Node n=g.addNode();
alpar@2390
   365
	  nodes.push_back(n);
alpar@2390
   366
	  coords[n]=center+rnd.disc()*area*
alpar@2390
   367
	    std::sqrt(sizes[s]/sum_sizes);
alpar@2390
   368
	}
alpar@2390
   369
    }
alpar@2390
   370
  
alpar@2402
   371
  if(ap["tsp"]) {
alpar@2390
   372
    tsp();
alpar@2390
   373
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
alpar@2390
   374
  }
alpar@2402
   375
  else if(ap["2con"]) {
alpar@2390
   376
    std::cout << "Make triangles\n";
alpar@2390
   377
    //   triangle();
alpar@2402
   378
    sparseTriangle(ap["g"]);
alpar@2390
   379
    std::cout << "Make it sparser\n";
alpar@2402
   380
    sparse2(ap["g"]);
alpar@2390
   381
  }
alpar@2402
   382
  else if(ap["tree"]) {
alpar@2390
   383
    minTree();
alpar@2390
   384
  }
alpar@2390
   385
  
alpar@2390
   386
alpar@2390
   387
  std::cout << "Number of nodes    : " << countNodes(g) << std::endl;
alpar@2390
   388
  std::cout << "Number of edges    : " << countUEdges(g) << std::endl;
alpar@2390
   389
  double tlen=0;
alpar@2390
   390
  for(UEdgeIt e(g);e!=INVALID;++e)
alpar@2390
   391
    tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
alpar@2390
   392
  std::cout << "Total edge length  : " << tlen << std::endl;
alpar@2402
   393
  if(ap["eps"])
alpar@2390
   394
    graphToEps(g,prefix+".eps").
alpar@2390
   395
      scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
alpar@2390
   396
      coords(coords).run();
alpar@2390
   397
alpar@2390
   398
  UGraphWriter<ListUGraph>(prefix+".lgf",g).
alpar@2390
   399
    writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
alpar@2390
   400
    writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).
alpar@2390
   401
    run();
alpar@2390
   402
}
alpar@2390
   403