Constraints¶
Constraint handling strategies¶
Available strategies (see vamos.foundation.constraints):
- feasibility_first: feasible dominates infeasible; aggregates objectives (sum/max/none).
- penalty_cv: adds lambda * constraint violation to aggregated objectives.
- cv_as_objective: ranks by violation with small objective tie-break.
- epsilon: feasibility first with epsilon tolerance.
Constraint DSL¶
Build constraints symbolically, then evaluate on populations.
import numpy as np
from vamos.foundation.constraints.dsl import constraint_model, build_constraint_evaluator
# Example: x0 + x1 <= 1 and x0 >= 0
with constraint_model(n_vars=2) as cm:
x0, x1 = cm.vars("x0", "x1")
cm.add(x0 + x1 <= 1.0)
cm.add(x0 >= 0.0)
eval_constraints = build_constraint_evaluator(cm)
X = np.array([[0.2, 0.3], [0.9, 0.4]])
G = eval_constraints(X) # shape (n_points, n_constraints), <=0 is satisfied
Notes:
- Constants in expressions must be scalar numbers (int/float/numpy scalar or 0-d array like np.array(1.0)).
- Vector constants (lists/tuples/ndarrays with shape (n,)) are not supported; expand them into separate constraints.
Example (vector constants are not supported):
# Not supported:
# cm.add(x0 <= np.array([1.0, 2.0]))
# Supported:
cm.add(x0 <= 1.0)
cm.add(x1 <= 2.0)
Using constraints in algorithms¶
- Provide problems that fill
out["G"]inevaluate(shape n_points x n_constraints, <=0 satisfied). - Algorithms like NSGA-II, MOEA/D, SMS-EMOA, SPEA2 honor constraints via the selected strategy inside kernels.
- Hypervolume early-stop and metrics continue to operate on objective values; feasibility affects selection and ranking.