A flexible metaheuristic optimization framework built for diverse search spaces and scalable execution.
Tyrannis is a Python framework for solving optimization problems with population-based metaheuristic algorithms while keeping the problem definition independent from the execution strategy. Define your search space, choose an optimization algorithm, and select whether to execute it locally, in parallel, or in a distributed environment. Tyrannis also provides support for mixed-variable optimization, allowing continuous, integer, binary, categorical, and permutation variables to coexist in the same problem.
Installation
Basic installation
Install Tyrannis with its standard local execution capabilities:
pip install tyrannis
The base installation includes the core optimization framework, local execution, and the basic LRU and disk caching mechanisms.
Optional features
Additional capabilities are available through optional dependency groups:
| Extra |
Installation |
Adds |
cache |
pip install "tyrannis[cache]" |
Additional LFU, FIFO, and Random Replacement caching strategies. |
spark |
pip install "tyrannis[spark]" |
Support for the SparkParallel and SparkDistributed backends. |
mpi |
pip install "tyrannis[mpi]" |
Support for the MPIParallel and MPIDistributed backends and the tyrannis-mpi launcher. |
Extras can also be combined explicitly in a single installation. For example:
pip install "tyrannis[cache,spark,mpi]"
The spark and mpi extras install the Python dependencies required by their respective backends. A compatible Spark or MPI runtime and execution infrastructure must also be available in the environment where those backends are used.
Public API
The core optimization requires three components: an optimizer, a search space, and an optimization algorithm. The processor, backend, and migration components are optional and can be added to control how the optimization is executed, parallelized, distributed, and coordinated.
Algorithms
tyrannis.algorithm
Algorithms are population-based metaheuristics used to search for a minimum within the defined search space.
| Import |
Description |
Family |
PSO |
Particle Swarm Optimization for population-based search. [1, 2] |
Agent Dynamics Algorithm |
GSA |
Gravitational Search Algorithm using gravitational interactions between candidate solutions to guide population-based search. [3] |
Agent Dynamics Algorithm |
PSOGSA |
Particle Swarm Optimization and Gravitational Search Algorithm for hybrid population-based search. [4] |
Agent Dynamics Algorithm |
AntColony |
Ant Colony Optimization for Continuous Domains (ACOR), which uses an archive of continuous solutions to guide the search. [5] |
Colony-Based Swarm Intelligence |
BeeColony |
Artificial Bee Colony optimization inspired by the foraging behavior of honey bees. [6] |
Colony-Based Swarm Intelligence |
GreyWolf |
Grey Wolf Optimization inspired by the social hierarchy and hunting behavior of grey wolves, with optional exploration enhancement through EEGWO. [7, 8] |
Leader-Guided Collective Search |
WhaleAlgorithm |
Whale Optimization Algorithm inspired by the bubble-net hunting behavior of humpback whales, with nonlinear control of the exploration-exploitation transition. [9, 10] |
Leader-Guided Collective Search |
GeneticAlgorithm |
Real-coded Genetic Algorithm for continuous optimization. [11, 12] |
Evolutionary Algorithm |
DifferentialEvolution |
Differential Evolution using differential mutation and crossover for continuous optimization. [13] |
Evolutionary Algorithm |
CMAES |
Covariance Matrix Adaptation Evolution Strategy for continuous optimization. [14, 15] |
Evolutionary Algorithm |
EGNA |
Estimation of Gaussian Networks Algorithm, a multivariate continuous Estimation of Distribution Algorithm that learns and samples Gaussian Bayesian networks. [16, 17] |
Estimation of Distribution Algorithm |
Spaces
tyrannis.space
Spaces define the search domain, including the variables, their bounds or available values, and the cost function to be minimized.
| Import |
Description |
Continuous |
Continuous variables bounded by numerical lower and upper limits. |
Integer |
Integer variables bounded by numerical lower and upper limits. |
Binary |
Binary variables restricted to False or True. |
Categorical |
Categorical variables selected from a finite set of discrete choices. |
Ordinal |
Ordinal variables selected from an ordered set of discrete choices, with optional positions defining their relative distances. |
Permutation |
Permutation variables for ordering and permutation-based optimization problems. |
Sequence |
Variable-length ordered sequences selected from a finite set of choices, with repetition allowed and categorical or ordinal decoding. |
Mixed |
Composition of multiple spaces, allowing heterogeneous variable types in the same optimization problem. |
Processors
tyrannis.processor
Processors define how the optimization loop is executed within each machine or execution environment.
| Import |
Description |
Joblib |
Executes particle processing in parallel using Joblib. |
ProcessPool |
Executes particle processing in parallel using multiple processes. |
ThreadsPool |
Executes particle processing in parallel using a pool of threads. |
When no processor is provided, Optimizer uses its internal serial processor. This keeps the default execution sequential while allowing parallel particle evaluation to be enabled explicitly.
Backends
tyrannis.backend
Backends define how optimization execution is organized across machines and, for distributed execution, across optimization islands.
| Import |
Description |
SparkParallel |
Distributes particle initialization and updates through Spark while keeping the optimization as a single population without islands; compatible with Databricks, including shared-access clusters. |
SparkDistributed |
Distributes independent optimization islands as Spark partitions and supports inter-island migration; compatible with Databricks Dedicated access mode clusters. |
MPIParallel |
Distributes particle initialization and updates through MPI while keeping the optimization as a single population without islands. |
MPIDistributed |
Distributes independent optimization islands across MPI worker ranks and supports inter-island migration. |
When no backend is provided, Optimizer uses the local backend and executes the optimization on a single machine.
Migrations
tyrannis.migration
Migration strategies define how solutions are exchanged between optimization islands when using a distributed, island-based backend.
| Import |
Description |
GlobalBest |
Shares the best solution between islands, with configurable synchronization behavior. |
IslandMigration |
Exchanges solutions between islands according to configurable selection, migration size, interval, movement, and trigger strategies. |
When no migration strategy is provided for an island-based backend, the islands remain isolated and do not exchange solutions.
Usage
The examples reuse imports and definitions from the basic example unless otherwise specified.
Basic optimization
Optimization problems are defined by a cost function, a search space, and an algorithm. This example minimizes a two-variable quadratic function using Continuous and PSO with the default local, sequential execution.
from tyrannis import Optimizer
from tyrannis.algorithm import PSO
from tyrannis.space import Continuous
def cost_function(x, y):
return x**2 + y**2
space = Continuous(
boundaries={"x": (-10, 10), "y": (-10, 10)},
cost_function=cost_function,
)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
)
optimizer.fit()
print(optimizer.best_solution)
print(optimizer.best_fitness)
Non-continuous search space
Non-continuous search spaces support discrete decisions and combinatorial problems. This example uses Permutation to optimize the ordering of five choices, with the cost function receiving the decoded order as a list.
from tyrannis.space import Permutation
def permutation_cost_function(order):
return sum(abs(a - b) for a, b in zip(order, order[1:]))
space = Permutation(
choices=(1, 2, 3, 4, 5),
cost_function=permutation_cost_function,
)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
)
optimizer.fit()
Binary search space
Binary represents Boolean decisions and supports positional or named inputs to the cost function. In this example, 32 bits represent option selections. Tyrannis unpacks the decoded list of Boolean values into positional arguments, which *selected collects into a tuple. The objective favors selecting eight options with the greatest combined benefit.
from tyrannis.space import Binary
def selection_cost(*selected):
benefit = sum(index for index, chosen in enumerate(selected, start=1) if chosen)
return 100 * abs(sum(selected) - 8) - benefit
space = Binary(bits=32, cost_function=selection_cost)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
)
optimizer.fit()
Mixed search space
Mixed combines different supported search-space types in one optimization problem, with each component's type and search range defined independently. This example uses one continuous, one categorical, and two binary variables, passing their decoded values to the cost function as keyword arguments.
from tyrannis.space import Binary, Categorical, Continuous, Mixed
def mixed_cost_function(x, category, option_a, option_b):
targets = {"low": 0.0, "medium": 1.0, "high": 2.0}
target = targets[category] + option_a - option_b
return (x - target) ** 2 + 0.1 * (option_a + option_b)
space = Mixed(
spaces={
"x": Continuous((-5.0, 5.0)),
"category": Categorical(("low", "medium", "high")),
"option_a": Binary(),
"option_b": Binary(),
},
cost_function=mixed_cost_function,
)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
)
optimizer.fit()
Caching
Caching can be enabled on search spaces to reuse cost-function results when equivalent solutions are evaluated again. This example applies an LRU cache to the 32-bit Binary selection problem, reusing selection_cost from the earlier binary example.
from tyrannis.space import Binary
space = Binary(
bits=32,
cost_function=selection_cost,
use_cache=True,
cache_type="lru",
cache_size=10_000,
)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
)
optimizer.fit()
Parallel processing with processors
Processors control how particles are processed and can be changed without redefining the optimization problem. This example uses Joblib with the loky backend (default process-based parallelism) for local parallel processing.
from tyrannis.processor import Joblib
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
processor=Joblib(),
)
optimizer.fit()
Spark backends
Spark backends support both independent optimization islands and parallel processing of a single population. The following examples use an active SparkSession assigned to spark.
Distributed optimization
Distributed Spark optimization runs independent islands, which can exchange solutions through migration strategies. This example uses SparkDistributed to create four islands as Spark partitions; n_executors sets the island count, not the number of physical executors. On Databricks, use a Dedicated cluster rather than Serverless or Shared access mode.
from tyrannis.backend import SparkDistributed
backend = SparkDistributed(
spark,
n_executors=4,
)
optimizer = Optimizer(
space=space,
algorithm=PSO(),
n_iterations=100,
n_particles=50,
backend=backend,
)
optimizer.fit()
Spark Parallel optimization