Migrating algorithms¶
This guide covers the 2026 optimizer-API changes and how to move code onto them. Nothing about the built-in algorithms’ mathematics changed: every catalog optimizer returns the same result for the same seed.
1. The classic API (cimport clypto.core as cy)¶
A classic algorithm is a Cython extension type on cy.Optimizer; evolve is
the C method cdef void evolve(self, int epoch):
cimport clypto.core as cy
cdef class RandomSearch(cy.Optimizer):
def __init__(self, epoch=100, pop_size=30, **kwargs):
super().__init__(parameters=["epoch", "pop_size"], sort_flag=True, **kwargs)
self.epoch = cy.validator(int, epoch, [1, 100000], "epoch")
self.population = cy.population(pop_size, range=[5, 10000])
cdef void evolve(self, int epoch):
cdef cy.Population n_population = cy.empty_snapshot(self.population)
for agent in self.population.toarray():
x = cy.correct_solution(self.problem, self.problem.generate_solution())
n_population.append(self.population.create_agent(x))
self.population = self.population.greedy(self.population.evaluate(n_population, self.mode), self.mode)
Moving an algorithm of the previous classic API onto it:
Before |
After |
|---|---|
|
|
|
|
|
|
agent subclasses with |
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a Population subclass with Python attributes |
the single |
|
|
2. From the MEALPY-style API¶
@cy.legacy, self.validator and the Target class are gone; the hooks are
public again and the helpers moved to the population, the agent or cy:
Before |
After |
|---|---|
|
a |
|
|
|
|
|
|
|
|
|
|
|
|
overriding |
a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unchanged; evaluations are counted by |
3. Port a classic algorithm to @cy.optimizer¶
The decorator API removes the constructor and the validator boilerplate. The mapping is mechanical:
Classic API |
Decorator API |
|---|---|
|
|
|
|
|
automatic ( |
|
|
|
|
|
|
|
|
|
|
|
compare |
|
|
Before: the classic RandomSearch of section 1.
After:
import clypto as cy
@cy.optimizer
class RandomSearch:
def evolve(self, epoch):
for idx in range(len(self.population)):
candidate = self.generate_agent()
if candidate.fitness < self.population[idx].fitness:
self.population[idx].solution = candidate.solution
epoch and pop_size are provided by the base class, so only algorithm-specific
hyper-parameters need an Argument.
For type-checked code, inherit the base explicitly instead of relying on the decorator:
class RandomSearch(cy.DecoratedOptimizer). mypy cannot see the base that@cy.optimizerinjects, so only the explicit form typesself.population,self.generate_agent,self.boundsandself.rng.
initialize replaces the constructor¶
There is no __init__ in the decorator API — set up state in initialize,
which runs after the population exists:
@cy.optimizer
class Seeded:
def initialize(self):
self.population.solutions = self.rng.uniform(
self.bounds.low, self.bounds.up,
(len(self.population), self.bounds.n_dims),
)
def evolve(self, epoch):
...
If initialize is omitted, the population is generated uniformly at random
inside the problem bounds.
Agent attributes¶
If the algorithm needs per-agent state, declare an agent and pass it to the
optimizer. Use generate to seed attributes:
@cy.agent
class Particle:
velocity: cy.Attribute[float, ..., 0.0]
@cy.optimizer(agent=Particle)
class MyPSO:
def generate(self, agent, solution):
agent.velocity = self.rng.normal(0, 1, self.bounds.n_dims)
return agent
def evolve(self, epoch):
for agent in self.population:
agent.velocity += self.rng.normal(0, 1, self.bounds.n_dims)
agent.solution = agent.solution + agent.velocity
4. Compilation¶
Compilation is opt-in per class:
@cy.agent(compile=True)
class Particle:
velocity: cy.Attribute[float, ..., 0.0]
@cy.optimizer(agent=Particle, compile=True)
class MyPSO:
...
Install the compile extra (pip install "clypto[compile]"). Without Cython or
a compiler, plain (uncompiled) classes still run; the compile flags fail loudly
instead of silently skipping the build.