-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc.py
182 lines (150 loc) · 7.02 KB
/
abc.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from PIL import Image
import numpy as np
import random
from matplotlib import pyplot as plt
image = Image.open('image.png')
print(image.format)
print(image.size)
print(image.mode)
image = image.convert('L')
image = image.resize((100,100))
image.show()
#print(image.format)
#print(image.size)
#print(image.mode)
DEBUG = False
def debug(str):
if DEBUG:
print(str)
def get_source():
image = Image.open('image.png')
image = image.convert('L')
image = image.resize((100,100))
source = np.array(image)
return source
#objective function = sum of all values in the row
def objective_function(array):
objective_function_array = np.sum(array,axis=1) #axis = 1 so that it calculates the sum of the array row wise.
#eg: a = np.sum([[0.5, 0.7, 0.2, 1.5],[0.5, 0.7, 0.2, 1.5]],axis=1)
return objective_function_array
def __generate_pi():
return random.uniform(0, 1)
def __individual_objective_function_and_fitness(array):
objective_score = np.sum(array)
if objective_score >=0:
fitness_score = 1/(1 + objective_score)
else:
fitness_score = 1 + objective_score
return objective_score, fitness_score
def create_trail_array(length):
return np.zeros(length)
def fitness_function(array):
fitness_function_array = []
for value in array:
if value >= 0:
fitness_function_array.append(1/(1 + value))
else:
fitness_function_array.append(1+value)
return fitness_function_array
def __choose_random_number():
return np.random.randint(1,100)
def __employed_bee_phase(source, objective_function_array, fitness_function_array, trail):
for bee in range (len(objective_function_array)):
random_varaible = __choose_random_number()
random_partner = __choose_random_number()
fi = __generate_pi()
bee_current_array = []
bee_current_array = source[bee]
source_new = (source[bee][random_varaible]) + (fi * (source[bee][random_varaible]-source[random_partner][random_varaible]))
debug("Employed bee phase: Bee No. {} and the modified value {}".format(bee, source_new))
debug(source_new)
bee_current_array[random_varaible] = source_new
objective_instance, fitness = __individual_objective_function_and_fitness(bee_current_array)
if (fitness < fitness_function_array[bee]):
source[bee][random_varaible] = source_new
objective_function_array[bee] = objective_instance
fitness_function_array[bee] = fitness
trail[bee] = 0
else:
trail[bee] += 1
return source, objective_function_array, fitness_function_array, trail
def __Onlooker_bee_phase(source, objective_function_array, fitness_function_array, trail, fitness_probability):
for onlooker_bee in range(len(objective_function_array)):
random_number = __generate_pi()
if (random_number<fitness_probability[onlooker_bee]):
random_varaible = __choose_random_number()
random_partner = __choose_random_number()
fi = __generate_pi()
bee_current_array = []
bee_current_array = source[onlooker_bee]
source_new = (source[onlooker_bee][random_varaible]) + (fi * (source[onlooker_bee][random_varaible]-source[random_partner][random_varaible]))
debug("Onlooker bee phase: Bee No. {} and the modified value {}".format(onlooker_bee, source_new))
debug(source_new)
bee_current_array[random_varaible] = source_new
objective_instance, fitness = __individual_objective_function_and_fitness(bee_current_array)
if (fitness < fitness_function_array[onlooker_bee]):
source[onlooker_bee][random_varaible] = source_new
objective_function_array[onlooker_bee] = objective_instance
fitness_function_array[onlooker_bee] = fitness
trail[onlooker_bee] = 0
else:
trail[onlooker_bee] += 1
else:
continue
return source, objective_function_array, fitness_function_array, trail
def __generate_new_source__():
return np.random.randint(0,100, size=(100))
def __onlooker_bee_phase_fitness_probability__(fitness_array):
probability_array = []
for individual_fitness in range (len(fitness_array)):
probability_array.append(individual_fitness/len(fitness_array))
return probability_array
def __scout_bee_phase(source, objective_function_array, fitness_function_array, trail, __scout_bee_limit__):
for scout_bee in range(len(trail)):
if (trail[scout_bee]>__scout_bee_limit__):
source[scout_bee] = __generate_new_source__()
objective_function_array[scout_bee], fitness_function_array[scout_bee] = __individual_objective_function_and_fitness(source[scout_bee])
trail[scout_bee] = 0
else:
continue
return source, objective_function_array, fitness_function_array, trail
def main():
for i in range (20):
__scout_bee_limit__ = 1
source = get_source()
#source = source.setflags(write=1)
debug("The source array is:")
debug(source)
#Employed bee phase
objective_function_array = objective_function(source)
debug("The objective function array is:")
debug(objective_function_array)
fitness_function_array = fitness_function(objective_function_array)
debug("The initial fitness function is")
debug(fitness_function_array)
trail = create_trail_array(len(objective_function_array))
debug("The initial trail array: ")
debug(trail)
source, objective_function_array, fitness_function_array, trail = __employed_bee_phase(source, objective_function_array, fitness_function_array, trail)
debug("The objective function array after employed bee phase is:")
debug(objective_function_array)
debug("The fitness function after employeed bee phase is")
debug(fitness_function_array)
debug("The trail array after employeed bee phase: ")
debug(trail)
#Onlooker bee phase
fitness_probability = __onlooker_bee_phase_fitness_probability__(fitness_function_array)
source, objective_function_array, fitness_function_array, trail = __Onlooker_bee_phase(source, objective_function_array, fitness_function_array, trail, fitness_probability)
debug("The objective function array after Onlooker bee phase is:")
debug(objective_function_array)
debug("The fitness function after onlooker bee phase is")
debug(fitness_function_array)
debug("The trail array after onlooker bee phase: ")
debug(trail)
#Scout bee phase
source, objective_function_array, fitness_function_array, trail = __scout_bee_phase(source, objective_function_array, fitness_function_array, trail, __scout_bee_limit__)
img = Image.fromarray(source)
img = img.convert("RGB")
img.save('my.png')
img.show()
main()