Skip to content

odys.solvers.solver

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

See Solvers in the User Guide for the public API.

Solver dispatch for energy system optimization.

This module provides the solve function that dispatches to any linopy-supported solver based on the SolverConfig.

solve(model, solver_config)

Solve the model with the configured solver.

Parameters:

Name Type Description Default
model Model

The linopy model to solve.

required
solver_config SolverConfig

Solver configuration.

required

Returns:

Type Description
SolveOutcome

The solver status, termination condition, objective value and solution. Without a solution

SolveOutcome

(a status other than OK, or OK with no solution, such as a time limit before the first

SolveOutcome

feasible solution) the objective value is None and the solution is empty.

Raises:

Type Description
OdysSolverError

If the configured solver is not available.

Source code in src/odys/solvers/solver.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
def solve(model: linopy.Model, solver_config: SolverConfig) -> SolveOutcome:
    """Solve the model with the configured solver.

    Args:
        model: The linopy model to solve.
        solver_config: Solver configuration.

    Returns:
        The solver status, termination condition, objective value and solution. Without a solution
        (a status other than `OK`, or `OK` with no solution, such as a time limit before the first
        feasible solution) the objective value is `None` and the solution is empty.

    Raises:
        OdysSolverError: If the configured solver is not available.

    """
    validate_solver_available(solver_config.solver_name)

    status, termination_condition = model.solve(
        solver_name=solver_config.solver_name,
        explicit_coordinate_names=True,
        **translate_solver_config(solver_config),
    )

    solve_status = SolveStatus(status)
    solution = _solution_of(model) if solve_status is SolveStatus.OK else None
    if solution is None:
        return SolveOutcome(
            status=solve_status,
            termination_condition=termination_condition,
            objective_value=None,
            solution=xr.Dataset(),
        )
    return SolveOutcome(
        status=solve_status,
        termination_condition=termination_condition,
        objective_value=model.objective.value,
        solution=solution,
    )

validate_solver_available(solver_name)

Validate that the solver is installed and available.

Parameters:

Name Type Description Default
solver_name SolverName

The solver name to check.

required

Raises:

Type Description
OdysSolverError

If the solver is not in linopy.available_solvers.

Source code in src/odys/solvers/solver.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def validate_solver_available(solver_name: SolverName) -> None:
    """Validate that the solver is installed and available.

    Args:
        solver_name: The solver name to check.

    Raises:
        OdysSolverError: If the solver is not in linopy.available_solvers.

    """
    if solver_name not in linopy.available_solvers:
        available = ", ".join(sorted(linopy.available_solvers)) or "none"
        msg = f"Solver '{solver_name}' is not available. Installed solvers: {available}."
        raise OdysSolverError(msg)