-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulateAnealing.py
42 lines (33 loc) · 1.03 KB
/
SimulateAnealing.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
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import numpy as np
def random_neighbour(x, bounds, fraction=1.):
"""
:param x:
:param fraction:
:return:
"""
size = len(x)
amplitude = (max(bounds) - min(bounds)) * fraction / 10.
delta = (-amplitude/2.) + amplitude * np.random.random(size=size)
return np.clip(x + delta, a_max=max(bounds), a_min=min(bounds))
class SA():
def __init__(self, state, costFunc, temperature, bounds):
"""
:param random_start:
:param costFunc:
:param temperature:
"""
self._state = state
self._costFunc = costFunc
self._temperature = temperature
self._bounds = bounds
def __call__(self, step_id, max_step):
"""
:return:
"""
cost = self._costFunc(self._state)
fraction = step_id / float(max_step)
T = self._temperature(fraction)
new_state = random_neighbour(self._state, self._bounds)