Пример #1
0
def test_linear_transition_model(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)
    next_state = reservoir.transition(state, action, batch=False)

    model = reservoir.get_linear_transition(state, action, batch=False)
    f = model.f
    f_x = model.f_x
    f_u = model.f_u

    assert tf.reduce_all(tf.abs(f - next_state) < 1e-3)

    a_t = tf.squeeze(action)
    s_t = tf.squeeze(state)
    C = tf.squeeze(reservoir.max_res_cap)
    grad_v_s = tf.linalg.diag(1 / 2 *
                              (tf.cos(s_t / C) * s_t / C + tf.sin(s_t / C)))
    grad_F_s = tf.linalg.diag(a_t)
    grad_I_s = tf.matmul(reservoir.downstream,
                         tf.linalg.diag(a_t),
                         transpose_a=True)
    f_x_expected = tf.eye(
        reservoir.state_size) - grad_v_s - grad_F_s + grad_I_s
    assert f_x.shape == f_x_expected.shape
    assert tf.reduce_all(tf.abs(f_x - f_x_expected) < 1e-3)

    grad_F_a = tf.linalg.diag(s_t)
    grad_I_a = tf.matmul(reservoir.downstream,
                         tf.linalg.diag(s_t),
                         transpose_a=True)
    f_u_expected = -grad_F_a + grad_I_a
    assert f_u.shape == f_u_expected.shape
    assert tf.reduce_all(tf.abs(f_u - f_u_expected) < 1e-3)
Пример #2
0
def test_cost(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)

    cost = reservoir.cost(state, action)
    assert cost.shape == []
    assert cost >= 0.0
Пример #3
0
def test_cost(hvac):
    state = conftest.sample_state(hvac)
    action = conftest.sample_action(hvac)

    cost = hvac.cost(state, action)
    assert cost.shape == []
    assert cost >= 0.0
Пример #4
0
def test_outflow(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)

    outflow = reservoir._outflow(action, state)
    assert outflow.shape == [reservoir.state_size, 1]

    assert tf.reduce_all(tf.abs(outflow / state - action) < 1e-3)
Пример #5
0
def test_outflow_batch(reservoir):
    batch_size = 10
    state = sample_state(reservoir, batch_size)
    action = sample_action(reservoir, batch_size)

    outflow = reservoir._outflow(action, state)
    assert outflow.shape == [batch_size, reservoir.state_size, 1]

    assert tf.reduce_all(tf.abs(outflow / state - action) < 1e-3)
Пример #6
0
def test_transition_batch(env, batch_size):
    state = sample_state(env, batch_size)
    action = sample_action(env, batch_size)

    next_state = env.transition(state, action, batch=True)
    assert next_state.shape == [batch_size, env.state_size, 1]

    for i, (s, a) in enumerate(zip(state, action)):
        next_s = env.transition(s, a, batch=False)
        assert tf.reduce_all(next_s == next_state[i])
Пример #7
0
def test_transition(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)

    next_state = reservoir.transition(state, action, batch=False)
    assert next_state.shape == state.shape

    balance = (tf.reduce_sum(state) +
               tf.reduce_sum(reservoir.rain_shape * reservoir.rain_scale) -
               tf.reduce_sum(reservoir._vaporated(state)) -
               action[-1] * state[-1] - tf.reduce_sum(next_state))
    assert tf.abs(balance) < 1e-3
Пример #8
0
def test_quadratic_cost_batch(env, batch_size):
    state = sample_state(env, batch_size)
    action = sample_action(env, batch_size)

    model = env.get_quadratic_cost(state, action, batch=True)

    for i, (s, a) in enumerate(zip(state, action)):
        m_i = env.get_quadratic_cost(s, a, batch=False)
        assert len(model) == len(m_i) == 7
        for l1, l2 in zip(model, m_i):
            assert l1.shape[0] == batch_size
            assert l1[i].shape == l2.shape
            assert tf.reduce_all(tf.abs(l1[i] - l2) < 1e-3)
Пример #9
0
def test_linear_transition_batch(env, batch_size):
    state = sample_state(env, batch_size)
    action = sample_action(env, batch_size)

    model = env.get_linear_transition(state, action, batch=True)

    for i, (s, a) in enumerate(zip(state, action)):
        m_i = env.get_linear_transition(s, a, batch=False)
        assert len(m_i) == len(model) == 3
        for f1, f2 in zip(model, m_i):
            assert f1.shape[0] == batch_size
            assert f1[i].shape == f2.shape
            assert tf.reduce_all(tf.abs(f1[i] - f2) < 1e-3)
