Skip to content

Commit 0b2a208

Browse files
daniel-rdtpre-commit-ci[bot]lkstrp
authored
add check for solver installation when initializing solver class instances (#377)
* add check for solver installation when initializing solver class instance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor check for solver installation using available_solvers and enum * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review Co-authored-by: Lukas Trippe <[email protected]> * Apply suggestions from code review --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lukas Trippe <[email protected]>
1 parent e314847 commit 0b2a208

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

linopy/solvers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ def __init__(
209209
):
210210
self.solver_options = solver_options
211211

212+
# Check for the solver to be initialized whether the package is installed or not.
213+
if self.solver_name.value not in available_solvers:
214+
msg = f"Solver package for '{self.solver_name.value}' is not installed. Please install first to initialize solver instance."
215+
raise ImportError(msg)
216+
212217
def safe_get_solution(self, status: Status, func: Callable) -> Solution:
213218
"""
214219
Get solution from function call, if status is unknown still try to run it.
@@ -301,6 +306,10 @@ def solve_problem(
301306
msg = "No problem file or model specified."
302307
raise ValueError(msg)
303308

309+
@property
310+
def solver_name(self) -> SolverName:
311+
return SolverName[self.__class__.__name__]
312+
304313

305314
class CBC(Solver):
306315
"""

0 commit comments

Comments
 (0)