Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions cpmpy/solvers/TEMPLATE.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@

from typing import Optional
import warnings
import pkg_resources
from pkg_resources import VersionConflict
from packaging.version import Version

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..expressions.core import Expression, Comparison, Operator
Expand Down Expand Up @@ -81,14 +80,14 @@ def supported():
try:
import TEMPLATEpy as gp
# optionally enforce a specific version
pkg_resources.require("TEMPLATEpy>=2.1.0")
tpl_version = CPM_template.version()
if Version(tpl_version) < Version("2.1.0"):
warnings.warn(f"CPMpy uses features only available from TEMPLATEpy version 0.2.1, "
f"but you have version {tpl_version}.")
return False
return True
except ModuleNotFoundError: # if solver's Python package is not installed
return False
except VersionConflict: # unsupported version of TEMPLATEpy (optional)
warnings.warn(f"CPMpy uses features only available from TEMPLATEpy version 0.2.1, "
f"but you have version {pkg_resources.get_distribution('TEMPLATEpy').version}.")
return False
except Exception as e:
raise e

Expand All @@ -97,9 +96,10 @@ def version(cls) -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('TEMPLATEpy').version
except pkg_resources.DistributionNotFound:
return version('TEMPLATEpy')
except PackageNotFoundError:
return None

# [GUIDELINE] If your solver supports different subsolvers, implement below method to return a list of subsolver names
Expand Down
18 changes: 9 additions & 9 deletions cpmpy/solvers/choco.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@
from typing import Optional

import numpy as np
from packaging.version import Version

import warnings
import pkg_resources
from pkg_resources import VersionConflict

from ..transformations.normalize import toplevel_list
from .solver_interface import SolverInterface, SolverStatus, ExitStatus
Expand Down Expand Up @@ -89,16 +88,16 @@ def supported():
try:
# check if pychoco is installed
import pychoco as chc
chc_version = CPM_choco.version()
# check it's the correct version
# CPMPy uses features only available from 0.2.1
pkg_resources.require("pychoco>=0.2.1")
if Version(chc_version) < Version("0.2.1"):
warnings.warn(f"CPMpy uses features only available from Pychoco version 0.2.1, "
f"but you have version {chc_version}.")
return False
return True
except ModuleNotFoundError:
return False
except VersionConflict: # unsupported version of pychoco
warnings.warn(f"CPMpy uses features only available from Pychoco version 0.2.1, "
f"but you have version {pkg_resources.get_distribution('pychoco').version}.")
return False
except Exception as e:
raise e

Expand All @@ -107,9 +106,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('pychoco').version
except pkg_resources.DistributionNotFound:
return version('pychoco')
except PackageNotFoundError:
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
9 changes: 5 additions & 4 deletions cpmpy/solvers/cplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ def version() -> Optional[str]:

Two version numbers get returned: ``<docplex version>/<solver version>``
"""
from importlib.metadata import version, PackageNotFoundError
try:
import pkg_resources
import cplex
cpx = cplex.Cplex()
return f"{pkg_resources.get_distribution('docplex').version}/{cpx.get_version()}"
except (pkg_resources.DistributionNotFound, ModuleNotFoundError):
cplex_version = cplex.Cplex().get_version()
docplex_version = version("docplex")
return f"{docplex_version}/{cplex_version}"
except (PackageNotFoundError, ModuleNotFoundError):
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
10 changes: 5 additions & 5 deletions cpmpy/solvers/cpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@
CPM_cpo
"""

import time
from typing import Optional
import warnings
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from .. import DirectConstraint
Expand Down Expand Up @@ -117,11 +115,13 @@ def version() -> Optional[str]:

For CPO, two version numbers get returned: ``<docplex version>/<solver version>``
"""
from importlib.metadata import version, PackageNotFoundError
try:
import docplex.cp as docp
s = docp.solver.solver.CpoSolver(docp.model.CpoModel())
return f"{pkg_resources.get_distribution('docplex').version}/{s.get_solver_version()}"
except (pkg_resources.DistributionNotFound, ModuleNotFoundError):
cpo_version = docp.solver.solver.CpoSolver(docp.model.CpoModel()).get_solver_version()
docplex_version = version("docplex")
return f"{docplex_version}/{cpo_version}"
except (PackageNotFoundError, ModuleNotFoundError):
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
22 changes: 9 additions & 13 deletions cpmpy/solvers/exact.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
import time
from typing import Optional

import pkg_resources
from pkg_resources import VersionConflict
from packaging.version import Version

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..expressions.core import *
Expand Down Expand Up @@ -90,16 +89,14 @@ def supported():
try:
# check if exact is installed
import exact
# check installed version
pkg_resources.require("exact>=2.1.0")
xct_version = CPM_exact.version()
if Version(xct_version) < Version("2.1.0"):
warnings.warn(f"CPMpy requires Exact version >=2.1.0 is required but you have version "
f"{xct_version}, beware exact>=2.1.0 requires Python 3.10 or higher.")
return False
return True
except ModuleNotFoundError: # exact is not installed
return False
except VersionConflict: # unsupported version of exact
warnings.warn(f"CPMpy requires Exact version >=2.1.0 is required but you have version "
f"{pkg_resources.get_distribution('exact').version}, beware exact>=2.1.0 requires "
f"Python 3.10 or higher.")
return False
except Exception as e:
raise e

Expand All @@ -108,9 +105,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('exact').version
except pkg_resources.DistributionNotFound:
return version('exact')
except PackageNotFoundError:
return None


Expand Down Expand Up @@ -563,8 +561,6 @@ def add(self, cpm_expr_orig):
assert isinstance(lhs, Operator)
# can be sum, wsum or mul
if lhs.name == "mul":
assert pkg_resources.require("exact>=2.1.0"), f"Multiplication constraint {cpm_expr} " \
f"only supported by Exact version 2.1.0 and above"
if is_num(rhs): # make dummy var
rhs = cp.intvar(rhs, rhs)
xct_rhs = self.solver_var(rhs)
Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
CPM_gcs
"""
from typing import Optional
import pkg_resources

