예제 #1
0
def test_parametrize_control_time_dependent_polynomial(model):
    model.tau = SX.sym('tau')
    model.create_control('u', 3)

    # Test parametrize by a time dependent polynomial
    u_par = SX.sym("u_par", 3, 2)
    u_expr = model.tau * u_par[:, 0] + (1 - model.tau) * u_par[:, 1]
    model.parametrize_control(model.u, u_expr, vec(u_par))
    assert is_equal(model.u_par, vec(u_par))
    assert is_equal(model.u_expr, u_expr, 30)
    for ind in range(model.n_u):
        assert is_equal(model._parametrized_controls[ind], model.u[ind])
def test_include_theta(model):
    new_theta_1 = SX.sym("new_theta")
    new_theta_2 = SX.sym("new_theta_2", 2)

    model_n_theta = model.n_theta
    model.include_theta(new_theta_1)

    assert model.n_theta == model_n_theta + 1
    assert is_equal(model.theta[-1], new_theta_1)

    model.include_theta(new_theta_2)
    assert model.n_theta == model_n_theta + 1 + 2
    assert is_equal(model.theta[-3], new_theta_1)
    assert is_equal(model.theta[-2:], new_theta_2)
예제 #3
0
def test_include_control(model: ControlMixin):
    new_u_1 = SX.sym("new_u")
    new_u_2 = SX.sym("new_u_2", 2)

    model_n_u = model.n_u
    model.include_control(new_u_1)

    assert model.n_u == model_n_u + 1
    assert (is_equal(model.u[-1], new_u_1))

    model.include_control(new_u_2)
    assert model.n_u == model_n_u + 1 + 2
    assert is_equal(model.u[-3], new_u_1)
    assert is_equal(model.u[-2:], new_u_2)
def test_include_parameter(model):
    new_p_1 = SX.sym("new_p")
    new_p_2 = SX.sym("new_p_2", 2)

    model_n_p = model.n_p
    model.include_parameter(new_p_1)

    assert model.n_p == model_n_p + 1
    assert is_equal(model.p[-1], new_p_1)

    model.include_parameter(new_p_2)
    assert model.n_p == model_n_p + 1 + 2
    assert is_equal(model.p[-3], new_p_1)
    assert is_equal(model.p[-2:], new_p_2)
예제 #5
0
def test_include_algebraic(model):
    new_y_1 = SX.sym("new_y")
    new_y_2 = SX.sym("new_y_2", 2)

    model_n_y = model.n_y
    alg = new_y_1 - 3
    model.include_algebraic(new_y_1, alg=alg)

    assert model.n_y == model_n_y + 1
    assert is_equal(model.y[-1], new_y_1)
    assert is_equal(model.alg[-1], alg, 10)

    model.include_algebraic(new_y_2)
    assert model.n_y == model_n_y + 1 + 2
    assert is_equal(model.y[-3], new_y_1)
    assert is_equal(model.y[-2:], new_y_2)
예제 #6
0
def test_parametrize_control_list_input(model):
    model.tau = SX.sym('tau')
    model.create_control('u', 3)

    # Test for list inputs, parametrize by a time dependent polynomial
    u_par = SX.sym("u_par", 3, 2)
    u_expr = model.tau * u_par[:, 0] + (1 - model.tau) * u_par[:, 1]
    model.parametrize_control(
        [model.u[ind] for ind in range(model.n_u)],
        [u_expr[ind] for ind in range(model.n_u)],
        [vec(u_par)[ind] for ind in range(u_par.numel())],
    )

    assert is_equal(model.u_par, vec(u_par))
    assert is_equal(model.u_expr, u_expr, 30)
    for ind in range(model.n_u):
        assert is_equal(model._parametrized_controls[ind], model.u[ind])
예제 #7
0
def test_replace_variable_u_par(model):
    # replace a u_par
    new_u_par = SX.sym("new_u", model.n_u)
    new_u_expr = new_u_par

    model.replace_variable(model.u_par, new_u_par)

    assert is_equal(model.u_expr, new_u_expr, 30)
예제 #8
0
def test_include_state(model):
    new_x_1 = SX.sym("new_x")
    new_x_2 = SX.sym("new_x_2", 2)

    model_n_x = model.n_x
    new_x_0_1 = model.include_state(new_x_1)

    assert model.n_x == model_n_x + 1
    assert model.x_0.numel() == model_n_x + 1
    assert new_x_0_1.numel() == new_x_1.numel()
    assert is_equal(model.x[-1], new_x_1)

    model_n_x = model.n_x
    new_x_0_2 = model.include_state(new_x_2)

    assert model.n_x == model_n_x + 2
    assert model.x_0.numel(), model_n_x + 2
    assert new_x_0_2.numel() == new_x_2.numel()
    assert is_equal(model.x[-3], new_x_1)
    assert is_equal(model.x[-2:], new_x_2)
