-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSA_code.py
More file actions
73 lines (53 loc) · 1.88 KB
/
Copy pathSA_code.py
File metadata and controls
73 lines (53 loc) · 1.88 KB
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
"""**********************************************************************/
Copyleft (C) 2016 Caetán Tojeiro Carpente
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/agpl-3.0.html>
/**********************************************************************"""
import random, numpy, math
import matplotlib.pyplot as plt
print "Introduce betta parameter (range[0,1])"
betta=input()
print "Introduce alpha parameter (range[0,1])"
alpha=input()
print "Introduce initial temperature (T)"
t_inicial=input()
def sim_anneal():
y=0
t_anterior=t_inicial/alpha #Multiply by alpha to start at initial temperature
p_actual=0
while True:
T=funcion_Temperatura(t_anterior)
if T<=0.01 :
return p_actual,y
t_anterior=T
p_seguinte=funcion_vecinho(p_actual)
custo=funcion_custo(p_seguinte)-funcion_custo(p_actual)
if custo>0:
p_actual=p_seguinte
else:
if (math.exp((custo)/T)>random.uniform(0,1)):
p_actual=p_seguinte
y=funcion_custo(p_actual)
print y
plt.plot(p_actual, y, 'b^')
def funcion_Temperatura(temp):
t=betta*temp #Change the formula
return t
def funcion_vecinho(p_actual):
vecinho=random.uniform((-alpha*40)+p_actual,(alpha*40)+p_actual)
if vecinho<0 or vecinho >40:
return funcion_vecinho(p_actual)
return vecinho
def funcion_custo(x):
y=numpy.sin(0.15*x)+numpy.cos(x)
return y
print sim_anneal()
plt.show()