Skip to content

gds_analysis.psuu.objective

Composable objective functions for multi-KPI optimization.

Composable objective functions for multi-KPI optimization.

Objective

Bases: BaseModel, ABC

Reduces KPIScores to a single scalar for optimizer consumption.

Source code in packages/gds-analysis/gds_analysis/psuu/objective.py
class Objective(BaseModel, ABC):
    """Reduces KPIScores to a single scalar for optimizer consumption."""

    model_config = ConfigDict(frozen=True)

    @abstractmethod
    def score(self, kpi_scores: KPIScores) -> float:
        """Compute a scalar objective value from KPI scores."""

score(kpi_scores) abstractmethod

Compute a scalar objective value from KPI scores.

Source code in packages/gds-analysis/gds_analysis/psuu/objective.py
@abstractmethod
def score(self, kpi_scores: KPIScores) -> float:
    """Compute a scalar objective value from KPI scores."""

SingleKPI

Bases: Objective

Optimize a single KPI.

Source code in packages/gds-analysis/gds_analysis/psuu/objective.py
class SingleKPI(Objective):
    """Optimize a single KPI."""

    name: str
    maximize: bool = True

    def score(self, kpi_scores: KPIScores) -> float:
        val = kpi_scores[self.name]
        return val if self.maximize else -val

WeightedSum

Bases: Objective

Weighted linear combination of KPIs.

Use negative weights to minimize a KPI.

Source code in packages/gds-analysis/gds_analysis/psuu/objective.py
class WeightedSum(Objective):
    """Weighted linear combination of KPIs.

    Use negative weights to minimize a KPI.
    """

    weights: dict[str, float]

    @model_validator(mode="after")
    def _validate_nonempty(self) -> Self:
        if not self.weights:
            raise PsuuValidationError("WeightedSum must have at least 1 weight")
        return self

    def score(self, kpi_scores: KPIScores) -> float:
        return sum(w * kpi_scores[k] for k, w in self.weights.items())