3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include <lemon/list_graph.h>
20 #include <lemon/graph_utils.h>
21 #include <lemon/random.h>
22 #include <lemon/dim2.h>
23 #include <lemon/bfs.h>
24 #include <lemon/counter.h>
25 #include <lemon/suurballe.h>
26 #include <lemon/graph_to_eps.h>
27 #include <lemon/graph_writer.h>
28 #include <lemon/arg_parser.h>
31 #include <lemon/unionfind.h>
33 using namespace lemon;
35 typedef dim2::Point<double> Point;
37 UGRAPH_TYPEDEFS(ListUGraph);
44 std::vector<Node> nodes;
45 ListUGraph::NodeMap<Point> coords(g);
49 const double EPSILON=1e-8;
50 bool tsp_improve(Node u, Node v)
52 double luv=std::sqrt((coords[v]-coords[u]).normSquare());
57 for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e);
60 if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
61 std::sqrt((coords[u]-coords[u2]).normSquare())+
62 std::sqrt((coords[v]-coords[v2]).normSquare()))
64 g.erase(findUEdge(g,u,v));
65 g.erase(findUEdge(g,u2,v2));
75 bool tsp_improve(Node u)
77 for(IncEdgeIt e(g,u);e!=INVALID;++e)
78 if(tsp_improve(u,g.runningNode(e))) return true;
87 for(NodeIt n(g);n!=INVALID;++n)
88 if(tsp_improve(n)) b=true;
94 for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
103 Line(Point _a,Point _b) :a(_a),b(_b) {}
104 Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
105 Line(const Edge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
106 Line(const UEdge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
109 inline std::ostream& operator<<(std::ostream &os, const Line &l)
111 os << l.a << "->" << l.b;
115 bool cross(Line a, Line b)
117 Point ao=rot90(a.b-a.a);
118 Point bo=rot90(b.b-b.a);
119 return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
120 (bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
130 bool pedgeLess(Pedge a,Pedge b)
135 std::vector<UEdge> edges;
139 Counter cnt("Number of edges added: ");
140 std::vector<Pedge> pedges;
141 for(NodeIt n(g);n!=INVALID;++n)
142 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
147 p.len=(coords[m]-coords[n]).normSquare();
150 std::sort(pedges.begin(),pedges.end(),pedgeLess);
151 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
153 Line li(pi->a,pi->b);
155 for(;e!=INVALID && !cross(e,li);++e) ;
158 ne=g.addEdge(pi->a,pi->b);
167 Counter cnt("Number of edges removed: ");
168 Bfs<ListUGraph> bfs(g);
169 for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
170 ei!=edges.rend();++ei)
172 Node a=g.source(*ei);
173 Node b=g.target(*ei);
176 if(bfs.predEdge(b)==INVALID || bfs.dist(b)>d)
184 Counter cnt("Number of edges removed: ");
185 for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
186 ei!=edges.rend();++ei)
188 Node a=g.source(*ei);
189 Node b=g.target(*ei);
191 ConstMap<Edge,int> cegy(1);
192 Suurballe<ListUGraph,ConstMap<Edge,int> > sur(g,cegy,a,b);
194 if(k<2 || sur.totalLength()>d)
197 // else std::cout << "Remove edge " << g.id(a) << "-" << g.id(b) << '\n';
201 void sparseTriangle(int d)
203 Counter cnt("Number of edges added: ");
204 std::vector<Pedge> pedges;
205 for(NodeIt n(g);n!=INVALID;++n)
206 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
211 p.len=(coords[m]-coords[n]).normSquare();
214 std::sort(pedges.begin(),pedges.end(),pedgeLess);
215 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
217 Line li(pi->a,pi->b);
219 for(;e!=INVALID && !cross(e,li);++e) ;
222 ConstMap<Edge,int> cegy(1);
223 Suurballe<ListUGraph,ConstMap<Edge,int> >
224 sur(g,cegy,pi->a,pi->b);
226 if(k<2 || sur.totalLength()>d)
228 ne=g.addEdge(pi->a,pi->b);
237 std::vector<Pedge> pedges;
238 for(NodeIt n(g);n!=INVALID;++n)
239 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
244 p.len=(coords[m]-coords[n]).normSquare();
247 std::sort(pedges.begin(),pedges.end(),pedgeLess);
248 ListUGraph::NodeMap<int> comp(g);
249 UnionFind<ListUGraph::NodeMap<int> > uf(comp);
250 for (NodeIt it(g); it != INVALID; ++it)
254 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
256 if ( uf.join(pi->a,pi->b) ) {
257 g.addEdge(pi->a,pi->b);
266 int main(int argc,char **argv)
268 ArgParser ap(argc,argv);
271 bool disc_d, square_d, gauss_d;
272 bool tsp_a,two_a,tree_a;
277 std::string ndist("disc");
278 ap.option("n", "Number of nodes (default is 100)", N)
279 .option("g", "Girth parameter (default is 10)", girth)
280 .option("cities", "Number of cities (default is 1)", num_of_cities)
281 .option("area", "Full relative area of the cities (default is 1)", area)
282 .option("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
283 .optionGroup("dist", "disc")
284 .option("square", "Nodes are evenly distributed on a unit square", square_d)
285 .optionGroup("dist", "square")
287 "Nodes are located according to a two-dim gauss distribution",
289 .optionGroup("dist", "gauss")
290 // .mandatoryGroup("dist")
291 .onlyOneGroup("dist")
292 .option("eps", "Also generate .eps output (prefix.eps)",eps)
293 .option("2con", "Create a two connected planar graph",two_a)
294 .optionGroup("alg","2con")
295 .option("tree", "Create a min. cost spanning tree",tree_a)
296 .optionGroup("alg","tree")
297 .option("tsp", "Create a TSP tour",tsp_a)
298 .optionGroup("alg","tsp")
300 .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
304 switch(ap.files().size())
307 prefix="lgf-gen-out";
310 prefix=ap.files()[0];
313 std::cerr << "\nAt most one prefix can be given\n\n";
318 std::vector<double> sizes;
319 std::vector<double> cum_sizes;
320 for(int s=0;s<num_of_cities;s++)
322 // sum_sizes+=rnd.exponential();
326 cum_sizes.push_back(sum_sizes);
329 for(int s=0;s<num_of_cities;s++)
331 Point center=(num_of_cities==1?Point(0,0):rnd.disc());
333 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
336 coords[n]=center+rnd.gauss2()*area*
337 std::sqrt(sizes[s]/sum_sizes);
340 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
343 coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
344 std::sqrt(sizes[s]/sum_sizes);
346 else if(disc_d || true)
347 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
350 coords[n]=center+rnd.disc()*area*
351 std::sqrt(sizes[s]/sum_sizes);
357 std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
360 std::cout << "Make triangles\n";
362 sparseTriangle(girth);
363 std::cout << "Make it sparser\n";
371 std::cout << "Number of nodes : " << countNodes(g) << std::endl;
372 std::cout << "Number of edges : " << countUEdges(g) << std::endl;
374 for(UEdgeIt e(g);e!=INVALID;++e)
375 tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
376 std::cout << "Total edge length : " << tlen << std::endl;
378 graphToEps(g,prefix+".eps").
379 scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
380 coords(coords).run();
382 UGraphWriter<ListUGraph>(prefix+".lgf",g).
383 writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
384 writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).