예제 #1
0
def test_vaporated(reservoir):
    rlevel = sample_state(reservoir)
    assert rlevel.shape == [reservoir.state_size, 1]

    value = reservoir._vaporated(rlevel)
    assert value.shape == rlevel.shape
    assert tf.reduce_all(value > 0.0)
예제 #2
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)
예제 #3
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
예제 #4
0
def test_rainfall(reservoir):
    rlevel = sample_state(reservoir)
    rainfall = reservoir._rainfall(cec=True)
    assert rainfall.shape == rlevel.shape

    rainfall = reservoir._rainfall(cec=False)
    assert rainfall.shape == rlevel.shape
예제 #5
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
예제 #6
0
def test_vaporated_batch(reservoir):
    batch_size = 10
    rlevel = sample_state(reservoir, batch_size=batch_size)
    assert rlevel.shape == [batch_size, reservoir.state_size, 1]

    value = reservoir._vaporated(rlevel)
    assert value.shape == rlevel.shape
    assert tf.reduce_all(value > 0.0)
예제 #7
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)
예제 #8
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)
예제 #9
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])
예제 #10
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
예제 #11
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)
예제 #12
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])
예제 #13
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)
예제 #14
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)
예제 #15
0
def test_conduction_with_hall(hvac):
    state = conftest.sample_state(hvac)

    conduction = hvac._conduction_with_hall(state)
    assert conduction.shape == state.shape

    idx1 = tf.where(tf.squeeze(hvac.adj_hall))
    cond = tf.gather(tf.squeeze(conduction), idx1)
    temp_diff = tf.gather(tf.squeeze(hvac.temp_hall - state), idx1)
    assert tf.reduce_all(tf.sign(cond) == tf.sign(temp_diff))

    idx2 = tf.where(tf.squeeze(tf.logical_not(hvac.adj_hall)))
    cond = tf.gather(tf.squeeze(conduction), idx2)
    assert tf.reduce_all(cond == 0.0)
예제 #16
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]))
예제 #17
0
def test_conduction_between_rooms(hvac):
    state = conftest.sample_state(hvac)

    conduction = hvac._conduction_between_rooms(state)
    assert conduction.shape == state.shape

    adj = tf.logical_or(hvac.adj, tf.transpose(hvac.adj))
    R_wall = hvac.R_wall
    temp = tf.squeeze(state)
    expected = []
    for s in range(hvac.state_size):
        c = 0.0
        for p in range(hvac.state_size):
            c += float(adj[s, p]) * (temp[p] - temp[s]) / R_wall[s, p]
        expected.append(c.numpy())
    expected = tf.constant(expected, shape=[hvac.state_size, 1], dtype=tf.float32)
    assert expected.shape == conduction.shape
    assert tf.reduce_all(tf.abs(conduction - expected) < 1e-3)
예제 #18
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]))
예제 #19
0
def test_final_cost(hvac):
    state = conftest.sample_state(hvac)

    final_cost = hvac.final_cost(state)
    assert final_cost.shape == []
    assert final_cost >= 0.0
예제 #20
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
예제 #21
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