[Lemon-commits] alpar: r3286 - lemon/trunk/tools
Lemon SVN
svn at lemon.cs.elte.hu
Tue Jun 5 16:48:23 CEST 2007
Author: alpar
Date: Tue Jun 5 16:48:20 2007
New Revision: 3286
Modified:
lemon/trunk/tools/lgf-gen.cc
Log:
Bugfix and improvement in -tsp2 algorithm
Modified: lemon/trunk/tools/lgf-gen.cc
==============================================================================
--- lemon/trunk/tools/lgf-gen.cc (original)
+++ lemon/trunk/tools/lgf-gen.cc Tue Jun 5 16:48:20 2007
@@ -532,12 +532,6 @@
std::cout << T.realTime() << "s: Done\n";
}
-Node common(UEdge e, UEdge f)
-{
- return (g.source(e)==g.source(f)||g.source(e)==g.target(f))?
- g.source(e):g.target(e);
-}
-
void tsp2()
{
std::cout << "Find a tree..." << std::endl;
@@ -552,28 +546,46 @@
std::vector<Node> leafs;
for(NodeIt n(g);n!=INVALID;++n)
if(countIncEdges(g,n)%2==1) leafs.push_back(n);
- for(unsigned int i=0;i<leafs.size();i+=2)
- g.addEdge(leafs[i],leafs[i+1]);
+
+// for(unsigned int i=0;i<leafs.size();i+=2)
+// g.addEdge(leafs[i],leafs[i+1]);
+
+ std::vector<Pedge> pedges;
+ for(unsigned int i=0;i<leafs.size()-1;i++)
+ for(unsigned int j=i+1;j<leafs.size();j++)
+ {
+ Node n=leafs[i];
+ Node m=leafs[j];
+ Pedge p;
+ p.a=n;
+ p.b=m;
+ p.len=(coords[m]-coords[n]).normSquare();
+ pedges.push_back(p);
+ }
+ std::sort(pedges.begin(),pedges.end(),pedgeLess);
+ for(unsigned int i=0;i<pedges.size();i++)
+ if(countIncEdges(g,pedges[i].a)%2 &&
+ countIncEdges(g,pedges[i].b)%2)
+ g.addEdge(pedges[i].a,pedges[i].b);
}
for(NodeIt n(g);n!=INVALID;++n)
- if(countIncEdges(g,n)%2)
+ if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
std::cout << "GEBASZ!!!" << std::endl;
+ for(UEdgeIt e(g);e!=INVALID;++e)
+ if(g.source(e)==g.target(e))
+ std::cout << "LOOP GEBASZ!!!" << std::endl;
+
std::cout << "Number of edges : " << countUEdges(g) << std::endl;
-
-// for(NodeIt n(g);n!=INVALID;++n)
-// if(countIncEdges(g,n)>2)
-// std::cout << "+";
-// std::cout << std::endl;
std::cout << "Total edge length (euler) : " << totalLen() << std::endl;
- ListUGraph::UEdgeMap<UEdge> enext(g);
+ ListUGraph::UEdgeMap<Edge> enext(g);
{
UEulerIt<ListUGraph> e(g);
- UEdge eo=e;
- UEdge ef=e;
+ Edge eo=e;
+ Edge ef=e;
// std::cout << "Tour edge: " << g.id(UEdge(e)) << std::endl;
for(++e;e!=INVALID;++e)
{
@@ -583,35 +595,44 @@
}
enext[eo]=ef;
}
-
+
std::cout << "Creating a tour from that..." << std::endl;
int nnum = countNodes(g);
int ednum = countUEdges(g);
- for(UEdge p=UEdgeIt(g);ednum>nnum;p=enext[p])
+ for(Edge p=enext[UEdgeIt(g)];ednum>nnum;p=enext[p])
{
// std::cout << "Checking edge " << g.id(p) << std::endl;
- UEdge e=enext[p];
- UEdge f=enext[e];
- Node n2=common(e,f);
+ Edge e=enext[p];
+ Edge f=enext[e];
+ Node n2=g.source(f);
Node n1=g.oppositeNode(n2,e);
Node n3=g.oppositeNode(n2,f);
if(countIncEdges(g,n2)>2)
{
// std::cout << "Remove an Edge" << std::endl;
- UEdge ff=enext[f];
+ Edge ff=enext[f];
g.erase(e);
g.erase(f);
- UEdge ne=g.addEdge(n1,n3);
- enext[p]=ne;
- enext[ne]=ff;
- ednum--;
+ if(n1!=n3)
+ {
+ Edge ne=g.direct(g.addEdge(n1,n3),n1);
+ enext[p]=ne;
+ enext[ne]=ff;
+ ednum--;
+ }
+ else {
+ enext[p]=ff;
+ ednum-=2;
+ }
}
}
std::cout << "Total edge length (tour) : " << totalLen() << std::endl;
+ std::cout << "2-opt the tour..." << std::endl;
+
tsp_improve();
std::cout << "Total edge length (2-opt tour) : " << totalLen() << std::endl;
@@ -761,11 +782,12 @@
for(UEdgeIt e(g);e!=INVALID;++e)
tlen+=sqrt((coords[g.source(e)]-coords[g.target(e)]).normSquare());
std::cout << "Total edge length : " << tlen << std::endl;
+
if(ap["eps"])
graphToEps(g,prefix+".eps").
scale(600).nodeScale(.2).edgeWidthScale(.001).preScale(false).
coords(coords).run();
-
+
if(ap["dir"])
GraphWriter<ListUGraph>(prefix+".lgf",g).
writeNodeMap("coordinates_x",scaleMap(xMap(coords),600)).
More information about the Lemon-commits
mailing list