Custom optimizers¶
clypto ships two ways to write an optimizer. The decorator API
(@cy.optimizer) is the recommended route for new algorithms: it is compact,
validates its own hyper-parameters, and can compile itself with Cython. The
classic API (subclassing cy.LegacyOptimizer) is what the built-in catalog
uses.
This series builds a complete optimizer from scratch with @cy.optimizer, gives
its agents extra state with @cy.agent, runs it, and compiles it with Cython. It
complements the reference-style Tutorial.
Each part is self-contained but builds on the one before it.
The problem¶
Every optimizer solves a Problem: an objective function, a search space, and a
direction. We minimize the sphere function in 30 dimensions. This same setup is
reused throughout the series.
import numpy as np
import clypto as cy
def sphere(solution):
return np.sum(solution ** 2)
problem = cy.Problem(
obj_func=sphere,
bounds=cy.NumberBounds(float, low=[-10.0] * 30, up=[10.0] * 30),
sense="min",
)
The episodes¶
Your first optimizer —
@cy.optimizer,evolve, and static typing.Hyper-parameters —
cy.Argument, bounds, and validation.The population — the
Populationcontainer and derived fitness.Custom agent state —
@cy.agentandcy.Attribute.Classic API and compilation —
cy.LegacyOptimizerand the compile flags.Agent subclasses — extra per-agent state with
duplicate_agent’s copy protocol.
Migrating existing code¶
Porting an existing MEALPY-style or Optimizer-based algorithm onto the new API
is covered in the migration guide.