Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check for solver installation when initializing solver class instances #377

Merged
merged 6 commits into from
Nov 15, 2024
9 changes: 9 additions & 0 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@
):
self.solver_options = solver_options

# Check for the solver to be initialized whether the package is installed or not.
if self.solver_name.value not in available_solvers:
msg = f"Solver package for '{self.solver_name.value}' is not installed. Please install first to initialize solver instance."
raise ImportError(msg)

Check warning on line 215 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L214-L215

Added lines #L214 - L215 were not covered by tests

def safe_get_solution(self, status: Status, func: Callable) -> Solution:
"""
Get solution from function call, if status is unknown still try to run it.
Expand Down Expand Up @@ -301,6 +306,10 @@
msg = "No problem file or model specified."
raise ValueError(msg)

@property
def solver_name(self) -> SolverName:
return SolverName[self.__class__.__name__]


class CBC(Solver):
"""
Expand Down