Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
sage.doctest.external.has_{cplex,gurobi}: Refactor through new Features
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Oct 7, 2021
1 parent 9ee3243 commit 50248ed
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/sage/databases/cremona.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,13 +1699,13 @@ def CremonaDatabase(name=None,mini=None,set_global=None):
sage: c = CremonaDatabase('should not exist',mini=True)
Traceback (most recent call last):
...
FeatureNotPresentError: Cremona's database of elliptic curves is not available.
FeatureNotPresentError: database_should_not_exist_ellcurve is not available.
'...db' not found in any of [...]
...Further installation instructions might be available at https://github.com/JohnCremona/ecdata.
sage: c = CremonaDatabase('should not exist',mini=False)
Traceback (most recent call last):
...
FeatureNotPresentError: Cremona's database of elliptic curves is not available.
FeatureNotPresentError: database_should_not_exist_ellcurve is not available.
'...db' not found in any of [...]
...Further installation instructions might be available at https://github.com/JohnCremona/ecdata.
"""
Expand Down
20 changes: 6 additions & 14 deletions src/sage/doctest/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,10 @@ def has_cplex():
sage: from sage.doctest.external import has_cplex
sage: has_cplex() # random, optional - CPLEX
True
FeatureTestResult('cplex', True)
"""
from sage.numerical.mip import MixedIntegerLinearProgram
try:
MixedIntegerLinearProgram(solver='cplex')
return True
except Exception:
return False
from sage.features.mip_backends import CPLEX
return CPLEX().is_present()

def has_gurobi():
"""
Expand All @@ -238,14 +234,10 @@ def has_gurobi():
sage: from sage.doctest.external import has_gurobi
sage: has_gurobi() # random, optional - Gurobi
True
FeatureTestResult('gurobi', True)
"""
from sage.numerical.mip import MixedIntegerLinearProgram
try:
MixedIntegerLinearProgram(solver='gurobi')
return True
except Exception:
return False
from sage.features.mip_backends import Gurobi
return Gurobi().is_present()

def has_graphviz():
"""
Expand Down
38 changes: 38 additions & 0 deletions src/sage/features/mip_backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from . import Feature
from join_feature import JoinFeature


class MIPBackend(Feature):
r"""
A feature describing whether a :class:`MixedIntegerLinearProgram` backend is available.
"""

def _is_present(self):
try:
from sage.numerical.mip import MixedIntegerLinearProgram
MixedIntegerLinearProgram(solver=self.name)
return FeatureTestResult(self, True)
except Exception:
return FeatureTestResult(self, False)


class CPLEX(MIPBackend):

def __init__(self):
MIPBackend.__init__('cplex',
spkg='sage_numerical_backends_cplex')


class Gurobi(MIPBackend):

def __init__(self):
MIPBackend.__init__('gurobi',
spkg='sage_numerical_backends_gurobi')


class COIN(JoinFeature):

def __init__(self):
JoinFeature.__init__('sage_numerical_backends_coin',
[MIPBackend('coin')],
spkg='sage_numerical_backends_coin')

0 comments on commit 50248ed

Please sign in to comment.