Custom agent state

Results so far only carry a solution. When an algorithm needs per-agent state (velocity, memory, a tag, …), declare an agent with cy.Attribute and pass it to the optimizer.

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",
)
@cy.agent
class Particle:
    velocity: cy.Attribute[float, ..., 0.0]


@cy.optimizer(agent=Particle)
class MyPSO:
    inertia: cy.Argument[float, (0.0, 1.5), 0.7]

    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.inertia * agent.velocity
                              + self.rng.normal(0, 1, self.bounds.n_dims))
            agent.solution = agent.solution + agent.velocity

Overriding generate is optional; without it, each new agent gets the declared defaults. The hook receives the freshly created agent — declared defaults already applied, not yet evaluated — plus the sampled solution, and returns the agent to keep.


Next: Classic API and compilation.