def compute_init_lam(nlp, x=None, lam_max=1e3): if x is None: x = nlp.init_primals() else: assert x.size == nlp.n_primals() nlp.set_primals(x) assert nlp.n_ineq_constraints( ) == 0, "only supported for equality constrained nlps for now" nx = nlp.n_primals() nc = nlp.n_constraints() # create Jacobian jac = nlp.evaluate_jacobian() # create gradient of objective df = nlp.evaluate_grad_objective() # create KKT system kkt = BlockMatrix(2, 2) kkt.set_block(0, 0, identity(nx)) kkt.set_block(1, 0, jac) kkt.set_block(0, 1, jac.transpose()) zeros = np.zeros(nc) rhs = BlockVector(2) rhs.set_block(0, -df) rhs.set_block(1, zeros) flat_kkt = kkt.tocoo().tocsc() flat_rhs = rhs.flatten() sol = spsolve(flat_kkt, flat_rhs) return sol[nlp.n_primals():nlp.n_primals() + nlp.n_constraints()]
def main(show_plot=True): if show_plot: import matplotlib.pylab as plt instance = create_problem(0.0, 10.0) # Discretize model using Orthogonal Collocation discretizer = pyo.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_new_vector('primals') x.fill(1.0) nlp.set_primals(x) lam = nlp.create_new_vector('duals') lam.fill(1.0) nlp.set_duals(lam) # Evaluate jacobian jac = nlp.evaluate_jacobian() if show_plot: plt.spy(jac) plt.title('Jacobian of the constraints\n') plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.evaluate_hessian_lag() if show_plot: plt.spy(hess_lag) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockMatrix(2, 2) kkt.set_block(0, 0, hess_lag) kkt.set_block(1, 0, jac) kkt.set_block(0, 1, jac.transpose()) if show_plot: plt.spy(kkt.tocoo()) plt.title('KKT system\n') plt.show()
def _create_hessian_structure(self): # Note: This method requires the complicated vars map to be # created beforehand hess_lag = BlockMatrix(self.nblocks + 1, self.nblocks + 1) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() yi = nlp.y_init() hess_lag.set_block(sid, sid, nlp.hessian_lag(xi, yi)) hess_lag[self.nblocks, self.nblocks] = coo_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
def test_abs(self): row = np.array([0, 3, 1, 2, 3, 0]) col = np.array([0, 0, 1, 2, 3, 3]) data = -1.0 * np.array([2., 1, 3, 4, 5, 1]) m = coo_matrix((data, (row, col)), shape=(4, 4)) self.block_m = m bm = BlockMatrix(2, 2) bm.set_block(0, 0, m) bm.set_block(1, 1, m) bm.set_block(0, 1, m) abs_flat = abs(bm.tocoo()) abs_mat = abs(bm) self.assertIsInstance(abs_mat, BlockMatrix) self.assertTrue(np.allclose(abs_flat.toarray(), abs_mat.toarray()))
# Interface pyomo model with nlp nlp = PyomoNLP(instance) x = nlp.create_new_vector('primals') x.fill(1.0) nlp.set_primals(x) lam = nlp.create_new_vector('duals') lam.fill(1.0) nlp.set_duals(lam) # Evaluate jacobian jac = nlp.evaluate_jacobian() plt.spy(jac) plt.title('Jacobian of the constraints\n') plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.evaluate_hessian_lag() plt.spy(hess_lag) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockMatrix(2, 2) kkt.set_block(0, 0, hess_lag) kkt.set_block(1, 0, jac) kkt.set_block(0, 1, jac.transpose()) plt.spy(kkt.tocoo()) plt.title('KKT system\n') plt.show()
def _create_jacobian_structures(self): # Note: This method requires the complicated vars map to be # created beforehand # build general jacobian jac_g = BlockMatrix(2 * self.nblocks, self.nblocks + 1) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_g.set_block(sid, sid, nlp.jacobian_g(xi)) # coupling matrices Ai scenario_vids = self._zid_to_vid[sid] col = np.array([vid for vid in scenario_vids]) row = np.arange(0, self.nz) data = np.ones(self.nz, dtype=np.double) jac_g[sid + self.nblocks, sid] = coo_matrix( (data, (row, col)), shape=(self.nz, nlp.nx)) # coupling matrices Bi jac_g[sid + self.nblocks, self.nblocks] = -identity(self.nz) self._internal_jacobian_g = jac_g flat_jac_g = jac_g.tocoo() self._irows_jac_g = flat_jac_g.row self._jcols_jac_g = flat_jac_g.col self._nnz_jac_g = flat_jac_g.nnz # build jacobian equality constraints jac_c = BlockMatrix(2 * self.nblocks, self.nblocks + 1) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_c.set_block(sid, sid, nlp.jacobian_c(xi)) # coupling matrices Ai scenario_vids = self._zid_to_vid[sid] col = np.array([vid for vid in scenario_vids]) row = np.arange(0, self.nz) data = np.ones(self.nz, dtype=np.double) jac_c[sid + self.nblocks, sid] = coo_matrix( (data, (row, col)), shape=(self.nz, nlp.nx)) # coupling matrices Bi jac_c[sid + self.nblocks, self.nblocks] = -identity(self.nz) self._internal_jacobian_c = jac_c flat_jac_c = jac_c.tocoo() self._irows_jac_c = flat_jac_c.row self._jcols_jac_c = flat_jac_c.col self._nnz_jac_c = flat_jac_c.nnz # build jacobian inequality constraints jac_d = BlockMatrix(self.nblocks, self.nblocks) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_d.set_block(sid, sid, nlp.jacobian_d(xi)) self._internal_jacobian_d = jac_d flat_jac_d = jac_d.tocoo() self._irows_jac_d = flat_jac_d.row self._jcols_jac_d = flat_jac_d.col self._nnz_jac_d = flat_jac_d.nnz
plt.title('Jacobian of the all constraints\n') plt.show() # Evaluate jacobian of the equality constraints jac = nlp.evaluate_jacobian_eq() plt.title('Jacobian of the equality constraints\n') plt.spy(jac) plt.show() # Evaluate jacobian of the inequality constraints jac = nlp.evaluate_jacobian_ineq() plt.title('Jacobian of the inequality constraints\n') plt.spy(jac) plt.show() # Evaluate hessian of the lagrangian hess_lag = nlp.evaluate_hessian_lag() plt.spy(hess_lag.tocoo()) plt.title('Hessian of the Lagrangian function\n') plt.show() # Build KKT matrix kkt = BlockMatrix(2, 2) kkt.set_block(0, 0, hess_lag) kkt.set_block(1, 0, jac_full) kkt.set_block(0, 1, jac_full.transpose()) full_kkt = kkt.tocoo() plt.spy(full_kkt) plt.title('Karush-Kuhn-Tucker Matrix\n') plt.show()
def _create_jacobian_structures(self): # Note: This method requires the complicated vars map to be # created beforehand # build general jacobian jac_g = BlockMatrix(2 * self.nblocks, self.nblocks + 1) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_g[sid, sid] = nlp.jacobian_g(xi) # coupling matrices Ai scenario_vids = self._zid_to_vid[sid] col = np.array([vid for vid in scenario_vids]) row = np.arange(0, self.nz) data = np.ones(self.nz, dtype=np.double) jac_g[sid + self.nblocks, sid] = coo_matrix((data, (row, col)), shape=(self.nz, nlp.nx)) # coupling matrices Bi jac_g[sid + self.nblocks, self.nblocks] = -identity(self.nz) self._internal_jacobian_g = jac_g flat_jac_g = jac_g.tocoo() self._irows_jac_g = flat_jac_g.row self._jcols_jac_g = flat_jac_g.col self._nnz_jac_g = flat_jac_g.nnz # build jacobian equality constraints jac_c = BlockMatrix(2 * self.nblocks, self.nblocks + 1) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_c[sid, sid] = nlp.jacobian_c(xi) # coupling matrices Ai scenario_vids = self._zid_to_vid[sid] col = np.array([vid for vid in scenario_vids]) row = np.arange(0, self.nz) data = np.ones(self.nz, dtype=np.double) jac_c[sid + self.nblocks, sid] = coo_matrix((data, (row, col)), shape=(self.nz, nlp.nx)) # coupling matrices Bi jac_c[sid + self.nblocks, self.nblocks] = -identity(self.nz) self._internal_jacobian_c = jac_c flat_jac_c = jac_c.tocoo() self._irows_jac_c = flat_jac_c.row self._jcols_jac_c = flat_jac_c.col self._nnz_jac_c = flat_jac_c.nnz # build jacobian inequality constraints jac_d = BlockMatrix(self.nblocks, self.nblocks) for sid, nlp in enumerate(self._nlps): xi = nlp.x_init() jac_d[sid, sid] = nlp.jacobian_d(xi) self._internal_jacobian_d = jac_d flat_jac_d = jac_d.tocoo() self._irows_jac_d = flat_jac_d.row self._jcols_jac_d = flat_jac_d.col self._nnz_jac_d = flat_jac_d.nnz