from cpmpy.transformations.comparison import only_numexpr_equality
from cpmpy.transformations.reification import reify_rewrite, only_bv_reifies
Expand Down Expand Up @@ -107,9 +106,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('gcspy').version
except pkg_resources.DistributionNotFound:
return version('gcspy')
except PackageNotFoundError:
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"""

from typing import Optional
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import NotSupportedError
Expand Down Expand Up @@ -117,9 +116,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('gurobipy').version
except pkg_resources.DistributionNotFound:
return version("gurobipy")
except PackageNotFoundError:
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
3 changes: 1 addition & 2 deletions cpmpy/solvers/hexaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@

from typing import Optional

from importlib.metadata import version, PackageNotFoundError

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..expressions.core import Expression, Comparison, Operator, BoolVal
from ..expressions.globalconstraints import GlobalConstraint, GlobalFunction, DirectConstraint
Expand Down Expand Up @@ -81,6 +79,7 @@ def version(cls) -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return version('hexaly')
except PackageNotFoundError:
Expand Down
8 changes: 5 additions & 3 deletions cpmpy/solvers/minizinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
from datetime import timedelta # for mzn's timeout

import numpy as np
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import MinizincNameException, MinizincBoundsException
Expand Down Expand Up @@ -225,10 +224,13 @@ def version() -> Optional[str]:

For Minizinc, two version numbers get returned: ``<minizinc python API version>/<minizinc driver version>``
"""
from importlib.metadata import version, PackageNotFoundError
try:
from minizinc import default_driver
return f"{pkg_resources.get_distribution('minizinc').version}/{'.'.join(str(a) for a in default_driver.parsed_version)}"
except (pkg_resources.DistributionNotFound, ModuleNotFoundError):
mzn_version = version("minizinc")
solver_version = '.'.join(str(a) for a in default_driver.parsed_version)
return f"{mzn_version}/{solver_version}"
except (PackageNotFoundError, ModuleNotFoundError):
return None

# variable name can not be any of these keywords
Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/ortools.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import sys
from typing import Optional # for stdout checking
import numpy as np
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import NotSupportedError
Expand Down Expand Up @@ -96,9 +95,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('ortools').version
except pkg_resources.DistributionNotFound:
return version('ortools')
except PackageNotFoundError:
return None


Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/pindakaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"""

import time
import pkg_resources
from datetime import timedelta
from typing import Optional

Expand Down Expand Up @@ -89,9 +88,10 @@ def supported():
@staticmethod
def version() -> Optional[str]:
"""Return the installed version of the solver's Python API."""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution("pindakaas").version
except pkg_resources.DistributionNotFound:
return version('pindakaas')
except PackageNotFoundError:
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
8 changes: 8 additions & 0 deletions cpmpy/solvers/pumpkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
Module details
==============
"""
import warnings
from typing import Optional
from importlib.metadata import version, PackageNotFoundError
from os.path import join

import numpy as np
from packaging.version import Version

from cpmpy.exceptions import NotSupportedError
from .solver_interface import SolverInterface, SolverStatus, ExitStatus
Expand Down Expand Up @@ -75,6 +77,11 @@ def supported():
# try to import the package
try:
import pumpkin_solver as psp
pum_version = CPM_pumpkin.version()
if Version(pum_version) < Version("0.2.2"):
warnings.warn(f"CPMpy uses features only available from Pumpkin version >=0.2.2 "
f"but you have version {pum_version}")
return False
return True
except ModuleNotFoundError:
return False
Expand All @@ -87,6 +94,7 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return version('pumpkin-solver')
except PackageNotFoundError:
Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/pysat.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
from threading import Timer
from typing import Optional
import warnings
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import NotSupportedError
Expand Down Expand Up @@ -162,9 +161,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('python-sat').version
except pkg_resources.DistributionNotFound:
return version('python-sat')
except PackageNotFoundError:
return None

def __init__(self, cpm_model=None, subsolver=None):
Expand Down
6 changes: 3 additions & 3 deletions cpmpy/solvers/pysdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"""
from functools import reduce
from typing import Optional
import pkg_resources

from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import NotSupportedError
Expand Down Expand Up @@ -89,9 +88,10 @@ def version() -> Optional[str]:
"""
Returns the installed version of the solver's Python API.
"""
from importlib.metadata import version, PackageNotFoundError
try:
return pkg_resources.get_distribution('pysdd').version
except pkg_resources.DistributionNotFound:
return version('pysdd')
except PackageNotFoundError:
return None


Expand Down
Loading
Loading