saealib.GradientRepairHandler#

class saealib.GradientRepairHandler(max_iter=1, epsilon=1e-06)[source]#

Bases: ConstraintHandler

Gradient-based constraint repair handler.

Implements the repair procedure of Chootinan & Chen (2006): each call to repair() runs up to max_iter outer iterations. In each iteration, the set of currently-violated constraints is determined (equality: |h(x)| > tolerance; inequality: g(x) > threshold), their raw values and gradients are stacked into ΔV and the Jacobian J, and a single simultaneous update is applied:

Δx = pinv(J) @ ΔV
x <- x + Δx

using the Moore-Penrose pseudoinverse, since J is generally not square. Satisfied constraints contribute no row to J/ΔV for that iteration, per the paper’s remark that “there is no need to adjust … non-violated constraints.” Iteration stops early once no constraint is violated, or once max(|Δx|) < epsilon.

ΔV follows the cited repair procedure. For an inequality constraint (single upper bound threshold only, per InequalityConstraint), ΔV_i = threshold - g(x). For an EqualityConstraint (h(x) = 0 by construction, i.e. the target value is already embedded as 0), ΔV_i = -h(x); both are the paper’s “target minus current value.” The paper’s worked example uses ΔV = c - h(x) = 1, which is the sign that the printed equality row does not literally show but the numeric example requires).

When gradient() returns None for a violated constraint, _numerical_gradient() supplies a forward-difference approximation instead of skipping it — this is what lets the handler now repair both equality and inequality constraints regardless of whether an analytical Jacobian is available.

After all iterations the result is clipped to [lb, ub] so bounds feasibility is always guaranteed.

Parameters:
  • max_iter (int, optional) – Maximum number of outer iterations per call. Default: 1.

  • epsilon (float, optional) – Convergence threshold: iteration stops early once max(abs(Δx)) < epsilon. Default: 1e-6.

References

[2]: Chootinan, P., & Chen, A. (2006). Constraint handling in genetic algorithms using a gradient-based repair method. Computers & Operations Research, 33(8), 2263-2281.

Methods

__init__

augment_objective

Transform objective values using constraint information.

compute_cv

Return sum of per-constraint violations (same as StaticToleranceHandler).

on_generation_end

Run end-of-generation bookkeeping.

repair

Apply simultaneous pseudoinverse updates toward feasibility, then clip.

Method Details

GradientRepairHandler.__init__(max_iter=1, epsilon=1e-06)[source]#
Parameters:
  • max_iter (int)

  • epsilon (float)

GradientRepairHandler.augment_objective(f, constraints, x, g)#

Transform objective values using constraint information.

Used by penalty-based or augmented-Lagrangian strategies. The default is the identity (objectives are returned unchanged).

Parameters:
  • f (np.ndarray) – Raw objective values. shape = (n_obj, )

  • constraints (list[InequalityConstraint]) – The problem’s inequality constraints, aligned with g.

  • x (np.ndarray) – The evaluated design vector. shape = (dim, )

  • g (np.ndarray) – Raw constraint values g(x). shape = (n_constraints, )

Returns:

The (possibly) augmented objective values. shape = (n_obj, )

Return type:

np.ndarray

GradientRepairHandler.compute_cv(constraints, x, g)[source]#

Return sum of per-constraint violations (same as StaticToleranceHandler).

Parameters:
Return type:

float

GradientRepairHandler.on_generation_end(gen, population)#

Run end-of-generation bookkeeping.

Used by adaptive strategies (e.g. ε-level control) that update their internal state between generations. The default is a no-op.

Parameters:
  • gen (int) – The generation index that just finished.

  • population (Population) – The current population.

Return type:

None

GradientRepairHandler.repair(x, constraints, lb, ub, **kwargs)[source]#

Apply simultaneous pseudoinverse updates toward feasibility, then clip.

Parameters:
Return type:

ndarray