Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
 def evaluate_jacobian_outputs(self):
     c = self._input_values[1]
     F = self._input_values[2]
     irow = np.asarray([0, 0, 0, 1, 1, 1], dtype=np.int64)
     jcol = np.asarray([1, 2, 3, 0, 1, 2], dtype=np.int64)
     nonzeros = np.asarray([-F**2, -c*2*F, 1, 1, -4*F**2, -4*c*2*F], dtype=np.float64)
     jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(2,5))
     return jac
Ejemplo n.º 4
0
 def evaluate_jacobian_equality_constraints(self):
     c = self._input_values[1]
     F = self._input_values[2]
     irow = np.asarray([0, 0, 0, 0, 1, 1, 1, 1], dtype=np.int64)
     jcol = np.asarray([0, 1, 2, 3, 1, 2, 3, 4], dtype=np.int64)
     nonzeros = np.asarray([-1, F**2, 2*c*F, 1, 2*F**2, 4*c*F, -1, 1], dtype=np.float64)
     jac = spa.coo_matrix((nonzeros, (irow, jcol)), shape=(2,5))
     return jac
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
    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))
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    def test_model1(self):
        model = create_model1()
        nlp = PyomoNLP(model)
        cynlp = CyIpoptNLP(nlp)

        # 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))
Ejemplo n.º 12
0
 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
Ejemplo n.º 13
0
 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
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
 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