-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle_Swarm_Optimization.py
266 lines (216 loc) · 9.48 KB
/
Particle_Swarm_Optimization.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 numpy as np
import random
from past.builtins import range
random.seed()
Nd = 9 # Number of digits (in the case of standard Sudoku puzzles, this is 9x9).
class Particle(object):
def __init__(self, valid, given, weights, lazy_threshold):
self.weights = weights
self.valid = valid
self.lazy_threshold = lazy_threshold
self.given = given
self.position = np.zeros((Nd, Nd))
self.rand_position()
self.valid = np.zeros((Nd, Nd))
self.fitness = 0
self.personal_best_fitness = 0
self.global_best_fitness = 0
self.lazy = 0
self.personal_best_position = self.position.copy()
self.global_best_position = np.zeros((Nd, Nd))
self.velocity = np.zeros((Nd, Nd))
for row in range(Nd):
for column in range(Nd):
if given[row][column] == 0:
self.velocity[row][column] = random.uniform(-2, 2)
self.update_fitness()
def mutate(self, mutate_rate):
for row in range(len(self.position)):
if random.uniform(0, 1) < mutate_rate:
from_column = random.randint(0, 8)
to_column = random.randint(0, 8)
while from_column == to_column or self.given[row][from_column] != 0 or self.given[row][to_column] != 0:
from_column = random.randint(0, 8)
to_column = random.randint(0, 8)
self.position[row][from_column], self.position[row][to_column] = self.position[row][to_column], \
self.position[row][from_column]
def rand_position(self):
for i in range(0, Nd): # New row in candidate.
# Fill in the givens.
for j in range(0, Nd): # New column j value in row i.
# If value is already given, don't change it.
if self.given[i][j] != 0:
self.position[i][j] = self.given[i][j]
# Fill in the gaps using the helper board.
elif self.given[i][j] == 0:
self.position[i][j] = self.valid[i][j][random.randint(0, len(self.valid[i][j]) - 1)]
def update_lazy(self):
if self.fitness == self.global_best_fitness:
self.lazy += 1
if self.lazy >= self.lazy_threshold:
self.rand_position()
self.personal_best_position = self.position.copy()
self.update_fitness()
else:
self.lazy = 0
def update_fitness(self):
# Calculate overall fitness.
column_count = np.zeros(Nd)
block_count = np.zeros(Nd)
column_sum = 0
block_sum = 0
self.position = self.position.astype(int)
for j in range(0, Nd):
for i in range(0, Nd):
column_count[self.position[i][j] - 1] += 1
for k in range(len(column_count)):
if column_count[k] == 1:
column_sum += (1 / Nd) / Nd
column_count = np.zeros(Nd)
# For each block...
for i in range(0, Nd, 3):
for j in range(0, Nd, 3):
block_count[self.position[i][j] - 1] += 1
block_count[self.position[i][j + 1] - 1] += 1
block_count[self.position[i][j + 2] - 1] += 1
block_count[self.position[i + 1][j] - 1] += 1
block_count[self.position[i + 1][j + 1] - 1] += 1
block_count[self.position[i + 1][j + 2] - 1] += 1
block_count[self.position[i + 2][j] - 1] += 1
block_count[self.position[i + 2][j + 1] - 1] += 1
block_count[self.position[i + 2][j + 2] - 1] += 1
for k in range(len(block_count)):
if block_count[k] == 1:
block_sum += (1 / Nd) / Nd
block_count = np.zeros(Nd)
if int(column_sum) == 1 and int(block_sum) == 1:
fitness = 1.0
else:
fitness = column_sum * block_sum
self.fitness = fitness
# Sync best position and fitness.
if self.fitness > self.personal_best_fitness:
self.personal_best_fitness = self.fitness
self.personal_best_position = self.position.copy()
if self.fitness > self.global_best_fitness:
self.global_best_fitness = self.fitness
self.global_best_position = self.position.copy()
return
def update_velocity(self):
for i in range(Nd):
for j in range(Nd):
t1 = self.weights[0] * self.velocity[i][j]
t2 = self.weights[1] * random.uniform(0, 1) * (self.personal_best_position[i][j] - self.given[i][j])
t3 = self.weights[2] * random.uniform(0, 1) * (self.global_best_position[i][j] - self.given[i][j])
self.velocity[i][j] = t1 + t2 + t3
if self.velocity[i][j] > 3:
self.velocity[i][j] = -3
if self.velocity[i][j] < -3:
self.velocity[i][j] = 3
def update_position(self):
for i in range(Nd):
for j in range(Nd):
self.position[i][j] = self.position[i][j] + self.velocity[i][j]
if self.position[i][j] >= 9.5:
self.position[i][j] = round(self.position[i][j]) - 9
if self.position[i][j] < .5:
self.position[i][j] = round(self.position[i][j]) + 9
self.mutate(0.5)
class Swarm(object):
globalbestvalid = None
def __init__(self, given, weights, lazy_threshold):
self.nparticles = 500
self.particles = []
self.global_best_position = None
self.global_best_fitness = 0
self.step_best = 0
valid = self.get_valid(given)
for _ in range(self.nparticles):
p = Particle(valid, given, weights, lazy_threshold)
self.particles.append(p)
self.update_global_best()
def get_valid(self, given):
# Determine the legal values that each square can take.
helper = [[[] for j in range(0, Nd)] for i in range(0, Nd)]
for row in range(0, Nd):
for column in range(0, Nd):
for value in range(1, 10):
if given[row][column] == 0 and not (
self.is_column_duplicate(given, column, value) or self.is_block_duplicate(given, row,
column,
value) or self.is_row_duplicate(
given, row, value)):
# Value is available.
helper[row][column].append(value)
elif given[row][column] != 0:
# Given/known value from file.
helper[row][column].append(given[row][column])
break
return helper
def is_row_duplicate(self, values, row, value):
""" Check duplicate in a row. """
for column in range(0, Nd):
if values[row][column] == value:
return True
return False
def is_column_duplicate(self, values, column, value):
""" Check duplicate in a column. """
for row in range(0, Nd):
if values[row][column] == value:
return True
return False
def is_block_duplicate(self, values, row, column, value):
""" Check duplicate in a 3 x 3 block. """
i = 3 * (int(row / 3))
j = 3 * (int(column / 3))
if ((values[i][j] == value)
or (values[i][j + 1] == value)
or (values[i][j + 2] == value)
or (values[i + 1][j] == value)
or (values[i + 1][j + 1] == value)
or (values[i + 1][j + 2] == value)
or (values[i + 2][j] == value)
or (values[i + 2][j + 1] == value)
or (values[i + 2][j + 2] == value)):
return True
else:
return False
def update_global_best(self):
best = self.global_best_fitness
self.step_best = 0
for p in self.particles:
if p.fitness > self.step_best:
self.step_best = p.fitness
if p.global_best_fitness > best:
best = p.global_best_fitness
self.global_best_position = p.global_best_position
self.global_best_fitness = best
for p in self.particles:
p.global_best_fitness = best
p.global_best_position = self.global_best_position
def optimize(self):
for p in self.particles:
p.update_velocity()
p.update_position()
p.update_fitness()
p.update_lazy()
self.update_global_best()
class Sudoku(object):
""" Solves a given Sudoku puzzle using a Particle Swarm Optimization Algorithm. """
def __init__(self):
self.given = None
return
def load(self, given):
self.given = given
return
def solve(self):
Ns = 2000 # Number of the moving steps
s = Swarm(self.given, [.3, .7, .5], 10)
print("Generate particle swarm.")
for step in range(Ns):
if s.global_best_fitness == 1:
return step, s
else:
s.optimize()
print("Step:", step, " Best fitness:", s.step_best)
return -1, 1