heuristique gloutonne
parent
bcb458a8fe
commit
2b74fed734
|
@ -0,0 +1,22 @@
|
|||
import networkx as nx
|
||||
|
||||
def nearN(G):
|
||||
vCur = 0
|
||||
seen = set([0])
|
||||
tour = nx.Graph()
|
||||
|
||||
for i in range(len(G.nodes()) - 1):
|
||||
nxt, w = 0, 1e9
|
||||
for nei in range(len(G.nodes())):
|
||||
if (nei not in seen and G[vCur][nei]['weight'] < w):
|
||||
nxt, w = nei, G[vCur][nei]['weight']
|
||||
seen.add(nxt)
|
||||
tour.add_edge(vCur, nxt, weight=w)
|
||||
vCur = nxt
|
||||
|
||||
tour.add_edge(vCur, 0, weight=G[vCur][0]['weight'])
|
||||
|
||||
size = 0
|
||||
for (d, a) in tour.edges():
|
||||
size += tour[d][a]['weight']
|
||||
return size, tour
|
12
testSep.py
12
testSep.py
|
@ -4,21 +4,22 @@ from separation_dual import LinearDualTSP
|
|||
from time import time
|
||||
import networkx as nx
|
||||
from heuristic_kruskal import heuristic
|
||||
from nearest_neig import nearN
|
||||
|
||||
#name = "burma14.tsp" #3001
|
||||
#name = "gr17.tsp" #1684
|
||||
#name = "gr21.tsp" #2707
|
||||
#name = "eil51.tsp" #416.5, 7s
|
||||
name = "gr24.tsp" #1224.5
|
||||
#name = "gr24.tsp" #1224.5
|
||||
#name = "st70.tsp" #623.5, 31s
|
||||
#name = "gr48.tsp" #4769
|
||||
#name = "pr76.tsp" #98994.5
|
||||
#name = "dantzig42.tsp"
|
||||
#name = "brazil58.tsp"
|
||||
#name = "berlin52.tsp"
|
||||
#name = "bayg29.tsp"
|
||||
#name = "bays29.tsp"
|
||||
#name = "berlin52.tsp"
|
||||
#name = "ulysses22.tsp"
|
||||
name = "ulysses22.tsp"
|
||||
|
||||
with open("TSPLIB/" + name, "r") as f:
|
||||
Adj = parse_file(f)
|
||||
|
@ -26,6 +27,8 @@ with open("TSPLIB/" + name, "r") as f:
|
|||
for l in range(len(Adj)):
|
||||
for c in range(l):
|
||||
G.add_edge(l, c, weight = Adj[l][c])
|
||||
|
||||
tnn, _ = nearN(G)
|
||||
t0 = time()
|
||||
ivalh, _ = heuristic(G)
|
||||
th = time() - t0
|
||||
|
@ -38,7 +41,8 @@ with open("TSPLIB/" + name, "r") as f:
|
|||
t0 = time()
|
||||
x2, ival2 = LinearDualTSP(Adj).solve()
|
||||
t2 = time() - t0
|
||||
print('heuristic : ', ivalh, th)
|
||||
print('tnn:', tnn)
|
||||
print('heuristic: ', ivalh, th)
|
||||
print("Primal:", ival, "time", t1)
|
||||
print("Dual:", ival2, "time", t2)
|
||||
#print(x)
|
||||
|
|
Loading…
Reference in New Issue