pyVolutionary 1.0.1__tar.gz
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-1.0.1/PKG-INFO +350 -0
- pyvolutionary-1.0.1/README.md +332 -0
- pyvolutionary-1.0.1/pyproject.toml +34 -0
- pyvolutionary-1.0.1/pyvolutionary/__init__.py +44 -0
- pyvolutionary-1.0.1/pyvolutionary/abstract.py +325 -0
- pyvolutionary-1.0.1/pyvolutionary/african_vulture/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/african_vulture/african_vulture_optimization.py +87 -0
- pyvolutionary-1.0.1/pyvolutionary/african_vulture/models.py +41 -0
- pyvolutionary-1.0.1/pyvolutionary/ant_colony/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/ant_colony/ant_colony_optimization.py +80 -0
- pyvolutionary-1.0.1/pyvolutionary/ant_colony/models.py +25 -0
- pyvolutionary-1.0.1/pyvolutionary/aquila/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/aquila/aquila_optimization.py +75 -0
- pyvolutionary-1.0.1/pyvolutionary/aquila/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/bacterial_foraging/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/bacterial_foraging/bacterial_foraging_optimization.py +152 -0
- pyvolutionary-1.0.1/pyvolutionary/bacterial_foraging/models.py +63 -0
- pyvolutionary-1.0.1/pyvolutionary/bat/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/bat/bat_optimization.py +66 -0
- pyvolutionary-1.0.1/pyvolutionary/bat/models.py +66 -0
- pyvolutionary-1.0.1/pyvolutionary/bee_colony/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/bee_colony/bee_colony_optimization.py +116 -0
- pyvolutionary-1.0.1/pyvolutionary/bee_colony/models.py +22 -0
- pyvolutionary-1.0.1/pyvolutionary/camel_caravan/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/camel_caravan/camel_caravan_optimization.py +96 -0
- pyvolutionary-1.0.1/pyvolutionary/camel_caravan/models.py +61 -0
- pyvolutionary-1.0.1/pyvolutionary/coral_reef/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/coral_reef/coral_reef_optimization.py +133 -0
- pyvolutionary-1.0.1/pyvolutionary/coral_reef/models.py +81 -0
- pyvolutionary-1.0.1/pyvolutionary/coyotes/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/coyotes/coyotes_optimization.py +96 -0
- pyvolutionary-1.0.1/pyvolutionary/coyotes/models.py +22 -0
- pyvolutionary-1.0.1/pyvolutionary/earthworms/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/earthworms/earthworms_optimization.py +124 -0
- pyvolutionary-1.0.1/pyvolutionary/earthworms/models.py +62 -0
- pyvolutionary-1.0.1/pyvolutionary/electromagnetic_field/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/electromagnetic_field/electromagnetic_field_optimization.py +58 -0
- pyvolutionary-1.0.1/pyvolutionary/electromagnetic_field/models.py +45 -0
- pyvolutionary-1.0.1/pyvolutionary/elephant_herd/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/elephant_herd/elephant_herd_optimization.py +56 -0
- pyvolutionary-1.0.1/pyvolutionary/elephant_herd/models.py +38 -0
- pyvolutionary-1.0.1/pyvolutionary/enums.py +39 -0
- pyvolutionary-1.0.1/pyvolutionary/firefly_swarm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/firefly_swarm/firefly_swarm_optimization.py +76 -0
- pyvolutionary-1.0.1/pyvolutionary/firefly_swarm/models.py +37 -0
- pyvolutionary-1.0.1/pyvolutionary/fireworks/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/fireworks/fireworks_optimization.py +96 -0
- pyvolutionary-1.0.1/pyvolutionary/fireworks/models.py +41 -0
- pyvolutionary-1.0.1/pyvolutionary/fish_school_search/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/fish_school_search/fish_school_search_optimization.py +142 -0
- pyvolutionary-1.0.1/pyvolutionary/fish_school_search/models.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/flower_pollination_algorithm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/flower_pollination_algorithm/flower_pollination_algorithm_optimization.py +46 -0
- pyvolutionary-1.0.1/pyvolutionary/flower_pollination_algorithm/models.py +29 -0
- pyvolutionary-1.0.1/pyvolutionary/forest_algorithm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/forest_algorithm/forest_optimization_algorithm.py +121 -0
- pyvolutionary-1.0.1/pyvolutionary/forest_algorithm/models.py +53 -0
- pyvolutionary-1.0.1/pyvolutionary/fox/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/fox/fox_optimization.py +43 -0
- pyvolutionary-1.0.1/pyvolutionary/fox/models.py +29 -0
- pyvolutionary-1.0.1/pyvolutionary/genetic_algorithm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/genetic_algorithm/genetic_algorithm_optimization.py +167 -0
- pyvolutionary-1.0.1/pyvolutionary/genetic_algorithm/models.py +21 -0
- pyvolutionary-1.0.1/pyvolutionary/grasshopper/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/grasshopper/grasshopper_optimization_algorithm.py +73 -0
- pyvolutionary-1.0.1/pyvolutionary/grasshopper/models.py +29 -0
- pyvolutionary-1.0.1/pyvolutionary/grey_wolf/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/grey_wolf/grey_wolf_optimization.py +62 -0
- pyvolutionary-1.0.1/pyvolutionary/grey_wolf/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/harmony_search/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/harmony_search/harmony_search_optimization.py +64 -0
- pyvolutionary-1.0.1/pyvolutionary/harmony_search/models.py +29 -0
- pyvolutionary-1.0.1/pyvolutionary/helpers.py +290 -0
- pyvolutionary-1.0.1/pyvolutionary/imperialist_competitive/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/imperialist_competitive/classes.py +119 -0
- pyvolutionary-1.0.1/pyvolutionary/imperialist_competitive/imperialist_competitive_optimization.py +198 -0
- pyvolutionary-1.0.1/pyvolutionary/imperialist_competitive/models.py +47 -0
- pyvolutionary-1.0.1/pyvolutionary/invasive_weed/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/invasive_weed/invasive_weed_optimization.py +52 -0
- pyvolutionary-1.0.1/pyvolutionary/invasive_weed/models.py +51 -0
- pyvolutionary-1.0.1/pyvolutionary/krill_herd/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/krill_herd/krill_herd_optimization.py +179 -0
- pyvolutionary-1.0.1/pyvolutionary/krill_herd/models.py +62 -0
- pyvolutionary-1.0.1/pyvolutionary/levi_jaya_swarm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/levi_jaya_swarm/levy_flight_jaya_swarm_optimization.py +40 -0
- pyvolutionary-1.0.1/pyvolutionary/levi_jaya_swarm/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/models.py +130 -0
- pyvolutionary-1.0.1/pyvolutionary/monarch_butterfly/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/monarch_butterfly/models.py +25 -0
- pyvolutionary-1.0.1/pyvolutionary/monarch_butterfly/monarch_butterfly_optimization.py +104 -0
- pyvolutionary-1.0.1/pyvolutionary/mountain_gazelle/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/mountain_gazelle/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/mountain_gazelle/mountain_gazelle_optimization.py +118 -0
- pyvolutionary-1.0.1/pyvolutionary/osprey/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/osprey/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/osprey/osprey_optimization.py +54 -0
- pyvolutionary-1.0.1/pyvolutionary/particle_swarm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/particle_swarm/models.py +40 -0
- pyvolutionary-1.0.1/pyvolutionary/particle_swarm/particle_swarm_optimization.py +78 -0
- pyvolutionary-1.0.1/pyvolutionary/pathfinder_algorithm/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/pathfinder_algorithm/models.py +12 -0
- pyvolutionary-1.0.1/pyvolutionary/pathfinder_algorithm/pathfinder_algorithm_optimization.py +56 -0
- pyvolutionary-1.0.1/pyvolutionary/pelican/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/pelican/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/pelican/pelican_optimization.py +46 -0
- pyvolutionary-1.0.1/pyvolutionary/seagull/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/seagull/models.py +21 -0
- pyvolutionary-1.0.1/pyvolutionary/seagull/seagull_optimization.py +43 -0
- pyvolutionary-1.0.1/pyvolutionary/siberian_tiger/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/siberian_tiger/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/siberian_tiger/siberian_tiger_optimization.py +53 -0
- pyvolutionary-1.0.1/pyvolutionary/tasmanian_devil/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/tasmanian_devil/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/tasmanian_devil/tasmanian_devil_optimization.py +69 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/fixtures.py +21 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_african_vulture_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_ant_colony_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_aquila_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_bacterial_foraging_optimization.py +37 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_bat_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_bee_colony_optimization.py +32 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_camel_caravan_optimization.py +37 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_coral_reef_optimization.py +39 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_coyotes_optimization.py +32 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_earthworms_optimization.py +37 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_electromagnetic_field_optimization.py +35 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_elephant_herd_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_firefly_swarm_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_fireworks_algorithm_optimization.py +36 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_fish_school_search_optimization.py +37 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_flower_pollination_algorithm_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_forest_algorithm_optimization.py +36 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_foxes_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_genetic_algorithm_optimization.py +32 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_grasshopper_optimization_algorithm.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_grey_wolf_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_harmony_search_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_imperialist_competitive_optimization.py +40 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_invasive_weed_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_krill_herd_optimization.py +40 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_levy_flight_jaya_swarm_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_monarch_butterfly_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_mountain_gazelle_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_osprey_optimization.py +26 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_particle_swarm_optimization.py +34 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_pathfinder_algorithm_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_pelican_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_seagull_optimization.py +32 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_siberian_tiger_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_task_bounds.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_tasmanian_devil_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_utils.py +43 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_virus_colony_search_optimization.py +33 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_walrus_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_whales_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_wildebeest_herd_optimization.py +41 -0
- pyvolutionary-1.0.1/pyvolutionary/tests/test_zebra_optimization.py +27 -0
- pyvolutionary-1.0.1/pyvolutionary/utils.py +94 -0
- pyvolutionary-1.0.1/pyvolutionary/virus_colony_search/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/virus_colony_search/models.py +29 -0
- pyvolutionary-1.0.1/pyvolutionary/virus_colony_search/virus_colony_search_optimization.py +99 -0
- pyvolutionary-1.0.1/pyvolutionary/walrus/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/walrus/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/walrus/walrus_optimization.py +43 -0
- pyvolutionary-1.0.1/pyvolutionary/whales/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/whales/models.py +12 -0
- pyvolutionary-1.0.1/pyvolutionary/whales/whales_optimization.py +56 -0
- pyvolutionary-1.0.1/pyvolutionary/wildebeest_herd/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/wildebeest_herd/models.py +94 -0
- pyvolutionary-1.0.1/pyvolutionary/wildebeest_herd/wildebeest_herd_optimization.py +143 -0
- pyvolutionary-1.0.1/pyvolutionary/zebra/__init__.py +2 -0
- pyvolutionary-1.0.1/pyvolutionary/zebra/models.py +9 -0
- pyvolutionary-1.0.1/pyvolutionary/zebra/zebra_optimization.py +53 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyVolutionary
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: pyVolutionary is a package collecting metaheuristic algorithms, available for free usage and able to solve a wide range of optimization problems.
|
|
5
|
+
Author: Matteo Cacciola
|
|
6
|
+
Author-email: matteo.cacciola@gmail.com
|
|
7
|
+
Requires-Python: >=3.11,<3.13
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Requires-Dist: coverage (>=7.3.2,<8.0.0)
|
|
11
|
+
Requires-Dist: install (>=1.3.5,<2.0.0)
|
|
12
|
+
Requires-Dist: matplotlib (>=3.8.1,<4.0.0)
|
|
13
|
+
Requires-Dist: numpy (>=1.26.1,<2.0.0)
|
|
14
|
+
Requires-Dist: pydantic (>=2.4.2,<3.0.0)
|
|
15
|
+
Requires-Dist: scikit-learn (>=1.3.2,<2.0.0)
|
|
16
|
+
Requires-Dist: sphynx (>=0.0.3,<0.0.4)
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# pyVolutionary
|
|
20
|
+
|
|
21
|
+
[](https://github.com/matteocacciola/pyvolutionary/releases)
|
|
22
|
+

|
|
23
|
+

|
|
24
|
+

|
|
25
|
+

|
|
26
|
+
[](https://git-scm.com/book/en/v2/GitHub-Contributing-to-a-Project)
|
|
27
|
+
[](https://opensource.org/licenses/MIT)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
## Introduction
|
|
31
|
+
<img style="width: 200px; background-color: #fff; float: left; margin: 0 15px 0 0;" src="logo.png" alt="pyVolutionary"/>
|
|
32
|
+
|
|
33
|
+
**pyVolutionary** stands as a versatile Python library dedicated to metaheuristic algorithms within the realm of
|
|
34
|
+
evolutionary computation. Engineered for ease of use, flexibility, and speed, it exhibits robustness and efficiency,
|
|
35
|
+
having undergone rigorous testing on large-scale problem instances. The primary objectives encompass the implementation
|
|
36
|
+
of both classical and cutting-edge nature-inspired algorithms. The library is conceived as a user-friendly resource
|
|
37
|
+
facilitating rapid access to optimization algorithms for researchers, fostering the dissemination of optimization
|
|
38
|
+
knowledge to a broad audience without financial barriers.
|
|
39
|
+
|
|
40
|
+
Nature-inspired algorithms constitute a widely embraced tool for addressing optimization challenges. Over the course of
|
|
41
|
+
their evolution, a plethora of variants have emerged ([paper 1](https://arxiv.org/abs/1307.4186),
|
|
42
|
+
[paper 2](https://www.mdpi.com/2076-3417/8/9/1521)), showcasing their adaptability and versatility across diverse domains
|
|
43
|
+
and applications. Noteworthy advancements have been achieved through hybridization, modification, and adaptation of these
|
|
44
|
+
algorithms. However, the implementation of nature-inspired algorithms can often pose a formidable challenge, characterized
|
|
45
|
+
by complexity and tedium. **pyVolutionary** is specifically crafted to surmount this challenge, offering a streamlined and
|
|
46
|
+
expedited approach to leveraging these algorithms without the need for arduous, time-consuming implementations from scratch.
|
|
47
|
+
|
|
48
|
+
The list of algorithms currently implemented in **pyVolutionary** can be consulted in the [Algorithms](#Algorithms) section.
|
|
49
|
+
The library is continuously updated with new algorithms and problems, and contributions are welcome.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
**pyVolutionary** is available on [PyPI](https://pypi.org/project/pyvolutionary/), and can be installed via pip:
|
|
53
|
+
```bash
|
|
54
|
+
pip install pyvolutionary
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
Once installed, **pyVolutionary** can be imported into your Python scripts as follows:
|
|
59
|
+
```python
|
|
60
|
+
import pyvolutionary
|
|
61
|
+
```
|
|
62
|
+
Now, you can access the algorithms and problems included in the library. For example, let us inspect how you can solve
|
|
63
|
+
the _sphere_ problem with the Particle Swarm Optimization algorithm.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from pyvolutionary import ContinuousVariable, ParticleSwarmOptimization, ParticleSwarmOptimizationConfig, Task
|
|
67
|
+
|
|
68
|
+
# Define the problem, you can replace the following class with your custom problem to optimize
|
|
69
|
+
class Sphere(Task):
|
|
70
|
+
def objective_function(self, x: list[float]) -> float:
|
|
71
|
+
x1, x2 = x
|
|
72
|
+
f1 = x1 - 2 * x2 + 3
|
|
73
|
+
f2 = 2 * x1 + x2 - 8
|
|
74
|
+
return f1 ** 2 + f2 ** 2
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Define the task with the bounds and the configuration of the optimizer
|
|
78
|
+
population = 200
|
|
79
|
+
dimension = 2
|
|
80
|
+
position_min = -100.0
|
|
81
|
+
position_max = 100.0
|
|
82
|
+
generation = 400
|
|
83
|
+
fitness_error = 10e-4
|
|
84
|
+
task = Sphere(
|
|
85
|
+
variables=[ContinuousVariable(
|
|
86
|
+
name=f"x{i}", lower_bound=position_min, upper_bound=position_max
|
|
87
|
+
) for i in range(dimension)],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
configuration = ParticleSwarmOptimizationConfig(
|
|
91
|
+
population_size=population,
|
|
92
|
+
fitness_error=fitness_error,
|
|
93
|
+
max_cycles=generation,
|
|
94
|
+
c1=0.1,
|
|
95
|
+
c2=0.1,
|
|
96
|
+
w=[0.35, 1],
|
|
97
|
+
)
|
|
98
|
+
optimization_result = ParticleSwarmOptimization(configuration).optimize(task)
|
|
99
|
+
```
|
|
100
|
+
You can pass the `minmax` parameter to the `Task` class to specify whether you want to minimize or maximize the function.
|
|
101
|
+
Therefore, if you want to maximize the function, you can write:
|
|
102
|
+
```python
|
|
103
|
+
task = Sphere(
|
|
104
|
+
variables=[ContinuousVariable(
|
|
105
|
+
name=f"x{i}", lower_bound=position_min, upper_bound=position_max
|
|
106
|
+
) for i in range(dimension)],
|
|
107
|
+
minmax="max",
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
optimization_result = ParticleSwarmOptimization(configuration).optimize(task)
|
|
111
|
+
```
|
|
112
|
+
By default, the `minmax` parameter is set to `min`.
|
|
113
|
+
|
|
114
|
+
You can also specify the mode of the solver by using the `mode` argument of the `optimize` method.
|
|
115
|
+
For instance, if you want to run the Particle Swarm Optimization algorithm in parallel with threads, you can write:
|
|
116
|
+
```python
|
|
117
|
+
optimization_result = ParticleSwarmOptimization(configuration).optimize(task, mode="thread")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The possible values of the `mode` parameter are:
|
|
121
|
+
- `serial`: the algorithm is run in serial mode;
|
|
122
|
+
- `process`: the algorithm is run in parallel with processes;
|
|
123
|
+
- `thread`: the algorithm is run in parallel with threads.
|
|
124
|
+
|
|
125
|
+
In case of `process` and `thread` modes, you can also specify the number of processes or threads to use by using the
|
|
126
|
+
`n_jobs` argument of the `optimize` method:
|
|
127
|
+
```python
|
|
128
|
+
optimization_result = ParticleSwarmOptimization(configuration).optimize(task, mode="thread", jobs=4)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Finally, you can also specify the seed of the random number generator by using the `seed` parameter of the definition
|
|
132
|
+
of the `Task`:
|
|
133
|
+
```python
|
|
134
|
+
task = Sphere(
|
|
135
|
+
variables=[ContinuousVariable(
|
|
136
|
+
name=f"x{i}", lower_bound=position_min, upper_bound=position_max
|
|
137
|
+
) for i in range(dimension)],
|
|
138
|
+
minmax="max",
|
|
139
|
+
seed=42,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
optimization_result = ParticleSwarmOptimization(configuration).optimize(task)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The optimization result is a dictionary containing the following keys:
|
|
146
|
+
- `evolution`: a list of the agents found at each generation
|
|
147
|
+
- `rates`: a list of the fitness values of the agents found at each generation
|
|
148
|
+
- `best_solution`: the best agent found by the algorithm
|
|
149
|
+
|
|
150
|
+
Explicitly, the `evolution` key contains a list of `Population`, i.e. a dictionary which `agents` key contains a list of
|
|
151
|
+
`Agent`. The latter is a dictionary composed by the following basic keys:
|
|
152
|
+
- `position`: the position of the agent
|
|
153
|
+
- `fitness`: the fitness value of the agent
|
|
154
|
+
- `cost`: the cost of the agent
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from pydantic import BaseModel
|
|
158
|
+
|
|
159
|
+
class Agent(BaseModel):
|
|
160
|
+
position: list[float]
|
|
161
|
+
cost: float
|
|
162
|
+
fitness: float
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
These are the basic information, but each algorithm can add more information to the agent, such as the velocity in the
|
|
166
|
+
case of PSO.
|
|
167
|
+
|
|
168
|
+
### Utilities
|
|
169
|
+
**pyVolutionary** also provides a set of utilities to facilitate the use of the library. For example, you can use the
|
|
170
|
+
`plot` function to plot the evolution of the algorithm. Its usage is as follows:
|
|
171
|
+
```python
|
|
172
|
+
plot(function: callable, pos_min: float, pos_max: float, evolution: list[Population])
|
|
173
|
+
```
|
|
174
|
+
where:
|
|
175
|
+
- `function`: the function to plot, i.e., the function to optimize
|
|
176
|
+
- `pos_min`: the minimum possible coordinates in the search space
|
|
177
|
+
- `pos_max`: the maximum possible coordinates in the search space
|
|
178
|
+
- `evolution`: the evolution of the algorithm, i.e., the list of the agents found at each generation
|
|
179
|
+
|
|
180
|
+
It is also possible to inspect an animation of the evolution of the algorithm by using the `animate` function:
|
|
181
|
+
```python
|
|
182
|
+
animate(function: callable, optimization_result: OptimizationResult, pos_min: float, pos_max: float, filename: str)
|
|
183
|
+
```
|
|
184
|
+
where:
|
|
185
|
+
- `function`: the same as above
|
|
186
|
+
- `optimization_result`: the result of the optimization, i.e., the dictionary returned by the `optimize` method
|
|
187
|
+
- `pos_min`: the same as above
|
|
188
|
+
- `pos_max`: the same as above
|
|
189
|
+
- `filename`: the name of the file where to save the animation
|
|
190
|
+
|
|
191
|
+
Furthermore, you can extract the trend of the best agent found by the algorithm by using the `best_agent_trend` function:
|
|
192
|
+
```python
|
|
193
|
+
best_agent_trend(optimization_result: OptimizationResult, iters: list[int] | None = None) -> list[float]
|
|
194
|
+
```
|
|
195
|
+
where:
|
|
196
|
+
- `optimization_result`: the same as above
|
|
197
|
+
- `iters`: a list of the iterations to consider. If `None`, all the iterations are considered
|
|
198
|
+
It returns a list of the cost values of the best agent found at each iteration.
|
|
199
|
+
|
|
200
|
+
If you prefer, you can extract the trend of a specific agent by using the `agent_trend` function:
|
|
201
|
+
```python
|
|
202
|
+
agent_trend(optimization_result: OptimizationResult, idx: int, iters: list[int] | None = None) -> list[float]
|
|
203
|
+
```
|
|
204
|
+
where:
|
|
205
|
+
- `optimization_result`: the same as above
|
|
206
|
+
- `idx`: the index of the agent to consider
|
|
207
|
+
- `iters`: the same as above
|
|
208
|
+
It returns a list of the cost values of the agent at each iteration.
|
|
209
|
+
|
|
210
|
+
## Practical example
|
|
211
|
+
With **pyVolutionary**, you can solve a wide range of problems. In the following, we provide some examples of how to
|
|
212
|
+
optimize the hyper-parameters of a Machine Learning model, specifically a Support Vector Classifier (SVC), by using the
|
|
213
|
+
Particle Swarm Optimization (PSO).
|
|
214
|
+
|
|
215
|
+
You can replace the PSO with any other algorithm implemented in the library.
|
|
216
|
+
```python
|
|
217
|
+
from typing import Any
|
|
218
|
+
from sklearn.svm import SVC
|
|
219
|
+
from sklearn.model_selection import train_test_split
|
|
220
|
+
from sklearn.preprocessing import StandardScaler
|
|
221
|
+
from sklearn import datasets, metrics
|
|
222
|
+
|
|
223
|
+
from pyvolutionary import (
|
|
224
|
+
best_agent,
|
|
225
|
+
ContinuousVariable,
|
|
226
|
+
DiscreteVariable,
|
|
227
|
+
ParticleSwarmOptimization,
|
|
228
|
+
ParticleSwarmOptimizationConfig,
|
|
229
|
+
Task,
|
|
230
|
+
TaskType,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# Load the data set; In this example, the breast cancer dataset is loaded.
|
|
234
|
+
X, y = datasets.load_breast_cancer(return_X_y=True)
|
|
235
|
+
|
|
236
|
+
# Create training and test split
|
|
237
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y)
|
|
238
|
+
|
|
239
|
+
sc = StandardScaler()
|
|
240
|
+
X_train_std = sc.fit_transform(X_train)
|
|
241
|
+
X_test_std = sc.transform(X_test)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class SvmOptimizedProblem(Task):
|
|
245
|
+
def objective_function(self, x: list[Any]):
|
|
246
|
+
x_transformed = self.transform_position(x)
|
|
247
|
+
C, kernel = x_transformed["C"], x_transformed["kernel"]
|
|
248
|
+
degree, gamma = x_transformed["degree"], x_transformed["gamma"]
|
|
249
|
+
|
|
250
|
+
svc = SVC(C=C, kernel=kernel, degree=degree, gamma=gamma, probability=True, random_state=1)
|
|
251
|
+
svc.fit(X_train_std, y_train)
|
|
252
|
+
y_predict = svc.predict(X_test_std)
|
|
253
|
+
return metrics.accuracy_score(y_test, y_predict)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
task = SvmOptimizedProblem(
|
|
257
|
+
variables=[
|
|
258
|
+
ContinuousVariable(lower_bound=0.01, upper_bound=1000., name="C"),
|
|
259
|
+
DiscreteVariable(choices=["linear", "poly", "rbf", "sigmoid"], name="kernel"),
|
|
260
|
+
DiscreteVariable(choices=[*range(1, 6)], name="degree"),
|
|
261
|
+
DiscreteVariable(choices=['scale', 'auto', 0.01, 0.05, 0.1, 0.5, 1.0], name="gamma"),
|
|
262
|
+
],
|
|
263
|
+
minmax=TaskType.MAX,
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
configuration = ParticleSwarmOptimizationConfig(
|
|
267
|
+
population_size=200,
|
|
268
|
+
fitness_error=10e-4,
|
|
269
|
+
max_cycles=100,
|
|
270
|
+
c1=0.1,
|
|
271
|
+
c2=0.1,
|
|
272
|
+
w=[0.35, 1],
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
result = ParticleSwarmOptimization(configuration).optimize(task)
|
|
276
|
+
best = best_agent(result.evolution[-1].agents, task.minmax)
|
|
277
|
+
|
|
278
|
+
print(f"Best parameters: {task.transform_position(best.position)}")
|
|
279
|
+
print(f"Best accuracy: {best.cost}")
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
## Extending the library
|
|
284
|
+
**pyVolutionary** is designed to be easily extensible. You can add your own algorithms and problems to the library by
|
|
285
|
+
following the instructions below.
|
|
286
|
+
|
|
287
|
+
### Adding a new algorithm
|
|
288
|
+
To add a new algorithm, you need to create a new class that inherits from the `OptimizationAbstract` class. The new
|
|
289
|
+
class must implement the `optimization_step` method, where you can implement your new metaheuristic algorithm.
|
|
290
|
+
|
|
291
|
+
The constructor of the new class must accept a `config` parameter, which is a Pydantic model extending the `BaseOptimizationConfig`
|
|
292
|
+
class. This class contains the parameters of the algorithm, such as the population size, the number of generations, etc.
|
|
293
|
+
|
|
294
|
+
```python
|
|
295
|
+
from pydantic import BaseModel
|
|
296
|
+
|
|
297
|
+
class BaseOptimizationConfig(BaseModel):
|
|
298
|
+
population_size: int
|
|
299
|
+
fitness_error: float | None = None
|
|
300
|
+
max_cycles: int
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Once you created your new classes, you can run the algorithm by calling the `optimize` method, which takes as input a
|
|
304
|
+
`Task` object and returns a dictionary as above described.
|
|
305
|
+
|
|
306
|
+
## Algorithms
|
|
307
|
+
The following algorithms are currently implemented in **pyVolutionary**:
|
|
308
|
+
|
|
309
|
+
| Algorithm | Class | Paper | Example |
|
|
310
|
+
|-------------------------------------------|------------------------------------------|---------------------------------------------------------------------------------------------------|-----------------------------|
|
|
311
|
+
| African Vulture Optimization | `AfricanVultureOptimization` | [paper](https://doi.org/10.1016/j.cie.2021.107408) | [example](./demos/avo.py) |
|
|
312
|
+
| Ant Colony Optimization | `AntColonyOptimization` | [paper](https://doi.org/10.1109/MCI.2006.329691) | [example](./demos/aco.py) |
|
|
313
|
+
| Aquila Optimization | `AquilaOptimization` | [paper](https://doi.org/10.1016/j.cie.2021.107250) | [example](./demos/ao.py) |
|
|
314
|
+
| Bacterial Foraging Optimization | `BacterialForagingOptimization` | [paper](https://api.semanticscholar.org/CorpusID:108291966) | [example](./demos/bfo.py) |
|
|
315
|
+
| Bat Optimization | `BatOptimization` | [paper](https://link.springer.com/chapter/10.1007/978-3-642-12538-6_6) | [example](./demos/bo.py) |
|
|
316
|
+
| Bee Colony Optimization | `BeeColonyOptimization` | [paper](https://api.semanticscholar.org/CorpusID:8215393) | [example](./demos/bco.py) |
|
|
317
|
+
| Camel Caravan Optimization | `CamelCaravanOptimization` | [paper](https://ijeee.edu.iq/Papers/Vol12-Issue2/118375.pdf) | [example](./demos/ccoa.py) |
|
|
318
|
+
| Coral Reef Optimization | `CoralReefOptimization` | [paper](https://www.hindawi.com/journals/tswj/2014/739768/) | [example](./demos/cro.py) |
|
|
319
|
+
| Coyotes Optimization | `CoyotesOptimization` | [paper](https://ieeexplore.ieee.org/document/8477769) | [example](./demos/co.py) |
|
|
320
|
+
| Earthworms Optimization | `EarthwormsOptimization` | [paper](https://www.inderscience.com/info/inarticle.php?artid=93328) | [example](./demos/ewo.py) |
|
|
321
|
+
| Electromagnetic Field Optimization | `ElectromagneticFieldOptimization` | [paper](https://doi.org/10.1016/j.swevo.2015.07.002) | [example](./demos/efo.py) |
|
|
322
|
+
| Elephant Herd Optimization | `ElephantHerdOptimization` | [paper](https://ieeexplore.ieee.org/document/7383528) | [example](./demos/eho.py) |
|
|
323
|
+
| Firefly Swarm Optimization | `FireflySwarmOptimization` | [paper](https://doi.org/10.1504/IJBIC.2010.032124) | [example](./demos/fso.py) |
|
|
324
|
+
| Fireworks Optimization | `FireworksOptimization` | [paper](https://doi.org/10.1504/IJBIC.2010.037285) | [example](./demos/fwa.py) |
|
|
325
|
+
| Fish School Search Optimization | `FishSchoolSearchOptimization` | [paper](https://doi.org/10.1109/ICSMC.2008.4811695) | [example](./demos/fsso.py) |
|
|
326
|
+
| Flower Pollination Algorithm Optimization | `FlowerPollinationAlgorithmOptimization` | [paper](https://link.springer.com/chapter/10.1007/978-3-642-32894-7_27) | [example](./demos/fpao.py) |
|
|
327
|
+
| Forest Optimization Algorithm | `ForestOptimizationAlgorithm` | [paper](https://doi.org/10.1016/j.eswa.2014.05.009) | [example](./demos/foa.py) |
|
|
328
|
+
| Fox Optimization | `FoxOptimization` | [paper](https://link.springer.com/article/10.1007/s10489-022-03533-0) | [example](./demos/fo.py) |
|
|
329
|
+
| Genetic Algorithm Optimization | `GeneticAlgorithmOptimization` | [paper](https://www.sciencedirect.com/book/9780128219867/nature-inspired-optimization-algorithms) | [example](./demos/gao.py) |
|
|
330
|
+
| Grasshopper Optimization Algorithm | `GrasshopperOptimization` | [paper](https://doi.org/10.1016/j.advengsoft.2017.01.00) | [example](./demos/goa.py) |
|
|
331
|
+
| Grey Wolf Optimization | `GreyWolfOptimization` | [paper](https://doi.org/10.1016/j.advengsoft.2013.12.007) | [example](./demos/gwo.py) |
|
|
332
|
+
| Harmony Search Optimization | `HarmonySearchOptimization` | [paper](https://doi.org/10.1177/003754970107600201) | [example](./demos/hso.py) |
|
|
333
|
+
| Imperialist Competitive Optimization | `ImperialistCompetitiveOptimization` | [paper](https://doi.org/10.1109/ICCKE.2013.6687751) | [example](./demos/ico.py) |
|
|
334
|
+
| Invasive Weed Optimization | `InvasiveWeedOptimization` | [paper](https://doi.org/10.1016/j.ecoinf.2006.07.003) | [example](./demos/iwo.py) |
|
|
335
|
+
| Krill Herd Optimization | `KrillHerdOptimization` | [paper](https://doi.org/10.1016/j.cnsns.2012.05.010) | [example](./demos/kho.py) |
|
|
336
|
+
| Levy Flight Jaya Swarm Optimization | `LeviFlightJayaSwarmOptimization` | [paper](https://doi.org/10.1016/j.eswa.2020.113902) | [example](./demos/lfjso.py) |
|
|
337
|
+
| Monarch Butterfly Optimization | `MonarchButterflyOptimization` | [paper](https://link.springer.com/article/10.1007/s00521-015-1923-y) | [example](./demos/mbo.py) |
|
|
338
|
+
| Mountain Gazelle Optimization | `MountainGazelleOptimization` | [paper](https://doi.org/10.1016/j.advengsoft.2022.103282) | [example](./demos/mgo.py) |
|
|
339
|
+
| Osprey Optimization | `OspreyOptimization` | [paper](https://www.frontiersin.org/articles/10.3389/fmech.2022.1126450/full) | [example](./demos/ooa.py) |
|
|
340
|
+
| Particle Swarm Optimization | `ParticleSwarmOptimization` | [paper](https://ieeexplore.ieee.org/document/488968) | [example](./demos/pso.py) |
|
|
341
|
+
| Pathfinder Algorithm Optimization | `PathfinderAlgorithmOptimization` | [paper](https://doi.org/10.1016/j.asoc.2019.03.012) | [example](./demos/pfao.py) |
|
|
342
|
+
| Pelican Optimization | `PelicanOptimization` | [paper](https://www.mdpi.com/1424-8220/22/3/855) | [example](./demos/poa.py) |
|
|
343
|
+
| Seagull Optimization | `SeagullOptimization` | [paper](https://doi.org/10.1016/j.knosys.2018.11.024) | [example](./demos/so.py) |
|
|
344
|
+
| Siberian Tiger Optimization | `SiberianTigerOptimization` | [paper](https://ieeexplore.ieee.org/document/9989374) | [example](./demos/stoa.py) |
|
|
345
|
+
| Tasmanian Devil Optimization | `TasmanianDevilOptimization` | [paper](https://ieeexplore.ieee.org/document/9714388) | [example](./demos/tdo.py) |
|
|
346
|
+
| Virus Colony Search Optimization | `VirusColonySearchOptimization` | [paper](https://doi.org/10.1016/j.advengsoft.2015.11.004) | [example](./demos/vcso.py) |
|
|
347
|
+
| Walrus Optimization | `WalrusOptimization` | [paper](http://doi.org/10.21203/rs.3.rs-2174098/v1) | [example](./demos/woa.py) |
|
|
348
|
+
| Whales Optimization | `WhalesOptimization` | [paper](https://doi.org/10.1016/j.advengsoft.2016.01.008) | [example](./demos/wo.py) |
|
|
349
|
+
| Wildebeest Herd Optimization | `WildebeestHerdOptimization` | [paper](https://content.iospress.com/articles/journal-of-intelligent-and-fuzzy-systems/ifs190495) | [example](./demos/who.py) |
|
|
350
|
+
| Zebra Optimization | `ZebraOptimization` | [paper](https://ieeexplore.ieee.org/document/9768820) | [example](./demos/zoa.py) |
|