|
| 1 | +""" |
| 2 | +Linopy module for solver capability tracking. |
| 3 | +
|
| 4 | +This module provides a centralized registry of solver capabilities, |
| 5 | +replacing scattered hardcoded checks throughout the codebase. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from dataclasses import dataclass |
| 11 | +from enum import Enum, auto |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from collections.abc import Sequence |
| 16 | + |
| 17 | + |
| 18 | +class SolverFeature(Enum): |
| 19 | + """Enumeration of all solver capabilities tracked by linopy.""" |
| 20 | + |
| 21 | + # Objective function support |
| 22 | + QUADRATIC_OBJECTIVE = auto() |
| 23 | + |
| 24 | + # I/O capabilities |
| 25 | + LP_FILE_NAMES = auto() # Support for named variables/constraints in LP files |
| 26 | + SOLUTION_FILE_NOT_NEEDED = auto() # Solver doesn't need a solution file |
| 27 | + |
| 28 | + # Advanced features |
| 29 | + IIS_COMPUTATION = auto() # Irreducible Infeasible Set computation |
| 30 | + |
| 31 | + # Solver-specific |
| 32 | + SOLVER_ATTRIBUTE_ACCESS = auto() # Direct access to solver variable attributes |
| 33 | + |
| 34 | + |
| 35 | +@dataclass(frozen=True) |
| 36 | +class SolverInfo: |
| 37 | + """Information about a solver's capabilities.""" |
| 38 | + |
| 39 | + name: str |
| 40 | + features: frozenset[SolverFeature] |
| 41 | + display_name: str = "" |
| 42 | + |
| 43 | + def __post_init__(self) -> None: |
| 44 | + if not self.display_name: |
| 45 | + object.__setattr__(self, "display_name", self.name.upper()) |
| 46 | + |
| 47 | + def supports(self, feature: SolverFeature) -> bool: |
| 48 | + """Check if this solver supports a given feature.""" |
| 49 | + return feature in self.features |
| 50 | + |
| 51 | + |
| 52 | +# Define all solver capabilities |
| 53 | +SOLVER_REGISTRY: dict[str, SolverInfo] = { |
| 54 | + "gurobi": SolverInfo( |
| 55 | + name="gurobi", |
| 56 | + display_name="Gurobi", |
| 57 | + features=frozenset( |
| 58 | + { |
| 59 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 60 | + SolverFeature.LP_FILE_NAMES, |
| 61 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 62 | + SolverFeature.IIS_COMPUTATION, |
| 63 | + SolverFeature.SOLVER_ATTRIBUTE_ACCESS, |
| 64 | + } |
| 65 | + ), |
| 66 | + ), |
| 67 | + "highs": SolverInfo( |
| 68 | + name="highs", |
| 69 | + display_name="HiGHS", |
| 70 | + features=frozenset( |
| 71 | + { |
| 72 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 73 | + SolverFeature.LP_FILE_NAMES, |
| 74 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 75 | + } |
| 76 | + ), |
| 77 | + ), |
| 78 | + "glpk": SolverInfo( |
| 79 | + name="glpk", |
| 80 | + display_name="GLPK", |
| 81 | + features=frozenset(), # No LP_FILE_NAMES support |
| 82 | + ), |
| 83 | + "cbc": SolverInfo( |
| 84 | + name="cbc", |
| 85 | + display_name="CBC", |
| 86 | + features=frozenset(), # No LP_FILE_NAMES support |
| 87 | + ), |
| 88 | + "cplex": SolverInfo( |
| 89 | + name="cplex", |
| 90 | + display_name="CPLEX", |
| 91 | + features=frozenset( |
| 92 | + { |
| 93 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 94 | + SolverFeature.LP_FILE_NAMES, |
| 95 | + } |
| 96 | + ), |
| 97 | + ), |
| 98 | + "xpress": SolverInfo( |
| 99 | + name="xpress", |
| 100 | + display_name="FICO Xpress", |
| 101 | + features=frozenset( |
| 102 | + { |
| 103 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 104 | + SolverFeature.LP_FILE_NAMES, |
| 105 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 106 | + SolverFeature.IIS_COMPUTATION, |
| 107 | + } |
| 108 | + ), |
| 109 | + ), |
| 110 | + "scip": SolverInfo( |
| 111 | + name="scip", |
| 112 | + display_name="SCIP", |
| 113 | + features=frozenset( |
| 114 | + { |
| 115 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 116 | + SolverFeature.LP_FILE_NAMES, |
| 117 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 118 | + } |
| 119 | + ), |
| 120 | + ), |
| 121 | + "mosek": SolverInfo( |
| 122 | + name="mosek", |
| 123 | + display_name="MOSEK", |
| 124 | + features=frozenset( |
| 125 | + { |
| 126 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 127 | + SolverFeature.LP_FILE_NAMES, |
| 128 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 129 | + } |
| 130 | + ), |
| 131 | + ), |
| 132 | + "copt": SolverInfo( |
| 133 | + name="copt", |
| 134 | + display_name="COPT", |
| 135 | + features=frozenset( |
| 136 | + { |
| 137 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 138 | + SolverFeature.LP_FILE_NAMES, |
| 139 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 140 | + } |
| 141 | + ), |
| 142 | + ), |
| 143 | + "mindopt": SolverInfo( |
| 144 | + name="mindopt", |
| 145 | + display_name="MindOpt", |
| 146 | + features=frozenset( |
| 147 | + { |
| 148 | + SolverFeature.QUADRATIC_OBJECTIVE, |
| 149 | + SolverFeature.LP_FILE_NAMES, |
| 150 | + SolverFeature.SOLUTION_FILE_NOT_NEEDED, |
| 151 | + } |
| 152 | + ), |
| 153 | + ), |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +def solver_supports(solver_name: str, feature: SolverFeature) -> bool: |
| 158 | + """ |
| 159 | + Check if a solver supports a given feature. |
| 160 | +
|
| 161 | + Parameters |
| 162 | + ---------- |
| 163 | + solver_name : str |
| 164 | + Name of the solver (e.g., "gurobi", "highs") |
| 165 | + feature : SolverFeature |
| 166 | + The feature to check for |
| 167 | +
|
| 168 | + Returns |
| 169 | + ------- |
| 170 | + bool |
| 171 | + True if the solver supports the feature, False otherwise. |
| 172 | + Returns False for unknown solvers. |
| 173 | + """ |
| 174 | + if solver_name not in SOLVER_REGISTRY: |
| 175 | + return False |
| 176 | + return SOLVER_REGISTRY[solver_name].supports(feature) |
| 177 | + |
| 178 | + |
| 179 | +def get_solvers_with_feature(feature: SolverFeature) -> list[str]: |
| 180 | + """ |
| 181 | + Get all solvers that support a given feature. |
| 182 | +
|
| 183 | + Parameters |
| 184 | + ---------- |
| 185 | + feature : SolverFeature |
| 186 | + The feature to filter by |
| 187 | +
|
| 188 | + Returns |
| 189 | + ------- |
| 190 | + list[str] |
| 191 | + List of solver names supporting the feature |
| 192 | + """ |
| 193 | + return [name for name, info in SOLVER_REGISTRY.items() if info.supports(feature)] |
| 194 | + |
| 195 | + |
| 196 | +def get_available_solvers_with_feature( |
| 197 | + feature: SolverFeature, available_solvers: Sequence[str] |
| 198 | +) -> list[str]: |
| 199 | + """ |
| 200 | + Get installed solvers that support a given feature. |
| 201 | +
|
| 202 | + Parameters |
| 203 | + ---------- |
| 204 | + feature : SolverFeature |
| 205 | + The feature to filter by |
| 206 | + available_solvers : Sequence[str] |
| 207 | + List of currently available/installed solvers |
| 208 | +
|
| 209 | + Returns |
| 210 | + ------- |
| 211 | + list[str] |
| 212 | + List of installed solver names supporting the feature |
| 213 | + """ |
| 214 | + return [s for s in get_solvers_with_feature(feature) if s in available_solvers] |
0 commit comments