def evaluate_jacobian(self, out=None): ret = BlockMatrix(len(self._nlps),1) for i,nlp in enumerate(self._nlps): ret.set_block(i, 0, nlp.evaluate_jacobian()) ret = ret.tocoo() if out is not None: assert np.array_equal(ret.row, out.row) assert np.array_equal(ret.col, out.col) np.copyto(out.data, ret.data) return out return ret
def _evaluate_hessian_if_necessary_and_cache(self): if self._cached_hessian is None: hess = BlockMatrix(2,2) hess.set_row_size(0,self._ex_model.n_inputs()) hess.set_row_size(1,self._ex_model.n_outputs()) hess.set_col_size(0,self._ex_model.n_inputs()) hess.set_col_size(1,self._ex_model.n_outputs()) # get the hessian w.r.t. the equality constraints eq_hess = None if self._ex_model.n_equality_constraints() > 0: eq_hess = self._ex_model.evaluate_hessian_equality_constraints() # let's check that it is lower triangular if np.any(eq_hess.row < eq_hess.col): raise ValueError('ExternalGreyBoxModel must return lower ' 'triangular portion of the Hessian only') eq_hess = make_lower_triangular_full(eq_hess) output_hess = None if self._ex_model.n_outputs() > 0: output_hess = self._ex_model.evaluate_hessian_outputs() # let's check that it is lower triangular if np.any(output_hess.row < output_hess.col): raise ValueError('ExternalGreyBoxModel must return lower ' 'triangular portion of the Hessian only') output_hess = make_lower_triangular_full(output_hess) input_hess = None if eq_hess is not None and output_hess is not None: # we may want to make this more efficient row = np.concatenate((eq_hess.row, output_hess.row)) col = np.concatenate((eq_hess.col, output_hess.col)) data = np.concatenate((eq_hess.data, output_hess.data)) assert eq_hess.shape == output_hess.shape input_hess = coo_matrix( (data, (row,col)), shape=eq_hess.shape) elif eq_hess is not None: input_hess = eq_hess elif output_hess is not None: input_hess = output_hess assert input_hess is not None # need equality or outputs or both hess.set_block(0,0,input_hess) self._cached_hessian = hess.tocoo()
def _evaluate_jacobian_if_necessary_and_cache(self): if self._cached_jacobian is None: jac = BlockMatrix(2,2) jac.set_row_size(0,self._ex_model.n_equality_constraints()) jac.set_row_size(1,self._ex_model.n_outputs()) jac.set_col_size(0,self._ex_model.n_inputs()) jac.set_col_size(1,self._ex_model.n_outputs()) if self._ex_model.n_equality_constraints() > 0: jac.set_block(0,0,self._ex_model.evaluate_jacobian_equality_constraints()) if self._ex_model.n_outputs() > 0: jac.set_block(1,0,self._ex_model.evaluate_jacobian_outputs()) jac.set_block(1,1,-1.0*identity(self._ex_model.n_outputs())) self._cached_jacobian = jac.tocoo()