-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulatedAnnealing.py
182 lines (150 loc) · 5.03 KB
/
SimulatedAnnealing.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
import random
import math
from copy import copy
from time import process_time
class Queen:
def __init__(self, row, column):
self.row = int(row)
self.column = int(column)
self.attackers = 0
def print(self):
print("(", self.row, ",", self.column, ")", sep='', end='')
def __eq__(self, other):
if self is not None:
return self.column == other.column and self.row == other.row
def __sub__(self, other):
if self is not None:
return abs(self.column - other.column) - abs(self.row - other.row)
class Board:
def __init__(self, w):
if w < 4:
self.w = 4
self.h = 4
else:
self.w = w
self.h = w
self.board = [[0 for x in range(self.w)] for y in range(self.h)]
self.queens = []
def print(self):
for i in self.board:
for j in range(self.w):
if isinstance(i[j], Queen):
i[j].print()
else:
print("(-", ",", "-)", sep='', end='')
print()
def choose_random(self):
rnd = random.randint(0, self.w - 1)
for i in self.queens:
if i.column == rnd:
x = copy(i)
return x
def choose_next(self):
queens = copy(self.queens)
q1 = self.choose_random()
rnd = random.randint(0, self.w - 1)
q1.row = rnd
for i, queen in enumerate(queens):
if queen.column == q1.column:
queens[i] = q1
break
return queens
def update(self):
queens = copy(self.queens)
for i, queen in enumerate(queens):
for j in range(self.w):
if isinstance(self.board[j][i], Queen):
if self.board[j][i] not in queens:
self.board[j][i] = 0
if j == queen.row:
self.board[j][i] = queen
def get_num_of_attackers(queens):
attackers = 0
for i in queens:
row1, row2, adj1, adj2, adj3, adj4 = True, True, True, True, True, True
for j in queens:
if i.row == j.row and i.column < j.column and i != j and row1:
attackers += 1
row1 = False
elif i.row == j.row and i.column > j.column and i != j and row2:
attackers += 1
row2 = False
elif i - j == 0 and i.column < j.column and i.row < j.row and i != j and adj1:
attackers += 1
adj1 = False
elif i - j == 0 and i.column > j.column and i.row > j.row and i != j and adj2:
attackers += 1
adj2 = False
elif i - j == 0 and i.column < j.column and i.row > j.row and i != j and adj3:
attackers += 1
adj3 = False
elif i - j == 0 and i.column > j.column and i.row < j.row and i != j and adj4:
attackers += 1
adj4 = False
return attackers
def init(board):
rand = []
for i in range(board.w):
x = random.randint(0, board.w - 1)
y = i
str1 = x, y
while str1 in rand:
x = random.randint(0, board.w - 1)
str1 = x, y
rand.append(str1)
q1 = Queen(x, y)
board.board[x][y] = q1
board.queens.append(q1)
def simulated_annealing(board, temp, alpha):
min_temp = 0.1
next_value = 10
steps_list = []
prop_list = []
energy_list = []
while next_value != 0:
current_value = get_num_of_attackers(board.queens)
# print("Current:")
for k in board.queens:
k.print()
qu1 = board.choose_next()
# print("\nNext:")
for k in qu1:
k.print()
print()
next_value = get_num_of_attackers(qu1)
energy = current_value - next_value
print()
# print("current =", current_value)
# print("next =", next_value)
print("energy =", energy)
print(temp)
rand = random.uniform(0, 1)
prop_func = math.exp(energy / temp)
prop_list.append(prop_func)
print("rand:", rand)
print("prop_func:", prop_func)
energy_list.append(energy)
if energy > 0:
board.queens = qu1
elif rand < prop_func:
board.queens = qu1
board.update()
steps_list.append(board.queens)
print("---------------------------------------------------")
# board.print()
if temp > min_temp:
temp = alpha * temp
if temp < min_temp:
temp = min_temp
return steps_list, prop_list, energy_list
def startAnnealing(n, temp, alpha):
start_time = process_time()
b1 = Board(n)
init(b1)
b1.print()
steps_list, prop_list, energy_list = simulated_annealing(b1, temp, alpha)
stop_time = process_time()
print("Elapsed time:", stop_time - start_time)
return steps_list, prop_list, energy_list
if __name__ == "__main__":
startAnnealing(4, 1000, 0.9)