Swarm
Black-box optimisation tool written in Rust. Clean API for solving complex single-objective and multi-objective optimisation problems using powerful metaheuristic algorithms. Swarm has the following features:
- NSGA-II for single/multi-objective problems.
- Particle Swarm Optimisation (PSO) for single-objective problems.
- Optional Parallel Execution (massively improves performance for expensive blackbox functions).
- Flexible Function Support.
Getting Started
Rust
To use Swarm in your project, add it as a dependency in your Cargo.toml:
[]
= "0.1.2" # (replace with current version)
This by default includes the parallel
feature. This allows the use of solve_par
which is very useful for computationally expensive objective functions. To disable it (for more lightweight build, or if parallel not necessary) use default-features = false
.
Python
Examples in Rust
All functions provided to Swarm must be of the signature:
FnMut
- The first argument is a reference to a vector of f64 values representing the variables. Variable types are currently limited to f64.
- The first return vector contains the objective values.
- The second return vector contains the constraint values (if any).
- For parallel execution, the function must also be thread-safe (i.e.,
Fn + Sync + Send
).
Single-Objective Optimisation with Particle Swarm (PSO)
Problem: Minimise f(x, y) = (x² + y - 11)² + (x + y² - 7)²
use ;
// Define the function (can also use closure)
Multi-Objective Optimisation with NSGA-II
This example solves the constrained Binh and Korn problem. NSGA-II is ideal for this as it finds a set of trade-off solutions, known as the Pareto Front, rather than a single point.
Problem: Minimise two objectives, f1(x, y) and f2(x, y), subject to two constraints.
use ;
// Define the problem (can also use closure)
After running the Binh and Korn example and plotting the solutions, you should see a Pareto front similar to the one shown below.
Examples in Python
Similar to the Rust examples all blackbox functions must have signatures of the form:
Here's the same Binh and Korn example in Python:
= 4.0 * **2 + 4.0 * **2
= **2 + **2
= **2 + **2 - 25.0
= 7.7 - **2 - **2
return
# Define variable bounds and optimisation settings
=
# Configure the NSGA-II optimiser
# Other properties are default unless specified
=
# Run the optimisation
=
# For parallel execution, use the following:
=
Performance (Python)
Serial Execution
Comparing to the same configuration Pymoo NSGA-II optimiser for the ZDT1 problem:
Parallel Execution
For expensive blackbox functions it makes sense to run swarm in parallel. If we simulate an expensive blackbox function by adding a sleep delay to the ZDT1 problem, i.e.,
# Artificial delay
=
=
= 1.0 + 9.0 * /
= 1.0 -
= *
return
then running swarm with NSGA-II in parallel is a massive improvement:
Build python from source
These instructions assume that Python3 and Cargo are installed on your system. To set up this project, follow these steps:
- Clone the repository:
- Create a virtual environment and install build system:
- Build the release binary:
- Build the python wheel:
License
MIT License - See LICENSE for details.
Support
If you'd like to support the project consider:
- Identifying the features you'd like to see implemented or bugs you'd like to fix and open an issue.
- Contributing to the code by resolving existing issues, I'm happy to have you.
- Donating to help me continue development, Buy Me a Coffee