Quickstart¶
Function style: @cm.declare¶
import numpy as np
import cmenpy as cm
@cm.declare(x=cm.Argument[int, (0, 3), 2])
def my_algorithm(args, epoch, ctx):
pop = cm.PopulationSwarm(ctx.buff)
pop.compute << ctx.rng.uniform(
ctx.bounds.low, ctx.bounds.up, (pop.size, ctx.bounds.ndim)
)
for _ in epoch: # one epoch of your operator, per iteration
...
best = my_algorithm(x=1).solve(
lambda x: np.sum(x**2), cm.Bounds([(-1, 1)] * 3), epochs=150, pop_size=15
)
Full version: example/hello_world_v1.py.
Class style: @cm.template¶
@cm.template
class MyAlgorithm:
a: cm.Argument[int, (1, 100)]
w: cm.Variable[np.ndarray]
def initialize(self, ctx) -> None:
pop = cm.PopulationSwarm(ctx.buff)
pop.compute << ctx.rng.uniform(-1, 1, (pop.size, ctx.bounds.ndim))
def evolve(self, e, ctx) -> None:
# ... generation `e` ...
...
model = MyAlgorithm(a=3, seed=1)
best = model.solve(lambda x: np.sum(x**2), cm.Bounds([(-1, 1)] * 3), 150, 15)
Full version: example/hello_world_v2.py.
The solve contract¶
(best_fitness, best_x) = model.solve(
f, bounds, epochs, pop_size, pop_range=None, debug=False, sense="min"
)
fis a callablex -> floatover one solution vector (or a vectorized target).boundsis acm.Bounds(e.g.cm.Bounds([(-1, 1)] * ndim)) — a reference benchmark'sf.boundssatisfies the same contract (Test functions).senseis"min"(default) or"max".
Run the examples¶
example/ is not a declared dependency target and is excluded from the wheel.
Every algorithm solves a reference test function from
opfunu-core, so install the
benchmark companions first:
uv sync --extra benchmarks
.venv/bin/python example/hello_world_v1.py
.venv/bin/python example/reduce_demo.py
See Examples for the full catalogue and
Test functions for the opfunu / enoppy patterns.