Source code for qdpy.algorithms.set_scalarization

# SPDX-License-Identifier: LGPL-3.0-or-later
## @file
# @brief Optimize a compact repertoire against a shared panel of behavior targets.
# @see https://arxiv.org/abs/2602.00478

"""Smooth Sum-of-Minimum and Smooth Tchebycheff Set optimization."""

from dataclasses import dataclass
from math import isfinite
from operator import index

import numpy as np
from scipy.special import logsumexp

from qdpy.containers import Grid
from qdpy.phenotype import Features, Fitness, Individual
from .differentiable import GradientEvaluation
from .task_search import _measure, _offer, _search_bounds, _validate_grid

__all__ = ["SetScalarizationResult", "set_scalarized_search"]


## @brief Final repertoire and per-target costs, with exact evaluator accounting.
[docs] @dataclass(frozen=True) class SetScalarizationResult: """Report the measured final set and unsmoothed minimum cost at each target. The objective is the final smooth loss and is minimized. Target costs retain probe order and are negative discounted qualities. A reporting archive, if supplied, contains the final set's elites. """ population: tuple[Individual, ...] archive: Grid | None evaluations: int objective: float target_costs: tuple[float, ...]
def _target_cost(qualities, descriptors, targets, bandwidth): with np.errstate(over="ignore"): offsets = (descriptors[:, None, :] - targets) / bandwidth kernel = np.exp(-np.square(offsets).sum(axis=-1)) return -qualities[:, None] * kernel, kernel, offsets def _scalarize(costs, smoothing, reference): """Return the smooth loss and its derivatives with respect to probe costs.""" minimum = costs.min(axis=0) with np.errstate(over="ignore"): logits = (minimum - costs) / smoothing normalizer = logsumexp(logits, axis=0) soft_min = minimum - smoothing * normalizer within = np.exp(logits - normalizer) if reference is None: return soft_min.sum(), within shortfall = soft_min - reference worst = shortfall.max() logits = (shortfall - worst) / smoothing normalizer = logsumexp(logits) return worst + smoothing * normalizer, within * np.exp(logits - normalizer) ## @brief Minimize smooth target costs jointly over a fixed population. # @return SetScalarizationResult with the final evaluated designs.