Skip to content

odys.solvers.config_translators

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

See Solvers in the User Guide for the public API.

Per-solver option translation for linopy solver backends.

Translates common option names (time_limit, presolve, etc.) into the format each solver expects.

CPLEXOptionTranslator

Option translator for CPLEX solver.

Source code in src/odys/solvers/config_translators.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class CPLEXOptionTranslator:
    """Option translator for CPLEX solver."""

    def translate(self, config: SolverConfig) -> dict[str, Any]:
        """Translate common options to CPLEX solver format."""
        result: dict[str, Any] = {}
        if config.time_limit is not None:
            result["timelimit"] = config.time_limit
        if config.mip_rel_gap is not None:
            result["mip.tolerances.mipgap"] = config.mip_rel_gap
        if config.threads is not None:
            result["threads"] = config.threads
        if config.presolve is not None:
            result["preprocessing.presolve"] = 1 if config.presolve else 0
        result["mip.display"] = 0 if not config.log_output else 2
        result["output.clonelog"] = 1 if config.log_output else 0
        return result

translate(config)

Translate common options to CPLEX solver format.

Source code in src/odys/solvers/config_translators.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def translate(self, config: SolverConfig) -> dict[str, Any]:
    """Translate common options to CPLEX solver format."""
    result: dict[str, Any] = {}
    if config.time_limit is not None:
        result["timelimit"] = config.time_limit
    if config.mip_rel_gap is not None:
        result["mip.tolerances.mipgap"] = config.mip_rel_gap
    if config.threads is not None:
        result["threads"] = config.threads
    if config.presolve is not None:
        result["preprocessing.presolve"] = 1 if config.presolve else 0
    result["mip.display"] = 0 if not config.log_output else 2
    result["output.clonelog"] = 1 if config.log_output else 0
    return result

GurobiOptionTranslator

Option translator for Gurobi solver.

Source code in src/odys/solvers/config_translators.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class GurobiOptionTranslator:
    """Option translator for Gurobi solver."""

    def translate(self, config: SolverConfig) -> dict[str, Any]:
        """Translate common options to Gurobi solver format."""
        result: dict[str, Any] = {}
        if config.time_limit is not None:
            result["TimeLimit"] = config.time_limit
        if config.mip_rel_gap is not None:
            result["MIPGap"] = config.mip_rel_gap
        if config.threads is not None:
            result["Threads"] = config.threads
        if config.presolve is not None:
            result["Presolve"] = 1 if config.presolve else 0
        result["OutputFlag"] = 1 if config.log_output else 0
        return result

translate(config)

Translate common options to Gurobi solver format.

Source code in src/odys/solvers/config_translators.py
41
42
43
44
45
46
47
48
49
50
51
52
53
def translate(self, config: SolverConfig) -> dict[str, Any]:
    """Translate common options to Gurobi solver format."""
    result: dict[str, Any] = {}
    if config.time_limit is not None:
        result["TimeLimit"] = config.time_limit
    if config.mip_rel_gap is not None:
        result["MIPGap"] = config.mip_rel_gap
    if config.threads is not None:
        result["Threads"] = config.threads
    if config.presolve is not None:
        result["Presolve"] = 1 if config.presolve else 0
    result["OutputFlag"] = 1 if config.log_output else 0
    return result

HiGHSOptionTranslator

Option translator for HiGHS solver.

Source code in src/odys/solvers/config_translators.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class HiGHSOptionTranslator:
    """Option translator for HiGHS solver."""

    def translate(self, config: SolverConfig) -> dict[str, Any]:
        """Translate common options to HiGHS solver format."""
        result: dict[str, Any] = {}
        if config.time_limit is not None:
            result["time_limit"] = config.time_limit
        if config.mip_rel_gap is not None:
            result["mip_rel_gap"] = config.mip_rel_gap
        if config.threads is not None:
            result["threads"] = config.threads
        if config.presolve is not None:
            result["presolve"] = "on" if config.presolve else "off"
        result["output_flag"] = config.log_output
        return result

translate(config)

Translate common options to HiGHS solver format.

