def setUp(self): row = np.array([0, 1, 4, 1, 2, 7, 2, 3, 5, 3, 4, 5, 4, 7, 5, 6, 6, 7]) col = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7]) data = np.array([27, 5, 12, 56, 66, 34, 94, 31, 41, 7, 98, 72, 24, 33, 78, 47, 98, 41]) off_diagonal_mask = row != col new_row = np.concatenate([row, col[off_diagonal_mask]]) new_col = np.concatenate([col, row[off_diagonal_mask]]) new_data = np.concatenate([data, data[off_diagonal_mask]]) m = coo_matrix((new_data, (new_row, new_col)), shape=(8, 8)) self.block00 = m row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) m = coo_matrix((data, (row, col)), shape=(4, 8)) self.block10 = m row = np.array([0, 1, 2, 3]) col = np.array([0, 1, 2, 3]) data = np.array([1, 1, 1, 1]) m = coo_matrix((data, (row, col)), shape=(4, 4)) self.block11 = m bm = BlockSymMatrix(2) bm.name = 'basic_matrix' bm[0, 0] = self.block00 bm[1, 0] = self.block10 bm[1, 1] = self.block11 self.basic_m = bm
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
return model ################################################################# m = create_model(4.5, 1.0) opt = aml.SolverFactory('ipopt') results = opt.solve(m, tee=True) ################################################################# nlp = PyomoNLP(m) x = nlp.x_init() y = compute_init_lam(nlp, x=x) J = nlp.jacobian_g(x) H = nlp.hessian_lag(x, y) M = BlockSymMatrix(2) M[0, 0] = H M[1, 0] = J Np = BlockMatrix(2, 1) Np[0, 0] = nlp.Hessian_lag(x, y, variables_cols=[m.eta1, m.eta2]) Np[1, 0] = nlp.Jacobian_g(x, variables=[m.eta1, m.eta2]) M_array = M.toarray() Np_array = Np.toarray() ds = np.linalg.solve(M_array, Np_array) print(nlp.variable_order()) #################################################################
# Discretize model using Orthogonal Collocation discretizer = aml.TransformationFactory('dae.collocation') discretizer.apply_to(instance, nfe=100, ncp=3, scheme='LAGRANGE-RADAU') discretizer.reduce_collocation_points(instance, var=instance.u, ncp=1, contset=instance.t) # Interface pyomo model with nlp nlp = PyomoNLP(instance) x = nlp.create_vector_x() lam = nlp.create_vector_y() # Evaluate jacobian jac_c = nlp.jacobian_g(x) plt.spy(jac_c) plt.title('Jacobian of the constraints\n') plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.hessian_lag(x, lam) plt.spy(hess_lag) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_c plt.spy(kkt.tocoo()) plt.title('KKT system\n') plt.show()
plt.spy(jac_g) plt.title('Jacobian of the all constraints\n') plt.show() # Evaluate jacobian of equality constraints jac_c = nlp.jacobian_c(x) plt.title('Jacobian of the equality constraints\n') plt.spy(jac_c) plt.show() # Evaluate jacobian of equality constraints jac_d = nlp.jacobian_d(x) plt.title('Jacobian of the inequality constraints\n') plt.spy(jac_d) plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.hessian_lag(x, y) plt.spy(hess_lag.tocoo()) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_g full_kkt = kkt.tocoo() plt.spy(full_kkt) plt.title('Karush-Kuhn-Tucker Matrix\n') plt.show()
m = create_model(4.5, 1.0) opt = pyo.SolverFactory('ipopt') results = opt.solve(m, tee=True) ################################################################# nlp = PyomoNLP(m) x = nlp.init_primals() y = compute_init_lam(nlp, x=x) nlp.set_primals(x) nlp.set_duals(y) J = nlp.evaluate_jacobian() H = nlp.evaluate_hessian_lag() M = BlockSymMatrix(2) M[0, 0] = H M[1, 0] = J sens_vars = [m.eta1, m.eta2] nsens = len(sens_vars) nr = M.shape[0] #Np = BlockMatrix(2, 1) #Np[0, 0] = nlp.extract_submatrix_hessian_lag(pyomo_variables_rows=nlp.get_pyomo_variables(), pyomo_variables_cols=[m.eta1, m.eta2]) #Np[1, 0] = nlp.extract_submatrix_jacobian(pyomo_variables=[m.eta1, m.eta2], pyomo_constraints=nlp.get_pyomo_constraints()) Np = np.zeros((nr, nsens)) Np[(nr - nsens):nr,:] = np.eye(nsens) sens_cons = ['consteta1', 'consteta2']
plt.spy(jac_g) plt.title('Jacobian of the all constraints\n') plt.show() # Evaluate jacobian of equality constraints jac_c = nlp.jacobian_c(x) plt.title('Jacobian of the equality constraints\n') plt.spy(jac_c) plt.show() # Evaluate jacobian of equality constraints jac_d = nlp.jacobian_d(x) plt.title('Jacobian of the inequality constraints\n') plt.spy(jac_d) plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.hessian_lag(x, y) plt.spy(hess_lag.tofullmatrix()) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_g full_kkt = kkt.tofullmatrix() plt.spy(full_kkt) plt.title('Karush-Kuhn-Tucker Matrix\n') plt.show()
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
plt.title('Jacobian of the all constraints\n') plt.show() # Evaluate jacobian of equality constraints jac_c = nlp.jacobian_c(x) plt.title('Jacobian of the equality constraints\n') plt.spy(jac_c) plt.show() # Evaluate jacobian of equality constraints jac_d = nlp.jacobian_d(x) plt.title('Jacobian of the inequality constraints\n') plt.spy(jac_d) plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.hessian_lag(x, y) plt.spy(hess_lag.tocoo()) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_g full_kkt = kkt.tocoo() plt.spy(full_kkt) plt.title('Karush-Kuhn-Tucker Matrix\n') plt.show()
instance = create_basic_dense_qp(G, A, bs[i], c) nlp = PyomoNLP(instance) models.append(instance) scenario_name = "s{}".format(i) scenarios[scenario_name] = nlp coupling_vars[scenario_name] = [nlp.variable_idx(instance.x[0])] nlp = TwoStageStochasticNLP(scenarios, coupling_vars) x = nlp.x_init() y = nlp.y_init() jac_c = nlp.jacobian_c(x) plt.spy(jac_c) plt.title('Jacobian of the constraints\n') plt.show() hess_lag = nlp.hessian_lag(x, y) plt.spy(hess_lag.tocoo()) plt.title('Hessian of the Lagrangian function\n') plt.show() kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_c plt.spy(kkt.tocoo()) plt.title('KKT system\n') plt.show()