Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Grégoire Grzeczkowicz
sho
Commits
3e398962
Commit
3e398962
authored
Oct 12, 2020
by
Grégoire GRZECZKOWICZ
Browse files
Simulated Annealing with two kind of temperature evolution
parents
0f79050a
f2d89939
Changes
3
Hide whitespace changes
Inline
Side-by-side
sho/algo.py
View file @
3e398962
...
...
@@ -36,13 +36,12 @@ def greedy(func, init, neighb, again):
return
best_val
,
best_sol
def
simulated_annealing
(
func
,
init
,
neighb
,
again
):
def
simulated_annealing
(
func
,
init
,
neighb
,
temp
,
again
):
"""Iterative randomized simulated-annealing template."""
best_sol
=
init
()
best_val
=
func
(
best_sol
)
val
,
sol
=
best_val
,
best_sol
i
=
1
T
=
1
while
again
(
i
,
best_val
,
best_sol
):
sol
=
neighb
(
best_sol
)
val
=
func
(
sol
)
...
...
@@ -50,7 +49,7 @@ def simulated_annealing(func, init, neighb, again):
if
val
>=
best_val
:
best_val
=
val
best_sol
=
sol
elif
np
.
random
.
random
()
<
np
.
exp
(
-
(
best_val
-
val
)
/
T
):
elif
np
.
random
.
random
()
<
np
.
exp
(
-
(
best_val
-
val
)
/
temp
(
i
,
best_val
,
best_sol
)
):
best_val
=
val
best_sol
=
sol
i
+=
1
...
...
sho/temp.py
0 → 100644
View file @
3e398962
########################################################################
# Temperature programme
########################################################################
class
continus
:
"""Continus decrease temperature from T_0 and with T_{i+1} =
\a
lpha x T_i"""
def
__init__
(
self
,
T_0
,
alpha
):
self
.
T
=
T_0
self
.
alpha
=
alpha
def
__call__
(
self
,
i
,
val
,
sol
):
T
=
self
.
T
self
.
T
*=
self
.
alpha
return
T
class
level
:
"""Level decrease temperature from T_0 and with T_{i+1} = T_i -
\a
lpha after
\b
eta changes"""
def
__init__
(
self
,
T_0
,
alpha
,
beta
):
self
.
T
=
T_0
self
.
alpha
=
alpha
self
.
beta
=
beta
self
.
val
=
None
self
.
count
=
0
def
__call__
(
self
,
i
,
val
,
sol
):
T
=
self
.
T
if
val
!=
self
.
val
:
self
.
count
+=
1
self
.
val
=
val
if
self
.
count
>
self
.
beta
:
self
.
count
=
0
self
.
T
-=
self
.
alpha
print
()
print
(
"T = "
+
str
(
T
))
return
T
\ No newline at end of file
snp.py
View file @
3e398962
...
...
@@ -3,7 +3,7 @@ import math
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
sho
import
make
,
algo
,
iters
,
plot
,
num
,
bit
,
pb
from
sho
import
make
,
algo
,
iters
,
plot
,
num
,
bit
,
pb
,
temp
########################################################################
# Interface
...
...
@@ -140,6 +140,7 @@ if __name__=="__main__":
make
.
neig
(
num
.
neighb_square
,
scale
=
the
.
variation_scale
,
domain_width
=
the
.
domain_width
),
temp
.
continus
(
100
,
0.99
),
iters
)
sensors
=
num
.
to_sensors
(
sol
)
...
...
@@ -156,6 +157,7 @@ if __name__=="__main__":
make
.
neig
(
bit
.
neighb_square
,
scale
=
the
.
variation_scale
,
domain_width
=
the
.
domain_width
),
temp
.
continus
(
100
,
0.99
),
iters
)
sensors
=
bit
.
to_sensors
(
sol
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment