-
Notifications
You must be signed in to change notification settings - Fork 0
/
FireflyOpt.py
39 lines (33 loc) · 1.47 KB
/
FireflyOpt.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
"""
Find optima of a given function using firefly algorithm
"""
from Firefly import Firefly
import operator
class FireflyOptimizer:
def __init__(self, obj_func, bounds, pop_size=10, dims=2, max_iters=50, alpha=0.25, beta_0=1, gamma=0.97):
self.benchmark = obj_func
self.max_iter = max_iters
self.best = None
self.population = [Firefly(alpha, beta_0, gamma, bounds[0], bounds[1], dims)
for _ in range(pop_size)]
# calculate initial intensity
for firefly in self.population:
firefly.update_intensity(self.benchmark)
def step(self):
# rank the solutions
self.population.sort(key=operator.attrgetter('intensity'), reverse=True)
# compare each pair of fireflies and move them accordingly
for i in self.population:
for j in self.population:
if j.intensity > i.intensity:
i.move_firefly(j.position)
i.update_intensity(self.benchmark)
# update the best solution
if not self.best or self.population[0].intensity > self.best:
self.best = self.population[0].intensity
def run_optim(self):
for it in range(self.max_iter):
self.step()
self.population.sort(key=operator.attrgetter('intensity'), reverse=True)
return self.population[0].position, self.population[0].intensity
# self.population[1].position, self.population[1].intensity