def test_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) f = 0.5 * x.transpose().dot(hessian_base.dot(x)) + c.dot(x) difference = A.dot(x) - z_estimates f += w_estimates.dot(difference) f += 0.5 * rho * np.linalg.norm(difference)**2 self.assertEqual(f, nlp.objective(x))
def test_grad_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) df = hessian_base.dot(x) + c df += A.transpose().dot(w_estimates) df += rho * (A.transpose().dot(A).dot(x) - A.transpose().dot(z_estimates)) self.assertTrue(np.allclose(df, nlp.grad_objective(x)))
def test_grad_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) df = hessian_base.dot(x) + c df += A.transpose().dot(w_estimates) df += rho * (A.transpose().dot(A).dot(x) - A.transpose().dot(z_estimates)) self.assertTrue(np.allclose(df, nlp.grad_objective(x))) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=3.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=3.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertTrue(np.allclose(nlp.grad_objective(x), nl.grad_objective(x))) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=8.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=8.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() x.fill(1.0) self.assertTrue(np.allclose(nlp.grad_objective(x), nl.grad_objective(x)))
def test_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) f = 0.5 * x.transpose().dot(hessian_base.dot(x)) + c.dot(x) difference = A.dot(x) - z_estimates f += w_estimates.dot(difference) f += 0.5 * rho * np.linalg.norm(difference)**2 self.assertEqual(f, nlp.objective(x)) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=5.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=5.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertAlmostEqual(nlp.objective(x), nl.objective((x))) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=7.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=7.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertAlmostEqual(nlp.objective(x), nl.objective((x)))
def test_hessian_lag(self): hessian_base = self.model.hessian_f ata = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 1]], dtype=np.double) rho = self.nlp.rho admm_hessian = hessian_base + ata * rho x = self.nlp.create_vector_x() y = self.nlp.create_vector_y() hess_lag = self.nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() self.assertTrue(np.allclose(dense_hess_lag, admm_hessian)) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=7.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=7.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() y = nlp.create_vector_y() hess_lag = nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() hess_lagp = nl.hessian_lag(x, y) dense_hess_lagp = hess_lagp.todense() self.assertTrue(np.allclose(dense_hess_lag, dense_hess_lagp)) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=1.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=1.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() y = nlp.create_vector_y() hess_lag = nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() hess_lagp = nl.hessian_lag(x, y) dense_hess_lagp = hess_lagp.todense() self.assertTrue(np.allclose(dense_hess_lag, dense_hess_lagp))
def test_grad_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) df = hessian_base.dot(x) + c df += A.transpose().dot(w_estimates) df += rho * (A.transpose().dot(A).dot(x) - A.transpose().dot(z_estimates)) self.assertTrue(np.allclose(df, nlp.grad_objective(x))) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=3.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using( m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=3.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertTrue( np.allclose(nlp.grad_objective(x), nl.grad_objective(x))) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=8.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=8.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() x.fill(1.0) self.assertTrue( np.allclose(nlp.grad_objective(x), nl.grad_objective(x)))
def test_objective(self): w_estimates = np.array([5.0, 5.0]) z_estimates = np.array([2.0, 2.0]) rho = 2.0 nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=rho, w_estimates=w_estimates, z_estimates=z_estimates) hessian_base = self.model.hessian_f c = self.model.grad_f A = np.array([[1, 0, 0], [0, 0, 1]], dtype=np.double) x = nlp.create_vector_x() x.fill(1.0) f = 0.5 * x.transpose().dot(hessian_base.dot(x)) + c.dot(x) difference = A.dot(x) - z_estimates f += w_estimates.dot(difference) f += 0.5 * rho * np.linalg.norm(difference)**2 self.assertEqual(f, nlp.objective(x)) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=5.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using( m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=5.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertAlmostEqual(nlp.objective(x), nl.objective((x))) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=7.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=7.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() self.assertAlmostEqual(nlp.objective(x), nl.objective((x)))
def test_hessian_lag(self): hessian_base = self.model.hessian_f ata = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 1]], dtype=np.double) rho = self.nlp.rho admm_hessian = hessian_base + ata * rho x = self.nlp.create_vector_x() y = self.nlp.create_vector_y() hess_lag = self.nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() self.assertTrue(np.allclose(dense_hess_lag, admm_hessian)) # second nlp w_estimates = np.array([1.0, 2.0, 3.0]) z_estimates = np.array([3.0, 4.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp2, self.coupling_vars2, rho=7.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model2 transform = AdmmModel() aug_model = transform.create_using( m, complicating_vars=[m.x[1], m.x[3], m.x[5]], z_estimates=z_estimates, w_estimates=w_estimates, rho=7.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() y = nlp.create_vector_y() hess_lag = nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() hess_lagp = nl.hessian_lag(x, y) dense_hess_lagp = hess_lagp.todense() self.assertTrue(np.allclose(dense_hess_lag, dense_hess_lagp)) # third nlp w_estimates = np.array([1.0]) z_estimates = np.array([3.0]) nlp = AdmmNLP(self.pyomo_nlp3, self.coupling_vars3, rho=1.0, w_estimates=w_estimates, z_estimates=z_estimates) m = self.model3 transform = AdmmModel() aug_model = transform.create_using(m, complicating_vars=[m.x[1]], z_estimates=z_estimates, w_estimates=w_estimates, rho=1.0) nl = PyomoNLP(aug_model) x = nlp.create_vector_x() y = nlp.create_vector_y() hess_lag = nlp.hessian_lag(x, y) dense_hess_lag = hess_lag.todense() hess_lagp = nl.hessian_lag(x, y) dense_hess_lagp = hess_lagp.todense() self.assertTrue(np.allclose(dense_hess_lag, dense_hess_lagp))