saealib.NSGA2Comparator#

class saealib.NSGA2Comparator(direction=None, *, eps_cv=1e-06, eps_obj=1e-06, sorter=<function non_dominated_sort>, dominator=None)[source]#

Bases: ParetoComparator

Comparator for multi-objective optimization via NSGA-II style ranking.

Extends ParetoComparator with crowding-distance-based secondary ordering:

  • sort_population: non-dominated sorting + crowding distance

  • compare_population: overrides ParetoComparator with Deb et al. (2002)’s crowded-comparison operator (prec_n): lower rank wins, ties broken by higher crowding distance – see compare_population()

Infeasible individuals (cv > eps) are always ranked after feasible ones, ordered by ascending constraint violation.

The sort result is cached in the Population cache and automatically invalidated when the population is modified.

Parameters:
  • direction (np.ndarray or None) – Per-objective optimization directions (+1 = maximize, -1 = minimize). None means all objectives are minimized.

  • eps_cv (float)

  • eps_obj (float)

  • sorter (NonDominatedSorter)

  • dominator (Dominator | None)

Methods

__init__

compare

Compare two solutions directly without a Population object.

compare_population

Compare via the NSGA-II crowded comparison operator.

get_required_attrs

Return population attributes this comparator needs to persist.

prepare_population

Freshly recompute and materialize population-relative ranking state.

rank_population

Prepare population-relative ranking state, then sort.

sort_population

Sort by Pareto front rank then crowding distance (NSGA-II style).

Method Details

NSGA2Comparator.__init__(direction=None, *, eps_cv=1e-06, eps_obj=1e-06, sorter=<function non_dominated_sort>, dominator=None)[source]#
Parameters:
NSGA2Comparator.compare(fa, cv_a, fb, cv_b)#

Compare two solutions directly without a Population object.

Parameters:
  • fa (ndarray)

  • cv_a (float)

  • fb (ndarray)

  • cv_b (float)

Return type:

int

NSGA2Comparator.compare_population(population, idx_a, idx_b)[source]#

Compare via the NSGA-II crowded comparison operator.

Implements the partial order defined by Deb et al. (2002) (Section III-B.2, “Crowded-Comparison Operator”, operator prec_n): prefer the lower (better) nondomination rank; if ranks are equal, prefer the individual with the larger crowding distance.

References

[3]: Deb, K., Pratap, A., Agarwal, S., & Meyarivan, T. (2002). A fast and elitist multiobjective genetic algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), 182-197.

Parameters:
Return type:

int

NSGA2Comparator.get_required_attrs(problem)#

Return population attributes this comparator needs to persist.

Population-relative ranking state (e.g. SPEA2 fitness) that must survive across selections belongs here as a named, typed attribute rather than in the population’s transient cache. Default: no additional attributes.

Parameters:

problem (Problem) – The optimization problem.

Returns:

Empty by default.

Return type:

list[PopulationAttribute]

NSGA2Comparator.prepare_population(population)#

Freshly recompute and materialize population-relative ranking state.

Default is a no-op — comparators whose ranking depends only on per-individual values already stored in f/cv do not need this. Comparators that declare attributes via get_required_attrs() should override this to recompute and write those attributes for the given population, unconditionally (not reusing any prior cached value).

Parameters:

population (Population) – The population to prepare ranking state for.

Return type:

None

NSGA2Comparator.rank_population(population)#

Prepare population-relative ranking state, then sort.

Equivalent to calling prepare_population() followed by sort_population(). Callers performing environmental selection on a freshly assembled pool (e.g. survivor selection merging population and offspring) should call this instead of sort_population() directly, so any population-relative state declared via get_required_attrs() is recomputed for the new set before ranking.

Parameters:

population (Population) – The population to rank.

Returns:

Sorted population indices, as returned by sort_population().

Return type:

np.ndarray

NSGA2Comparator.sort_population(population)[source]#

Sort by Pareto front rank then crowding distance (NSGA-II style).

Parameters:

population (Population)

Return type:

np.ndarray