Skip to content

Commit 16ad30a

Browse files
committed
Refactor some stuff. Finish Variable Neighborhood Search.
Although we need to statistically analyze the Python and Ruby versions, ASAP since there seems to be a considerable discrepancy between the Python and Ruby versions of Variable Neighborhood Search.
1 parent 0eb91f3 commit 16ad30a

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

python/stochastic/common.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ def path_cost(permutation, cities):
3939
distance += euc_2d(cities[permutation[i]], cities[c2])
4040

4141
return distance
42+
43+
# Problem configuration
44+
berlin52 = [[565,575],[25,185],[345,750],[945,685],[845,655],
45+
[880,660],[25,230],[525,1000],[580,1175],[650,1130],[1605,620],
46+
[1220,580],[1465,200],[1530,5],[845,680],[725,370],[145,665],
47+
[415,635],[510,875],[560,365],[300,465],[520,585],[480,415],
48+
[835,625],[975,580],[1215,245],[1320,315],[1250,400],[660,180],
49+
[410,250],[420,555],[575,665],[1150,1160],[700,580],[685,595],
50+
[685,610],[770,610],[795,645],[720,635],[760,650],[475,960],
51+
[95,260],[875,920],[700,500],[555,815],[830,485],[1170,65],
52+
[830,610],[605,625],[595,360],[1340,725],[1740,245]]

python/stochastic/iterated_local_search.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,7 @@ def search(cities, max_iterations, max_no_improv):
102102
return best
103103

104104
if __name__ == "__main__":
105-
# Problem configuration
106-
berlin52 = [[565,575],[25,185],[345,750],[945,685],[845,655],
107-
[880,660],[25,230],[525,1000],[580,1175],[650,1130],[1605,620],
108-
[1220,580],[1465,200],[1530,5],[845,680],[725,370],[145,665],
109-
[415,635],[510,875],[560,365],[300,465],[520,585],[480,415],
110-
[835,625],[975,580],[1215,245],[1320,315],[1250,400],[660,180],
111-
[410,250],[420,555],[575,665],[1150,1160],[700,580],[685,595],
112-
[685,610],[770,610],[795,645],[720,635],[760,650],[475,960],
113-
[95,260],[875,920],[700,500],[555,815],[830,485],[1170,65],
114-
[830,610],[605,625],[595,360],[1340,725],[1740,245]]
105+
from .common import berlin52
115106

116107
# Algorithm configuration
117108
max_iterations = 100

python/stochastic/variable_neighborhood_search.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /usr/bin/env python3
22

3-
from .common import cost, random_permutation
3+
from .common import path_cost, random_permutation
44

55
import random
66

@@ -40,7 +40,7 @@ def local_search(best, cities, max_no_improv, neighborhood_size):
4040
for _ in range(neighborhood_size):
4141
stochastic_two_opt(candidate["vector"])
4242

43-
candidate["cost"] = cost(candidate["vector"], cities)
43+
candidate["cost"] = path_cost(candidate["vector"], cities)
4444

4545
if candidate["cost"] < best["cost"]:
4646
count, best = 0, candidate
@@ -52,22 +52,35 @@ def local_search(best, cities, max_no_improv, neighborhood_size):
5252
def search(cities, neigborhoods, max_no_improv, max_no_improv_ls):
5353
best = {}
5454
best["vector"] = random_permutation(cities)
55-
best["cost"] = cost(best["vector"], cities)
55+
best["cost"] = path_cost(best["vector"], cities)
5656
iter_, count = 0, 0
5757

5858
while count < max_no_improv:
59-
for neigh in negihborhoods:
59+
for neigh in neighborhoods:
6060
candidate = {}
6161
candidate["vector"] = [v for v in best["vector"]]
6262

6363
for _ in range(neigh):
6464
stochastic_two_opt(candidate["vector"])
6565

66-
candidate["cost"] - cost(candidate["vector"], cities)
66+
candidate["cost"] = path_cost(candidate["vector"], cities)
6767
candidate = local_search(candidate, cities, max_no_improv_ls, neigh)
6868
print("> iteration #%s, neigh=%s, best=%s" % (iter_ + 1, neigh, best["cost"]))
6969
iter_ += 1
7070

7171
if candidate["cost"] < best["cost"]:
7272
best, count = candidate, 0
7373
print("New best, restarting neighborhood search")
74+
break
75+
else:
76+
count += 1
77+
78+
return best
79+
80+
if __name__ == "__main__":
81+
from .common import berlin52
82+
max_no_improv = 50
83+
max_no_improv_ls = 70
84+
neighborhoods = list(range(20))
85+
best = search(berlin52, neighborhoods, max_no_improv, max_no_improv_ls)
86+
print("Done. Best Solution: c=%s, v=%s" % (best["cost"], best["vector"]))

0 commit comments

Comments
 (0)