Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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_scip import SCIPSolver # noqa 

48from .solver_smcp import SMCPSolver # noqa 

49 

50 

51# Map solver names to their implementation classes. 

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

53 CPLEXSolver, 

54 CVXOPTSolver, 

55 ECOSSolver, 

56 GLPKSolver, 

57 GurobiSolver, 

58 MOSEKSolver, 

59 MOSEKFusionSolver, 

60 SCIPSolver, 

61 SMCPSolver 

62)} 

63 

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

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

66 

67 

68def get_solver(name): 

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

70 return _solvers[name] 

71 

72 

73def get_solver_name(solver): 

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

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

76 if isinstance(solver, solverClass): 

77 return name 

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

79 

80 

81def all_solvers(): 

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

83 return _solvers.copy() 

84 

85 

86def available_solvers(problem=None): 

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

88 

89 :param problem: DEPRECATED 

90 """ 

91 if problem is not None: 

92 throw_deprecation_warning( 

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

94 

95 return sorted( 

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

97 

98 

99# -------------------------------------- 

100__all__ = api_end(_API_START, globals())