def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.variables()[0].id} data[s.C], data[s.OFFSET] = ConicSolver.get_coeff_offset( problem.objective.args[0]) data[s.C] = data[s.C].ravel() inv_data[s.OFFSET] = data[s.OFFSET][0] constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[self.EQ_CONSTR] = constr_map[Zero] data[s.A], data[s.B] = self.group_coeff_offset( problem, constr_map[Zero], ECOS.EXP_CONE_ORDER) # Order and group nonlinear constraints. neq_constr = constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] inv_data[self.NEQ_CONSTR] = neq_constr data[s.G], data[s.H] = self.group_coeff_offset( problem, neq_constr, ECOS.EXP_CONE_ORDER) return data, inv_data
def _prepare_data_and_inv_data(self, problem): data = {} inv_data = {self.VAR_ID: problem.x.id} # Format constraints # # SCS requires constraints to be specified in the following order: # 1. zero cone # 2. non-negative orthant # 3. soc # 4. psd # 5. exponential constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] zero_constr = constr_map[Zero] neq_constr = (constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] + constr_map[ExpCone]) inv_data[SCS.EQ_CONSTR] = zero_constr inv_data[SCS.NEQ_CONSTR] = neq_constr if not problem.formatted: problem = self.format_constraints(problem, self.EXP_CONE_ORDER) data[s.PARAM_PROB] = problem return problem, data, inv_data
def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.x.id} constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] len_eq = sum([c.size for c in constr_map[Zero]]) inv_data[self.EQ_CONSTR] = constr_map[Zero] neq_constr = constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] inv_data[self.NEQ_CONSTR] = neq_constr if not problem.formatted: problem = self.format_constraints(problem, ECOS.EXP_CONE_ORDER) data[s.PARAM_PROB] = problem c, d, A, b = problem.apply_parameters() data[s.C] = c inv_data[s.OFFSET] = d data[s.A] = -A[:len_eq] if data[s.A].shape[0] == 0: data[s.A] = None data[s.B] = b[:len_eq].flatten() if data[s.B].shape[0] == 0: data[s.B] = None data[s.G] = -A[len_eq:] data[s.H] = b[len_eq:].flatten() return data, inv_data
def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.x.id} # Format constraints # # ECOS requires constraints to be specified in the following order: # 1. zero cone # 2. non-negative orthant # 3. soc # 4. exponential constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] len_eq = sum([c.size for c in constr_map[Zero]]) inv_data[self.EQ_CONSTR] = constr_map[Zero] neq_constr = constr_map[NonPos] + constr_map[SOC] + constr_map[ExpCone] inv_data[self.NEQ_CONSTR] = neq_constr if not problem.formatted: problem = self.format_constraints(problem, self.EXP_CONE_ORDER) data[s.PARAM_PROB] = problem c, d, A, b = problem.apply_parameters() data[s.C] = c inv_data[s.OFFSET] = d data[s.A] = -A[:len_eq] if data[s.A].shape[0] == 0: data[s.A] = None data[s.B] = b[:len_eq].flatten() if data[s.B].shape[0] == 0: data[s.B] = None data[s.G] = -A[len_eq:] if 0 in data[s.G].shape: data[s.G] = None data[s.H] = b[len_eq:].flatten() if 0 in data[s.H].shape: data[s.H] = None return data, inv_data
def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.x.id} # Format constraints # # SCS requires constraints to be specified in the following order: # 1. zero cone # 2. non-negative orthant # 3. soc # 4. psd # 5. exponential constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] zero_constr = constr_map[Zero] neq_constr = (constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] + constr_map[ExpCone]) inv_data[SCS.EQ_CONSTR] = zero_constr inv_data[SCS.NEQ_CONSTR] = neq_constr if not problem.formatted: problem = self.format_constraints(problem, self.EXP_CONE_ORDER) data[s.PARAM_PROB] = problem # Apply parameter values. # Obtain A, b such that Ax + s = b, s \in cones. # # Note that scs mandates that the cones MUST be ordered with # zero cones first, then non-nonnegative orthant, then SOC, # then PSD, then exponential. c, d, A, b = problem.apply_parameters() data[s.C] = c inv_data[s.OFFSET] = d data[s.A] = -A data[s.B] = b return data, inv_data
def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.variables()[0].id} # Parse the coefficient vector from the objective. data[s.C], data[s.OFFSET] = self.get_coeff_offset( problem.objective.args[0]) data[s.C] = data[s.C].ravel() inv_data[s.OFFSET] = data[s.OFFSET][0] # Order and group nonlinear constraints. constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] # SCS requires constraints to be specified in the following order: # 1. zero cone # 2. non-negative orthant # 3. soc # 4. psd # 5. exponential zero_constr = constr_map[Zero] neq_constr = (constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] + constr_map[ExpCone]) inv_data[SCS.EQ_CONSTR] = zero_constr inv_data[SCS.NEQ_CONSTR] = neq_constr # Obtain A, b such that Ax + s = b, s \in cones. # # Note that scs mandates that the cones MUST be ordered with # zero cones first, then non-nonnegative orthant, then SOC, # then PSD, then exponential. data[s.A], data[s.B] = self.group_coeff_offset( problem, zero_constr + neq_constr, self.EXP_CONE_ORDER) return data, inv_data
def apply(self, problem): """Returns a new problem and data for inverting the new solution. Returns ------- tuple (dict of arguments needed for the solver, inverse data) """ data = {} inv_data = {self.VAR_ID: problem.x.id} constr_map = group_constraints(problem.constraints) data[ConicSolver.DIMS] = ConeDims(constr_map) inv_data[ConicSolver.DIMS] = data[ConicSolver.DIMS] len_eq = sum([c.size for c in constr_map[Zero]]) inv_data[self.EQ_CONSTR] = constr_map[Zero] neq_constr = constr_map[NonPos] + constr_map[SOC] + constr_map[PSD] inv_data[self.NEQ_CONSTR] = neq_constr if not problem.formatted: problem = self.format_constraints(problem, ECOS.EXP_CONE_ORDER) data[s.PARAM_PROB] = problem c, d, A, b = problem.apply_parameters() data[s.C] = c inv_data[s.OFFSET] = d data[s.A] = -A[:len_eq] if data[s.A].shape[0] == 0: data[s.A] = None data[s.B] = b[:len_eq].flatten() if data[s.B].shape[0] == 0: data[s.B] = None if len_eq > A.shape[1]: # Then the given optimization problem has no conic constraints. # This is certainly a degenerate case, but we'll handle it downstream. data[s.G] = sp.csc_matrix((1, A.shape[1])) data[s.H] = np.array([0]) else: data[s.G] = -A[len_eq:] data[s.H] = b[len_eq:].flatten() return data, inv_data