예제 #1
0
 def test_qp_maximization_reduction_path_qp_solver(self):
     qp_maximization = Problem(Maximize(QuadForm(self.x, -self.Q)),
                               [self.x <= -1])
     path = PathFinder().reduction_path(ProblemType(qp_maximization),
                                        [QpSolver])
     self.assertEquals(3, len(path))
     self.assertEquals(path[1], QpMatrixStuffing)
     self.assertEquals(path[2], FlipObjective)
예제 #2
0
	def apply(self, problem):
		inverse_data = InverseData(problem)
		is_minimize = type(problem.objective) == Minimize
		chance_expr, chance_constraints = self.chance_tree(problem.objective.args[0], is_minimize, True, False)
		chance_objective = Minimize(chance_expr) if is_minimize else Maximize(chance_expr)
		
		if any([type(atom) == cc.quantile for con in problem.constraints for atom in con.atoms()]):
			raise DCPError("Quantile atom may not be nested in constraints.")
		new_problem = cc.problem.Problem(chance_objective, problem.constraints + chance_constraints)
		return new_problem, inverse_data
예제 #3
0
 def test_qp_maximization_reduction_path_ecos(self):
     qp_maximization = Problem(Maximize(-sum_squares(self.x)),
                               [self.x <= -1])
     self.assertTrue(qp_maximization.is_dcp())
     path = PathFinder().reduction_path(ProblemType(qp_maximization),
                                        [ECOS])
     self.assertEquals(4, len(path))
     self.assertEquals(path[1], ConeMatrixStuffing)
     self.assertEquals(path[2], Dcp2Cone)
     self.assertEquals(path[3], FlipObjective)
예제 #4
0
def targets_and_priorities(
        objectives: List[Union[Minimize, Maximize]],
        priorities,
        targets,
        limits=None,
        off_target: float = 1e-5) -> Union[Minimize, Maximize]:
    """Combines objectives with penalties within a range between target and limit.

    Each Minimize objective i has value

        priorities[i]*objectives[i] when objectives[i] >= targets[i]

        +infinity when objectives[i] > limits[i]

    Each Maximize objective i has value

        priorities[i]*objectives[i] when objectives[i] <= targets[i]

        +infinity when objectives[i] < limits[i]

    Args:
      objectives: A list of Minimize/Maximize objectives.
      priorities: The weight within the trange.
      targets: The start (end) of penalty for Minimize (Maximize)
      limits: The hard end (start) of penalty for Minimize (Maximize)
      off_target: Penalty outside of target.

    Returns:
      A Minimize/Maximize objective.
    """
    num_objs = len(objectives)
    new_objs: List[Union[Minimize, Maximize]] = []
    for i in range(num_objs):
        obj = objectives[i]
        sign = 1 if Constant.cast_to_const(priorities[i]).is_nonneg() else -1
        off_target *= sign
        if type(obj) == Minimize:
            expr = (priorities[i] - off_target) * atoms.pos(obj.args[0] -
                                                            targets[i])
            expr += off_target * obj.args[0]
            if limits is not None:
                expr += sign * indicator([obj.args[0] <= limits[i]])
            new_objs.append(expr)
        else:  # Maximize
            expr = (priorities[i] - off_target) * atoms.min_elemwise(
                obj.args[0], targets[i])
            expr += off_target * obj.args[0]
            if limits is not None:
                expr += sign * indicator([obj.args[0] >= limits[i]])
            new_objs.append(expr)
    obj_expr = sum(new_objs)
    if obj_expr.is_convex():
        return Minimize(obj_expr)
    else:
        return Maximize(obj_expr)
예제 #5
0
 def _value_impl(self):
     from cvxpy.problems.problem import Problem
     from cvxpy.problems.objective import Maximize
     y_val = self.args[0].value.round(decimals=9).ravel(order='F')
     x_flat = self._parent.x.flatten()
     cons = self._parent.constraints
     if len(cons) == 0:
         dummy = Variable()
         cons = [dummy == 1]
     prob = Problem(Maximize(y_val @ x_flat), cons)
     val = prob.solve(solver='SCS', eps=1e-6)
     return val
