Coverage for picos/solvers/__init__.py: 93.55%

31 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-26 07:46 +0000

1# ------------------------------------------------------------------------------ 

2# Copyright (C) 2017-2018 Maximilian Stahlberg 

3# 

4# This file is part of PICOS. 

5# 

6# PICOS is free software: you can redistribute it and/or modify it under the 

7# terms of the GNU General Public License as published by the Free Software 

8# Foundation, either version 3 of the License, or (at your option) any later 

9# version. 

10# 

11# PICOS is distributed in the hope that it will be useful, but WITHOUT ANY 

12# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 

13# A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

14# 

15# You should have received a copy of the GNU General Public License along with 

16# this program. If not, see <http://www.gnu.org/licenses/>. 

17# ------------------------------------------------------------------------------ 

18 

19"""Optimization solver interfaces. 

20 

21This package contains the interfaces to the optimization solvers that PICOS uses 

22as its backend. You do not need to instanciate any of the solver classes 

23directly; if you want to select a particular solver, it is most convenient to 

24name it to :meth:`~.problem.Problem.solve` via the ``solver`` keyword argument. 

25""" 

26 

27from ..apidoc import api_end, api_start 

28from ..legacy import throw_deprecation_warning 

29 

30 

31_API_START = api_start(globals()) 

32# ------------------------------- 

33 

34# Import the solver base class and exceptions. 

35from .solver import (Solver, SolverError, ProblemUpdateError, # noqa 

36 OptionError, UnsupportedOptionError, 

37 ConflictingOptionsError, DependentOptionError, OptionValueError) 

38 

39# Import all solvers. 

40from .solver_cplex import CPLEXSolver # noqa 

41from .solver_cvxopt import CVXOPTSolver # noqa 

42from .solver_ecos import ECOSSolver # noqa 

43from .solver_glpk import GLPKSolver # noqa 

44from .solver_gurobi import GurobiSolver # noqa 

45from .solver_mosek import MOSEKSolver # noqa 

46from .solver_mskfsn import MOSEKFusionSolver # noqa 

47from .solver_osqp import OSQPSolver # noqa 

48from .solver_scip import SCIPSolver # noqa 

49from .solver_smcp import SMCPSolver # noqa 

50 

51 

52# Map solver names to their implementation classes. 

53_solvers = {solver.names()[0]: solver for solver in ( 

54 CPLEXSolver, 

55 CVXOPTSolver, 

56 ECOSSolver, 

57 GLPKSolver, 

58 GurobiSolver, 

59 MOSEKSolver, 

60 MOSEKFusionSolver, 

61 OSQPSolver, 

62 SCIPSolver, 

63 SMCPSolver 

64)} 

65 

66# Make sure all solvers inherit from their abstract base class. 

67assert all(issubclass(solver, Solver) for solver in _solvers.values()) 

68 

69 

70def get_solver(name): 

71 """Return the implementation class of the solver with the given name.""" 

72 return _solvers[name] 

73 

74 

75def get_solver_name(solver): 

76 """Return the registry name of a solver instance.""" 

77 for name, solverClass in _solvers.items(): 

78 if isinstance(solver, solverClass): 

79 return name 

80 raise LookupError("The given object's type is not regisered as a solver.") 

81 

82 

83def all_solvers(): 

84 """Return a dictionary mapping solver names to implementation classes.""" 

85 return _solvers.copy() 

86 

87 

88def available_solvers(problem=None): 

89 """Return a sorted list of names of available solvers. 

90 

91 :param problem: DEPRECATED 

92 """ 

93 if problem is not None: 

94 throw_deprecation_warning( 

95 "Arguments 'problem' to picos.available_solvers is deprecated.") 

96 

97 return sorted( 

98 name for name, solver in _solvers.items() if solver.available()) 

99 

100 

101# -------------------------------------- 

102__all__ = api_end(_API_START, globals())