Пример #10
0
def test_cost_batch(env, batch_size):
    state = sample_state(env, batch_size)
    action = sample_action(env, batch_size)

    cost = env.cost(state, action, batch=True)
    assert cost.shape == [
        batch_size,
    ]
    assert tf.reduce_all(cost >= 0.0)

    for i, (s, a) in enumerate(zip(state, action)):
        c = env.cost(s, a, batch=False)
        assert tf.reduce_all(c == cost[i])
Пример #11
0
def test_inflow(reservoir):
    outflow = sample_action(reservoir)
    assert outflow.shape == [reservoir.action_size, 1]
    assert tf.reduce_all(outflow >= 0.0)
    assert tf.reduce_all(outflow <= 1.0)

    value = reservoir._inflow(outflow)
    assert value.shape == [reservoir.state_size, 1]

    assert tf.reduce_all(value[0] == 0.0)
    assert tf.reduce_all(value[1:] == outflow[:-1])

    balance = (tf.reduce_sum(value) + outflow[-1] - tf.reduce_sum(outflow))
    assert tf.abs(balance) < 1e-3
Пример #12
0
def test_linear_transition(hvac):
    state = conftest.sample_state(hvac)
    action = conftest.sample_action(hvac)

    model = hvac.get_linear_transition(state, action, batch=False)

    # check f
    next_state = hvac.transition(state, action, batch=False)
    assert model.f.shape == next_state.shape
    assert tf.reduce_all(model.f == next_state)

    # check f_x
    air = action * hvac.air_max
    heating_x = - tf.linalg.diag(tf.squeeze(air * hvac.CAP_AIR))

    adj = tf.logical_or(hvac.adj, tf.transpose(hvac.adj))
    adj = tf.cast(adj, tf.float32)
    conduction_between_rooms_x = - adj / hvac.R_wall * (-tf.ones_like(adj))

    adj_outside = tf.cast(hvac.adj_outside, tf.float32)
    conduction_with_outside_x = - tf.linalg.diag(
        tf.squeeze(adj_outside / hvac.R_outside))

    adj_hall = tf.cast(hvac.adj_hall, tf.float32)
    conduction_with_hall_x = - tf.linalg.diag(
        tf.squeeze(adj_hall / hvac.R_hall))

    C = tf.linalg.diag(tf.squeeze(hvac.TIME_DELTA / hvac.capacity))
    expected_f_x = (
        tf.eye(hvac.state_size)
        + tf.matmul(C,
            heating_x
            + conduction_between_rooms_x
            + conduction_with_outside_x
            + conduction_with_hall_x
        )
    )

    # print(expected_f_x)
    # print(model.f_x)
    assert expected_f_x.shape == model.f_x.shape
    # assert tf.reduce_all(tf.abs(model.f_x - expected_f_x) < 1e-3)

    # check f_u
    temp = state
    expected_f_u = tf.linalg.diag(
        tf.squeeze(
            hvac.TIME_DELTA / hvac.capacity * hvac.air_max * hvac.CAP_AIR * (hvac.TEMP_AIR - temp)))
    assert model.f_u.shape == expected_f_u.shape
    assert tf.reduce_all(tf.abs(model.f_u - expected_f_u) < 1e-3)
