Skip to main content

A particle swarm optimization implementation

Project description

pswarm A clean, simplified implementation of the Particle Swarm Optimization algorithm for parameter optimization problems. Installation pip install pso-optimizer Features • Simple, well-documented implementation of PSO • Customizable inertia weight decay • Parameter-specific boundary handling • Built-in early stopping • Optimization history tracking • Utility functions for visualization and result saving Quick Start import numpy as np from pso_optimizer import PSOOptimizer, plot_convergence, save_results

Define objective function (example: minimize a simple quadratic function)

def objective_function(x): return -np.sum(x**2) # Negative because PSO maximizes by default

Define parameter bounds

param_min = np.array([-5.0, -5.0, -5.0]) param_max = np.array([5.0, 5.0, 5.0])

Create optimizer

optimizer = PSOOptimizer( objective_function=objective_function, param_min=param_min, param_max=param_max, swarm_size=30, max_iterations=100, maximize=False, # We're minimizing save_history=True )

Run optimization

best_params, best_fitness, history = optimizer.optimize()

Print results

print(f"Best parameters: {best_params}") print(f"Best fitness: {best_fitness}")

Plot convergence

plot_convergence(history, "convergence.png")

Save results

save_results(best_params, best_fitness, ['x', 'y', 'z'], "results.txt") Advanced Usage Special Boundary Handling You can specify special boundary handling for certain parameters:

Define parameter with wrapping boundary (like an angle)

special_boundaries = { 2: {'type': 'wrap'} # Parameter at index 2 will wrap around its bounds }

optimizer = PSOOptimizer( # ... other parameters ... special_boundary_handling=special_boundaries ) Loading Parameters from Configuration from pso_optimizer import load_parameter_bounds_from_config

Load parameter bounds from YAML config

param_min, param_max = load_parameter_bounds_from_config('config.yaml')

optimizer = PSOOptimizer( # ... other parameters ... param_min=param_min, param_max=param_max ) License This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pswarm-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pswarm-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file pswarm-0.1.1.tar.gz.

File metadata

  • Download URL: pswarm-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for pswarm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6a4bf8400a232052b63a3d6fd20b27ba25137343d9e77ce85198c81467becc87
MD5 c5a9cc637f1729d006c7460174bf6af4
BLAKE2b-256 cde32fe5c06f3c15327208c130ae856f40a35d88460bf098d5de09a53230f05a

See more details on using hashes here.

File details

Details for the file pswarm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pswarm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for pswarm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e491816621de4fdf724bfaf728d59d9470206cb39e1a8b277a2c727c847c63b
MD5 eace1cf9eca43f0412c06800960bf27f
BLAKE2b-256 46ce4becceb8b810f341623fde5d04861ee89255a85b3ca88340a8140a6c1278

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page