/
cs.py
79 lines (70 loc) · 3.14 KB
/
cs.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
"""Cuckoo search optimization
This module was used in the older version of gridf.
"""
import random
import lhs
from math import sin, gamma, pi
class Space(object):
def __init__(self, dimension, bound, d_function, n_nest):
self.pa = 0.25 #parameter
self.dimension = dimension
self.bound = bound
self.d_function = d_function
self.nests = [(d_function(*p), p) for p in lhs.latin_hypercube(dimension, bound, n_nest)]
self.best_value, self.best = max(self.nests)
def new_nest(space):
position = [2 * space.bound * random.random()
- space.bound for _ in xrange(space.dimension)]
value = space.d_function(*position)
return (value, position)
def get_cuckoos(space):
beta = 1.5
sigma = (gamma(1. + beta) * sin(pi * beta / 2.) / (gamma((1. + beta) / 2.) *
beta * 2. ** ((beta - 1.) / 2))) ** (1. / beta)
u_a = [[random.gauss(0, 1) * sigma for _ in xrange(space.dimension)] for _ in
xrange(len(space.nests))]
v_a = [[random.gauss(0, 1) for _ in xrange(space.dimension)] for _ in
xrange(len(space.nests))]
r_a = [[random.gauss(0, 1) for _ in xrange(space.dimension)] for _ in
xrange(len(space.nests))]
step = [[u / abs(v) ** (1. / beta) for (u, v) in zip(u_l, v_l)]
for (u_l, v_l) in zip(u_a, v_a)]
stepsize = [[0.01 * st * (n_e - be) for (st, n_e, be)
in zip(step_l, n_l, space.best)]
for (step_l, (_, n_l)) in zip(step, space.nests)]
s = [[n + st * r for (n, st, r) in zip(n_l, st_l, r_l)]
for ((_, n_l), st_l, r_l) in zip(space.nests, stepsize, r_a)]
cuckoos = [[min(max(st, - space.bound), space.bound) for st in st_l]
for st_l in s]
return [(space.d_function(*c), c) for c in cuckoos]
def get_empty(space):
r = random.random()
r_arr = [[random.random() for _ in xrange(space.dimension)] for _ in
xrange(len(space.nests))]
perm1 = [n for (_, n) in space.nests]
random.shuffle(perm1)
perm2 = [n for (_, n) in space.nests]
random.shuffle(perm2)
stepsize = [[p1 - p2 for (p1, p2) in zip (p1l, p2l)] for (p1l, p2l) in
zip(perm1, perm2)]
step = [[(r * p * (1 if random.random() > space.pa else 0)) for p in n] for n in stepsize]
empty = [[(p + s) for (p, s) in zip(sl, n)]
for (sl, (_, n)) in zip(step, space.nests)]
empty = [[min(max(st, - space.bound), space.bound) for st in st_l]
for st_l in empty]
return [(space.d_function(*e), e) for e in empty]
def next_turn(space):
cuckoos = get_cuckoos(space)
space.nests = [max(n, m) for (n, m) in zip(space.nests, cuckoos)]
nests = get_empty(space)
space.nests = [max(n, m) for (n, m) in zip(space.nests, nests)]
space.best_value, space.best = max(space.nests)
def optimize(dimension, boundary, function_d, n_nest, n_turns, reset=1):
best_list = []
for i in xrange(reset):
space = Space(dimension, boundary, function_d, n_nest)
for _ in xrange(n_turns / reset):
next_turn(space)
best_list.append((space.best_value, space.best))
# print space.best_value
return max(best_list)[1]