def setUpClass(cls): # test problem 1 G = np.array([[6, 2, 1], [2, 5, 2], [1, 2, 4]]) A = np.array([[1, 0, 1], [0, 1, 1]]) b = np.array([3, 0]) c = np.array([-8, -3, -3]) cls.model = create_basic_dense_qp(G, A, b, c) cls.pyomo_nlp = PyomoNLP(cls.model) cls.coupling_vars = [ cls.pyomo_nlp.variable_idx(cls.model.x[0]), cls.pyomo_nlp.variable_idx(cls.model.x[2]) ] cls.nlp = AdmmNLP(cls.pyomo_nlp, cls.coupling_vars, rho=2.0) # test problem 2 cls.model2 = create_model2() cls.pyomo_nlp2 = PyomoNLP(cls.model2) cls.coupling_vars2 = [ cls.pyomo_nlp2.variable_idx(cls.model2.x[1]), cls.pyomo_nlp2.variable_idx(cls.model2.x[3]), cls.pyomo_nlp2.variable_idx(cls.model2.x[5]) ] cls.nlp2 = AdmmNLP(cls.pyomo_nlp2, cls.coupling_vars2, rho=1.0) # test problem 3 cls.model3 = create_basic_model() cls.pyomo_nlp3 = PyomoNLP(cls.model3) cls.coupling_vars3 = [cls.pyomo_nlp3.variable_idx(cls.model3.x[1])] cls.nlp3 = AdmmNLP(cls.pyomo_nlp3, cls.coupling_vars3, rho=1.0)
def test_z_estimates(self): z_estimates = np.array([5.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=2.0, z_estimates=z_estimates) self.assertTrue(np.allclose(nlp.z_estimates, z_estimates)) z_estimates = np.array([6.0, 5.0]) nlp.z_estimates = z_estimates self.assertTrue(np.allclose(nlp.z_estimates, z_estimates)) self.assertEqual(len(nlp.create_vector_z()), 2)
def test_w_estimates(self): w_estimates = np.array([5.0, 5.0]) nlp = AdmmNLP(self.pyomo_nlp, self.coupling_vars, rho=2.0, w_estimates=w_estimates) self.assertTrue(np.allclose(nlp.w_estimates(), w_estimates)) w_estimates = np.array([6.0, 5.0]) nlp.set_w_estimates(w_estimates) self.assertTrue(np.allclose(nlp.w_estimates(), w_estimates)) self.assertEqual(len(nlp.create_vector_w()), 2)
def setUpClass(cls): # test problem 1 G = np.array([[6, 2, 1], [2, 5, 2], [1, 2, 4]]) A = np.array([[1, 0, 1], [0, 1, 1]]) b = np.array([3, 0]) c = np.array([-8, -3, -3]) cls.model = create_basic_dense_qp(G, A, b, c) cls.pyomo_nlp = PyomoNLP(cls.model) cls.coupling_vars = [ cls.pyomo_nlp.variable_idx(cls.model.x[0]), cls.pyomo_nlp.variable_idx(cls.model.x[2]) ] cls.nlp = AdmmNLP(cls.pyomo_nlp, cls.coupling_vars, rho=2.0)
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))