-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalSearch.py
63 lines (58 loc) · 2.31 KB
/
LocalSearch.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
import time
from random import random
class LocalSearch:
neighbourhood_range = 100
neighbourhood_step_multiplier = 2
minimum = ()
@staticmethod
def minimalize_function(f, starting_position, time_to_run):
deadline = time.time() + time_to_run
x = starting_position
found_lower = True
temp_x = x
temp_min = f(*x)
LocalSearch.minimum = (temp_x, temp_min)
while time.time() < deadline:
if found_lower:
x = temp_x
else:
x = LocalSearch.change_x(x, random.random(), random.randint(0, 3))
for i in range(4):
for increase_radius in [True, False]:
start = 1
for step in range(LocalSearch.neighbourhood_range):
if time.time() > deadline:
break
if increase_radius:
start *= LocalSearch.neighbourhood_step_multiplier
else:
start /= LocalSearch.neighbourhood_step_multiplier
try:
new_x = LocalSearch.change_x(x, start, i)
new_value = f(*new_x)
except OverflowError:
continue
if new_value < temp_min:
temp_x = new_x
temp_min = new_value
try:
new_x = LocalSearch.change_x(x, -start, i)
new_value = f(*new_x)
except OverflowError:
continue
if new_value < temp_min:
temp_x = new_x
temp_min = new_value
if temp_min < LocalSearch.minimum[1]:
LocalSearch.minimum = (temp_x, temp_min)
return LocalSearch.minimum
@staticmethod
def change_x(x, step, dimension):
if dimension == 0:
return x[0] + step, x[1], x[2], x[3]
if dimension == 1:
return x[0], x[1] + step, x[2], x[3]
if dimension == 2:
return x[0], x[1], x[2] + step, x[3]
if dimension == 3:
return x[0], x[1], x[2], x[3] + step