def evaluate_hessian_equality_constraints(self): N = self._N F1 = self._F1 F2 = self._F2 h1 = self._h1 h2 = self._h2 A1 = self._A1 A2 = self._A2 c1 = self._c1 c2 = self._c2 dt = self._dt lam = self._eq_con_mult_values nnz = 2 * (N - 1) irow = np.zeros(nnz, dtype=np.int64) jcol = np.zeros(nnz, dtype=np.int64) data = np.zeros(nnz, dtype=np.float64) idx = 0 for i in range(N - 1): irow[idx] = 2 * (N - 1) + i + 1 jcol[idx] = 2 * (N - 1) + i + 1 data[idx] = lam[i] * dt / A1 * (-c1 / 4) * h1[i + 1]**(-1.5) + lam[ (N - 1) + i] * dt / A2 * (c1 / 4) * h1[i + 1]**(-1.5) idx += 1 irow[idx] = 2 * (N - 1) + N + i + 1 jcol[idx] = 2 * (N - 1) + N + i + 1 data[idx] = lam[(N - 1) + i] * dt / A2 * (-c2 / 4) * h2[i + 1]**(-1.5) idx += 1 assert idx == nnz hess = spa.coo_matrix((data, (irow, jcol)), shape=(2 * (N - 1) + 2 * N, 2 * (N - 1) + 2 * N)) return hess
def evaluate_jacobian_outputs(self): N = self._N F1 = self._F1 F2 = self._F2 h1 = self._h1 h2 = self._h2 A1 = self._A1 A2 = self._A2 c1 = self._c1 c2 = self._c2 dt = self._dt nnz = 2 * N irow = np.zeros(nnz, dtype=np.int64) jcol = np.zeros(nnz, dtype=np.int64) data = np.zeros(nnz, dtype=np.float64) idx = 0 # Jac F12 for i in range(N): irow[idx] = i jcol[idx] = 2 * (N - 1) + i data[idx] = 1 / 2 * c1 * h1[i]**(-0.5) idx += 1 for i in range(N): irow[idx] = N + i jcol[idx] = 2 * (N - 1) + N + i data[idx] = 1 / 2 * c2 * h2[i]**(-0.5) idx += 1 assert idx == nnz return spa.coo_matrix((data, (irow, jcol)), shape=(2 * N, 2 * (N - 1) + 2 * N))
def evaluate_derivatives(self): jac = [[1, -self._F**2, 0, -2 * self._c1 * self._F], [ 1, -self._F**2, -self._F**2, -2 * self._F * (self._c1 + self._c2) ]] jac = np.asarray(jac, dtype=np.float64) return spa.coo_matrix(jac)
def evaluate_jacobian_outputs(self): c = self._input_values[1] F = self._input_values[2] irow = np.asarray([0, 0, 0], dtype=np.int64) jcol = np.asarray([0, 1, 2], dtype=np.int64) nonzeros = np.asarray([1, -4 * F**2, -4 * c * 2 * F], dtype=np.float64) jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(1, 3)) return jac
def evaluate_hessian_outputs(self): c = self._input_values[1] F = self._input_values[2] irow = np.asarray([2, 2], dtype=np.int64) jcol = np.asarray([1, 2], dtype=np.int64) data = self._output_con_mult_values[0] * np.asarray([-8 * F, -8 * c], dtype=np.float64) hess = spa.coo_matrix((data, (irow, jcol)), shape=(3, 3)) return hess
def evaluate_hessian_equality_constraints(self): c = self._input_values[1] F = self._input_values[2] irow = np.asarray([2, 2], dtype=np.int64) jcol = np.asarray([1, 2], dtype=np.int64) nonzeros = self._eq_con_mult_values[0] * np.asarray([8 * F, 8 * c], dtype=np.float64) hess = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(4, 4)) return hess
def evaluate_jacobian_equality_constraints(self): c = self._input_values[1] F = self._input_values[2] irow = np.asarray([0, 0, 0, 0], dtype=np.int64) jcol = np.asarray([0, 1, 2, 3], dtype=np.int64) nonzeros = np.asarray([-1, 4 * F**2, 4 * 2 * c * F, 1], dtype=np.float64) jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(1, 4)) return jac
def evaluate_jacobian_equality_constraints(self): N = self._N F1 = self._F1 F2 = self._F2 h1 = self._h1 h2 = self._h2 A1 = self._A1 A2 = self._A2 c1 = self._c1 c2 = self._c2 dt = self._dt nnz = 3 * (N - 1) + 4 * (N - 1) irow = np.zeros(nnz, dtype=np.int64) jcol = np.zeros(nnz, dtype=np.int64) data = np.zeros(nnz, dtype=np.float64) idx = 0 # Jac h1bal for i in range(N - 1): irow[idx] = i jcol[idx] = i data[idx] = -dt / A1 idx += 1 irow[idx] = i jcol[idx] = 2 * (N - 1) + i data[idx] = -1 idx += 1 irow[idx] = i jcol[idx] = 2 * (N - 1) + i + 1 data[idx] = 1 + dt / A1 * c1 * 1 / 2 * (h1[i + 1])**(-0.5) idx += 1 # Jac h2bal for i in range(N - 1): irow[idx] = i + (N - 1) jcol[idx] = i + (N - 1) data[idx] = -dt / A2 idx += 1 irow[idx] = i + (N - 1) jcol[idx] = 2 * (N - 1) + i + 1 data[idx] = -dt / A2 * c1 * 1 / 2 * (h1[i + 1])**(-0.5) idx += 1 irow[idx] = i + (N - 1) jcol[idx] = 2 * (N - 1) + N + i data[idx] = -1 idx += 1 irow[idx] = i + (N - 1) jcol[idx] = 2 * (N - 1) + N + i + 1 data[idx] = 1 + dt / A2 * c2 * 1 / 2 * (h2[i + 1])**(-0.5) idx += 1 assert idx == nnz return spa.coo_matrix((data, (irow, jcol)), shape=(2 * (N - 1), 2 * (N - 1) + 2 * N))
def evaluate_hessian_outputs(self): c = self._input_values[1] F = self._input_values[2] y1 = self._output_con_mult_values[0] y2 = self._output_con_mult_values[1] irow = np.asarray([2, 2], dtype=np.int64) jcol = np.asarray([1, 2], dtype=np.int64) nonzeros = np.asarray( [y1 * (-2 * F) + y2 * (-8 * F), y1 * (-2 * c) + y2 * (-8 * c)], dtype=np.float64) hess = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(5, 5)) return hess
def test_explicit_zeros(self): m = pyo.ConcreteModel() m.x = pyo.Var(initialize=1.0) m.y = pyo.Var(initialize=0.0) m.eqn = pyo.Constraint(expr=m.x**2 + m.y**3 == 1.0) variables = [m.x, m.y] row = np.array([0, 1]) col = np.array([0, 1]) data = np.array([2.0, 0.0]) expected_hess = sps.coo_matrix((data, (row, col)), shape=(2, 2)) hess = get_hessian_of_constraint(m.eqn, variables) np.testing.assert_allclose(hess.row, row, atol=0) np.testing.assert_allclose(hess.col, col, atol=0) np.testing.assert_allclose(hess.data, data, rtol=1e-8)
def evaluate_hessian_outputs(self): N = self._N F1 = self._F1 F2 = self._F2 h1 = self._h1 h2 = self._h2 A1 = self._A1 A2 = self._A2 c1 = self._c1 c2 = self._c2 dt = self._dt lam = self._output_con_mult_values nnz = 2 * N irow = np.zeros(nnz, dtype=np.int64) jcol = np.zeros(nnz, dtype=np.int64) data = np.zeros(nnz, dtype=np.float64) idx = 0 # Hess F12_t for i in range(N): irow[idx] = 2 * (N - 1) + i jcol[idx] = 2 * (N - 1) + i data[idx] = lam[i] * c1 * (-1 / 4) * h1[i]**(-1.5) idx += 1 # Hess Fo_t for i in range(N): irow[idx] = 2 * (N - 1) + N + i jcol[idx] = 2 * (N - 1) + N + i data[idx] = lam[N + i] * c2 * (-1 / 4) * h2[i]**(-1.5) idx += 1 assert idx == nnz hess = spa.coo_matrix((data, (irow, jcol)), shape=(2 * (N - 1) + 2 * N, 2 * (N - 1) + 2 * N)) return hess
def evaluate_hessian_outputs(self): irow = np.asarray([], dtype=np.int64) jcol = np.asarray([], dtype=np.int64) data = np.asarray([], dtype=np.float64) hess = spa.coo_matrix((data, (irow, jcol)), shape=(1, 1)) return hess
def evaluate_jacobian_equality_constraints(self): irow = np.asarray([0], dtype=np.int64) jcol = np.asarray([0], dtype=np.int64) nonzeros = np.asarray([2 * self._u], dtype=np.float64) jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(1, 1)) return jac
def evaluate_jacobian_outputs(self): irow = np.asarray([0], dtype=np.int64) jcol = np.asarray([0], dtype=np.int64) nonzeros = np.asarray([5.0], dtype=np.float64) jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(1, 1)) return jac
def evaluate_hessian_equality_constraints(self): irow = np.asarray([0], dtype=np.int64) jcol = np.asarray([0], dtype=np.int64) data = np.asarray([self._equality_mult * 2.0], dtype=np.float64) hess = spa.coo_matrix((data, (irow, jcol)), shape=(1, 1)) return hess
def _check_model1(self, nlp, cynlp): # test x_init expected_xinit = np.asarray([4.0, 4.0, 4.0], dtype=np.float64) xinit = cynlp.x_init() self.assertTrue(np.array_equal(xinit, expected_xinit)) # test x_lb expected_xlb = list() for v in nlp.get_pyomo_variables(): if v.lb == None: expected_xlb.append(-np.inf) else: expected_xlb.append(v.lb) expected_xlb = np.asarray(expected_xlb) xlb = cynlp.x_lb() self.assertTrue(np.array_equal(xlb, expected_xlb)) # test x_ub expected_xub = list() for v in nlp.get_pyomo_variables(): if v.ub == None: expected_xub.append(np.inf) else: expected_xub.append(v.ub) expected_xub = np.asarray(expected_xub) xub = cynlp.x_ub() self.assertTrue(np.array_equal(xub, expected_xub)) # test g_lb expected_glb = np.asarray([-np.inf, 0.0], dtype=np.float64) glb = cynlp.g_lb() self.assertTrue(np.array_equal(glb, expected_glb)) # test g_ub expected_gub = np.asarray([18, 0.0], dtype=np.float64) gub = cynlp.g_ub() print(expected_gub) print(gub) self.assertTrue(np.array_equal(gub, expected_gub)) x = cynlp.x_init() # test objective self.assertEqual(cynlp.objective(x), -504) # test gradient expected = np.asarray([-576, 8, 64], dtype=np.float64) self.assertTrue(np.allclose(expected, cynlp.gradient(x))) # test constraints expected = np.asarray([20, -5], dtype=np.float64) constraints = cynlp.constraints(x) self.assertTrue(np.allclose(expected, constraints)) # test jacobian expected = np.asarray([[8.0, 0, 1.0], [0.0, 8.0, 1.0]]) spexpected = spa.coo_matrix(expected).todense() rows, cols = cynlp.jacobianstructure() values = cynlp.jacobian(x) jac = spa.coo_matrix((values, (rows, cols)), shape=(len(constraints), len(x))).todense() self.assertTrue(np.allclose(spexpected, jac)) # test hessian y = constraints.copy() y.fill(1.0) rows, cols = cynlp.hessianstructure() values = cynlp.hessian(x, y, obj_factor=1.0) hess_lower = spa.coo_matrix((values, (rows, cols)), shape=(len(x), len(x))).todense() expected_hess_lower = np.asarray( [[-286.0, 0.0, 0.0], [0.0, 4.0, 0.0], [-144.0, 0.0, 192.0]], dtype=np.float64) self.assertTrue(np.allclose(expected_hess_lower, hess_lower))
def test_polynomial(self): m = pyo.ConcreteModel() n_x = 3 x1 = 1.1 x2 = 1.2 x3 = 1.3 m.x = pyo.Var(range(1, n_x + 1), initialize={1: x1, 2: x2, 3: x3}) m.eqn = pyo.Constraint(expr=5 * (m.x[1]**5) + # T1 5 * (m.x[1]**4) * (m.x[2]) + # T2 5 * (m.x[1]**3) * (m.x[2]) * (m.x[3]) + # T3 5 * (m.x[1]) * (m.x[2]**2) * (m.x[3]**2) + # T4 4 * (m.x[1]**2) * (m.x[2]) * (m.x[3]) + # T5 4 * (m.x[2]**2) * (m.x[3]**2) + # T6 4 * (m.x[3]**4) + # T7 3 * (m.x[1]) * (m.x[2]) * (m.x[3]) + # T8 3 * (m.x[2]**3) + # T9 3 * (m.x[2]**2) * (m.x[3]) + # T10 2 * (m.x[1]) * (m.x[2]) + # T11 2 * (m.x[2]) * (m.x[3]) # T12 == 0) rcd = [] rcd.append(( 0, 0, ( # wrt x1, x1 5 * 5 * 4 * x1**3 + # T1 5 * 4 * 3 * x1**2 * x2 + # T2 5 * 3 * 2 * x1 * x2 * x3 + # T3 4 * 2 * 1 * x2 * x3 # T5 ))) rcd.append(( 1, 1, ( # wrt x2, x2 5 * x1 * 2 * x3**2 + # T4 4 * 2 * x3**2 + # T6 3 * 3 * 2 * x2 + # T9 3 * 2 * x3 # T10 ))) rcd.append(( 2, 2, ( # wrt x3, x3 5 * x1 * x2**2 * 2 + # T4 4 * x2**2 * 2 + # T6 4 * 4 * 3 * x3**2 # T7 ))) rcd.append(( 1, 0, ( # wrt x2, x1 5 * 4 * x1**3 + # T2 5 * 3 * x1**2 * x3 + # T3 5 * 2 * x2 * x3**2 + # T4 4 * 2 * x1 * x3 + # T5 3 * x3 + # T8 2 # T11 ))) rcd.append(( 2, 0, ( # wrt x3, x1 5 * 3 * x1**2 * x2 + # T3 5 * x2**2 * 2 * x3 + # T4 4 * 2 * x1 * x2 + # T5 3 * x2 # T8 ))) rcd.append(( 2, 1, ( # wrt x3, x2 5 * x1**3 + # T3 5 * x1 * 2 * x2 * 2 * x3 + # T4 4 * x1**2 + # T5 4 * 2 * x2 * 2 * x3 + # T6 3 * x1 + # T8 3 * 2 * x2 + # T10 2 # T12 ))) row = [r for r, _, _ in rcd] col = [c for _, c, _ in rcd] data = [d for _, _, d in rcd] expected_hess = sps.coo_matrix((data, (row, col)), shape=(n_x, n_x)) expected_hess_array = expected_hess.toarray() expected_hess_array = (expected_hess_array + np.transpose(expected_hess_array) - np.diag(np.diagonal(expected_hess_array))) hess = get_hessian_of_constraint(m.eqn, list(m.x.values())) hess_array = hess.toarray() np.testing.assert_allclose(expected_hess_array, hess_array, rtol=1e-8)