Пример #1
0
    def apply_parameters(self,
                         id_to_param_value=None,
                         zero_offset: bool = False,
                         keep_zeros: bool = 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
        """
        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)

        if keep_zeros and self._P_mapping_nonzero is None:
            self._P_mapping_nonzero = canonInterface.A_mapping_nonzero_rows(
                self.P, self.x.size)
        P, _ = canonInterface.get_matrix_from_tensor(
            self.reduced_P,
            param_vec,
            self.x.size,
            nonzero_rows=self._P_mapping_nonzero,
            with_offset=False,
            problem_data_index=self.problem_data_index_P)

        q, d = canonInterface.get_matrix_from_tensor(self.q,
                                                     param_vec,
                                                     self.x.size,
                                                     with_offset=True)
        q = q.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_A)
        return P, q, d, A, np.atleast_1d(b)
    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 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
        """
        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)

        # TODO special code to handle P in canonInterface.
        if param_vec is None:
            tensor_application = self.P
        else:
            if sp.issparse(self.P):
                tensor_application = self.P @ sp.csc_matrix(param_vec[:, None])
            else:
                tensor_application = self.P @ param_vec
        P = tensor_application.reshape((self.x.size, self.x.size),
                                       order='F').tocsc()

        q, d = canonInterface.get_matrix_and_offset_from_tensor(
            self.q, param_vec, self.x.size)
        q = q.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_and_offset_from_tensor(
            self.A,
            param_vec,
            self.x.size,
            nonzero_rows=self._A_mapping_nonzero)
        return P, q, d, A, np.atleast_1d(b)