PyApso is an implementation of the Adaptive Particle Swarm Optimization (APSO) algorithm and designed for solving continuous, multi-objective, and multi-dimensional optimization problems.
Install the package using pip:
pip install pyapsoOr from source:
git clone https://github.com/smohamadabedy/pyapso.git
cd pyapso
pip install .ADAPTIVE - key parameters: (inertia weight, avg weight) Continuous optimization support Single and multi-objective fitness evaluation Constraint-aware evolution Batch evaluation and parallel execution Excel and JSON logging Custom callbacks and visualizations
from pyapso import APSO
import numpy as np
def fitness(x):
return np.sum(x**2)
if __name__ == "__main__":
best, score, history, hd = APSO(
objective_function=fitness,
dim=1,
bounds=([-10], [10]),
num_particles=100,
max_iter=10,
verbose = 1,live_plot=False,folder="results_folder",save_prefix="results_file"
).run()
print("Best solution:", best)
print("Best score:", score)
# Save optimization history (JSON,CSV)
hd.save()
# Plot optimization history
hd.plot()apso.run("avg", c1=1.8, c2=1.8, w_min=0.5, w_max=0.9)"avg" mode adapts the inertia weight dynamically between w_min and w_max using swarm behavior.
apso.run("inertia",c1=1.8, c2=1.8, inertia=0.05)"inertia" mode adapts a fixed inertia weight throughout the optimization (default).
💡 Tip:
- A higher c1 encourages particles to explore their own search path.
- A higher c2 encourages convergence to the global best.
- Lower inertia means the swarm is more reactive and less explorative.
You can find runnable demos in the examples/ directory:
examples/
├── demo_1.py # 5D Rastrigin function
├── demo_2.py # 3D constrained benchmark
├── demo_3.py # 2D McCormick functionRun an example:
python examples/demo_2.py