-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_anneal_utils.py
266 lines (236 loc) · 7.23 KB
/
sim_anneal_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import random
import numpy as np
import matplotlib.pyplot as plt
from tqdm.autonotebook import tqdm
def calculate_weight_matrix(n):
weights = np.abs(np.arange(n) - np.arange(n)[:, None])
for i in range(n):
for j in range(n):
if i == j:
continue
weights[i][j] += (i + j) * 0.01
return weights
def calculate_score(cm, weights):
return int(np.tensordot(cm, weights, axes=((0, 1), (0, 1))))
def calculate_score_min(cm, weights):
return -1 * int(np.tensordot(cm, weights, axes=((0, 1), (0, 1))))
def swap(cm, i, j):
"""
Swap row and column i and j in-place.
Parameters
----------
cm : ndarray
i : int
j : int
Examples
--------
>> cm = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>> swap(cm, 2, 0)
array([[8, 7, 6],
[5, 4, 3],
[2, 1, 0]])
"""
# swap columns
copy = cm[:, i].copy()
cm[:, i] = cm[:, j]
cm[:, j] = copy
# swap rows
copy = cm[i, :].copy()
cm[i, :] = cm[j, :]
cm[j, :] = copy
return cm
def move_1d(perm, from_start, from_end, insert_pos):
"""
Move a block in a list.
Parameters
----------
perm : ndarray
Permutation
from_start : int
from_end : int
insert_pos : int
Returns
-------
perm : ndarray
The new permutation
"""
if not (insert_pos < from_start or insert_pos > from_end):
raise ValueError(
"insert_pos={} needs to be smaller than from_start={}"
" or greater than from_end={}".format(insert_pos, from_start, from_end)
)
if insert_pos > from_end:
p_new = list(range(from_end + 1, insert_pos + 1)) + list(
range(from_start, from_end + 1)
)
else:
p_new = list(range(from_start, from_end + 1)) + list(
range(insert_pos, from_start)
)
p_old = sorted(p_new)
perm[p_old] = perm[p_new]
return perm
def move(cm, from_start, from_end, insert_pos):
"""
Move rows from_start - from_end to insert_pos in-place.
Parameters
----------
cm : ndarray
from_start : int
from_end : int
insert_pos : int
Returns
-------
cm : ndarray
Examples
--------
>> cm = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 0, 1], [2, 3, 4, 5]])
>> move(cm, 1, 2, 0)
array([[5, 6, 4, 7],
[9, 0, 8, 1],
[1, 2, 0, 3],
[3, 4, 2, 5]])
"""
if not (insert_pos < from_start or insert_pos > from_end):
raise ValueError(
"insert_pos={} needs to be smaller than from_start={}"
" or greater than from_end={}".format(insert_pos, from_start, from_end)
)
if insert_pos > from_end:
p_new = list(range(from_end + 1, insert_pos + 1)) + list(
range(from_start, from_end + 1)
)
else:
p_new = list(range(from_start, from_end + 1)) + list(
range(insert_pos, from_start)
)
p_old = sorted(p_new)
# swap columns
cm[:, p_old] = cm[:, p_new]
# swap rows
cm[p_old, :] = cm[p_new, :]
return cm
def swap_1d(perm, i, j):
perm[i], perm[j] = perm[j], perm[i]
return perm
def apply_permutation(cm, perm):
return cm[perm].transpose()[perm].transpose()
def generate_permutation(n, current_perm, tmp_cm):
"""
Generate a new permutation.
Parameters
----------
n : int
current_perm : List[int]
tmp_cm : np.ndarray
Return
------
perm, make_swap : List[int], bool
"""
swap_prob = 0.5
make_swap = random.random() < swap_prob
if n < 3:
# In this case block-swaps don't make any sense
make_swap = True
if make_swap:
# Choose what to swap
i = random.randint(0, n - 1)
j = i
while j == i:
j = random.randint(0, n - 1)
# Define permutation
perm = swap_1d(current_perm.copy(), i, j)
# Define values after swap
tmp_cm = swap(tmp_cm, i, j)
else:
# block-swap
block_len = n
while block_len >= n - 1:
from_start = random.randint(0, n - 3)
from_end = random.randint(from_start + 1, n - 2)
block_len = from_start - from_end
insert_pos = from_start
while not (insert_pos < from_start or insert_pos > from_end):
insert_pos = random.randint(0, n - 1)
perm = move_1d(current_perm.copy(), from_start, from_end, insert_pos)
# Define values after swap
tmp_cm = move(tmp_cm, from_start, from_end, insert_pos)
return perm, make_swap
def simulated_annealing(
current_cm,
current_perm=None,
score=calculate_score,
steps=2 * 10 ** 5,
temp=100.0,
cooling_factor=0.99,
deterministic=False,
calculate_weight_matrix=calculate_weight_matrix
):
"""
Optimize current_cm by randomly swapping elements.
Parameters
----------
current_cm : ndarray
current_perm : None or iterable, optional (default: None)
steps : int, optional (default: 2 * 10**4)
temp : float > 0.0, optional (default: 100.0)
Temperature
cooling_factor: float in (0, 1), optional (default: 0.99)
Returns
-------
best_result : Dict[str, Any]
"best_cm"
"best_perm"
"""
if temp <= 0.0:
raise ValueError("temp={} needs to be positive".format(temp))
if cooling_factor <= 0.0 or cooling_factor >= 1.0:
raise ValueError(
"cooling_factor={} needs to be in the interval "
"(0, 1)".format(cooling_factor)
)
n = len(current_cm)
if current_perm is None:
current_perm = list(range(n))
current_perm = np.array(current_perm)
# Pre-calculate weights
weights = calculate_weight_matrix(n)
# Apply the permutation
current_cm = apply_permutation(current_cm, current_perm)
current_score = score(current_cm, weights)
best_cm = current_cm
best_score = current_score
best_perm = current_perm
_scores = []
_scores_step = 1000
# print("## Starting Score: {:0.2f}".format(current_score))
pbar = tqdm(range(steps))
for step in pbar:
tmp_cm = np.array(current_cm, copy=True)
perm, make_swap = generate_permutation(n, current_perm, tmp_cm)
tmp_score = score(tmp_cm, weights)
# Should be swapped?
if deterministic:
chance = 1.0
else:
chance = random.random()
temp *= 0.99
hot_prob_thresh = min(1, np.exp(-(tmp_score - current_score) / temp))
if chance <= hot_prob_thresh:
changed = False
if best_score > tmp_score: # minimize
best_perm = perm
best_cm = tmp_cm
best_score = tmp_score
changed = True
current_score = tmp_score
current_cm = tmp_cm
current_perm = perm
if changed:
pbar.set_description("Current: %0.2f (best: %0.2f)" % (current_score, best_score))
if step > _scores_step * 2 and step % _scores_step == 0:
_scores.append(best_score)
plt.figure()
plt.plot(np.arange(len(_scores)) * _scores_step, _scores)
plt.title('Optimization score')
return {"cm": best_cm, "perm": best_perm, "scores": _scores}