Example #1
0
    def expansion_matrix_xu(self):

        Pxu = BlockMatrix(self.nblocks + 1, self.nblocks + 1)
        for sid, nlp in enumerate(self._nlps):
            Pxu[sid, sid] = nlp.expansion_matrix_xu()
        Pxu[self.nblocks, self.nblocks] = empty_matrix(self.nz, 0)
        return Pxu
Example #2
0
    def expansion_matrix_xu(self):

        Pxu = BlockMatrix(self.nblocks + 1, self.nblocks + 1)
        for sid, nlp in enumerate(self._nlps):
            Pxu[sid, sid] = nlp.expansion_matrix_xu()
        Pxu[self.nblocks, self.nblocks] = empty_matrix(self.nz, 0)
        return Pxu
Example #3
0
    def jacobian_c(self, x, out=None, **kwargs):
        """Return the jacobian of equality constraints evaluated at x

        Parameters
        ----------
        x : 1d-array
            array with values of primal variables. Size nx
        out : 1d-array
            coo_matrix with the structure of the jacobian already defined. Optional

        Returns
        -------
        The jacobian of the equality contraints in a coo_matrix format

        """
        return empty_matrix(0, self.nx)
Example #4
0
    def jacobian_c(self, x, out=None, **kwargs):
        """Return the jacobian of equality constraints evaluated at x

        Parameters
        ----------
        x : 1d-array
            array with values of primal variables. Size nx
        out : 1d-array
            coo_matrix with the structure of the jacobian already defined. Optional

        Returns
        -------
        The jacobian of the equality contraints in a coo_matrix format

        """
        return empty_matrix(0, self.nx)
Example #5
0
    def _create_hessian_structure(self):

        # Note: This method requires the complicated vars map to be
        # created beforehand

        hess_lag = BlockSymMatrix(self.nblocks + 1)
        for sid, nlp in enumerate(self._nlps):
            xi = nlp.x_init()
            yi = nlp.y_init()
            hess_lag[sid, sid] = nlp.hessian_lag(xi, yi)

        hess_lag[self.nblocks, self.nblocks] = empty_matrix(self.nz, self.nz)

        flat_hess = hess_lag.tocoo()
        self._irows_hess = flat_hess.row
        self._jcols_hess = flat_hess.col
        self._nnz_hess_lag = flat_hess.nnz
Example #6
0
    def _create_hessian_structure(self):

        # Note: This method requires the complicated vars map to be
        # created beforehand

        hess_lag = BlockSymMatrix(self.nblocks + 1)
        for sid, nlp in enumerate(self._nlps):
            xi = nlp.x_init()
            yi = nlp.y_init()
            hess_lag[sid, sid] = nlp.hessian_lag(xi, yi)

        hess_lag[self.nblocks, self.nblocks] = empty_matrix(self.nz, self.nz)

        flat_hess = hess_lag.tocoo()
        self._irows_hess = flat_hess.row
        self._jcols_hess = flat_hess.col
        self._nnz_hess_lag = flat_hess.nnz
