|
| 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 | + iteration: Current iteration. |
| 70 | + n_iterations: Maximum number of iterations. |
| 71 | +
|
| 72 | + """ |
| 73 | + |
| 74 | + for agent in space.agents: |
| 75 | + |
| 76 | + fit = function(agent.position) |
| 77 | + |
| 78 | + if fit < self.alpha_fit: |
| 79 | + self.alpha_fit = fit |
| 80 | + self.alpha_pos = copy.deepcopy(agent.position) |
| 81 | + |
| 82 | + if fit < self.alpha_fit and fit < self.beta_fit: |
| 83 | + self.beta_fit = fit |
| 84 | + self.beta_pos = copy.deepcopy(agent.position) |
| 85 | + |
| 86 | + if fit < self.alpha_fit and fit < self.beta_fit and fit < self.gamma_fit: |
| 87 | + self.gamma_fit = fit |
| 88 | + self.gamma_pos = copy.deepcopy(agent.position) |
| 89 | + |
| 90 | + ws = 3 - 3 * iteration/n_iterations |
| 91 | + s_gamma = np.log10(r.generate_uniform_random_number(1, 300000)) |
| 92 | + s_beta = np.log10(r.generate_uniform_random_number(1, 270000)) |
| 93 | + s_alpha = np.log10(r.generate_uniform_random_number(1, 16000)) |
| 94 | + |
| 95 | + for agent in space.agents: |
| 96 | + |
| 97 | + r1 = r.generate_uniform_random_number( |
| 98 | + size=(space.n_variables, space.n_dimensions) |
| 99 | + ) |
| 100 | + r2 = r.generate_uniform_random_number( |
| 101 | + size=(space.n_variables, space.n_dimensions) |
| 102 | + ) |
| 103 | + r3 = r.generate_uniform_random_number( |
| 104 | + size=(space.n_variables, space.n_dimensions) |
| 105 | + ) |
| 106 | + |
| 107 | + rho_gamma = np.pi * r1 * r1 / s_gamma - ws * r2 |
| 108 | + a_gamma = r3 * r3 * np.pi |
| 109 | + grad_gamma = np.abs(a_gamma * self.gamma_pos - agent.position) |
| 110 | + v_gamma = agent.position - rho_gamma * grad_gamma |
| 111 | + |
| 112 | + r1 = r.generate_uniform_random_number( |
| 113 | + size=(space.n_variables, space.n_dimensions) |
| 114 | + ) |
| 115 | + r2 = r.generate_uniform_random_number( |
| 116 | + size=(space.n_variables, space.n_dimensions) |
| 117 | + ) |
| 118 | + r3 = r.generate_uniform_random_number( |
| 119 | + size=(space.n_variables, space.n_dimensions) |
| 120 | + ) |
| 121 | + |
| 122 | + rho_beta = np.pi * r1 * r1 / (0.5 * s_beta) - ws * r2 |
| 123 | + a_beta = r3 * r3 * np.pi |
| 124 | + grad_beta = np.abs(a_beta * self.beta_pos - agent.position) |
| 125 | + v_beta = 0.5 * (agent.position - rho_beta * grad_beta) |
| 126 | + |
| 127 | + r1 = r.generate_uniform_random_number( |
| 128 | + size=(space.n_variables, space.n_dimensions) |
| 129 | + ) |
| 130 | + r2 = r.generate_uniform_random_number( |
| 131 | + size=(space.n_variables, space.n_dimensions) |
| 132 | + ) |
| 133 | + r3 = r.generate_uniform_random_number( |
| 134 | + size=(space.n_variables, space.n_dimensions) |
| 135 | + ) |
| 136 | + |
| 137 | + rho_alpha = np.pi * r1 * r1 / (0.25 * s_alpha) - ws * r2 |
| 138 | + a_alpha = r3 * r3 * np.pi |
| 139 | + grad_alpha = np.abs(a_alpha * self.alpha_pos - agent.position) |
| 140 | + v_alpha = 0.25 * (agent.position - rho_alpha * grad_alpha) |
| 141 | + |
| 142 | + agent.position = (v_alpha + v_beta + v_gamma) / 3 |
0 commit comments