예제 #1
0
def test_penalty_custom_with_bounds_failing_max_bound(value):
    def custom_with_bounds(ocp, nlp, t, x, u, p):
        my_values = DM.zeros((12, 1)) + x[0]
        return -10, my_values, 10

    ocp = prepare_test_ocp()
    x = [DM.ones((12, 1)) * value]

    penalty_type = ConstraintFcn.CUSTOM
    penalty = Constraint(penalty_type)

    penalty.max_bound = 0
    penalty.custom_function = custom_with_bounds

    with pytest.raises(RuntimeError):
        penalty_type.value[0](penalty, ocp, ocp.nlp[0], [], x, [], [])
예제 #2
0
def test_penalty_custom_with_bounds_failing_min_bound(value):
    def custom_with_bounds(pn):
        return -10, pn.nlp.states["q"].cx, 10

    ocp = prepare_test_ocp()
    t = [0]
    x = [DM.ones((12, 1)) * value]
    u = [0]

    penalty_type = ConstraintFcn.CUSTOM
    penalty = Constraint(penalty_type)

    penalty.min_bound = 0
    penalty.custom_function = custom_with_bounds

    with pytest.raises(RuntimeError):
        penalty_type.value[0](penalty,
                              PenaltyNodeList(ocp, ocp.nlp[0], t, x, [], []))
예제 #3
0
def test_penalty_custom_with_bounds(value):
    def custom_with_bounds(ocp, nlp, t, x, u, p):
        my_values = DM.zeros((12, 1)) + x[0]
        return -10, my_values, 10

    ocp = prepare_test_ocp()
    x = [DM.ones((12, 1)) * value]

    penalty_type = ConstraintFcn.CUSTOM
    penalty = Constraint(penalty_type)

    penalty.custom_function = custom_with_bounds
    penalty_type.value[0](penalty, ocp, ocp.nlp[0], [], x, [], [])

    res = ocp.nlp[0].g[0][0]["val"]

    np.testing.assert_almost_equal(res, np.array([[value]] * 12))
    np.testing.assert_almost_equal(ocp.nlp[0].g[0][0]["bounds"].min,
                                   np.array([[-10]] * 12))
    np.testing.assert_almost_equal(ocp.nlp[0].g[0][0]["bounds"].max,
                                   np.array([[10]] * 12))
예제 #4
0
def test_penalty_custom_with_bounds_failing_max_bound(value):
    def custom_with_bounds(pn):
        return -10, pn.nlp.states["q"].cx, 10

    ocp = prepare_test_ocp()
    t = [0]
    x = [DM.ones((12, 1)) * value]
    u = [0]

    penalty_type = ConstraintFcn.CUSTOM
    penalty = Constraint(penalty_type)

    penalty.max_bound = 0
    penalty.custom_function = custom_with_bounds

    with pytest.raises(
            RuntimeError,
            match=
            "You cannot have non linear bounds for custom constraints and min_bound or max_bound defined",
    ):
        penalty_type.value[0](penalty,
                              PenaltyNodeList(ocp, ocp.nlp[0], t, x, [], []))