Skip to content

Commit 5e3ddcf

Browse files
authored
Merge pull request #38 from gugarosa/feature/chernobyl_disaster_optimizer
chore(opytimizer): Adds Chernobyl Disaster Optimizer
2 parents d058bc5 + 4c4bae0 commit 5e3ddcf

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from opytimizer.optimizers.science import CDO
2+
3+
# Creates a CDO optimizer
4+
o = CDO()

opytimizer/optimizers/science/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from opytimizer.optimizers.science.aig import AIG
66
from opytimizer.optimizers.science.aso import ASO
77
from opytimizer.optimizers.science.bh import BH
8+
from opytimizer.optimizers.science.cdo import CDO
89
from opytimizer.optimizers.science.efo import EFO
910
from opytimizer.optimizers.science.eo import EO
1011
from opytimizer.optimizers.science.esa import ESA

opytimizer/optimizers/science/cdo.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"""Chernobyl Disaster Optimizer.
2+
"""
3+
4+
import copy
5+
from typing import Any, Dict, Optional, Tuple
6+
7+
import numpy as np
8+
9+
import opytimizer.math.random as r
10+
import opytimizer.utils.constant as c
11+
from opytimizer.core.function import Function
12+
from opytimizer.core.space import Space
13+
from opytimizer.core import Optimizer
14+
from opytimizer.utils import logging
15+
16+
logger = logging.get_logger(__name__)
17+
18+
19+
class CDO(Optimizer):
20+
"""An CDO class, inherited from Optimizer.
21+
22+
This is the designed class to define CDO-related
23+
variables and methods.
24+
25+
References:
26+
H. Abedinpourshotorban et al.
27+
Electromagnetic field optimization: A physics-inspired metaheuristic optimization algorithm.
28+
Swarm and Evolutionary Computation (2016).
29+
30+
"""
31+
32+
def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
33+
"""Initialization method.
34+
35+
Args:
36+
params: Contains key-value parameters to the meta-heuristics.
37+
38+
"""
39+
40+
super(CDO, self).__init__()
41+
42+
43+
self.build(params)
44+
45+
logger.info("Class overrided.")
46+
47+
def compile(self, space: Space) -> None:
48+
"""Compiles additional information that is used by this optimizer.
49+
50+
Args:
51+
space: A Space object containing meta-information.
52+
53+
"""
54+
55+
self.gamma_pos = np.zeros((space.n_variables, space.n_dimensions))
56+
self.gamma_fit = c.FLOAT_MAX
57+
58+
self.beta_pos = np.zeros((space.n_variables, space.n_dimensions))
59+
self.beta_fit = c.FLOAT_MAX
60+
61+
self.alpha_pos = np.zeros((space.n_variables, space.n_dimensions))
62+
self.alpha_fit = c.FLOAT_MAX
63+
64+
def update(self, space: Space, function: Function, iteration: int, n_iterations: int) -> None:
65+
"""Wraps Chernobyl Disaster Optimizer over all agents and variables.
66+
67+
Args:
68+
space: Space containing agents and update-related information.
69+
function: A Function object that will be used as the objective function.
70+
iteration: Current iteration.
71+
n_iterations: Maximum number of iterations.
72+
73+
"""
74+
75+
for agent in space.agents:
76+
77+
fit = function(agent.position)
78+
79+
if fit < self.alpha_fit:
80+
self.alpha_fit = fit
81+
self.alpha_pos = copy.deepcopy(agent.position)
82+
83+
if fit < self.alpha_fit and fit < self.beta_fit:
84+
self.beta_fit = fit
85+
self.beta_pos = copy.deepcopy(agent.position)
86+
87+
if fit < self.alpha_fit and fit < self.beta_fit and fit < self.gamma_fit:
88+
self.gamma_fit = fit
89+
self.gamma_pos = copy.deepcopy(agent.position)
90+
91+
ws = 3 - 3 * iteration/n_iterations
92+
s_gamma = np.log10(r.generate_uniform_random_number(1, 300000))
93+
s_beta = np.log10(r.generate_uniform_random_number(1, 270000))
94+
s_alpha = np.log10(r.generate_uniform_random_number(1, 16000))
95+
96+
for agent in space.agents:
97+
98+
r1 = r.generate_uniform_random_number(
99+
size=(space.n_variables, space.n_dimensions)
100+
)
101+
r2 = r.generate_uniform_random_number(
102+
size=(space.n_variables, space.n_dimensions)
103+
)
104+
r3 = r.generate_uniform_random_number(
105+
size=(space.n_variables, space.n_dimensions)
106+
)
107+
108+
rho_gamma = np.pi * r1 * r1 / s_gamma - ws * r2
109+
a_gamma = r3 * r3 * np.pi
110+
grad_gamma = np.abs(a_gamma * self.gamma_pos - agent.position)
111+
v_gamma = agent.position - rho_gamma * grad_gamma
112+
113+
r1 = r.generate_uniform_random_number(
114+
size=(space.n_variables, space.n_dimensions)
115+
)
116+
r2 = r.generate_uniform_random_number(
117+
size=(space.n_variables, space.n_dimensions)
118+
)
119+
r3 = r.generate_uniform_random_number(
120+
size=(space.n_variables, space.n_dimensions)
121+
)
122+
123+
rho_beta = np.pi * r1 * r1 / (0.5 * s_beta) - ws * r2
124+
a_beta = r3 * r3 * np.pi
125+
grad_beta = np.abs(a_beta * self.beta_pos - agent.position)
126+
v_beta = 0.5 * (agent.position - rho_beta * grad_beta)
127+
128+
r1 = r.generate_uniform_random_number(
129+
size=(space.n_variables, space.n_dimensions)
130+
)
131+
r2 = r.generate_uniform_random_number(
132+
size=(space.n_variables, space.n_dimensions)
133+
)
134+
r3 = r.generate_uniform_random_number(
135+
size=(space.n_variables, space.n_dimensions)
136+
)
137+
138+
rho_alpha = np.pi * r1 * r1 / (0.25 * s_alpha) - ws * r2
139+
a_alpha = r3 * r3 * np.pi
140+
grad_alpha = np.abs(a_alpha * self.alpha_pos - agent.position)
141+
v_alpha = 0.25 * (agent.position - rho_alpha * grad_alpha)
142+
143+
agent.position = (v_alpha + v_beta + v_gamma) / 3
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
from opytimizer.optimizers.science import cdo
4+
from opytimizer.spaces import search
5+
6+
7+
def test_cdo_update():
8+
def square(x):
9+
return np.sum(x**2)
10+
11+
new_cdo = cdo.CDO()
12+
13+
search_space = search.SearchSpace(
14+
n_agents=20, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]
15+
)
16+
17+
new_cdo.update(search_space, square)

0 commit comments

Comments
 (0)