Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
EXP_CAPABLE = True
MIP_CAPABLE = False
# EXITCODES from ECOS
# ECOS_OPTIMAL (0) Problem solved to optimality
# ECOS_PINF (1) Found certificate of primal infeasibility
# ECOS_DINF (2) Found certificate of dual infeasibility
# ECOS_INACC_OFFSET (10) Offset exitflag at inaccurate results
# ECOS_MAXIT (-1) Maximum number of iterations reached
# ECOS_NUMERICS (-2) Search direction unreliable
# ECOS_OUTCONE (-3) s or z got outside the cone, numerics?
# ECOS_SIGINT (-4) solver interrupted by a signal/ctrl-c
# ECOS_FATAL (-7) Unknown problem in solver
# Map of ECOS status to CVXPY status.
STATUS_MAP = {0: s.OPTIMAL,
1: s.INFEASIBLE,
2: s.UNBOUNDED,
10: s.OPTIMAL_INACCURATE,
11: s.INFEASIBLE_INACCURATE,
12: s.UNBOUNDED_INACCURATE,
-1: s.SOLVER_ERROR,
-2: s.SOLVER_ERROR,
-3: s.SOLVER_ERROR,
-4: s.SOLVER_ERROR,
-7: s.SOLVER_ERROR}
def import_solver(self):
"""Imports the solver.
"""
import ecos
ecos # For flake8
def ufunc(x, y):
if isinstance(y, np.ndarray) and y.ndim > 0 \
and y[0] is s.NP_EQUAL_STR:
raise Exception("Prevent Numpy equal ufunc.")
return getattr(np, name)(x, y)
return ufunc
G = G.tocsr()
G_leq = G[:dims[s.LEQ_DIM], :]
h_leq = h[:dims[s.LEQ_DIM]].ravel()
G_other = G[dims[s.LEQ_DIM]:, :]
h_other = h[dims[s.LEQ_DIM]:].ravel()
G_leq, h_leq, P_leq = compress_matrix(G_leq, h_leq)
dims[s.LEQ_DIM] = int(h_leq.shape[0])
data["P_leq"] = intf.sparse2cvxopt(P_leq)
G = sp.vstack([G_leq, G_other])
h = np.hstack([h_leq, h_other])
# Convert A, b, G, h to CVXOPT matrices.
data[s.A] = A
data[s.G] = G
data[s.B] = b
data[s.H] = h
return s.OPTIMAL
def __init__(self, shape=(), name=None, value=None, id=None, **kwargs):
if id is None:
self.id = lu.get_id()
else:
self.id = id
if name is None:
self._name = "%s%d" % (s.PARAM_PREFIX, self.id)
else:
self._name = name
# Initialize with value if provided.
self._value = None
super(Parameter, self).__init__(shape, value, **kwargs)
self._is_constant = True
Parameters
----------
constraints : list
A list of constraints.
Returns
-------
dict
A map of type key to an ordered set of constraints.
"""
constr_map = {s.EQ: [],
s.LEQ: [],
s.SOC: [],
s.PSD: [],
s.EXP: [],
s.BOOL: [],
s.INT: []}
for c in constraints:
if isinstance(c, lo.LinEqConstr):
constr_map[s.EQ].append(c)
elif isinstance(c, lo.LinLeqConstr):
constr_map[s.LEQ].append(c)
elif isinstance(c, SOC):
constr_map[s.SOC].append(c)
elif isinstance(c, PSD):
constr_map[s.PSD].append(c)
elif isinstance(c, ExpCone):
constr_map[s.EXP].append(c)
return constr_map
dims : dict
The dimensions of the cones.
var_offsets : dict
A dict of variable id to horizontal offset.
var_shapes : dict
A dict of variable id to variable dimensions.
Returns
-------
tuple
A list of indices for the boolean variables and integer variables.
"""
bool_idx = []
int_idx = []
for indices, constr_type in zip([bool_idx, int_idx],
[s.BOOL_IDS, s.INT_IDS]):
for var_id in dims[constr_type]:
offset = var_offsets[var_id]
shape = var_shapes[var_id]
for i in range(shape[0]*shape[1]):
indices.append(offset + i)
del dims[constr_type]
return bool_idx, int_idx
break
else:
for subproblem, solve_result in zip(self._separable_problems,
solve_results):
for var, primal_value in zip(subproblem.variables(),
solve_result.primal_values):
var.save_value(primal_value)
for constr, dual_value in zip(subproblem.constraints,
solve_results):
constr.save_value(dual_value)
self._value = sum(solve_result.opt_value
for solve_result in solve_results)
if s.OPTIMAL_INACCURATE in statuses:
self._status = s.OPTIMAL_INACCURATE
else:
self._status = s.OPTIMAL
return self._value
def name(self):
"""The name of the solver.
"""
return s.XPRESS
Parameters
----------
eq_constr : list
A list of the equality constraints in the canonical problem.
leq_constr : list
A list of the inequality constraints in the canonical problem.
dims : dict
A dict with the dimensions of the conic constraints.
solver : str
The solver being called.
"""
leq_constr += self.__format[1]
# Update dims.
for cone_size in self.size:
dims[s.SOC_DIM].append(cone_size[0])
CVXOPT_con(), GLPK_con(), XPRESS(),
GLPK_MI_con(), CBC_con(), SCS_con(), SuperSCS_con(),
GUROBI_con(), MOSEK_con(), CPLEX_con(), NAG_con()]
solver_qp_intf = [OSQP_qp(),
GUROBI_qp(),
CPLEX_qp()
]
SOLVER_MAP_CONIC = {solver.name(): solver for solver in solver_conic_intf}
SOLVER_MAP_QP = {solver.name(): solver for solver in solver_qp_intf}
# CONIC_SOLVERS and QP_SOLVERS are sorted in order of decreasing solver
# preference. QP_SOLVERS are those for which we have written interfaces
# and are supported by QpSolver.
CONIC_SOLVERS = [s.MOSEK, s.ECOS, s.SUPER_SCS, s.SCS,
s.CPLEX, s.GUROBI, s.GLPK, s.XPRESS,
s.GLPK_MI, s.CBC, s.CVXOPT, s.ECOS_BB, s.NAG, s.DIFFCP]
QP_SOLVERS = [s.OSQP,
s.GUROBI,
s.CPLEX]
def installed_solvers():
"""List the installed solvers.
"""
installed = []
# Check conic solvers
for name, solver in SOLVER_MAP_CONIC.items():
if solver.is_installed():
installed.append(name)
# Check QP solvers
for name, solver in SOLVER_MAP_QP.items():