-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulatedAnnealing.py
200 lines (165 loc) · 5.74 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
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
import math
import random
# functii de testat
def rast_function(parameters = []):
nr_parameters = len(parameters)
s = 0
for number in parameters:
s = s + (pow(number,2) - 10 * math.cos(2 * math.pi * number))
return 10 * nr_parameters + s
def griew_function(parameters = []):
nr_parameters = len(parameters)
fr = 4000
s = 0
p = 1
for number in parameters:
s = s + pow(number,2)
for i in range(1,len(parameters)):
p = p * math.cos(parameters[i] / math.sqrt(i))
return s / fr - p + 1
def six_function (parameters = []):
for number in parameters:
return (4 - 2.1 * pow(number, 2) + float((number ** 4)/3)) * pow(number,2) + number * number + (-4 + 4 * pow(number, 2)) * pow(number, 2)
def rosen_function (parameters = []):
s = 0
for i in range(len(parameters)-1):
x = parameters[i] * parameters[i] - parameters[i+1]
print("x-ul", x)
s = s + 100 * pow(x, 2) + pow(parameters[i]-1, 2)
print("s-ul", s)
return s
# Generare numar random binar
def random_bytes_string(lungime):
bytes=[0,1]
rezultat=[]
for i in range(lungime):
rezultat.append(random.choice(bytes))
return rezultat
# Conversie in nr. zecimal
def conversie(bytesString=[]):
sir=""
for i in bytesString:
sir=sir+str(i)
return sir
# vecini
def vecini(vector_biti= []):
b = len(vector_biti)
matrice = []
for i in range(b):
aux = []
for j in range(b):
aux.append(1)
matrice.append(aux)
for i in range(b):
for j in range(b):
if i == j and vector_biti[j] == 1:
matrice[i][j] = 0
else:
if i == j and vector_biti[j] == 0:
matrice[i][j] = 1
else:
matrice[i][j] = vector_biti[j]
return matrice
# Determinare numere reale
def X_real(numar,a,b,n,precizie):
nr_real = a + ( numar * (b-a) / (2 ** n - 1) )
return round(nr_real,precizie)
def probabilitateAcceptare(bestVechi, bestNou, temperatura):
if bestNou < bestVechi:
return 1.0;
return math.exp((-abs(bestVechi - bestNou)) / temperatura)
# MAIN
def tema1 ():
# Initializari:
t = 0
oprire = 0
pas = 1
# Citire:
a = -10 #float(input("Minim interval: "))
b = 10 #float(input("Maxim interval: "))
precizie = 12 #int(input("Precizie: "))
# Reprezentarea solutiilor
numere_in_interval = b-a
N = numere_in_interval * 10 ** precizie
n = math.ceil(math.log(N, 2))
valoare_initiala = random_bytes_string(n)
# Initializare best
x = X_real(int(conversie(valoare_initiala),2), a, b, n, precizie)
vector_best = [1]
vector_best[0] = x
best = rast_function(vector_best)
# MAI SUS SCHIMBAM FUNCTIA
print(best)
#Salvam valoare minima cea mai buna impreuna cu pozitia sa
marsBest = 10000
marsBestPosition = 123
# Conditii Simulated Annealing
numar_random = random.uniform(0,1)
temperatura = 10000
temperaturaMinima = 1000
conditie = pas / temperatura
# SIMULATED ANNEALING
while temperatura > temperaturaMinima:
rep = 1
while rep <= 100:
print("Repetitia: ", t)
# Determinare vecini
vecini_biti = vecini(valoare_initiala)
vecini_zecimali = [0 for x in range(n)]
for i in range(n):
aux = []
for j in range(n):
aux.append(vecini_biti[i][j])
vecini_zecimali[i] = int(conversie(aux),2)
# Calcul X
vector_X = [0 for x in range(n+1)]
vector_X[0] = X_real(int(conversie(valoare_initiala),2), a, b, n, precizie)
for i in range(1, n+1):
vector_X[i] = X_real(vecini_zecimali[i-1], a, b, n, precizie)
# Rezultate
lista_rezultate = []
for number in vector_X:
list_number = []
list_number.append(number)
lista_rezultate.append(rast_function(list_number))
# MAI SUS SCHIMBAM FUNCTIA
# Determinare solutiei minime
print("Vecini biti: ", vecini_biti)
print("Vecini zecimali: ", vecini_zecimali)
print("Vector X: ", vector_X)
print("Lista rezultate:", lista_rezultate)
minim = min(lista_rezultate)
#Decidem daca sa acceptam minimul (pentru variante mai slabe decat best)
if probabilitateAcceptare(minim, best, temperatura) > random.uniform(0, 1):
best = minim
#salvam cea mai buna solutie
if best < marsBest:
#adaugare chestii care ne interesaza
#precum: stare curenta pentru mars best... + restu
marsBest = best
marsBestPosition = t
pozitie = 0
for i in range(n+1):
if minim == lista_rezultate[i]:
pozitie = i
print("Pozitie valoare minima: ", pozitie)
for i in range(n):
valoare_initiala[i] = vecini_biti[pozitie-1][i]
# Incrementam pasul
t = t + 1
print("Solutie optima la repetitia ", t, " : ", best, "\n")
pas = pas +1
temperatura -= 10
rep += 1
valoare_initiala = random_bytes_string(n)
print("Noua valoare numerica pe biti random: ", valoare_initiala)
print("Temperatura: ", temperatura)
print("Numar random intre 0 si 1: ", numar_random)
conditie = pas / temperatura
print("Valoarea conditiei: ", conditie)
numar_random = random.uniform(0,1)
conditie = pas / temperatura
print(pas)
print(marsBest)
print(marsBestPosition)
tema1()