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

  1. Your first optimizer — @cy.optimizer, evolve, and static typing.

  2. Hyper-parameters — cy.Argument, bounds, and validation.

  3. The population — the Population container and derived fitness.

  4. Custom agent state — @cy.agent and cy.Attribute.

  5. Classic API and compilation — cy.LegacyOptimizer and the compile flags.

  6. 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.