Skip to content

odys.optimization.objective_terms.base

Internal implementation detail. The ObjectiveTermFormulation base class and ObjectiveTermInputs.

Base class of the model formulation of one objective term.

ObjectiveTermFormulation

Bases: ConstraintGroup, ABC

The optimization model of one objective term: its variables, its @constraint methods and its expression.

It is built only when the objective has a term of its type. Terms read the profit of the entity formulations with per_scenario_profit(self.formulations), once the formulations' variables exist.

Source code in src/odys/optimization/objective_terms/base.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class ObjectiveTermFormulation(ConstraintGroup, ABC):
    """The optimization model of one objective term: its variables, its `@constraint` methods and its expression.

    It is built only when the objective has a term of its type. Terms read the
    profit of the entity formulations with `per_scenario_profit(self.formulations)`,
    once the formulations' variables exist.
    """

    def __init__(self, inputs: ObjectiveTermInputs) -> None:
        """Initialize with the shared indexing and the entity formulations of the problem.

        Args:
            inputs: The objective, the shared indexing, and the entity formulations.
        """
        self.context = inputs.context
        self.formulations = inputs.formulations

    @classmethod
    @abstractmethod
    def build(cls, inputs: ObjectiveTermInputs) -> Self | None:
        """Return the formulation of this type's objective term, or None if the objective has none.

        Args:
            inputs: The objective, the shared indexing, and the entity formulations.
        """

    def add_variables(self, model: linopy.Model) -> None:
        """Add this term's decision variables to the model. The default adds none.

        Args:
            model: The linopy model to add the variables to.
        """

    @abstractmethod
    def expression(self) -> linopy.LinearExpression:
        """Return the weighted term to maximize."""

__init__(inputs)

Initialize with the shared indexing and the entity formulations of the problem.

Parameters:

Name Type Description Default
inputs ObjectiveTermInputs

The objective, the shared indexing, and the entity formulations.

required
Source code in src/odys/optimization/objective_terms/base.py
33
34
35
36
37
38
39
40
def __init__(self, inputs: ObjectiveTermInputs) -> None:
    """Initialize with the shared indexing and the entity formulations of the problem.

    Args:
        inputs: The objective, the shared indexing, and the entity formulations.
    """
    self.context = inputs.context
    self.formulations = inputs.formulations

add_variables(model)

Add this term's decision variables to the model. The default adds none.

Parameters:

Name Type Description Default
model Model

The linopy model to add the variables to.

required
Source code in src/odys/optimization/objective_terms/base.py
51
52
53
54
55
56
def add_variables(self, model: linopy.Model) -> None:
    """Add this term's decision variables to the model. The default adds none.

    Args:
        model: The linopy model to add the variables to.
    """

build(inputs) abstractmethod classmethod

Return the formulation of this type's objective term, or None if the objective has none.

Parameters:

Name Type Description Default
inputs ObjectiveTermInputs

The objective, the shared indexing, and the entity formulations.

required
Source code in src/odys/optimization/objective_terms/base.py
42
43
44
45
46
47
48
49
@classmethod
@abstractmethod
def build(cls, inputs: ObjectiveTermInputs) -> Self | None:
    """Return the formulation of this type's objective term, or None if the objective has none.

    Args:
        inputs: The objective, the shared indexing, and the entity formulations.
    """

expression() abstractmethod

Return the weighted term to maximize.

Source code in src/odys/optimization/objective_terms/base.py
58
59
60
@abstractmethod
def expression(self) -> linopy.LinearExpression:
    """Return the weighted term to maximize."""

ObjectiveTermInputs

Bases: BaseModel

What an objective term is built from: the objective, the shared indexing, and the entity formulations.

Source code in src/odys/optimization/objective_terms/base.py
15
16
17
18
19
20
21
22
class ObjectiveTermInputs(BaseModel):
    """What an objective term is built from: the objective, the shared indexing, and the entity formulations."""

    model_config = ConfigDict(frozen=True, extra="forbid", arbitrary_types_allowed=True)

    objective: Objective
    context: ModelContext
    formulations: tuple[Formulation, ...]