A simple, zero-dependency Particle Swarm Optimization (PSO) library for Python.
Project description
pypsoa
A simple, zero-dependency Particle Swarm Optimization (PSO) library for Python.
pypsoa is a lightweight implementation of the Particle Swarm Optimization algorithm, designed for educational purposes and simple optimization tasks. It provides a clean, object-oriented interface with no external dependencies beyond Python's standard library.
Features
- Zero Dependencies: Only requires Python standard library
- Simple API: Easy-to-use classes for Vector, Particle, and Swarm
- Educational: Clear implementation perfect for learning PSO concepts
- Flexible: Customizable parameters for different optimization scenarios
- 2D Optimization: Optimized for 2-dimensional search spaces
Installation
pip install pypsoa
Or install from source:
git clone https://github.com/atasoglu/pypsoa.git
cd pypsoa
pip install .
Quick Start
import math
from pypsoa import Vector, Swarm
# Define a fitness function (minimize distance to target)
target = Vector(50, 50)
def fitness_function(pos):
return math.sqrt((pos.x - target.x)**2 + (pos.y - target.y)**2)
# Create a swarm with 50 particles
swarm = Swarm.from_bounds(
num_particles=50,
x_bounds=(0, 100),
y_bounds=(0, 100),
w=0.5, # inertia weight
c1=1.0, # cognitive coefficient
c2=2.0 # social coefficient
)
# Run optimization for 20 iterations
for _ in range(20):
swarm.step() # Update particle positions
swarm.eval(fitness_function) # Evaluate fitness
# Get best solution
best_position, best_fitness = swarm.g_best
print(f"Best position: {best_position}")
print(f"Best fitness: {best_fitness}")
See API Reference for more details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pypsoa-0.1.0.tar.gz.
File metadata
- Download URL: pypsoa-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0788cbd8fdbd4aca547be0f3119a572b3dfb1a4421e7f1cc67665d3addffb133
|
|
| MD5 |
bcd34feb766fd2e70e6e98a2a4633f90
|
|
| BLAKE2b-256 |
2a6e85a55624073253dc42e412fef62b187e2eb0098f3d6db29a6f869b87acb6
|
File details
Details for the file pypsoa-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pypsoa-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
840cf3b7a73bd8f35b8ccbdc384e4ef665c791331ce28869f5f58c1f9fc41f19
|
|
| MD5 |
c9e809f424c3d055030f4a42c9d2edc6
|
|
| BLAKE2b-256 |
2cd85683d780662201be327888e4f5c18957031e8e6b7351f9e3da67128d925d
|