heuristique ok

master
daSau 2016-12-28 15:10:41 +01:00
parent f96581cf5e
commit 0dc65f09b2
2 changed files with 24 additions and 9 deletions

View File

@ -26,11 +26,12 @@ def kruskal(G):
merge(root[a], root[b])
spanningTree.add_edge(a, b, w)
return spanningTree, root[0]
return spanningTree, findRoot(0)
def heuristic(G):
spG, root = kruskal(G)
tour = nx.Graph()
seen = []
size = 0
@ -45,24 +46,26 @@ def heuristic(G):
iInit = 0
if(spG.neighbors(nd)[0] == anc):
iInit += 1
nxt = spG.neighbors(nd)[iInit]
size += spG[nd][nxt]['weight']
leaf = walk(nxt, nd)
tour.add_edge(nd, nxt, weight = spG[nd][nxt]['weight'])
for iNxt in range(iInit+1, len(spG.neighbors(nd))):
nxt = spG.neighbors(nd)[iNxt]
if(nxt != anc):
size += G[leaf][nxt]['weight']
leaf = walk(nxt, nd)
tour.add_edge(nd, nxt, weight = spG[nd][nxt]['weight'])
return leaf
walk(root, -1)
#print('root : ', root)
#nx.draw(spG)
#plt.show()
print('seen :', len(seen))
print('taille tour :', len(tour.edges()))
#print('size :', size)
return size, spG
return size, tour

View File

@ -2,11 +2,14 @@ from parser import parse_file
from separation import separation
from separation_dual import LinearDualTSP
from time import time
import networkx as nx
from heuristic_kruskal import heuristic
#name = "burma14.tsp" #3001
#name = "gr17.tsp" #1684
#name = "gr21.tsp" #2707
name = "eil51.tsp" #416.5, 7s
#name = "gr24.tsp" #1224.5
#name = "eil51.tsp" #416.5, 7s
name = "gr24.tsp" #1224.5
#name = "st70.tsp" #623.5, 31s
#name = "gr48.tsp" #4769
#name = "pr76.tsp" #98994.5
@ -19,6 +22,14 @@ name = "eil51.tsp" #416.5, 7s
with open("TSPLIB/" + name, "r") as f:
Adj = parse_file(f)
G = nx.Graph()
for l in range(len(Adj)):
for c in range(l):
G.add_edge(l, c, weight = Adj[l][c])
t0 = time()
ivalh, _ = heuristic(G)
th = time() - t0
t0 = time()
x, ival = separation(Adj)
#x, ival = 0,0
@ -27,6 +38,7 @@ with open("TSPLIB/" + name, "r") as f:
t0 = time()
x2, ival2 = LinearDualTSP(Adj).solve()
t2 = time() - t0
print('heuristic : ', ivalh, th)
print("Primal:", ival, "time", t1)
print("Dual:", ival2, "time", t2)
#print(x)
@ -36,4 +48,4 @@ with open("TSPLIB/" + name, "r") as f:
#for l in Adj:
# for c in l:
# print(c, end=' ')
# print(' ')
# print(' ')