Skip to main content

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.

Metadata

Release files for pypsoa 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypsoa 0.1.0
File Size Uploaded
pypsoa-0.1.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pypsoa 0.1.0
File Interpreter ABI Platform
pypsoa-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / pypsoa-0.1.0.tar.gz

Download URL pypsoa-0.1.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0788cbd8fdbd4aca547be0f3119a572b3dfb1a4421e7f1cc67665d3addffb133
BLAKE2b-256 checksum
How to use checksums
2a6e85a55624073253dc42e412fef62b187e2eb0098f3d6db29a6f869b87acb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.7

Release files / pypsoa-0.1.0-py3-none-any.whl

Download URL pypsoa-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
840cf3b7a73bd8f35b8ccbdc384e4ef665c791331ce28869f5f58c1f9fc41f19
BLAKE2b-256 checksum
How to use checksums
2cd85683d780662201be327888e4f5c18957031e8e6b7351f9e3da67128d925d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page