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

Commit 0a70975

Browse files
author
Matthias Koeppe
committed
default_mip_solver, get_solver: Handle default solver = a callable
1 parent 746a826 commit 0a70975

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

src/sage/numerical/backends/generic_backend.pyx

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,46 +1526,22 @@ cdef class GenericBackend:
15261526

15271527
default_solver = None
15281528

1529-
def default_mip_solver(solver = None):
1529+
def default_mip_solver(solver=None):
15301530
"""
1531-
Returns/Sets the default MILP Solver used by Sage
1531+
Returns/sets the default MILP solver used by Sage
15321532
15331533
INPUT:
15341534
1535-
- ``solver`` -- defines the solver to use:
1535+
- ``solver`` -- one of the following:
15361536
1537-
- GLPK (``solver="GLPK"``). See the `GLPK
1538-
<http://www.gnu.org/software/glpk/>`_ web site.
1539-
1540-
- GLPK's implementation of an exact rational simplex
1541-
method (``solver="GLPK/exact"``).
1542-
1543-
- COIN Branch and Cut (``solver="Coin"``). See the `COIN-OR
1544-
<http://www.coin-or.org>`_ web site.
1545-
1546-
- CPLEX (``solver="CPLEX"``). See the
1547-
`CPLEX <http://www.ilog.com/products/cplex/>`_ web site.
1548-
1549-
- CVXOPT (``solver="CVXOPT"``). See the `CVXOPT
1550-
<http://cvxopt.org/>`_ web site.
1551-
1552-
- Gurobi (``solver="Gurobi"``). See the `Gurobi
1553-
<http://www.gurobi.com/>`_ web site.
1554-
1555-
- PPL (``solver="PPL"``). See the `PPL
1556-
<http://bugseng.com/products/ppl/>`_ web site. This solver is
1557-
an exact rational solver.
1537+
- a string indicating one of the available solvers
1538+
(see :class:`MixedIntegerLinearProgram`);
15581539
1559-
- ``InteractiveLPProblem`` (``solver="InteractiveLP"``). A didactical
1560-
implementation of the revised simplex method in Sage. It works over
1561-
any exact ordered field, the default is ``QQ``.
1540+
- a callable (typically a subclass of
1541+
:class:`sage.numerical.backends.generic_backend.GenericBackend`);
15621542
1563-
``solver`` should then be equal to one of ``"GLPK"``,
1564-
``"Coin"``, ``"CPLEX"``, ``"CVXOPT"``, ``"Gurobi"``, ``"PPL"`, or
1565-
``"InteractiveLP"``,
1566-
1567-
- If ``solver=None`` (default), the current default solver's name is
1568-
returned.
1543+
- ``None`` (default), in which case the current default solver
1544+
is returned; this is either a string or a callable.
15691545
15701546
OUTPUT:
15711547
@@ -1608,6 +1584,10 @@ def default_mip_solver(solver = None):
16081584
except ValueError:
16091585
pass
16101586

1587+
if callable(solver):
1588+
default_solver = solver
1589+
return
1590+
16111591
solver = solver.capitalize()
16121592

16131593
if solver == "Cplex":
@@ -1652,7 +1632,7 @@ def default_mip_solver(solver = None):
16521632
default_solver = solver
16531633

16541634
else:
1655-
raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'CVXOPT', 'Gurobi', 'PPL', 'InteractiveLP', or None.")
1635+
raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'CVXOPT', 'Gurobi', 'PPL', 'InteractiveLP', a callable, or None.")
16561636

16571637
cpdef GenericBackend get_solver(constraint_generation = False, solver = None, base_ring = None):
16581638
"""
@@ -1759,6 +1739,18 @@ cpdef GenericBackend get_solver(constraint_generation = False, solver = None, ba
17591739
sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4,solver=glpk_exact_solver) # long time
17601740
8
17611741
1742+
TESTS:
1743+
1744+
Test that it works when the default solver is a callable, see :trac:`28914`::
1745+
1746+
sage: old_default = default_mip_solver()
1747+
sage: from sage.numerical.backends.glpk_backend import GLPKBackend
1748+
sage: default_mip_solver(GLPKBackend)
1749+
sage: M = MixedIntegerLinearProgram() # indirect doctest
1750+
sage: M.get_backend()
1751+
<...GLPKBackend...>
1752+
sage: default_mip_solver(old_default)
1753+
17621754
"""
17631755
if solver is None:
17641756

@@ -1779,7 +1771,7 @@ cpdef GenericBackend get_solver(constraint_generation = False, solver = None, ba
17791771
if solver == "Coin" and constraint_generation:
17801772
solver = "Glpk"
17811773

1782-
elif callable(solver):
1774+
if callable(solver):
17831775
kwds = {}
17841776
if base_ring is not None:
17851777
kwds['base_ring']=base_ring

0 commit comments

Comments
 (0)