-
Notifications
You must be signed in to change notification settings - Fork 0
/
bees.py
103 lines (82 loc) · 2.66 KB
/
bees.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from math import pi, sqrt, cos, exp
from random import uniform, seed
import matplotlib.pyplot as plt
plt.style.use('ggplot')
seed(49)
def func(x, y):
return (x**2 + y - 11)**2 + (x + y**2 - 7)**2 #функція Химмельблау
#return (x + 2*y - 7)**2 + (2*x + y - 5)**2 #функція Бута
#return -20*exp(-0.2*sqrt(0.5*(x**2 + y**2))) - exp(0.5*(cos(2*pi*x) + cos(2*pi*y))) + exp(1) + 20 #функція Еклі
iterations = 40
ns = 50
nb = 20
ne = 5
nre = 10
nrb = 5
r = 1
graf_bee = []
graf_mk = []
#________________________________INIT________________________________
bees = {}
for i in range(ns):
x = uniform(-5, 5)
y = uniform(-5, 5)
f = func(x, y)
bees[f] = {'x':x, 'y':y}
elits = {}
bests = {}
for i in range(nb):
if i < ne:
elits[min(bees)] = bees[min(bees)]
else:
bests[min(bees)] = bees[min(bees)]
del bees[min(bees)]
#________________________________MAIN________________________________
for iter in range(iterations):
bees = {}
for i in range(ns-nb):
x = uniform(-5, 5)
y = uniform(-5, 5)
f = func(x, y)
bees[f] = {'x':x, 'y':y}
for bee in elits:
forag = {'bee':bee, 'x':elits[bee]['x'], 'y':elits[bee]['y']}
for i in range(nre):
x = uniform(elits[bee]['x']-r, elits[bee]['x']+r)
y = uniform(elits[bee]['y']-r, elits[bee]['y']+r)
f = func(x, y)
if f < forag['bee']: forag = {'bee':f, 'x':x, 'y':y}
bees[forag['bee']] = {'x':forag['x'], 'y':forag['y']}
for bee in bests:
forag = {'bee':bee, 'x':bests[bee]['x'], 'y':bests[bee]['y']}
for i in range(nrb):
x = uniform(bests[bee]['x']-r, bests[bee]['x']+r)
y = uniform(bests[bee]['y']-r, bests[bee]['y']+r)
f = func(x, y)
if f < forag['bee']: forag = {'bee':f, 'x':x, 'y':y}
bees[forag['bee']] = {'x':forag['x'], 'y':forag['y']}
elits = {}
bests = {}
for i in range(nb):
if i < ne:
elits[min(bees)] = bees[min(bees)]
else:
bests[min(bees)] = bees[min(bees)]
del bees[min(bees)]
graf_bee.append(min(elits))
print(min(elits), elits[min(elits)]['x'], elits[min(elits)]['y'])
for iter in range(iterations):
temp = float('Inf')
for i in range(ns+(nb-ne)*nrb + ne*nre):
x = uniform(-5, 5)
y = uniform(-5, 5)
f = func(x, y)
if f < temp: temp = f
if graf_mk == [] or temp <= graf_mk[-1]:
graf_mk.append(temp)
elif temp > graf_mk[-1]:
graf_mk.append(graf_mk[-1])
else: print('Error!')
plt.plot(graf_bee)
plt.plot(graf_mk)
plt.show()