Пример #1
0
    def __init__(self,
                 P,
                 q,
                 x,
                 A,
                 variables,
                 var_id_to_col,
                 constraints,
                 parameters,
                 param_id_to_col,
                 formatted: bool = False) -> None:
        self.P = P
        self.q = q
        self.x = x
        self.A = A

        # Form a reduced representation of A, for faster application of
        # parameters.
        if np.prod(A.shape) != 0:
            reduced_A, indices, indptr, shape = (
                canonInterface.reduce_problem_data_tensor(A, self.x.size))
            self.reduced_A = reduced_A
            self.problem_data_index_A = (indices, indptr, shape)
        else:
            self.reduced_A = A
            self.problem_data_index_A = None
        self._A_mapping_nonzero = None

        # Form a reduced representation of P, for faster application of
        # parameters.
        if np.prod(P.shape) != 0:
            reduced_P, indices, indptr, shape = (
                canonInterface.reduce_problem_data_tensor(P,
                                                          self.x.size,
                                                          quad_form=True))
            self.reduced_P = reduced_P
            self.problem_data_index_P = (indices, indptr, shape)
        else:
            self.reduced_P = P
            self.problem_data_index_P = None
        self._P_mapping_nonzero = None

        self.constraints = constraints
        self.constr_size = sum([c.size for c in constraints])
        self.parameters = parameters
        self.param_id_to_col = param_id_to_col
        self.id_to_param = {p.id: p for p in self.parameters}
        self.param_id_to_size = {p.id: p.size for p in self.parameters}
        self.total_param_size = sum([p.size for p in self.parameters])
        # TODO technically part of inverse data.
        self.variables = variables
        self.var_id_to_col = var_id_to_col
        self.id_to_var = {v.id: v for v in self.variables}
        # whether this param cone prog has been formatted for a solver
        self.formatted = formatted
    def apply_parameters(self,
                         id_to_param_value=None,
                         zero_offset=False,
                         keep_zeros=False):
        """Returns A, b after applying parameters (and reshaping).

        Args:
          id_to_param_value: (optional) dict mapping parameter ids to values
          zero_offset: (optional) if True, zero out the constant offset in the
                       parameter vector
          keep_zeros: (optional) if True, store explicit zeros in A where
                        parameters are affected
        """
        if self.reduced_A is None:
            # Form a reduced representation of A, for faster application of
            # parameters.
            if np.prod(self.A.shape) != 0:
                reduced_A, indices, indptr, shape = (
                    canonInterface.reduce_problem_data_tensor(
                        self.A, self.x.size))
                self.reduced_A = reduced_A
                self.problem_data_index = (indices, indptr, shape)
            else:
                self.reduced_A = self.A
                self.problem_data_index = None

        def param_value(idx):
            return (np.array(self.id_to_param[idx].value)
                    if id_to_param_value is None else id_to_param_value[idx])

        param_vec = canonInterface.get_parameter_vector(
            self.total_param_size,
            self.param_id_to_col,
            self.param_id_to_size,
            param_value,
            zero_offset=zero_offset)
        c, d = canonInterface.get_matrix_from_tensor(self.c,
                                                     param_vec,
                                                     self.x.size,
                                                     with_offset=True)
        c = c.toarray().flatten()
        if keep_zeros and self._A_mapping_nonzero is None:
            self._A_mapping_nonzero = canonInterface.A_mapping_nonzero_rows(
                self.A, self.x.size)
        A, b = canonInterface.get_matrix_from_tensor(
            self.reduced_A,
            param_vec,
            self.x.size,
            nonzero_rows=self._A_mapping_nonzero,
            with_offset=True,
            problem_data_index=self.problem_data_index)
        return c, d, A, np.atleast_1d(b)
Пример #3
0
    def __init__(self,
                 c,
                 x,
                 A,
                 variables,
                 var_id_to_col,
                 constraints,
                 parameters,
                 param_id_to_col,
                 formatted=False):
        # The problem data tensors; c is for the constraint, and A for
        # the problem data matrix
        self.c = c
        self.A = A

        # The variable
        self.x = x

        # Form a reduced representation of A, for faster application of
        # parameters.
        if np.prod(A.shape) != 0:
            reduced_A, indices, indptr, shape = (
                canonInterface.reduce_problem_data_tensor(A, self.x.size))
            self.reduced_A = reduced_A
            self.problem_data_index = (indices, indptr, shape)
        else:
            self.reduced_A = A
            self.problem_data_index = None

        self._A_mapping_nonzero = None

        self.constraints = constraints
        self.constr_size = sum([c.size for c in constraints])
        self.constr_map = group_constraints(constraints)
        self.cone_dims = ConeDims(self.constr_map)
        self.parameters = parameters
        self.param_id_to_col = param_id_to_col
        self.id_to_param = {p.id: p for p in self.parameters}
        self.param_id_to_size = {p.id: p.size for p in self.parameters}
        self.total_param_size = sum([p.size for p in self.parameters])

        # TODO technically part of inverse data.
        self.variables = variables
        self.var_id_to_col = var_id_to_col
        self.id_to_var = {v.id: v for v in self.variables}

        # whether this param cone prog has been formatted for a solver
        self.formatted = formatted