Source code in src/odys/solvers/config_translators.py
23
24
25
26
27
28
29
30
31
32
33
34
35
def translate(self, config: SolverConfig) -> dict[str, Any]:
    """Translate common options to HiGHS solver format."""
    result: dict[str, Any] = {}
    if config.time_limit is not None:
        result["time_limit"] = config.time_limit
    if config.mip_rel_gap is not None:
        result["mip_rel_gap"] = config.mip_rel_gap
    if config.threads is not None:
        result["threads"] = config.threads
    if config.presolve is not None:
        result["presolve"] = "on" if config.presolve else "off"
    result["output_flag"] = config.log_output
    return result

SCIPOptionTranslator

Option translator for SCIP solver.

Source code in src/odys/solvers/config_translators.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class SCIPOptionTranslator:
    """Option translator for SCIP solver."""

    def translate(self, config: SolverConfig) -> dict[str, Any]:
        """Translate common options to SCIP solver format."""
        result: dict[str, Any] = {}
        if config.time_limit is not None:
            result["limits/time"] = config.time_limit
        if config.mip_rel_gap is not None:
            result["limits/gap"] = config.mip_rel_gap
        if config.threads is not None:
            result["parallel/maxnthreads"] = config.threads
        if config.presolve is not None:
            result["presolving/maxrounds"] = -1 if config.presolve else 0
        result["display/verblevel"] = 4 if config.log_output else 0
        return result

translate(config)

Translate common options to SCIP solver format.

Source code in src/odys/solvers/config_translators.py
78
79
80
81
82
83
84
85
86
87
88
89
90
def translate(self, config: SolverConfig) -> dict[str, Any]:
    """Translate common options to SCIP solver format."""
    result: dict[str, Any] = {}
    if config.time_limit is not None:
        result["limits/time"] = config.time_limit
    if config.mip_rel_gap is not None:
        result["limits/gap"] = config.mip_rel_gap
    if config.threads is not None:
        result["parallel/maxnthreads"] = config.threads
    if config.presolve is not None:
        result["presolving/maxrounds"] = -1 if config.presolve else 0
    result["display/verblevel"] = 4 if config.log_output else 0
    return result

SolverOptionTranslator

Bases: Protocol

Protocol for translating common options to solver-specific format.

Source code in src/odys/solvers/config_translators.py
12
13
14
15
16
17
class SolverOptionTranslator(Protocol):
    """Protocol for translating common options to solver-specific format."""

    def translate(self, config: SolverConfig) -> dict[str, Any]:
        """Translate common options to solver-specific format."""
        ...

translate(config)

Translate common options to solver-specific format.

Source code in src/odys/solvers/config_translators.py
15
16
17
def translate(self, config: SolverConfig) -> dict[str, Any]:
    """Translate common options to solver-specific format."""
    ...

translate_solver_config(solver_config)

Translate common option names to solver-specific option names.

For known solvers (HiGHS, Gurobi, CPLEX, SCIP), applies the appropriate translation. Raw solver_options are applied last and override translated options with the same name.

Parameters:

Name Type Description Default
solver_config SolverConfig

Solver config

required

Returns:

Type Description
dict[str, Any]

Solver-specific options dict ready for linopy's Model.solve(**kwargs).

Source code in src/odys/solvers/config_translators.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def translate_solver_config(solver_config: SolverConfig) -> dict[str, Any]:
    """Translate common option names to solver-specific option names.

    For known solvers (HiGHS, Gurobi, CPLEX, SCIP), applies the
    appropriate translation. Raw ``solver_options`` are applied last and
    override translated options with the same name.

    Args:
        solver_config: Solver config

    Returns:
        Solver-specific options dict ready for linopy's ``Model.solve(**kwargs)``.
    """
    translator = {
        SolverName.HIGHS: HiGHSOptionTranslator(),
        SolverName.GUROBI: GurobiOptionTranslator(),
        SolverName.CPLEX: CPLEXOptionTranslator(),
        SolverName.SCIP: SCIPOptionTranslator(),
    }[solver_config.solver_name]
    return translator.translate(solver_config) | (solver_config.solver_options or {})