A PSO library with a focus on visualization.
Project description
Visual Swarm
A modern, visualization-first Particle Swarm Optimization (PSO) library for Python.
Visual Swarm makes it easy to not only run PSO but to see it in action. It's designed for researchers, students, and developers who want to gain an intuitive understanding of how swarms explore a search space.
This Visualization may not be visible on PyPI. ! The constraint line in blue has been added by editing the generated videp and is not available through code. The constraints may take form that I cannot account for when coding the visualization function
Table of Contents
Key Features
- Built-in Animations: Generate 1D, 2D, and 3D animations of the optimization process with a single line of code.
- Save to Video: Save your animations directly to
.mp4files for presentations or reports. - Modern API: A clean, object-oriented interface that's easy to extend.
- Flexible Topologies: Switch between global best (gbest) and local best (lbest) PSO with a simple boolean flag.
- Constraint Handling: Easily define constraints and penalties for complex optimization problems.
Installation
To save animations, you will need FFmpeg installed on your system.
# On macOS (using Homebrew):
brew install ffmpeg
# On Debian/Ubuntu:
sudo apt-get install ffmpeg
# On Windows (using Chocolatey):
choco install ffmpeg
Once FFmpeg is installed, you can install Visual Swarm and its dependencies:
There are two ways to install Visual Swarm: 1. For Development (Recommended):
# Clone the repository and install in editable mode for development
git clone https://github.com/AnshulPatil29/visual-swarm.git
cd visual-swarm
pip install -e .
1. For Development (Recommended): Once available on the Python Package Index (PyPI), you can install it directly with pip.
pip install visual-swarm
Quick Start: 2D Optimization
Here's how easy it is to find the peak of a 2D function and watch the swarm converge.
import numpy as np
import matplotlib.pyplot as plt
from visual_swarm import ParticleSwarm
# 1. Define a fitness function to maximize
def peak_function(x, y):
return -((x - 3)**2 + (y - 2)**2) + 10
# 2. Define the search space bounds: [[x_min, x_max], [y_min, y_max]]
bounds = np.array([[0.0, 5.0], [0.0, 5.0]])
# 3. Initialize the optimizer
pso = ParticleSwarm(
num_particles=50,
fitness_function=peak_function,
bounds=bounds
)
# 4. Create the animation! This also runs the optimization.
# show_grid=True helps visualize the fitness landscape.
ani = pso.create_animation(iterations=100, show_grid=True)
# 5. Show the final results and the animation plot
print(f"Best solution found: {pso.global_best_particle}")
print(f"Best fitness: {pso.global_best_fitness:.4f}")
plt.show()
Advanced Usage
Saving an Animation
To save the animation to a file instead of viewing it interactively, use the save and save_path arguments.
# Creates a file named '2d_peak_optimization.mp4' in the current directory
pso.create_animation(
iterations=100,
show_grid=True,
save=True,
save_path='2d_peak_optimization.mp4'
)
Local Best PSO (lbest)
To help avoid premature convergence on complex problems, you can use the local best topology. Each particle is influenced by the best particle in its immediate neighborhood, not the entire swarm.
pso_local = ParticleSwarm(
num_particles=50,
fitness_function=peak_function,
bounds=bounds,
is_global=False, # Set to False for local best
neighborhood_size=5 # Define how many neighbors to consider
)
AI Assistance
To enhance productivity and ensure high-quality documentation, this project utilized AI assistance (via Google's Gemini) for the following specific tasks:
- Generating initial docstrings based on function signatures.
- Creating boilerplate for test case scenarios.
- Refining grammar and phrasing in the documentation.
The core algorithmic logic, class structure, and final implementation were developed entirely by the author. To gain insights in the learning/development process, my notes are available at Notes
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
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 visual_swarm-0.1.2.tar.gz.
File metadata
- Download URL: visual_swarm-0.1.2.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df629d3566031b8484da71f72541e2a897d854928db1159938aa1b7a4491f0b5
|
|
| MD5 |
0f35a028bd7e66e3cb873e9daed2b94f
|
|
| BLAKE2b-256 |
0f16568e18a23e97800b31e658888519c0988157ad08c7da0ecf827244d66658
|
File details
Details for the file visual_swarm-0.1.2-py3-none-any.whl.
File metadata
- Download URL: visual_swarm-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49c1ec76fe40201380748ec6d22d0a00d8653da4e2e0f95f3ea2c256e7fd9677
|
|
| MD5 |
7160e59e75461886afc1a55561254232
|
|
| BLAKE2b-256 |
7d7dbea8d4e06c9b856a383c35ab58a332764f2ef713f2a66c5caac7f1764f96
|