Пример #13
0
def test_quadratic_cost_model(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)
    cost = reservoir.cost(state, action)

    model = reservoir.get_quadratic_cost(state, action, batch=False)
    l = model.l
    l_x = model.l_x
    l_u = model.l_u
    l_xx = model.l_xx
    l_uu = model.l_uu
    l_ux = model.l_ux
    l_xu = model.l_xu

    assert l == cost

    s_t = tf.squeeze(state)
    LB = tf.squeeze(reservoir.lower_bound)
    UB = tf.squeeze(reservoir.upper_bound)
    HP = -tf.squeeze(reservoir.high_penalty)
    LP = -tf.squeeze(reservoir.low_penalty)
    SPP = -tf.squeeze(reservoir.set_point_penalty)

    e1 = HP * tf.cast(((s_t - UB) > 0.0), tf.float32)
    e2 = -LP * tf.cast(((LB - s_t) > 0.0), tf.float32)
    e3 = -SPP * tf.sign((UB + LB) / 2 - s_t)
    l_x_expected = tf.expand_dims(e1 + e2 + e3, -1)

    assert l_x.shape == state.shape
    assert tf.reduce_all(tf.abs(l_x - l_x_expected) < 1e-3)

    assert l_u.shape == action.shape
    assert tf.reduce_all(l_u == tf.zeros_like(action))

    assert l_xx.shape == [reservoir.state_size, reservoir.state_size]
    assert tf.reduce_all(
        l_xx == tf.zeros([reservoir.state_size, reservoir.state_size]))

    assert l_uu.shape == [reservoir.action_size, reservoir.action_size]
    assert tf.reduce_all(
        l_uu == tf.zeros([reservoir.action_size, reservoir.action_size]))

    assert l_ux.shape == [reservoir.action_size, reservoir.state_size]
    assert tf.reduce_all(
        l_ux == tf.zeros([reservoir.action_size, reservoir.state_size]))

    assert l_xu.shape == [reservoir.state_size, reservoir.action_size]
    assert tf.reduce_all(
        l_xu == tf.zeros([reservoir.state_size, reservoir.action_size]))
Пример #14
0
def test_inflow_batch(reservoir):
    batch_size = 10
    outflow = sample_action(reservoir, batch_size=batch_size)
    assert outflow.shape == [batch_size, reservoir.action_size, 1]
    assert tf.reduce_all(outflow >= 0.0)
    assert tf.reduce_all(outflow <= 1.0)

    value = reservoir._inflow(outflow)
    assert value.shape == [batch_size, reservoir.state_size, 1]

    assert tf.reduce_all(value[:, 0] == 0.0)
    assert tf.reduce_all(value[:, 1:] == outflow[:, :-1])

    balance = (tf.reduce_sum(value, axis=1) + outflow[:, -1] -
               tf.reduce_sum(outflow, axis=1))
    assert tf.reduce_all(tf.abs(balance) < 1e-3)
Пример #15
0
def test_quadratic_cost(hvac):
    state = conftest.sample_state(hvac)
    action = conftest.sample_action(hvac)

    model = hvac.get_quadratic_cost(state, action, batch=False)

    # check l
    assert model.l == hvac.cost(state, action, batch=False)

    # check l_x
    temp = state

    below_limit = - tf.linalg.diag(
        tf.squeeze(tf.sign(tf.maximum(0.0, hvac.temp_lower_bound - temp))))
    above_limit = tf.linalg.diag(
        tf.squeeze(tf.sign(tf.maximum(0.0, temp - hvac.temp_upper_bound))))
    out_of_bounds_penalty_x = (
        hvac.PENALTY * (below_limit + above_limit)
    )

    set_point_penalty_x = - hvac.SET_POINT_PENALTY * tf.linalg.diag(
        tf.squeeze(tf.sign(
            (hvac.temp_lower_bound + hvac.temp_upper_bound) / 2 - temp)))

    expected_l_x = tf.reduce_sum(
        out_of_bounds_penalty_x + set_point_penalty_x,
        axis=-1,
        keepdims=True)

    assert model.l_x.shape == expected_l_x.shape
    assert tf.reduce_all(tf.abs(model.l_x - expected_l_x) < 1e-3)

    # check l_u
    expected_l_u = hvac.air_max * hvac.COST_AIR
    assert model.l_u.shape == expected_l_u.shape
    assert tf.reduce_all(tf.abs(model.l_u - expected_l_u) < 1e-3)

    # check all 2nd order derivatives are null
    assert tf.reduce_all(model.l_xx == tf.zeros([hvac.state_size, hvac.state_size]))
    assert tf.reduce_all(model.l_uu == tf.zeros([hvac.state_size, hvac.state_size]))
    assert tf.reduce_all(model.l_xu == tf.zeros([hvac.state_size, hvac.state_size]))
    assert tf.reduce_all(model.l_ux == tf.zeros([hvac.state_size, hvac.state_size]))
Пример #16
0
def test_transition(hvac):
    state = conftest.sample_state(hvac)
    action = conftest.sample_action(hvac)

    next_state = hvac.transition(state, action)
    assert next_state.shape == state.shape
Пример #17
0
def test_probabilistic_transition(reservoir):
    state = sample_state(reservoir)
    action = sample_action(reservoir)

    next_state = reservoir.transition(state, action, batch=False, cec=False)
    assert next_state.shape == state.shape