pyVolutionary 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyvolutionary/__init__.py +44 -0
- pyvolutionary/abstract.py +325 -0
- pyvolutionary/african_vulture/__init__.py +2 -0
- pyvolutionary/african_vulture/african_vulture_optimization.py +87 -0
- pyvolutionary/african_vulture/models.py +41 -0
- pyvolutionary/ant_colony/__init__.py +2 -0
- pyvolutionary/ant_colony/ant_colony_optimization.py +80 -0
- pyvolutionary/ant_colony/models.py +25 -0
- pyvolutionary/aquila/__init__.py +2 -0
- pyvolutionary/aquila/aquila_optimization.py +75 -0
- pyvolutionary/aquila/models.py +9 -0
- pyvolutionary/bacterial_foraging/__init__.py +2 -0
- pyvolutionary/bacterial_foraging/bacterial_foraging_optimization.py +152 -0
- pyvolutionary/bacterial_foraging/models.py +63 -0
- pyvolutionary/bat/__init__.py +2 -0
- pyvolutionary/bat/bat_optimization.py +66 -0
- pyvolutionary/bat/models.py +66 -0
- pyvolutionary/bee_colony/__init__.py +2 -0
- pyvolutionary/bee_colony/bee_colony_optimization.py +116 -0
- pyvolutionary/bee_colony/models.py +22 -0
- pyvolutionary/camel_caravan/__init__.py +2 -0
- pyvolutionary/camel_caravan/camel_caravan_optimization.py +96 -0
- pyvolutionary/camel_caravan/models.py +61 -0
- pyvolutionary/coral_reef/__init__.py +2 -0
- pyvolutionary/coral_reef/coral_reef_optimization.py +133 -0
- pyvolutionary/coral_reef/models.py +81 -0
- pyvolutionary/coyotes/__init__.py +2 -0
- pyvolutionary/coyotes/coyotes_optimization.py +96 -0
- pyvolutionary/coyotes/models.py +22 -0
- pyvolutionary/earthworms/__init__.py +2 -0
- pyvolutionary/earthworms/earthworms_optimization.py +124 -0
- pyvolutionary/earthworms/models.py +62 -0
- pyvolutionary/electromagnetic_field/__init__.py +2 -0
- pyvolutionary/electromagnetic_field/electromagnetic_field_optimization.py +58 -0
- pyvolutionary/electromagnetic_field/models.py +45 -0
- pyvolutionary/elephant_herd/__init__.py +2 -0
- pyvolutionary/elephant_herd/elephant_herd_optimization.py +56 -0
- pyvolutionary/elephant_herd/models.py +38 -0
- pyvolutionary/enums.py +39 -0
- pyvolutionary/firefly_swarm/__init__.py +2 -0
- pyvolutionary/firefly_swarm/firefly_swarm_optimization.py +76 -0
- pyvolutionary/firefly_swarm/models.py +37 -0
- pyvolutionary/fireworks/__init__.py +2 -0
- pyvolutionary/fireworks/fireworks_optimization.py +96 -0
- pyvolutionary/fireworks/models.py +41 -0
- pyvolutionary/fish_school_search/__init__.py +2 -0
- pyvolutionary/fish_school_search/fish_school_search_optimization.py +142 -0
- pyvolutionary/fish_school_search/models.py +27 -0
- pyvolutionary/flower_pollination_algorithm/__init__.py +2 -0
- pyvolutionary/flower_pollination_algorithm/flower_pollination_algorithm_optimization.py +46 -0
- pyvolutionary/flower_pollination_algorithm/models.py +29 -0
- pyvolutionary/forest_algorithm/__init__.py +2 -0
- pyvolutionary/forest_algorithm/forest_optimization_algorithm.py +121 -0
- pyvolutionary/forest_algorithm/models.py +53 -0
- pyvolutionary/fox/__init__.py +2 -0
- pyvolutionary/fox/fox_optimization.py +43 -0
- pyvolutionary/fox/models.py +29 -0
- pyvolutionary/genetic_algorithm/__init__.py +2 -0
- pyvolutionary/genetic_algorithm/genetic_algorithm_optimization.py +167 -0
- pyvolutionary/genetic_algorithm/models.py +21 -0
- pyvolutionary/grasshopper/__init__.py +2 -0
- pyvolutionary/grasshopper/grasshopper_optimization_algorithm.py +73 -0
- pyvolutionary/grasshopper/models.py +29 -0
- pyvolutionary/grey_wolf/__init__.py +2 -0
- pyvolutionary/grey_wolf/grey_wolf_optimization.py +62 -0
- pyvolutionary/grey_wolf/models.py +9 -0
- pyvolutionary/harmony_search/__init__.py +2 -0
- pyvolutionary/harmony_search/harmony_search_optimization.py +64 -0
- pyvolutionary/harmony_search/models.py +29 -0
- pyvolutionary/helpers.py +290 -0
- pyvolutionary/imperialist_competitive/__init__.py +2 -0
- pyvolutionary/imperialist_competitive/classes.py +119 -0
- pyvolutionary/imperialist_competitive/imperialist_competitive_optimization.py +198 -0
- pyvolutionary/imperialist_competitive/models.py +47 -0
- pyvolutionary/invasive_weed/__init__.py +2 -0
- pyvolutionary/invasive_weed/invasive_weed_optimization.py +52 -0
- pyvolutionary/invasive_weed/models.py +51 -0
- pyvolutionary/krill_herd/__init__.py +2 -0
- pyvolutionary/krill_herd/krill_herd_optimization.py +179 -0
- pyvolutionary/krill_herd/models.py +62 -0
- pyvolutionary/levi_jaya_swarm/__init__.py +2 -0
- pyvolutionary/levi_jaya_swarm/levy_flight_jaya_swarm_optimization.py +40 -0
- pyvolutionary/levi_jaya_swarm/models.py +9 -0
- pyvolutionary/models.py +130 -0
- pyvolutionary/monarch_butterfly/__init__.py +2 -0
- pyvolutionary/monarch_butterfly/models.py +25 -0
- pyvolutionary/monarch_butterfly/monarch_butterfly_optimization.py +104 -0
- pyvolutionary/mountain_gazelle/__init__.py +2 -0
- pyvolutionary/mountain_gazelle/models.py +9 -0
- pyvolutionary/mountain_gazelle/mountain_gazelle_optimization.py +118 -0
- pyvolutionary/osprey/__init__.py +2 -0
- pyvolutionary/osprey/models.py +9 -0
- pyvolutionary/osprey/osprey_optimization.py +54 -0
- pyvolutionary/particle_swarm/__init__.py +2 -0
- pyvolutionary/particle_swarm/models.py +40 -0
- pyvolutionary/particle_swarm/particle_swarm_optimization.py +78 -0
- pyvolutionary/pathfinder_algorithm/__init__.py +2 -0
- pyvolutionary/pathfinder_algorithm/models.py +12 -0
- pyvolutionary/pathfinder_algorithm/pathfinder_algorithm_optimization.py +56 -0
- pyvolutionary/pelican/__init__.py +2 -0
- pyvolutionary/pelican/models.py +9 -0
- pyvolutionary/pelican/pelican_optimization.py +46 -0
- pyvolutionary/seagull/__init__.py +2 -0
- pyvolutionary/seagull/models.py +21 -0
- pyvolutionary/seagull/seagull_optimization.py +43 -0
- pyvolutionary/siberian_tiger/__init__.py +2 -0
- pyvolutionary/siberian_tiger/models.py +9 -0
- pyvolutionary/siberian_tiger/siberian_tiger_optimization.py +53 -0
- pyvolutionary/tasmanian_devil/__init__.py +2 -0
- pyvolutionary/tasmanian_devil/models.py +9 -0
- pyvolutionary/tasmanian_devil/tasmanian_devil_optimization.py +69 -0
- pyvolutionary/tests/fixtures.py +21 -0
- pyvolutionary/tests/test_african_vulture_optimization.py +34 -0
- pyvolutionary/tests/test_ant_colony_optimization.py +34 -0
- pyvolutionary/tests/test_aquila_optimization.py +27 -0
- pyvolutionary/tests/test_bacterial_foraging_optimization.py +37 -0
- pyvolutionary/tests/test_bat_optimization.py +34 -0
- pyvolutionary/tests/test_bee_colony_optimization.py +32 -0
- pyvolutionary/tests/test_camel_caravan_optimization.py +37 -0
- pyvolutionary/tests/test_coral_reef_optimization.py +39 -0
- pyvolutionary/tests/test_coyotes_optimization.py +32 -0
- pyvolutionary/tests/test_earthworms_optimization.py +37 -0
- pyvolutionary/tests/test_electromagnetic_field_optimization.py +35 -0
- pyvolutionary/tests/test_elephant_herd_optimization.py +34 -0
- pyvolutionary/tests/test_firefly_swarm_optimization.py +34 -0
- pyvolutionary/tests/test_fireworks_algorithm_optimization.py +36 -0
- pyvolutionary/tests/test_fish_school_search_optimization.py +37 -0
- pyvolutionary/tests/test_flower_pollination_algorithm_optimization.py +33 -0
- pyvolutionary/tests/test_forest_algorithm_optimization.py +36 -0
- pyvolutionary/tests/test_foxes_optimization.py +33 -0
- pyvolutionary/tests/test_genetic_algorithm_optimization.py +32 -0
- pyvolutionary/tests/test_grasshopper_optimization_algorithm.py +33 -0
- pyvolutionary/tests/test_grey_wolf_optimization.py +27 -0
- pyvolutionary/tests/test_harmony_search_optimization.py +33 -0
- pyvolutionary/tests/test_imperialist_competitive_optimization.py +40 -0
- pyvolutionary/tests/test_invasive_weed_optimization.py +34 -0
- pyvolutionary/tests/test_krill_herd_optimization.py +40 -0
- pyvolutionary/tests/test_levy_flight_jaya_swarm_optimization.py +33 -0
- pyvolutionary/tests/test_monarch_butterfly_optimization.py +33 -0
- pyvolutionary/tests/test_mountain_gazelle_optimization.py +27 -0
- pyvolutionary/tests/test_osprey_optimization.py +26 -0
- pyvolutionary/tests/test_particle_swarm_optimization.py +34 -0
- pyvolutionary/tests/test_pathfinder_algorithm_optimization.py +27 -0
- pyvolutionary/tests/test_pelican_optimization.py +27 -0
- pyvolutionary/tests/test_seagull_optimization.py +32 -0
- pyvolutionary/tests/test_siberian_tiger_optimization.py +27 -0
- pyvolutionary/tests/test_task_bounds.py +9 -0
- pyvolutionary/tests/test_tasmanian_devil_optimization.py +27 -0
- pyvolutionary/tests/test_utils.py +43 -0
- pyvolutionary/tests/test_virus_colony_search_optimization.py +33 -0
- pyvolutionary/tests/test_walrus_optimization.py +27 -0
- pyvolutionary/tests/test_whales_optimization.py +27 -0
- pyvolutionary/tests/test_wildebeest_herd_optimization.py +41 -0
- pyvolutionary/tests/test_zebra_optimization.py +27 -0
- pyvolutionary/utils.py +94 -0
- pyvolutionary/virus_colony_search/__init__.py +2 -0
- pyvolutionary/virus_colony_search/models.py +29 -0
- pyvolutionary/virus_colony_search/virus_colony_search_optimization.py +99 -0
- pyvolutionary/walrus/__init__.py +2 -0
- pyvolutionary/walrus/models.py +9 -0
- pyvolutionary/walrus/walrus_optimization.py +43 -0
- pyvolutionary/whales/__init__.py +2 -0
- pyvolutionary/whales/models.py +12 -0
- pyvolutionary/whales/whales_optimization.py +56 -0
- pyvolutionary/wildebeest_herd/__init__.py +2 -0
- pyvolutionary/wildebeest_herd/models.py +94 -0
- pyvolutionary/wildebeest_herd/wildebeest_herd_optimization.py +143 -0
- pyvolutionary/zebra/__init__.py +2 -0
- pyvolutionary/zebra/models.py +9 -0
- pyvolutionary/zebra/zebra_optimization.py +53 -0
- pyvolutionary-1.0.1.dist-info/METADATA +350 -0
- pyvolutionary-1.0.1.dist-info/RECORD +173 -0
- pyvolutionary-1.0.1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..models import ContinuousVariable
|
|
4
|
+
from ..helpers import parse_obj_doc # type: ignore
|
|
5
|
+
from ..abstract import OptimizationAbstract
|
|
6
|
+
from .models import Particle, ParticleSwarmOptimizationConfig
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ParticleSwarmOptimization(OptimizationAbstract):
|
|
10
|
+
"""
|
|
11
|
+
Implementation of the Particle Swarm Optimization algorithm.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
config (ParticleSwarmOptimizationConfig): an instance of ParticleSwarmOptimizationConfig class.
|
|
15
|
+
{parse_obj_doc(ParticleSwarmOptimizationConfig)}
|
|
16
|
+
|
|
17
|
+
Bibliography
|
|
18
|
+
----------
|
|
19
|
+
[1] Kennedy, J., & Eberhart, R. (1995). Particle swarm optimization. Proceedings of ICNN’95 - International
|
|
20
|
+
Conference on Neural Networks, 4, 1942–1948 vol.4. https://doi.org/10.1109/ICNN.1995.488968
|
|
21
|
+
[2] Kennedy, J., & Eberhart, R. C. (1995). A new optimizer using particle swarm theory. Proceedings of the Sixth
|
|
22
|
+
International Symposium on Micro Machine and Human Science, 39–43. https://doi.org/10.1109/MHS.1995.494215
|
|
23
|
+
3[] Eberhart, R. C., & Shi, Y. (2001). Particle swarm optimization: Developments, applications and resources.
|
|
24
|
+
Proceedings of the 2001 Congress on Evolutionary Computation (IEEE Cat. No.01TH8546), 1, 81–86.
|
|
25
|
+
https://doi.org/10.1109/CEC.2001.934374
|
|
26
|
+
"""
|
|
27
|
+
def __init__(self, config: ParticleSwarmOptimizationConfig, debug: bool | None = False):
|
|
28
|
+
super().__init__(config, debug)
|
|
29
|
+
self.__v: np.ndarray | None = None
|
|
30
|
+
self.__pbest: list[Particle] = []
|
|
31
|
+
|
|
32
|
+
def before_initialization(self):
|
|
33
|
+
self.__v = 0.2 * self._bandwidth()
|
|
34
|
+
|
|
35
|
+
def _init_agent(
|
|
36
|
+
self,
|
|
37
|
+
position: list[float] | np.ndarray | None = None,
|
|
38
|
+
velocity: list[float] | np.ndarray | None = None
|
|
39
|
+
) -> Particle:
|
|
40
|
+
agent = super()._init_agent(position=position)
|
|
41
|
+
|
|
42
|
+
if velocity is None:
|
|
43
|
+
return Particle(**agent.model_dump(), velocity=np.zeros(self._task.space_dimension).tolist())
|
|
44
|
+
|
|
45
|
+
velocity = np.where(
|
|
46
|
+
isinstance(self._task.variables, ContinuousVariable), np.clip(velocity, -self.__v, self.__v), velocity
|
|
47
|
+
).tolist()
|
|
48
|
+
return Particle(**agent.model_dump(), velocity=velocity)
|
|
49
|
+
|
|
50
|
+
def _init_population(self):
|
|
51
|
+
super()._init_population()
|
|
52
|
+
self.__pbest = self._population.copy()
|
|
53
|
+
|
|
54
|
+
def optimization_step(self):
|
|
55
|
+
"""
|
|
56
|
+
Merge the population and the archive, sort them by cost and update the population with the best n_ants
|
|
57
|
+
"""
|
|
58
|
+
# Update the particles
|
|
59
|
+
w_min, w_max = self._config.w
|
|
60
|
+
|
|
61
|
+
for idx in range(0, self._config.population_size):
|
|
62
|
+
particle = self._population[idx].model_copy()
|
|
63
|
+
pbest = self.__pbest[idx].model_copy()
|
|
64
|
+
|
|
65
|
+
# Randomly generate r1, r2 and inertia weight from normal distribution, and then calculate the new velocity
|
|
66
|
+
w = np.random.uniform(w_min, w_max) # Inertia weight * (cycles + 1) / max_cycles
|
|
67
|
+
|
|
68
|
+
velocity = w * np.array(particle.velocity) + self._config.c1 * np.random.rand() * (
|
|
69
|
+
np.array(pbest.position) - np.array(particle.position)
|
|
70
|
+
) + self._config.c2 * np.random.rand() * (
|
|
71
|
+
np.array(self._best_agent.position) - np.array(particle.position)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Move particles by adding velocity
|
|
75
|
+
self._population[idx] = self._init_agent(position=particle.position + velocity, velocity=velocity)
|
|
76
|
+
|
|
77
|
+
# Update personal best
|
|
78
|
+
self.__pbest[idx] = self._greedy_select_agent(self._population[idx], pbest)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from ..models import Agent, BaseOptimizationConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Pathfinder(Agent):
|
|
5
|
+
pass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class PathfinderAlgorithmOptimizationConfig(BaseOptimizationConfig):
|
|
9
|
+
"""
|
|
10
|
+
Configuration class of the Pathfinder Algorithm Optimization algorithm.
|
|
11
|
+
"""
|
|
12
|
+
pass
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..helpers import (
|
|
4
|
+
distance,
|
|
5
|
+
parse_obj_doc, # type: ignore
|
|
6
|
+
)
|
|
7
|
+
from ..abstract import OptimizationAbstract
|
|
8
|
+
from .models import PathfinderAlgorithmOptimizationConfig
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PathfinderAlgorithmOptimization(OptimizationAbstract):
|
|
12
|
+
"""
|
|
13
|
+
Implementation of the Pathfinder Algorithm Optimization.
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
config (PathfinderAlgorithmOptimizationConfig): an instance of PathfinderAlgorithmOptimizationConfig class.
|
|
17
|
+
{parse_obj_doc(PathfinderAlgorithmOptimizationConfig)}
|
|
18
|
+
|
|
19
|
+
Bibliography
|
|
20
|
+
----------
|
|
21
|
+
[1] Yapici, H. and Cetinkaya, N., 2019. A new meta-heuristic optimizer: Pathfinder algorithm. Applied soft
|
|
22
|
+
computing, 78, pp.545-568.
|
|
23
|
+
"""
|
|
24
|
+
def __init__(self, config: PathfinderAlgorithmOptimizationConfig, debug: bool | None = False):
|
|
25
|
+
super().__init__(config, debug)
|
|
26
|
+
|
|
27
|
+
def optimization_step(self):
|
|
28
|
+
alpha, beta = np.random.uniform(1, 2, 2)
|
|
29
|
+
best_position = np.array(self._best_agent.position)
|
|
30
|
+
|
|
31
|
+
A = self._uniform_position() * np.exp(-2 * self._cycles / self._config.max_cycles)
|
|
32
|
+
t = 1. - self._cycles / (self._config.max_cycles + 1)
|
|
33
|
+
space = self._bandwidth()
|
|
34
|
+
|
|
35
|
+
# update the position of pathfinder and check the bound
|
|
36
|
+
new_pathfinder = self._init_agent(np.array(self._population[0].position) + 2 * np.random.uniform() * (
|
|
37
|
+
best_position - np.array(self._population[0].position)
|
|
38
|
+
) + A)
|
|
39
|
+
|
|
40
|
+
# update positions of members, check the bound and calculate new fitness
|
|
41
|
+
for idx in range(1, self._config.population_size):
|
|
42
|
+
pathfinder_position = np.array(self._population[idx].position)
|
|
43
|
+
pos_new = pathfinder_position.copy().astype(float)
|
|
44
|
+
|
|
45
|
+
dist = distance(new_pathfinder.position, pathfinder_position.tolist()) / self._task.space_dimension
|
|
46
|
+
pos_new += alpha * np.random.uniform() * (best_position - pathfinder_position) + (
|
|
47
|
+
beta * np.random.uniform() * (self._population[idx - 1].position - pathfinder_position)
|
|
48
|
+
) + np.random.uniform() * t * (dist / space)
|
|
49
|
+
|
|
50
|
+
pos_new = self._correct_position(pos_new)
|
|
51
|
+
agent = self._init_agent(pos_new)
|
|
52
|
+
|
|
53
|
+
self._population[idx] = self._greedy_select_agent(agent, self._population[idx])
|
|
54
|
+
|
|
55
|
+
# update the position of the pathfinder
|
|
56
|
+
self._population[0] = self._greedy_select_agent(new_pathfinder, self._population[0])
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..helpers import parse_obj_doc # type: ignore
|
|
4
|
+
from ..abstract import OptimizationAbstract
|
|
5
|
+
from .models import PelicanOptimizationConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class PelicanOptimization(OptimizationAbstract):
|
|
9
|
+
"""
|
|
10
|
+
Implementation of the Pelican Optimization algorithm.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
config (PelicanOptimizationConfig): an instance of PelicanOptimizationConfig class.
|
|
14
|
+
{parse_obj_doc(OspreyOptimizationConfig)}
|
|
15
|
+
|
|
16
|
+
Bibliography
|
|
17
|
+
----------
|
|
18
|
+
[1] Trojovský, P., & Dehghani, M. (2022). Pelican optimization algorithm: A novel nature-inspired algorithm for
|
|
19
|
+
engineering applications. Sensors, 22(3), 855.
|
|
20
|
+
"""
|
|
21
|
+
def __init__(self, config: PelicanOptimizationConfig, debug: bool | None = False):
|
|
22
|
+
super().__init__(config, debug)
|
|
23
|
+
|
|
24
|
+
def optimization_step(self):
|
|
25
|
+
n_dims = self._task.space_dimension
|
|
26
|
+
for idx, pelican in enumerate(self._population):
|
|
27
|
+
# update location of food
|
|
28
|
+
kk = np.random.permutation(self._config.population_size)[0]
|
|
29
|
+
pos_kk = np.array(self._population[kk].position)
|
|
30
|
+
|
|
31
|
+
pos = np.array(pelican.position)
|
|
32
|
+
|
|
33
|
+
# phase 1: moving towards prey (exploration phase), Eq. 4
|
|
34
|
+
pos_new = pos + np.random.random() * (
|
|
35
|
+
pos_kk - np.random.randint(1, 3) * pos
|
|
36
|
+
) if self._population[kk].cost < pelican.cost else pos + np.random.random() * (pos - pos_kk)
|
|
37
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
38
|
+
|
|
39
|
+
pelican = self._greedy_select_agent(pelican, agent)
|
|
40
|
+
|
|
41
|
+
# phase 2: Winging on the water surface (exploitation phase), with Eq. 6
|
|
42
|
+
pos_new = np.array(pelican.position) + 0.2 * (1 - self._cycles / self._config.max_cycles) * (
|
|
43
|
+
2 * np.random.random(n_dims) - 1
|
|
44
|
+
) * np.array(pelican.position)
|
|
45
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
46
|
+
self._population[idx] = self._greedy_select_agent(pelican, agent)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from pydantic import field_validator
|
|
2
|
+
|
|
3
|
+
from ..models import Agent, BaseOptimizationConfig
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Seagull(Agent):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SeagullOptimizationConfig(BaseOptimizationConfig):
|
|
11
|
+
"""
|
|
12
|
+
Configuration class for the Seagull Optimization algorithm.
|
|
13
|
+
fc (float): [1.0, 10.0] -> frequency of employing variable A (A linear decreased from fc to 0)
|
|
14
|
+
"""
|
|
15
|
+
fc: float
|
|
16
|
+
|
|
17
|
+
@field_validator("fc")
|
|
18
|
+
def fc_validator(cls, v):
|
|
19
|
+
if not (1.0 <= v <= 10.0):
|
|
20
|
+
raise ValueError("fc must be between 1.0 and 10.0")
|
|
21
|
+
return v
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..helpers import parse_obj_doc # type: ignore
|
|
4
|
+
from ..abstract import OptimizationAbstract
|
|
5
|
+
from .models import SeagullOptimizationConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SeagullOptimization(OptimizationAbstract):
|
|
9
|
+
"""
|
|
10
|
+
Implementation of the Aquila Optimization algorithm.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
config (SeagullOptimizationConfig): an instance of AquilaOptimizationConfig class.
|
|
14
|
+
{parse_obj_doc(AquilaOptimizationConfig)}
|
|
15
|
+
|
|
16
|
+
Bibliography
|
|
17
|
+
----------
|
|
18
|
+
[1] Dhiman, G., & Kumar, V. (2019). Seagull optimization algorithm: Theory and its applications for large-scale
|
|
19
|
+
industrial engineering problems. Knowledge-based systems, 165, 169-196.
|
|
20
|
+
"""
|
|
21
|
+
def __init__(self, config: SeagullOptimizationConfig, debug: bool | None = False):
|
|
22
|
+
super().__init__(config, debug)
|
|
23
|
+
|
|
24
|
+
def optimization_step(self):
|
|
25
|
+
A = self._config.fc - self._cycles * self._config.fc / self._config.max_cycles # Eq. 6
|
|
26
|
+
uu = vv = 1
|
|
27
|
+
|
|
28
|
+
best_pos = np.array(self._best_agent.position)
|
|
29
|
+
for idx, seagull in enumerate(self._population):
|
|
30
|
+
pos = np.array(seagull.position)
|
|
31
|
+
|
|
32
|
+
B = 2 * A ** 2 * np.random.random() # Eq. 8
|
|
33
|
+
M = B * (best_pos - pos) # Eq. 7
|
|
34
|
+
C = A * pos # Eq. 5
|
|
35
|
+
D = np.abs(C + M) # Eq. 9
|
|
36
|
+
k = np.random.uniform(0, 2 * np.pi)
|
|
37
|
+
r = uu * np.exp(k * vv)
|
|
38
|
+
|
|
39
|
+
pos_new = r * np.cos(k) * r * np.sin(k) * r * k * D + np.random.normal(0, 1) * best_pos # Eq. 14
|
|
40
|
+
pos_new = self._correct_position(pos_new)
|
|
41
|
+
agent = self._init_agent(pos_new)
|
|
42
|
+
|
|
43
|
+
self._population[idx] = self._greedy_select_agent(agent, seagull)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..helpers import parse_obj_doc # type: ignore
|
|
4
|
+
from ..abstract import OptimizationAbstract
|
|
5
|
+
from .models import SiberianTiger, SiberianTigerOptimizationConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SiberianTigerOptimization(OptimizationAbstract):
|
|
9
|
+
"""
|
|
10
|
+
Implementation of the Siberian Tiger Optimization algorithm.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
config (SiberianTigerOptimizationConfig): an instance of SiberianTigerOptimizationConfig class.
|
|
14
|
+
{parse_obj_doc(SiberianTigerOptimizationConfig)}
|
|
15
|
+
|
|
16
|
+
Bibliography
|
|
17
|
+
----------
|
|
18
|
+
[1] Trojovský, P., Dehghani, M., & Hanuš, P. (2022). Siberian Tiger Optimization: A New Bio-Inspired
|
|
19
|
+
Metaheuristic Algorithm for Solving Engineering Optimization Problems. IEEE Access, 10, 132396-132431.
|
|
20
|
+
"""
|
|
21
|
+
def __init__(self, config: SiberianTigerOptimizationConfig, debug: bool | None = False):
|
|
22
|
+
super().__init__(config, debug)
|
|
23
|
+
|
|
24
|
+
def __get_indexes_better__(self, siberian_tiger: SiberianTiger) -> np.ndarray:
|
|
25
|
+
costs = np.array([agent.cost for agent in self._population])
|
|
26
|
+
idxs = np.where(costs < siberian_tiger.cost)
|
|
27
|
+
return idxs[0]
|
|
28
|
+
|
|
29
|
+
def __sf__(self, siberian_tiger: SiberianTiger) -> np.ndarray:
|
|
30
|
+
idxs = self.__get_indexes_better__(siberian_tiger)
|
|
31
|
+
if len(idxs) == 0 or np.random.random() < 0.5:
|
|
32
|
+
return np.array(self._best_agent.position)
|
|
33
|
+
|
|
34
|
+
kk = np.random.permutation(idxs)[0]
|
|
35
|
+
return np.array(self._population[kk].position)
|
|
36
|
+
|
|
37
|
+
def optimization_step(self):
|
|
38
|
+
for idx, siberian_tiger in enumerate(self._population):
|
|
39
|
+
pos = np.array(siberian_tiger.position)
|
|
40
|
+
|
|
41
|
+
# phase 1: hunting (exploration)
|
|
42
|
+
sf = self.__sf__(siberian_tiger)
|
|
43
|
+
r1 = np.random.randint(1, 3)
|
|
44
|
+
pos_new = pos + np.random.random() * (sf - r1 * pos) # Eq. 5
|
|
45
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
46
|
+
|
|
47
|
+
siberian_tiger = self._greedy_select_agent(siberian_tiger, agent)
|
|
48
|
+
|
|
49
|
+
# phase 2: exploitation
|
|
50
|
+
pos_new = self._increase_position(siberian_tiger.position, self._cycles) # Eq. 7
|
|
51
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
52
|
+
|
|
53
|
+
self._population[idx] = self._greedy_select_agent(siberian_tiger, agent)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ..helpers import parse_obj_doc # type: ignore
|
|
4
|
+
from ..abstract import OptimizationAbstract
|
|
5
|
+
from .models import TasmanianDevil, TasmanianDevilOptimizationConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TasmanianDevilOptimization(OptimizationAbstract):
|
|
9
|
+
"""
|
|
10
|
+
Implementation of the Tasmanian Devil Optimization algorithm.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
config (TasmanianDevilOptimizationConfig): an instance of TasmanianDevilOptimizationConfig class.
|
|
14
|
+
{parse_obj_doc(TasmaniaDevilOptimizationConfig)}
|
|
15
|
+
|
|
16
|
+
Bibliography
|
|
17
|
+
----------
|
|
18
|
+
[1] Dehghani, M., Hubálovský, Š., & Trojovský, P. (2022). Tasmanian devil optimization: a new bio-inspired
|
|
19
|
+
optimization algorithm for solving optimization algorithm. IEEE Access, 10, 19599-19620.
|
|
20
|
+
"""
|
|
21
|
+
def __init__(self, config: TasmanianDevilOptimizationConfig, debug: bool | None = False):
|
|
22
|
+
super().__init__(config, debug)
|
|
23
|
+
|
|
24
|
+
def __strategy__(self, idx: int, tasmanian_devil: TasmanianDevil) -> TasmanianDevil:
|
|
25
|
+
"""
|
|
26
|
+
Application of strategy.
|
|
27
|
+
:param idx: index of the agent
|
|
28
|
+
:param tasmanian_devil: current agent
|
|
29
|
+
:return: picked agent
|
|
30
|
+
:rtype: TasmanianDevil
|
|
31
|
+
"""
|
|
32
|
+
pop_size = self._config.population_size
|
|
33
|
+
n_dims = self._task.space_dimension
|
|
34
|
+
|
|
35
|
+
pos = np.array(tasmanian_devil.position)
|
|
36
|
+
|
|
37
|
+
kk = np.random.choice(list(set(range(0, pop_size)) - {idx}))
|
|
38
|
+
|
|
39
|
+
pos_new = pos + np.random.random(n_dims) * (
|
|
40
|
+
pos - np.random.randint(1, 3) * pos
|
|
41
|
+
) if self._population[kk].cost < tasmanian_devil.cost else pos + np.random.random(n_dims) * (
|
|
42
|
+
pos - np.array(self._population[kk].position)
|
|
43
|
+
)
|
|
44
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
45
|
+
return self._greedy_select_agent(tasmanian_devil, agent)
|
|
46
|
+
|
|
47
|
+
def optimization_step(self):
|
|
48
|
+
epoch = self._cycles
|
|
49
|
+
epochs = self._config.max_cycles
|
|
50
|
+
n_dims = self._task.space_dimension
|
|
51
|
+
|
|
52
|
+
for idx, tasmanian_devil in enumerate(self._population):
|
|
53
|
+
# phase 1: hunting feeding
|
|
54
|
+
if np.random.random() > 0.5:
|
|
55
|
+
# strategy 1: feeding by eating carrion (exploration phase)
|
|
56
|
+
# carrion selection using (3)
|
|
57
|
+
self._population[idx] = self.__strategy__(idx, tasmanian_devil)
|
|
58
|
+
else:
|
|
59
|
+
# strategy 2: feeding by eating prey (exploitation phase)
|
|
60
|
+
# stage 1: prey selection and attack it
|
|
61
|
+
agent = self.__strategy__(idx, tasmanian_devil)
|
|
62
|
+
pos = np.array(agent.position)
|
|
63
|
+
|
|
64
|
+
# phase 2: prey chasing
|
|
65
|
+
rr = 0.01 * (1 - epoch / epochs) # Calculating the neighborhood radius using(9)
|
|
66
|
+
pos_new = pos + (-rr + 2 * rr * np.random.random(n_dims)) * pos
|
|
67
|
+
agent = self._init_agent(self._correct_position(pos_new))
|
|
68
|
+
|
|
69
|
+
self._population[idx] = self._greedy_select_agent(tasmanian_devil, agent)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from pyvolutionary.models import Task, ContinuousVariable
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Rastrigin(Task):
|
|
6
|
+
def objective_function(self, x: list[float]) -> float:
|
|
7
|
+
A = 10
|
|
8
|
+
n = len(x)
|
|
9
|
+
sum_term = sum(xi ** 2 - A * np.cos(2 * np.pi * xi) for xi in x)
|
|
10
|
+
return A * n + sum_term
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Sphere(Task):
|
|
14
|
+
def objective_function(self, x: list[float]) -> float:
|
|
15
|
+
return sum(xi ** 2 for xi in x)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
dimension = 3
|
|
19
|
+
task = Rastrigin(
|
|
20
|
+
variables=[ContinuousVariable(name=f"x{i}", lower_bound=-10, upper_bound=10) for i in range(dimension)],
|
|
21
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, AfricanVultureOptimization, AfricanVultureOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> AfricanVultureOptimizationConfig:
|
|
9
|
+
return AfricanVultureOptimizationConfig(
|
|
10
|
+
population_size=20,
|
|
11
|
+
fitness_error=0.01,
|
|
12
|
+
max_cycles=100,
|
|
13
|
+
p=[0.6, 0.4, 0.6],
|
|
14
|
+
alpha=0.8,
|
|
15
|
+
gamma=2.5,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_valid_optimization(optimization_config):
|
|
20
|
+
o = AfricanVultureOptimization(optimization_config)
|
|
21
|
+
result = o.optimize(task)
|
|
22
|
+
assert isinstance(result, OptimizationResult)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
26
|
+
o = AfricanVultureOptimization(optimization_config)
|
|
27
|
+
result = o.optimize(task, mode="thread")
|
|
28
|
+
assert isinstance(result, OptimizationResult)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
32
|
+
o = AfricanVultureOptimization(optimization_config)
|
|
33
|
+
result = o.optimize(task, mode="process")
|
|
34
|
+
assert isinstance(result, OptimizationResult)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, AntColonyOptimization, AntColonyOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> AntColonyOptimizationConfig:
|
|
9
|
+
return AntColonyOptimizationConfig(
|
|
10
|
+
population_size=20,
|
|
11
|
+
fitness_error=0.01,
|
|
12
|
+
max_cycles=100,
|
|
13
|
+
archive_size=20,
|
|
14
|
+
intent_factor=0.1,
|
|
15
|
+
zeta=0.85,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_valid_optimization(optimization_config):
|
|
20
|
+
o = AntColonyOptimization(optimization_config)
|
|
21
|
+
result = o.optimize(task)
|
|
22
|
+
assert isinstance(result, OptimizationResult)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
26
|
+
o = AntColonyOptimization(optimization_config)
|
|
27
|
+
result = o.optimize(task, mode="thread")
|
|
28
|
+
assert isinstance(result, OptimizationResult)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
32
|
+
o = AntColonyOptimization(optimization_config)
|
|
33
|
+
result = o.optimize(task, mode="process")
|
|
34
|
+
assert isinstance(result, OptimizationResult)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, AquilaOptimization, AquilaOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> AquilaOptimizationConfig:
|
|
9
|
+
return AquilaOptimizationConfig(population_size=20, fitness_error=0.01, max_cycles=100)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_valid_optimization(optimization_config):
|
|
13
|
+
o = AquilaOptimization(optimization_config)
|
|
14
|
+
result = o.optimize(task)
|
|
15
|
+
assert isinstance(result, OptimizationResult)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
19
|
+
o = AquilaOptimization(optimization_config)
|
|
20
|
+
result = o.optimize(task, mode="thread")
|
|
21
|
+
assert isinstance(result, OptimizationResult)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
25
|
+
o = AquilaOptimization(optimization_config)
|
|
26
|
+
result = o.optimize(task, mode="process")
|
|
27
|
+
assert isinstance(result, OptimizationResult)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, BacterialForagingOptimization, BacterialForagingOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> BacterialForagingOptimizationConfig:
|
|
9
|
+
return BacterialForagingOptimizationConfig(
|
|
10
|
+
population_size=10,
|
|
11
|
+
fitness_error=0.01,
|
|
12
|
+
max_cycles=100,
|
|
13
|
+
C_s=0.1,
|
|
14
|
+
C_e=0.001,
|
|
15
|
+
Ped=0.01,
|
|
16
|
+
Ns=4,
|
|
17
|
+
N_adapt=2,
|
|
18
|
+
N_split=40,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_valid_optimization(optimization_config):
|
|
23
|
+
o = BacterialForagingOptimization(optimization_config)
|
|
24
|
+
result = o.optimize(task)
|
|
25
|
+
assert isinstance(result, OptimizationResult)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
29
|
+
o = BacterialForagingOptimization(optimization_config)
|
|
30
|
+
result = o.optimize(task, mode="thread")
|
|
31
|
+
assert isinstance(result, OptimizationResult)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
35
|
+
o = BacterialForagingOptimization(optimization_config)
|
|
36
|
+
result = o.optimize(task, mode="process")
|
|
37
|
+
assert isinstance(result, OptimizationResult)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, BatOptimization, BatOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> BatOptimizationConfig:
|
|
9
|
+
return BatOptimizationConfig(
|
|
10
|
+
population_size=20,
|
|
11
|
+
fitness_error=0.01,
|
|
12
|
+
max_cycles=100,
|
|
13
|
+
loudness=[1, 2],
|
|
14
|
+
pulse_rate=[0.1, 0.9],
|
|
15
|
+
pulse_frequency=[-0.5, 0.5],
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_valid_optimization(optimization_config):
|
|
20
|
+
o = BatOptimization(optimization_config)
|
|
21
|
+
result = o.optimize(task)
|
|
22
|
+
assert isinstance(result, OptimizationResult)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
26
|
+
o = BatOptimization(optimization_config)
|
|
27
|
+
result = o.optimize(task, mode="thread")
|
|
28
|
+
assert isinstance(result, OptimizationResult)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
32
|
+
o = BatOptimization(optimization_config)
|
|
33
|
+
result = o.optimize(task, mode="process")
|
|
34
|
+
assert isinstance(result, OptimizationResult)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pyvolutionary import OptimizationResult, BeeColonyOptimization, BeeColonyOptimizationConfig
|
|
4
|
+
from pyvolutionary.tests.fixtures import task
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def optimization_config() -> BeeColonyOptimizationConfig:
|
|
9
|
+
return BeeColonyOptimizationConfig(
|
|
10
|
+
population_size=20,
|
|
11
|
+
fitness_error=0.01,
|
|
12
|
+
max_cycles=100,
|
|
13
|
+
scouting_limit=5,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_valid_optimization(optimization_config):
|
|
18
|
+
o = BeeColonyOptimization(optimization_config)
|
|
19
|
+
result = o.optimize(task)
|
|
20
|
+
assert isinstance(result, OptimizationResult)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_valid_optimization_with_threads(optimization_config):
|
|
24
|
+
o = BeeColonyOptimization(optimization_config)
|
|
25
|
+
result = o.optimize(task, mode="thread")
|
|
26
|
+
assert isinstance(result, OptimizationResult)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_valid_optimization_with_processes(optimization_config):
|
|
30
|
+
o = BeeColonyOptimization(optimization_config)
|
|
31
|
+
result = o.optimize(task, mode="process")
|
|
32
|
+
assert isinstance(result, OptimizationResult)
|