move pygame out of the way
[imago.git] / src / cs.py
1 """Cuckoo search optimization"""
2
3 import random
4 import lhs
5 from math import sin, gamma, pi
6
7 class Space(object):
8     def __init__(self, dimension, bound, d_function, n_nest):
9         self.pa = 0.25 #parameter
10         self.dimension = dimension
11         self.bound = bound
12         self.d_function = d_function
13         self.nests = [(d_function(*p), p) for p in lhs.latin_hypercube(dimension, bound, n_nest)]
14         self.best_value, self.best = max(self.nests)
15
16 def new_nest(space):
17     position = [2 * space.bound * random.random() 
18                 - space.bound for _ in xrange(space.dimension)]
19     value = space.d_function(*position)
20     return (value, position)
21
22 def get_cuckoos(space):
23     beta = 1.5
24     sigma = (gamma(1. + beta) * sin(pi * beta / 2.) / (gamma((1. + beta) / 2.) *
25              beta * 2. ** ((beta - 1.) / 2))) ** (1. / beta)
26     u_a = [[random.gauss(0, 1) * sigma for _ in xrange(space.dimension)] for _ in
27          xrange(len(space.nests))]
28     v_a = [[random.gauss(0, 1) for _ in xrange(space.dimension)] for _ in
29          xrange(len(space.nests))]
30     r_a = [[random.gauss(0, 1) for _ in xrange(space.dimension)] for _ in
31          xrange(len(space.nests))]
32     step = [[u / abs(v) ** (1. / beta) for (u, v) in zip(u_l, v_l)]
33             for (u_l, v_l) in zip(u_a, v_a)]
34     stepsize = [[0.01 * st * (n_e - be) for (st, n_e, be) 
35                 in zip(step_l, n_l, space.best)]
36                 for (step_l, (_, n_l)) in zip(step, space.nests)]
37     s = [[n + st * r for (n, st, r) in zip(n_l, st_l, r_l)]
38          for ((_, n_l), st_l, r_l) in zip(space.nests, stepsize, r_a)]
39     cuckoos = [[min(max(st, - space.bound), space.bound) for st in st_l]
40          for st_l in s]
41     return [(space.d_function(*c), c) for c in cuckoos]
42
43 def get_empty(space):
44     r = random.random()
45     r_arr = [[random.random() for _ in xrange(space.dimension)] for _ in
46              xrange(len(space.nests))]
47     perm1 = [n for (_, n) in space.nests]
48     random.shuffle(perm1)
49     perm2 = [n for (_, n) in space.nests]
50     random.shuffle(perm2)
51     stepsize = [[p1 - p2 for (p1, p2) in zip (p1l, p2l)] for (p1l, p2l) in
52                 zip(perm1, perm2)]
53     step = [[(r * p * (1 if random.random() > space.pa else 0)) for p in n] for n in stepsize]
54     empty = [[(p + s) for (p, s) in zip(sl, n)] 
55             for (sl, (_, n)) in zip(step, space.nests)]
56     empty = [[min(max(st, - space.bound), space.bound) for st in st_l]
57          for st_l in empty]
58     return [(space.d_function(*e), e) for e in empty]
59
60 def next_turn(space):
61     cuckoos = get_cuckoos(space)
62     space.nests = [max(n, m) for (n, m) in zip(space.nests, cuckoos)]
63     nests = get_empty(space)
64     space.nests = [max(n, m) for (n, m) in zip(space.nests, nests)]
65     space.best_value, space.best = max(space.nests)
66
67 def optimize(dimension, boundary, function_d, n_nest, n_turns, reset=1):
68     best_list = []
69     for i in xrange(reset):
70         space = Space(dimension, boundary, function_d, n_nest)
71         for _ in xrange(n_turns / reset):
72             next_turn(space)
73         best_list.append((space.best_value, space.best))
74         # print space.best_value
75
76     return max(best_list)[1]