saealib.Comparator#
- class saealib.Comparator(weights, eps_cv, eps_obj, direction=None)[source]#
Bases:
ABCBase class for comparator.
- Parameters:
weights (np.ndarray)
eps_cv (float)
eps_obj (float)
direction (np.ndarray | None)
- weights#
Weights for objectives. shape = (n_obj, )
- Type:
np.ndarray
- eps_cv#
Epsilon for constraint violation feasibility threshold. Under
Optimizerexecution this value is overwritten every generation fromproblem.handler.feasibility_threshold; the constructor argument only matters for standalone (non-Optimizer) use.- Type:
float
- eps_obj#
Epsilon for objective value equality comparison.
- Type:
float
- direction#
Per-objective optimization directions (+1 = maximize, -1 = minimize). None means all objectives are minimized.
- Type:
np.ndarray or None
Methods
Compare two solutions directly from raw values. |
|
Compare individual a and b in population. |
|
Return population attributes this comparator needs to persist. |
|
Freshly recompute and materialize population-relative ranking state. |
|
Prepare population-relative ranking state, then sort. |
|
Sort population based on their attributes. |
Method Details
- abstractmethod Comparator.__init__(weights, eps_cv, eps_obj, direction=None)[source]#
- Parameters:
weights (ndarray)
eps_cv (float)
eps_obj (float)
direction (ndarray | None)
- abstractmethod Comparator.compare(fa, cv_a, fb, cv_b)[source]#
Compare two solutions directly from raw values.
This is the low-overhead variant of
compare_populationthat avoids constructing or indexing into a Population object.- Parameters:
fa (np.ndarray) – Objective values of solution a. shape = (n_obj,)
cv_a (float) – Constraint violation of solution a.
fb (np.ndarray) – Objective values of solution b. shape = (n_obj,)
cv_b (float) – Constraint violation of solution b.
- Returns:
-1 if a is better than b, 1 if b is better than a, 0 if equal.
- Return type:
int
- abstractmethod Comparator.compare_population(population, idx_a, idx_b)[source]#
Compare individual a and b in population.
- Parameters:
population (Population) – The population to compare.
idx_a (int) – Index of the first individual.
idx_b (int) – Index of the second individual.
- Returns:
Comparison result. -1 if the individual a is better than the individual b. 1 if the individual a is worse than the individual b. 0 if the individual a is equal to the individual b.
- Return type:
int
- Comparator.get_required_attrs(problem)[source]#
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]
- Comparator.prepare_population(population)[source]#
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/cvdo not need this. Comparators that declare attributes viaget_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
- Comparator.rank_population(population)[source]#
Prepare population-relative ranking state, then sort.
Equivalent to calling
prepare_population()followed bysort_population(). Callers performing environmental selection on a freshly assembled pool (e.g. survivor selection merging population and offspring) should call this instead ofsort_population()directly, so any population-relative state declared viaget_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
- abstractmethod Comparator.sort_population(population)[source]#
Sort population based on their attributes.
- Parameters:
population (Population) – The population to sort.
- Returns:
Sorted population indices.
- Return type:
np.ndarray