def test_remove_theta(model):
    model.create_theta("par", 3)
    ind_to_remove = 0
    to_remove = model.theta[ind_to_remove]
    n_theta_original = model.n_theta

    model.remove_theta(to_remove)

    # removed var
    assert model.n_theta == n_theta_original - 1
    for ind in range(model.n_theta):
        assert not is_equal(model.theta[ind], to_remove)
예제 #10
0
def test_remove_parameter(model):
    model.create_parameter("par", 3)
    ind_to_remove = 1
    to_remove = model.p[ind_to_remove]
    n_p_original = model.n_p

    model.remove_parameter(to_remove)

    # removed var
    assert model.n_p == n_p_original - 1
    for ind in range(model.n_p):
        assert not is_equal(model.p[ind], to_remove)
예제 #11
0
def test_remove_control(model: ControlMixin):
    model.create_control('u', 4)
    ind_to_remove = 2
    to_remove = model.u[ind_to_remove]
    n_u_original = model.n_u

    model.remove_control(to_remove)

    # removed var
    assert model.n_u == n_u_original - 1
    for ind in range(model.n_u):
        assert not is_equal(model.u[ind], to_remove)
예제 #12
0
def test_remove_algebraic(model):
    y = model.create_algebraic_variable('y', 4)
    model.include_equations(alg=[i * y_i for i, y_i in enumerate(y.nz)])

    ind_to_remove = 3
    to_remove = model.y[ind_to_remove]
    to_remove_eq = model.alg[ind_to_remove]

    n_y_original = model.n_y
    n_alg_original = model.alg.numel()

    model.remove_algebraic(to_remove, eq=to_remove_eq)

    # removed var
    assert model.n_y == n_y_original - 1
    for ind in range(model.n_y):
        assert not is_equal(model.y[ind], to_remove)

    # removed alg
    assert model.alg.numel() == n_alg_original - 1
    for ind in range(model.alg.numel()):
        assert not is_equal(model.alg[ind], to_remove_eq)
예제 #13
0
def test_remove_state(model: StateMixin):
    x = model.create_state('x', 5)
    model.include_equations(
        ode=vertcat(*[i * x_i for i, x_i in enumerate(x.nz)]), x=x)

    ind_to_remove = 3
    to_remove = model.x[ind_to_remove]
    to_remove_eq = model.ode[ind_to_remove]

    n_x_original = model.n_x
    n_ode_original = model.ode.numel()

    model.remove_state(to_remove)

    # removed var
    assert model.n_x == n_x_original - 1
    for ind in range(model.n_x):
        assert not is_equal(model.x[ind], to_remove)

    # removed ode
    assert model.ode.numel() == n_ode_original - 1
    for ind in range(model.ode.numel()):
        assert not is_equal(model.ode[ind], to_remove_eq)
예제 #14
0
    def control_is_parametrized(self, u):
        """
            Check if  the control "u" is parametrized

        :param casadi.SX u:
        :rtype bool:
        """
        u = vertcat(u)
        if not u.numel() == 1:
            raise ValueError(
                'The parameter "u" is expected to be of size 1x1, given: {}x{}'
                .format(*u.shape))
        if any([
                is_equal(u, parametrized_u)
                for parametrized_u in self._parametrized_controls
        ]):
            return True
        return False
예제 #15
0
def test_create_state(model: StateMixin):
    n_x_initial = model.n_x
    n_new_x = 4
    x = model.create_state("x", n_new_x)
    assert model.n_x == n_x_initial + n_new_x
    assert is_equal(model.x[-n_new_x:], x)
예제 #16
0
def test_create_control(model: ControlMixin):
    n_u_initial = model.n_u
    n_new_u = 4
    u = model.create_control("u", n_new_u)
    assert model.n_u == n_u_initial + n_new_u
    assert is_equal(model.u[-n_new_u:], u)
예제 #17
0
def test_include_equations_alg(model):
    y = model.create_algebraic_variable('y')
    alg = -y

    model.include_equations(alg=alg)
    assert is_equal(model.alg, -y, 20)
예제 #18
0
def test_include_equations_w_equality(model):
    y = model.create_algebraic_variable('y')

    model.include_equations(y == 2)

    assert is_equal(model.alg, y - 2, 10)
예제 #19
0
def test_create_algebraic_variable(model):
    n_y_initial = model.n_y
    n_new_y = 4
    y = model.create_algebraic_variable("y", n_new_y)
    assert model.n_y == n_y_initial + n_new_y
    assert is_equal(model.y[-n_new_y:], y)
예제 #20
0
def test_create_parameter(model):
    n_p_initial = model.n_p
    n_new_p = 4
    p = model.create_parameter("p", n_new_p)
    assert model.n_p == n_p_initial + n_new_p
    assert is_equal(model.p[-n_new_p:], p)
예제 #21
0
def test_create_theta(model):
    n_theta_initial = model.n_theta
    n_new_theta = 4
    theta = model.create_theta("theta", n_new_theta)
    assert model.n_theta == n_theta_initial + n_new_theta
    assert is_equal(model.theta[-n_new_theta:], theta)