Example #7
0
    def hessian_lag(self, x, y, out=None, **kwargs):
        """Return the Hessian of the Lagrangian function evaluated at x and y

        Parameters
        ----------
        x : array_like
            Array with values of primal variables.
        y : array_like
            Array with values of dual variables.
        out : BlockMatrix
            Output matrix with the structure of the hessian already defined. Optional

        Returns
        -------
        BlockMatrix

        """
        assert x.size == self.nx, 'Dimension mismatch'
        assert y.size == self.ng, 'Dimension mismatch'

        eval_f_c = kwargs.pop('eval_f_c', True)

        if isinstance(x, BlockVector) and isinstance(y, BlockVector):
            assert x.nblocks == self.nblocks + 1
            assert y.nblocks == 2 * self.nblocks
            x_ = x
            y_ = y
        elif isinstance(x, np.ndarray) and isinstance(y, BlockVector):
            assert y.nblocks == 2 * self.nblocks
            block_x = self.create_vector_x()
            block_x.copyfrom(x)
            x_ = block_x
            y_ = y
        elif isinstance(x, BlockVector) and isinstance(y, np.ndarray):
            assert x.nblocks == self.nblocks + 1
            x_ = x
            block_y = self.create_vector_y()
            block_y.copyfrom(y)
            y_ = block_y
        elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            block_x = self.create_vector_x()
            block_x.copyfrom(x)
            x_ = block_x
            block_y = self.create_vector_y()
            block_y.copyfrom(y)
            y_ = block_y
        else:
            raise NotImplementedError('Input vector format not recognized')

        if out is None:
            hess_lag = BlockSymMatrix(self.nblocks + 1)
            for sid, nlp in enumerate(self._nlps):
                xi = x_[sid]
                yi = y_[sid]
                hess_lag[sid, sid] = nlp.hessian_lag(xi, yi, eval_f_c=eval_f_c)

            hess_lag[self.nblocks,
                     self.nblocks] = empty_matrix(self.nz, self.nz)
            return hess_lag
        else:
            assert isinstance(out, BlockSymMatrix), \
                'out must be a BlockSymMatrix'
            assert out.bshape == (self.nblocks + 1, self.nblocks + 1), \
                'Block shape mismatch'
            hess_lag = out
            for sid, nlp in enumerate(self._nlps):
                xi = x_[sid]
                yi = y_[sid]
                nlp.hessian_lag(xi,
                                yi,
                                out=hess_lag[sid, sid],
                                eval_f_c=eval_f_c)

            Hz = hess_lag[self.nblocks, self.nblocks]
            nb = self.nblocks
            assert Hz.shape == (self.nz, self.nz), \
                'out must have an {}x{} empty matrix in block {}'.format(nb,
                                                                         nb,
                                                                         (nb, nb))
            assert Hz.nnz == 0, \
                'out must have an empty matrix in block {}'.format((nb, nb))
            return hess_lag
Example #8
0
    def hessian_lag(self, x, y, out=None, **kwargs):
        """Return the Hessian of the Lagrangian function evaluated at x and y

        Parameters
        ----------
        x : array_like
            Array with values of primal variables.
        y : array_like
            Array with values of dual variables.
        out : BlockMatrix
            Output matrix with the structure of the hessian already defined. Optional

        Returns
        -------
        BlockMatrix

        """
        assert x.size == self.nx, 'Dimension mismatch'
        assert y.size == self.ng, 'Dimension mismatch'

        eval_f_c = kwargs.pop('eval_f_c', True)

        if isinstance(x, BlockVector) and isinstance(y, BlockVector):
            assert x.nblocks == self.nblocks + 1
            assert y.nblocks == 2 * self.nblocks
            x_ = x
            y_ = y
        elif isinstance(x, np.ndarray) and isinstance(y, BlockVector):
            assert y.nblocks == 2 * self.nblocks
            block_x = self.create_vector_x()
            block_x.copyfrom(x)
            x_ = block_x
            y_ = y
        elif isinstance(x, BlockVector) and isinstance(y, np.ndarray):
            assert x.nblocks == self.nblocks + 1
            x_ = x
            block_y = self.create_vector_y()
            block_y.copyfrom(y)
            y_ = block_y
        elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            block_x = self.create_vector_x()
            block_x.copyfrom(x)
            x_ = block_x
            block_y = self.create_vector_y()
            block_y.copyfrom(y)
            y_ = block_y
        else:
            raise NotImplementedError('Input vector format not recognized')

        if out is None:
            hess_lag = BlockSymMatrix(self.nblocks + 1)
            for sid, nlp in enumerate(self._nlps):
                xi = x_[sid]
                yi = y_[sid]
                hess_lag[sid, sid] = nlp.hessian_lag(xi, yi, eval_f_c=eval_f_c)

            hess_lag[self.nblocks, self.nblocks] = empty_matrix(self.nz, self.nz)
            return hess_lag
        else:
            assert isinstance(out, BlockSymMatrix), \
                'out must be a BlockSymMatrix'
            assert out.bshape == (self.nblocks + 1, self.nblocks + 1), \
                'Block shape mismatch'
            hess_lag = out
            for sid, nlp in enumerate(self._nlps):
                xi = x_[sid]
                yi = y_[sid]
                nlp.hessian_lag(xi,
                                yi,
                                out=hess_lag[sid, sid],
                                eval_f_c=eval_f_c)

            Hz = hess_lag[self.nblocks, self.nblocks]
            nb = self.nblocks
            assert Hz.shape == (self.nz, self.nz), \
                'out must have an {}x{} empty matrix in block {}'.format(nb,
                                                                         nb,
                                                                         (nb, nb))
            assert Hz.nnz == 0, \
                'out must have an empty matrix in block {}'.format((nb, nb))
            return hess_lag