Skip to content

Commit 799c479

Browse files
committed
Working refactoring.
1 parent a917cbe commit 799c479

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

python/stochastic/common.py

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

33
import math
4+
import random
45

56
"""
67
Not in the original code listing. This factors out the common functions
@@ -11,3 +12,33 @@ def euc_2d(p1, p2):
1112
dx = p1[0] - p2[0]
1213
dy = p1[1] - p2[1]
1314
return math.sqrt((dx ** 2) + (dy ** 2))
15+
16+
def random_permutation(cities):
17+
"""
18+
Given a list of cities, return a random permutation _of the indices_.
19+
"""
20+
indices = [idx for idx, val in enumerate(cities)]
21+
for i in indices:
22+
# Guaranteed not to choose i as well
23+
r = random.randint(0, len(cities) - 1 - i) + i
24+
indices[r], indices[i] = indices[i], indices[r]
25+
26+
return indices
27+
28+
def path_cost(permutation, cities):
29+
"""
30+
Given a permutation of indices and the actual location of the cities,
31+
the total cost (assessed as Euclidean distance) of traversing the path
32+
described by the permutation.
33+
"""
34+
distance = 0
35+
limit = len(permutation)
36+
37+
for i in range(limit):
38+
if i == (limit - 1):
39+
c2 = 0
40+
else:
41+
c2 = i + 1
42+
distance += euc_2d(cities[permutation[i]], cities[c2])
43+
44+
return distance

python/stochastic/iterated_local_search.py

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

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

55
import math
66
import random
@@ -23,29 +23,6 @@
2323
7542 units.
2424
"""
2525

26-
def cost(permutation, cities):
27-
distance = 0
28-
limit = len(permutation)
29-
30-
for i in range(limit):
31-
if i == (limit - 1):
32-
c2 = permutation[0]
33-
else:
34-
c2 = permutation[i + 1]
35-
36-
distance += euc_2d(cities[permutation[i]], cities[c2])
37-
38-
return distance
39-
40-
def random_permutation(cities):
41-
perm = [i for i in range(len(cities))]
42-
43-
for i in perm:
44-
r = random.randint(0, len(perm) - 1 - i) + i
45-
perm[r], perm[i] = perm[i], perm[r]
46-
47-
return perm
48-
4926
def stochastic_two_opt(permutation):
5027
perm = [permutation[i] for i in range(len(permutation))]
5128
upper_bound = len(perm) - 1
@@ -81,7 +58,7 @@ def local_search(best, cities, max_no_improv):
8158
while count < max_no_improv:
8259
candidate = {}
8360
candidate["vector"] = stochastic_two_opt(best["vector"])
84-
candidate["cost"] = cost(candidate["vector"], cities)
61+
candidate["cost"] = path_cost(candidate["vector"], cities)
8562

8663
if candidate["cost"] < best["cost"]:
8764
count = 0
@@ -104,13 +81,13 @@ def double_bridge_move(perm):
10481
def perturbation(cities, best):
10582
candidate = {}
10683
candidate["vector"] = double_bridge_move(best["vector"])
107-
candidate["cost"] = cost(candidate["vector"], cities)
84+
candidate["cost"] = path_cost(candidate["vector"], cities)
10885
return candidate
10986

11087
def search(cities, max_iterations, max_no_improv):
11188
best = {}
11289
best["vector"] = random_permutation(cities)
113-
best["cost"] = cost(best["vector"], cities)
90+
best["cost"] = path_cost(best["vector"], cities)
11491
best = local_search(best, cities, max_no_improv)
11592

11693
for i in range(max_iterations):

python/stochastic/stochastic_hill_climbing.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
Implementation notes:
2222
The reference implementation uses a list of (one-character) strings.
2323
I opted to use a String object directly.
24-
25-
@author Chad Estioco
2624
"""
2725

2826
def onemax(vector):

python/stochastic/variable_local_search.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,3 @@
1313
global minimum is local minimum for all possible neighborhood structures, and
1414
3) local minima are relatively close to global minima for many problem classes.
1515
"""
16-
17-
def cost(permutation, cities):
18-
distance = 0
19-
limit = len(permutation)
20-
21-
for idx, perm:
22-
c2 = permutation[idx % limit]

0 commit comments

Comments
 (0)