Sorry...
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>
32 #include <lemon/time_measure.h>
34 using namespace lemon;
36 typedef dim2::Point<double> Point;
38 UGRAPH_TYPEDEFS(ListUGraph);
47 std::vector<Node> nodes;
48 ListUGraph::NodeMap<Point> coords(g);
52 const double EPSILON=1e-8;
53 bool tsp_improve(Node u, Node v)
55 double luv=std::sqrt((coords[v]-coords[u]).normSquare());
60 for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e);
63 if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
64 std::sqrt((coords[u]-coords[u2]).normSquare())+
65 std::sqrt((coords[v]-coords[v2]).normSquare()))
67 g.erase(findUEdge(g,u,v));
68 g.erase(findUEdge(g,u2,v2));
78 bool tsp_improve(Node u)
80 for(IncEdgeIt e(g,u);e!=INVALID;++e)
81 if(tsp_improve(u,g.runningNode(e))) return true;
90 for(NodeIt n(g);n!=INVALID;++n)
91 if(tsp_improve(n)) b=true;
97 for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
106 Line(Point _a,Point _b) :a(_a),b(_b) {}
107 Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
108 Line(const Edge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
109 Line(const UEdge &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
112 inline std::ostream& operator<<(std::ostream &os, const Line &l)
114 os << l.a << "->" << l.b;
118 bool cross(Line a, Line b)
120 Point ao=rot90(a.b-a.a);
121 Point bo=rot90(b.b-b.a);
122 return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
123 (bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
133 bool pedgeLess(Pedge a,Pedge b)
138 std::vector<UEdge> edges;
142 Counter cnt("Number of edges added: ");
143 std::vector<Pedge> pedges;
144 for(NodeIt n(g);n!=INVALID;++n)
145 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
150 p.len=(coords[m]-coords[n]).normSquare();
153 std::sort(pedges.begin(),pedges.end(),pedgeLess);
154 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
156 Line li(pi->a,pi->b);
158 for(;e!=INVALID && !cross(e,li);++e) ;
161 ne=g.addEdge(pi->a,pi->b);
170 Counter cnt("Number of edges removed: ");
171 Bfs<ListUGraph> bfs(g);
172 for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
173 ei!=edges.rend();++ei)
175 Node a=g.source(*ei);
176 Node b=g.target(*ei);
179 if(bfs.predEdge(b)==INVALID || bfs.dist(b)>d)
187 Counter cnt("Number of edges removed: ");
188 for(std::vector<UEdge>::reverse_iterator ei=edges.rbegin();
189 ei!=edges.rend();++ei)
191 Node a=g.source(*ei);
192 Node b=g.target(*ei);
194 ConstMap<Edge,int> cegy(1);
195 Suurballe<ListUGraph,ConstMap<Edge,int> > sur(g,cegy,a,b);
197 if(k<2 || sur.totalLength()>d)
200 // else std::cout << "Remove edge " << g.id(a) << "-" << g.id(b) << '\n';
204 void sparseTriangle(int d)
206 Counter cnt("Number of edges added: ");
207 std::vector<Pedge> pedges;
208 for(NodeIt n(g);n!=INVALID;++n)
209 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
214 p.len=(coords[m]-coords[n]).normSquare();
217 std::sort(pedges.begin(),pedges.end(),pedgeLess);
218 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
220 Line li(pi->a,pi->b);
222 for(;e!=INVALID && !cross(e,li);++e) ;
225 ConstMap<Edge,int> cegy(1);
226 Suurballe<ListUGraph,ConstMap<Edge,int> >
227 sur(g,cegy,pi->a,pi->b);
229 if(k<2 || sur.totalLength()>d)
231 ne=g.addEdge(pi->a,pi->b);
242 std::vector<Pedge> pedges;
244 std::cout << T.realTime() << "s: Setting up the edges...\n";
245 for(NodeIt n(g);n!=INVALID;++n)
247 for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
252 p.len=(coords[m]-coords[n]).normSquare();
256 if(progress && en>=pr*double(N)/100)
258 std::cout << pr << "% \r" << std::flush;
262 std::cout << T.realTime() << "s: Sorting the edges...\n";
263 std::sort(pedges.begin(),pedges.end(),pedgeLess);
264 std::cout << T.realTime() << "s: Creating the tree...\n";
265 ListUGraph::NodeMap<int> comp(g);
266 UnionFind<ListUGraph::NodeMap<int> > uf(comp);
267 for (NodeIt it(g); it != INVALID; ++it)
269 for(std::vector<Pedge>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
271 if ( uf.join(pi->a,pi->b) ) {
272 g.addEdge(pi->a,pi->b);
277 std::cout << T.realTime() << "s: Done\n";
282 int main(int argc,const char **argv)
284 ArgParser ap(argc,argv);
287 bool disc_d, square_d, gauss_d;
288 // bool tsp_a,two_a,tree_a;
293 std::string ndist("disc");
294 ap.refOption("n", "Number of nodes (default is 100)", N)
295 .intOption("g", "Girth parameter (default is 10)", 10)
296 .refOption("cities", "Number of cities (default is 1)", num_of_cities)
297 .refOption("area", "Full relative area of the cities (default is 1)", area)
298 .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
299 .optionGroup("dist", "disc")
300 .refOption("square", "Nodes are evenly distributed on a unit square", square_d)
301 .optionGroup("dist", "square")
303 "Nodes are located according to a two-dim gauss distribution",
305 .optionGroup("dist", "gauss")
306 // .mandatoryGroup("dist")
307 .onlyOneGroup("dist")
308 .boolOption("eps", "Also generate .eps output (prefix.eps)")
309 .boolOption("2con", "Create a two connected planar graph")
310 .optionGroup("alg","2con")
311 .boolOption("tree", "Create a min. cost spanning tree")
312 .optionGroup("alg","tree")
313 .boolOption("tsp", "Create a TSP tour")
314 .optionGroup("alg","tsp")
316 .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
320 switch(ap.files().size())
323 prefix="lgf-gen-out";
326 prefix=ap.files()[0];
329 std::cerr << "\nAt most one prefix can be given\n\n";
334 std::vector<double> sizes;
335 std::vector<double> cum_sizes;
336 for(int s=0;s<num_of_cities;s++)
338 // sum_sizes+=rnd.exponential();
342 cum_sizes.push_back(sum_sizes);
345 for(int s=0;s<num_of_cities;s++)
347 Point center=(num_of_cities==1?Point(0,0):rnd.disc());
349 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
352 coords[n]=center+rnd.gauss2()*area*
353 std::sqrt(sizes[s]/sum_sizes);
356 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
359 coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
360 std::sqrt(sizes[s]/sum_sizes);
362 else if(disc_d || true)
363 for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
366 coords[n]=center+rnd.disc()*area*
367 std::sqrt(sizes[s]/sum_sizes);
373 std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
375 else if(ap["2con"]) {
376 std::cout << "Make triangles\n";
378 sparseTriangle(ap["g"]);
379 std::cout << "Make it sparser\n";
382 else if(ap["tree"]) {
387 std::cout << "Number of nodes : " << countNodes(g) << std::endl;
388 std::cout << "Number of edges : " << countUEdges(g) << std::endl;
390 for(UEdgeIt e(g);e!=INVALID;++e)
391 tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
392 std::cout << "Total edge length : " << tlen << std::endl;
394 graphToEps(g,prefix+".eps").
395 scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
396 coords(coords).run();
398 UGraphWriter<ListUGraph>(prefix+".lgf",g).
399 writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
400 writeNodeMap("coordinates_y",scaleMap(yMap(coords),600)).