Skip to content

odys.optimization.model.model_builder

Internal implementation detail. Most users do not need this page directly.

Builds the linopy model of an optimization problem: variables, constraints and objective.

build_model(problem)

Build the optimization model of an energy system.

Variables come first (entity formulations, then objective terms), then constraints (entity formulations, the power balance, objective terms), then the objective: the sum of the weighted terms, maximized. A problem is built into one model only; its formulations refuse a second add_variables.

Parameters:

Name Type Description Default
problem OptimizationProblem

The formulations of the energy system and of its objective terms.

required

Returns:

Type Description
Model

The linopy model, ready for solving.

Source code in src/odys/optimization/model/model_builder.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def build_model(problem: OptimizationProblem) -> linopy.Model:
    """Build the optimization model of an energy system.

    Variables come first (entity formulations, then objective terms), then
    constraints (entity formulations, the power balance, objective terms), then
    the objective: the sum of the weighted terms, maximized. A problem is built
    into one model only; its formulations refuse a second `add_variables`.

    Args:
        problem: The formulations of the energy system and of its objective terms.

    Returns:
        The linopy model, ready for solving.

    """
    model = linopy.Model(force_dim_names=True)
    for formulation in problem.formulations:
        formulation.add_variables(model)
    for term in problem.objective_terms:
        term.add_variables(model)

    for formulation in problem.formulations:
        formulation.add_to_model(model)
    PowerBalance(problem.formulations).add_to_model(model)
    for term in problem.objective_terms:
        term.add_to_model(model)

    objective: linopy.LinearExpression = reduce(add, (term.expression() for term in problem.objective_terms))
    model.add_objective(objective, sense="max")
    return model