Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_dcp(self):
p = Problem(Minimize(normInf(self.a)))
self.assertEqual(p.is_dcp(), True)
p = Problem(Maximize(normInf(self.a)))
self.assertEqual(p.is_dcp(), False)
def _lower_problem(problem):
"""Evaluates lazy constraints."""
constrs = [c() if callable(c) else c for c in problem.constraints]
constrs = [c for c in constrs if c is not None]
if s.INFEASIBLE in constrs:
# Indicates that the problem is infeasible.
return None
return problems.problem.Problem(Minimize(0), constrs)
result = bound(self, booleans)
# check if gap is small enough
if result['gap'] < epsilon:
return result['obj']
result = solve_wrapper(self, 0, booleans, depth, epsilon)
# set the boolean values to the solution
for b, value in zip(booleans, result['sol']):
b.save_value(value)
b.fix_values = cvxopt.matrix(True, b.size)
return result['obj']
# add branch and bound a solution method
problem.Problem.register_solve("branch and bound", branch_and_bound)
inverse_data = InverseData(problem)
canon_objective, canon_constraints = self.canonicalize_tree(
problem.objective)
for constraint in problem.constraints:
# canon_constr is the constraint rexpressed in terms of
# its canonicalized arguments, and aux_constr are the constraints
# generated while canonicalizing the arguments of the original
# constraint
canon_constr, aux_constr = self.canonicalize_tree(
constraint)
canon_constraints += aux_constr + [canon_constr]
inverse_data.cons_id_map.update({constraint.id: canon_constr.id})
new_problem = problems.problem.Problem(canon_objective,
canon_constraints)
return new_problem, inverse_data
constrs = []
for constraint in problem.constraints:
if type(constraint) == Equality:
constraint = utilities.lower_equality(constraint)
elif type(constraint) == Inequality:
constraint = utilities.lower_inequality(constraint)
# real2imag maps variable id to a potential new variable
# created for the imaginary part.
real_constrs, imag_constrs = self.canonicalize_tree(
constraint, inverse_data.real2imag, leaf_map)
if real_constrs is not None:
constrs.extend(real_constrs)
if imag_constrs is not None:
constrs.extend(imag_constrs)
new_problem = problems.problem.Problem(real_obj,
constrs)
return new_problem, inverse_data
for con in prob.constraints:
if isinstance(con,Inequality):
lhs = fix_expr(con.args[0], fix_var, param_list)
rhs = fix_expr(con.args[1], fix_var, param_list)
new_constr.append(lhs <= rhs)
else:
fix_con = fix_expr(con.expr, fix_var, param_list)
if isinstance(con, NonPos):
new_constr.append(fix_con <= 0)
elif isinstance(con,NonNeg):
new_constr.append(fix_con >= 0)
elif isinstance(con, PSD):
new_constr.append(fix_con >> 0)
else:
new_constr.append(fix_con == 0)
new_prob = Problem(new_obj, new_constr)
return new_prob
Parameters
----------
x: cvxpy.Variable
constraints: list of cvxpy.constraints.constraint.Constraint
Each Constraint object must be DCP-compatible.
Notes
-----
This function DOES NOT work when ``x`` has attributes, like ``PSD=True``,
``diag=True``, ``symmetric=True``, etc...
"""
from cvxpy.problems.problem import Problem
from cvxpy.problems.objective import Minimize
from cvxpy.atoms.affine.sum import sum
prob = Problem(Minimize(sum(x)), constraints)
# ^ The objective value is only used to make sure that "x"
# participates in the problem. So, if constraints is an
# empty list, then the support function is the standard
# support function for R^n.
data, chain, invdata = prob.get_problem_data(solver='SCS')
inv = invdata[-2]
x_offset = inv.var_offsets[x.id]
x_indices = np.arange(x_offset, x_offset + x.size)
A = data['A']
x_selector = np.zeros(shape=(A.shape[1],), dtype=bool)
x_selector[x_indices] = True
A_x = A[:, x_selector]
A_other = A[:, ~x_selector]
A = -sparse.hstack([A_x, A_other])
b = data['b']
K = data['dims']
def apply(self, problem):
"""Recursively canonicalize the objective and every constraint.
"""
constraints = []
for constr in problem.constraints:
constraints += self._canonicalize_constraint(constr)
feas_problem = problems.problem.Problem(Minimize(0), constraints)
objective = problem.objective.expr
if objective.is_nonneg():
t = Parameter(nonneg=True)
elif objective.is_nonpos():
t = Parameter(nonpos=True)
else:
t = Parameter()
constraints += self._canonicalize_constraint(objective <= t)
param_problem = problems.problem.Problem(Minimize(0), constraints)
param_problem._bisection_data = BisectionData(
feas_problem, t, *tighten.tighten_fns(objective))
return param_problem, InverseData(problem)
from cvxpy.utilities.coeff_extractor import CoeffExtractor
from cvxpy.problems.objective import Minimize
from cvxpy.expressions.attributes import is_quadratic, is_affine
from cvxpy.constraints.constraint import Constraint
from cvxpy.constraints.attributes import is_qp_constraint
from cvxpy.problems.objective_attributes import is_qp_objective
from cvxpy.problems.problem import Problem
from cvxpy.problems.attributes import is_minimization
class QpMatrixStuffing(MatrixStuffing):
"""Fills in numeric values for this problem instance.
"""
preconditions = {
(Problem, is_minimization, True),
(Minimize, is_quadratic, True),
(Constraint, is_affine, True),
(Constraint, is_qp_constraint, True)
}
@staticmethod
def postconditions(problem_type):
return QpMatrixStuffing.preconditions.union({(Minimize, is_qp_objective, True)})
def stuffed_objective(self, problem, inverse_data):
extractor = CoeffExtractor(inverse_data)
# extract to x.T * P * x + q.T * x, store r
(P, q, r) = extractor.quad_form(problem)
# concatenate all variables in one vector
x = cvxpy.Variable(inverse_data.x_length)