예제 #6
0
def targets_and_priorities(objectives, priorities, targets, limits=None, off_target=1e-5):
    """Combines objectives with penalties within a range between target and limit.

    Args:
      objectives: A list of Minimize/Maximize objectives.
      priorities: The weight within the trange.
      targets: The start (end) of penalty for Minimize (Maximize)
      limits: The hard end (start) of penalty for Minimize (Maximize)
      off_target: Penalty outside of target.

    Returns:
      A Minimize/Maximize objective.
    """
    num_objs = len(objectives)
    new_objs = []
    for i in range(num_objs):
        obj = objectives[i]
        sign = 1 if Constant(priorities[i]).is_positive() else -1
        off_target *= sign
        if type(obj) == Minimize:
            expr = (priorities[i] - off_target)*atoms.pos(obj.args[0] - targets[i])
            expr += off_target*obj.args[0]
            if limits is not None:
                expr += sign*indicator([obj.args[0] <= limits[i]])
            new_objs.append(expr)
        else:  # Maximize
            expr = (priorities[i] - off_target)*atoms.min_elemwise(obj.args[0], targets[i])
            expr += off_target*obj.args[0]
            if limits is not None:
                expr += sign*indicator([obj.args[0] >= limits[i]])
            new_objs.append(expr)
    obj_expr = sum(new_objs)
    if obj_expr.is_convex():
        return Minimize(obj_expr)
    else:
        return Maximize(obj_expr)
예제 #7
0
파일: problem.py 프로젝트: giserh/cvxpy
    def iter_DCCP(self,solver,max_iter, tau, miu, tau_max):
        it = 1
        # keep the values from the previous iteration or initialization
        previous_cost = float("inf")
        variable_pres_value = []
        for var in self.variables():
            variable_pres_value.append(var.value)
        # each non-dcp constraint needs a slack variable
        var_slack = []
        for constr in self.constraints:
            if not constr.is_dcp():
                rows, cols = constr.size
                var_slack.append(Variable(rows, cols))

        while it<=max_iter and all(var.value is not None for var in self.variables()):
            constr_new = []
            #cost functions
            if not self.objective.is_dcp():
                temp = self.objective.convexify()
                flag = temp[2]
                flag_var = temp[3]
                while flag == 1:
                    for var in flag_var:
                        var_index = self.variables().index(var)
                        var.value = 0.8*var.value + 0.2* variable_pres_value[var_index]
                    temp = self.objective.convexify()
                    flag = temp[2]
                    flag_var = temp[3]
                cost_new =  temp[0] # new cost function
                for dom in temp[1]: # domain constraints
                    constr_new.append(dom)
            else:
                cost_new = self.objective.args[0]
            #constraints
            count_slack = 0
            for arg in self.constraints:
                if not arg.is_dcp():
                    temp = arg.convexify()
                    flag = temp[2]
                    flag_var = temp[3]
                    while flag == 1:
                        for var in flag_var:
                            var_index = self.variables().index(var)
                            var.value = 0.8*var.value + 0.2* variable_pres_value[var_index]
                        temp = arg.convexify()
                        flag = temp[2]
                        flag_var = temp[3]
                    newcon = temp[0]   #new constraint without slack variable
                    for dom in temp[1]:#domain
                        constr_new.append(dom)
                    right = newcon.args[1] + var_slack[count_slack]
                    constr_new.append(newcon.args[0]<=right)
                    constr_new.append(var_slack[count_slack]>=0)
                    count_slack = count_slack+1
                else:
                    constr_new.append(arg)
            #objective
            if self.objective.NAME == 'minimize':
                for var in var_slack:
                    cost_new += np.ones((var._cols,var._rows))*var*tau
                obj_new = Minimize(cost_new)
            else:
                for var in var_slack:
                    cost_new -= var*tau
                obj_new = Maximize(cost_new)
            #new problem
            prob_new = Problem(obj_new, constr_new)
            variable_pres_value = []
            for var in self.variables():
                variable_pres_value.append(var.value)
            if not var_slack == []:
                if solver is None:
                    print "iteration=",it, "cost value = ", prob_new.solve(), "tau = ", tau
                else:
                    print "iteration=",it, "cost value = ", prob_new.solve(solver = solver), "tau = ", tau
                max_slack = []
                for i in range(len(var_slack)):
                    max_slack.append(np.max(var_slack[i].value))
                max_slack = np.max(max_slack)
                print "max slack = ", max_slack
            else:
                if solver is None:
                    co = prob_new.solve()
                else:
                    co = prob_new.solve(solver = solver)
                print "iteration=",it, "cost value = ", co , "tau = ", tau
            if np.abs(previous_cost - prob_new.value) <= 1e-4: #terminate
                it_real = it
                it = max_iter+1
            else:
                previous_cost = prob_new.value
                it_real = it
                tau = min([tau*miu,tau_max])
                it += 1
        if not var_slack == []:
            return(it_real, tau, max(var_slack[i].value for i in range(len(var_slack))))
        else:
            return(it_real, tau)