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
56 changes: 56 additions & 0 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import contextlib
import enum
import importlib.util
import io
import logging
import os
Expand Down Expand Up @@ -301,6 +302,15 @@
msg = "No problem file or model specified."
raise ValueError(msg)

def check_solver_installation(self, package_name: str):
"""
Check for the solver to be initialized whether the package is installed or not.
Gives an ImportError if that is not the case.
"""
if importlib.util.find_spec(package_name) is None:
msg = f"Solver package '{package_name}' is not installed. Please install first to initialize solver instance."
raise ImportError(msg)

Check warning on line 312 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L311-L312

Added lines #L311 - L312 were not covered by tests


class CBC(Solver):
"""
Expand All @@ -312,12 +322,23 @@
options for the given solver
"""

package_name = "coin-or-cbc"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def check_solver_installation(self, package_name):
if (
sub.run([which, "cbc"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode
!= 0
):
msg = f"Solver package '{package_name}' is not installed. Please install first to initialize solver instance."
raise ImportError(msg)

Check warning on line 340 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L339-L340

Added lines #L339 - L340 were not covered by tests

def solve_problem_from_model(
self,
model: Model,
Expand Down Expand Up @@ -469,12 +490,23 @@
options for the given solver
"""

package_name = "glpk"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def check_solver_installation(self, package_name):
if (
sub.run([which, "glpsol"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode
!= 0
):
msg = f"Solver package '{package_name}' is not installed. Please install first to initialize solver instance."
raise ImportError(msg)

Check warning on line 508 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L507-L508

Added lines #L507 - L508 were not covered by tests

def solve_problem_from_model(
self,
model: Model,
Expand Down Expand Up @@ -651,10 +683,13 @@
options for the given solver
"""

package_name = "highspy"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -868,10 +903,13 @@
options for the given solver
"""

package_name = "gurobipy"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1100,10 +1138,13 @@
options for the given solver
"""

package_name = "cplex"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1242,10 +1283,13 @@
options for the given solver
"""

package_name = "pyscipopt"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1383,10 +1427,13 @@
options for the given solver
"""

package_name = "xpress"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1528,10 +1575,13 @@
options for the given solver
"""

package_name = "mosek"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)

Check warning on line 1584 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L1584

Added line #L1584 was not covered by tests
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1850,10 +1900,13 @@
options for the given solver
"""

package_name = "coptpy"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down Expand Up @@ -1992,10 +2045,13 @@
options for the given solver
"""

package_name = "mindoptpy"

def __init__(
self,
**solver_options,
):
self.check_solver_installation(self.package_name)
super().__init__(**solver_options)

def solve_problem_from_model(
Expand Down