saealib.Problem#

class saealib.Problem(func, dim, n_obj, direction, lb=None, ub=None, comparator=None, constraints=None, *, eps_cv=1e-06, eps_obj=1e-06, handler=None, variables=None, space=None, evaluation_adapter=None)[source]#

Bases: object

Definition of optimization problem.

Parameters:
dim#

Dimension of the design variables.

Type:

int

n_obj#

Number of objectives.

Type:

int

direction#

Optimization direction per objective. shape = (n_obj, ) Each element must be +1 (maximize) or -1 (minimize).

Type:

np.ndarray

lb#

Lower bounds for design variables. shape = (dim, )

Type:

np.ndarray

ub#

Upper bounds for design variables. shape = (dim, )

Type:

np.ndarray

variables#

Per-dimension variable definitions.

Type:

list[Variable]

comparator#

Comparator instance to compare solutions.

Type:

Comparator

eps_cv#

Epsilon for constraint violation feasibility threshold. Only used to seed the default handler/comparator at construction time; mutating this attribute afterwards has no runtime effect. The actual running threshold is handler.feasibility_threshold, which the Runner syncs into comparator/pareto_archive every generation. Explicit handlers may therefore use a running threshold intentionally different from this value. This value remains the feasibility basis for Result and summary best_f; an explicit handler with a different running threshold can therefore make reported solutions disagree with the comparator’s criterion.

Type:

float

eps_obj#

Epsilon for objective value equality comparison.

Type:

float

func#

Objective function to evaluate solutions.

Type:

callable -> float

constraints#

List of inequality constraint definitions.

Type:

list[InequalityConstraint]

handler#

Constraint-handling strategy used to aggregate violations and augment objectives.

Type:

ConstraintHandler

Methods

__init__

Initialize Problem instance.

evaluate

Evaluate the objective function at given solution x.

evaluate_batch

Evaluate the objective and constraint functions for a batch of solutions.

evaluate_constraints

Evaluate all constraint functions at x.

repair

Project x onto valid variable domains.

Method Details

Problem.__init__(func, dim, n_obj, direction, lb=None, ub=None, comparator=None, constraints=None, *, eps_cv=1e-06, eps_obj=1e-06, handler=None, variables=None, space=None, evaluation_adapter=None)[source]#

Initialize Problem instance.

Parameters:
  • func (callable -> float) – Objective function to evaluate solutions.

  • dim (int or None) – Dimension of the design variables.

  • n_obj (int) – Number of objectives.

  • direction (np.ndarray) – Optimization direction per objective. shape = (n_obj, ) Each element must be +1 (maximize) or -1 (minimize).

  • lb (list[float], optional) – Lower bounds for design variables. length = dim. Required when variables is not provided.

  • ub (list[float], optional) – Upper bounds for design variables. length = dim. Required when variables is not provided.

  • comparator (Comparator, optional) – Comparator instance to use. If None, auto-selected based on n_obj: n_obj == 1 -> SingleObjectiveComparator, n_obj > 1 -> NSGA2Comparator.

  • constraints (list[InequalityConstraint], optional) – List of inequality constraint definitions. Default: empty list.

  • eps_cv (float, optional) – Epsilon for constraint violation feasibility threshold. Default: 1e-6.

  • eps_obj (float, optional) – Epsilon for objective value equality comparison. Default: 1e-6.

  • handler (ConstraintHandler, optional) – Constraint-handling strategy. If None, a StaticToleranceHandler (sum-of-violations, fixed eps_cv) is used, reproducing the default behavior.

  • variables (list[Variable], optional) – Per-dimension variable definitions. When provided, lb and ub are derived from the variable bounds. len(variables) must equal dim.

  • space (SearchSpace, optional) – Search space to use for non-vector representations. When omitted, a VectorSpace is constructed from variables or bounds.

  • evaluation_adapter (EvaluationAdapter, optional) – Adapter that transforms genome batches into evaluation payloads.

Problem.evaluate(x, g=None)[source]#

Evaluate the objective function at given solution x.

After computing the raw objective, handler.augment_objective is applied so that penalty-based or augmented-Lagrangian handlers can transform the objective using constraint information. The default StaticToleranceHandler leaves the objective unchanged.

Parameters:
  • x (np.ndarray) – The solution to evaluate.

  • g (np.ndarray, optional) – Pre-computed raw constraint values g(x), shape = (n_constraints, ). When None, constraints are evaluated internally if any are defined. Pass this to avoid re-evaluating constraints when g is already available (e.g. from evaluate_constraints()).

Returns:

The objective value(s) at solution x. shape = (n_obj, )

Return type:

np.ndarray

Problem.evaluate_batch(x)[source]#

Evaluate the objective and constraint functions for a batch of solutions.

The default implementation returns None, meaning batch evaluation is not supported by this Problem – callers should fall back to the row-by-row path (evaluate() / evaluate_constraints() applied one row at a time). Subclasses (or Problem instances constructed around a batch-capable func, e.g. a vectorized function, a GPU kernel, or an external library such as pymoo that can score many designs in one call) may override this method to evaluate all rows of x in a single call.

The returned values are raw: unlike evaluate(), the objective batch is not passed through handler.augment_objective, and unlike evaluate_constraints(), the constraint batch is not reduced via handler.compute_cv. Callers are expected to apply those transformations themselves afterward, per row.

Parameters:

x (np.ndarray) – Batch of solutions to evaluate. shape = (n, dim)

Returns:

(f_batch, g_batch) if batch evaluation is supported, else None. f_batch : raw objective values. shape = (n, n_obj) g_batch : raw constraint values, in the same column order as self.constraints. shape = (n, n_constraints); (n, 0) when the problem has no constraints. Row order matches the order of rows in x.

Return type:

tuple[np.ndarray, np.ndarray] or None

Problem.evaluate_constraints(x)[source]#

Evaluate all constraint functions at x.

Parameters:

x (np.ndarray) – The solution to evaluate. shape = (dim, )

Returns:

  • g (np.ndarray) – Raw constraint values. shape = (n_constraints, ) Empty array when no constraints are defined.

  • cv (float) – Aggregate constraint violation as computed by handler.compute_cv. 0.0 when no constraints are defined.

Return type:

tuple[ndarray, float]

Problem.repair(x)[source]#

Project x onto valid variable domains.

Rounds integer dimensions to the nearest integer and clips to bounds. Rounds categorical dimensions to the nearest valid index. Clips continuous dimensions to [lb, ub].

Parameters:

x (np.ndarray) – Design variable array. shape = (dim,) or (n, dim).

Returns:

Repaired array, same shape as x.

Return type:

np.ndarray