-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesdlsolver.py
159 lines (121 loc) · 4.83 KB
/
esdlsolver.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
import numpy as np
import time
import math
class Bee:
def __init__(self, lb, ub, limit, graph, N):
self.lb = lb
self.ub = ub
self.limit = limit
self.graph = graph
self.N = N
self.tried = 0
self.pos = np.random.uniform(low=lb, high=ub, size=N)
self.fitness = graph.cost(self.pos)
def set_pos(self, pos):
self.pos = np.copy(pos)
self.fitness = self.graph.cost(self.pos)
self.tried = 0
def fit_boundaries(self, pos):
pos[pos > self.ub] = self.ub
pos[pos < self.lb] = self.lb
def check(self):
if self.tried >= self.limit:
self.set_pos(np.random.uniform(low=self.lb, high=self.ub, size=self.N))
def search(self, gbest, m, e, flag):
new_bee = np.copy(self.pos)
j, h = np.random.choice(range(self.N), size=2, replace=False)
phi1 = np.random.uniform(low=-0.5, high=0.5)
phi2 = np.random.uniform(low=0, high=1)
if flag:
new_bee[j] = 0.5 * (m[h] + gbest[j]) + phi1 * (self.pos[h] - e[j]) + phi2 * (self.pos[h] - gbest[j])
else:
new_bee[j] = 0.5 * (m[j] + gbest[h]) + phi1 * (self.pos[j] - e[h]) + phi2 * (self.pos[j] - gbest[h])
self.fit_boundaries(new_bee)
if self.graph.cost(new_bee) > self.fitness:
self.set_pos(new_bee)
else:
self.tried += 1
class Solver:
def __init__(self, iters, SN, limit, graph, lb, ub, alpha, k, p, M):
self.iters = iters
self.SN = SN
self.limit = limit
self.graph = graph
self.lb = lb
self.ub = ub
self.alpha = alpha
self.CHI = k
self.p = p
self.N = graph.nodes
self.M = M
self.population = None
self.elite = None
self.fitness = None
self.weights = None
self.max_res = None
self.max_sol = None
self.cum_weights = None
def generate_population(self):
self.population = [Bee(self.lb, self.ub, self.limit, self.graph, self.N) for _ in range(self.SN)]
self.get_maximum()
def get_maximum(self):
for i in range(self.SN):
if self.max_res is None or self.max_res < self.population[i].fitness:
self.max_res = self.population[i].fitness
self.max_sol = np.copy(self.population[i].pos)
sorted_pos = np.argsort([self.population[i].fitness for i in range(self.SN)])
self.elite = sorted_pos[-self.M:]
def build(self):
self.fitness = np.array([(1 + self.population[i].fitness) if self.population[i].fitness > 0 else 1 / (1 - self.population[i].fitness)
for i in range(self.SN)])
total_sum = sum([self.fitness[i] ** self.alpha for i in range(self.SN)])
self.weights = np.array([(self.fitness[i] ** self.alpha) / total_sum for i in range(self.SN)])
self.cum_weights = np.cumsum(self.weights)
def search_for(self, i, m = -1):
e = np.random.choice(self.elite)
if m == -1:
m = e
self.population[i].search(self.max_sol, self.population[m].pos, self.population[e].pos, e == m)
def employed_bee_phase(self):
for i in range(self.SN):
self.search_for(i)
def onlooker_bee_phase(self):
for _ in range(self.SN):
i = np.random.choice(range(self.SN), p=self.weights)
for j in range(self.M):
self.search_for(i, self.elite[j])
def scout_bee_phase(self):
for i in range(self.SN):
self.population[i].check()
def solve(self):
iter_sol = np.zeros(self.iters)
self.generate_population()
for ith in range(self.iters):
# st = time.time()
self.employed_bee_phase()
# en = time.time()
# print('Employed bee phase took {} seconds'.format(en - st))
# st = time.time()
self.get_maximum()
# en = time.time()
# print('get_minimum took {} seconds'.format(en - st))
# st = time.time()
self.build()
# en = time.time()
# print('build took {} seconds'.format(en - st))
# st = time.time()
self.onlooker_bee_phase()
# en = time.time()
# print('onlooker bee phase took {} seconds'.format(en - st))
self.get_maximum()
# st = time.time()
self.scout_bee_phase()
# en = time.time()
# print('scout bee phase took {} seconds'.format(en - st))
# st = time.time()
self.get_maximum()
# en = time.time()
# print('get_minimum took {} seconds'.format(en - st))
iter_sol[ith] = self.max_res
# print('iteration no = {} Result = {}'.format(ith, self.max_res))
return self.max_res, self.max_sol, iter_sol