Пример #1
0
def test_candidate_update():
    agent = '0'
    idx = {str(i): i for i in range(3)}
    cs = np.arange(12).reshape(3, 4)
    sids = ['s0'] * 3
    perf = -23
    candidate_i = planning.Candidate(agent, dict(idx), cs, sids, perf)
    candidate_j = candidate_i.update('1', [0, 0, 0, 0], 's1',
                                     lambda x: x.sum())

    # Assert that a deep copy was made
    assert candidate_j is not candidate_i
    assert candidate_j.idx is not candidate_i.idx
    assert candidate_j.cs is not candidate_i.cs
    assert candidate_j.sids is not candidate_i.sids

    # Assert that the original values are unchanged
    assert candidate_i.idx == idx
    assert np.array_equal(candidate_i.cs, np.array([[0, 1, 2, 3],
                                                    [4, 5, 6, 7],
                                                    [8, 9, 10, 11]]))
    assert candidate_i == planning.Candidate('0', idx,
                                             np.array([[0, 1, 2, 3],
                                                       [4, 5, 6, 7],
                                                       [8, 9, 10, 11]]),
                                             ['s0'] * 3, -23)

    # Assert that the new values are correct
    assert candidate_j == planning.Candidate('1', idx,
                                             np.array([[0, 1, 2, 3],
                                                       [0, 0, 0, 0],
                                                       [8, 9, 10, 11]]),
                                             ['s0', 's1', 's0'], 44)
Пример #2
0
def ua_mocks(event_loop, containers, ctrl_obs):
    """Generate multiple ua mocks connected to the ctrl."""
    ctrl, obs = ctrl_obs
    ua_mocks = [UnitAgentMock(containers[1]) for i in range(3)]
    solution = planning.Candidate(
        ua_mocks[0].addr, {a.addr: i for i, a in enumerate(ua_mocks)},
        np.arange(12, dtype=float).reshape(4, 3), ['s0', 's4', 's8'], 1)

    futs = []
    for ua in ua_mocks:
        futs.append(ua.setup(ctrl.addr, obs.addr))
        ua.solution = solution

    event_loop.run_until_complete(asyncio.gather(*futs))

    return ua_mocks
Пример #3
0
def test_get_extra_codecs(Codec):
    codec = Codec()
    codec.add_serializer(*aiomas.util.arrow_serializer())
    for s in util.get_extra_serializers():
        codec.add_serializer(*s())

    sc = planning.SystemConfig({'1': 0}, np.arange(1), [1], [2])
    ret = codec.decode(codec.encode(sc))
    assert ret == sc

    cand = planning.Candidate('foo', {'1': 0}, np.arange(1), [1], 2)
    ret = codec.decode(codec.encode(cand))
    assert ret == cand

    ts = TS(arrow.get(), 15, np.arange(4))
    ret = codec.decode(codec.encode(ts))
    assert ret == ts
Пример #4
0
def test_planner_init_negotiation(planner, wm, model, ua_mock, ctrl_mock, obs_mock):
    # ua_mocks[0] is associated with the planner, the others are neighbors:
    neighbors = [ua_mock.addr]
    wm.sysconf = planning.SystemConfig(
        {planner.name: 0}, np.array([[0, 1, 2, 3]]), ['s0'], [0])
    wm.candidate = planning.Candidate(
        planner.name, {planner.name: 0}, np.array([[0, 1, 2, 3]]), ['s0'], -34)

    # Run test
    yield from planner.init_negotiation(neighbors, wm.start, wm.res,
                                        wm.ts, wm.weights, True)

    sent_sc, sent_cand = yield from ua_mock.update_called

    assert len(planner.wm.neighbors) == 1
    assert 'UnitAgentMockProxy' in str(planner.wm.neighbors[0])

    wm.neighbors = planner.wm.neighbors
    wm.msgs_out = 1
    assert planner.wm == wm

    assert sent_sc == planner.wm.sysconf
    assert sent_cand == planner.wm.candidate

    assert planner.inbox == []

    # Check update_stats in ctrl_mock
    stats = yield from obs_mock.update_stats_called

    assert stats[0] == 'tcp://127.0.0.1:5555/0'
    assert stats[1] > 0
    assert stats[2:] == (-34, 1, 0, 1, True)

    # Check update_stats in obs_mock
    stats = yield from obs_mock.update_stats_called
    assert stats[0] == 'tcp://127.0.0.1:5555/0'
    assert stats[1] > 0
    assert stats[2:] == (-34, 1, 0, 1, True)
Пример #5
0
class Fixtures:
    # A: {0, 1}
    sysconf_a = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        cnt=(1, 1),
    )
    candidate_a = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        perf=-12,
    )

    # B: {0, 1} (Same as A, but different agents choose the candidate)
    sysconf_b = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        cnt=(1, 1),
    )
    candidate_b = planning.Candidate(
        agent='tcp://127.0.0.1:5555/0',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        perf=-12,
    )

    # C: {0, 1}  (Same as A, but but better performance)
    sysconf_c = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(1, 9).reshape(2, 4),
        sids=('s1', 's5'),
        cnt=(0, 2),
    )
    candidate_c = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(1, 9).reshape(2, 4),
        sids=('s1', 's5'),
        perf=-8,
    )

    # D: {0, 1}  (Same as A, but but different sysconf)
    sysconf_d = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        cnt=(1, 2),
    )
    candidate_d = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        perf=-12,
    )

    # E: {1, 2}  # Overlaps with A
    sysconf_e = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/1': 0, 'tcp://127.0.0.1:5555/2': 1},
        cs=np.arange(1, 9).reshape(2, 4),
        sids=('s1', 's5'),
        cnt=[0, 1],
    )
    candidate_e = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/1': 0, 'tcp://127.0.0.1:5555/2': 1},
        cs=np.arange(8).reshape(2, 4),
        sids=('s0', 's4'),
        perf=-12,
    )

    # F: {1}  # Subset of A
    sysconf_f = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/1': 0},
        cs=np.arange(4, 8).reshape(1, 4),
        sids=('s4'),
        cnt=[1],
    )
    candidate_f = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/1': 0},
        cs=np.arange(4, 8).reshape(1, 4),
        sids=('s4'),
        perf=-18,
    )

    # G: {0, 1, 2}  # Super set of A
    sysconf_g = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1,
             'tcp://127.0.0.1:5555/2': 2},
        cs=np.arange(1, 13).reshape(3, 4),
        cnt=[0, 1, 1],
        sids=('s1', 's5', 's9'),
    )
    candidate_g = planning.Candidate(
        agent='tcp://127.0.0.1:5555/2',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1,
             'tcp://127.0.0.1:5555/2': 2},
        cs=np.arange(0, 12).reshape(3, 4),
        sids=('s0', 's4', 's8'),
        perf=-26,
    )

    # A|A merged
    sysconf_aa = sysconf_a
    candidate_aa = candidate_a

    # A|B merged
    sysconf_ab = sysconf_a
    candidate_ab = candidate_b

    # A|C merged
    sysconf_ac = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.array([[0, 1, 2, 3], [5, 6, 7, 8]]),
        sids=('s0', 's5'),
        cnt=(1, 2),
    )
    candidate_ac = candidate_c
    # Merged sysconf after perceive() *and* decide().
    # Sysconf_c contains an older (but better) OS for 5555/0. Thus its count is
    # also raised from 1 to 2 (compared to the sysconf above).
    sysconf_ac2 = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1},
        cs=np.array([[1, 2, 3, 4], [5, 6, 7, 8]]),
        sids=('s1', 's5'),
        cnt=(2, 2),
    )
    candidate_ac2 = candidate_c

    # C|A merged
    sysconf_ca = sysconf_ac
    candidate_ca = candidate_c

    # A|D merged
    sysconf_ad = sysconf_d
    candidate_ad = candidate_a

    # A|E merged
    sysconf_ae = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1,
             'tcp://127.0.0.1:5555/2': 2},
        cs=np.array([[0, 1, 2, 3], [4, 5, 6, 7], [5, 6, 7, 8]]),
        sids=('s0', 's4', 's5'),
        cnt=[1, 1, 1],
    )
    candidate_ae = planning.Candidate(
        agent='tcp://127.0.0.1:5555/1',
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1,
             'tcp://127.0.0.1:5555/2': 2},
        cs=np.array([[0, 1, 2, 3], [4, 5, 6, 7], [4, 5, 6, 7]]),
        sids=('s0', 's4', 's4'),
        perf=-14,
    )

    # A|F merged
    sysconf_af = sysconf_a
    candidate_af = candidate_a

    # A|G merged
    sysconf_ag = planning.SystemConfig(
        idx={'tcp://127.0.0.1:5555/0': 0, 'tcp://127.0.0.1:5555/1': 1,
             'tcp://127.0.0.1:5555/2': 2},
        cs=np.array([[0, 1, 2, 3], [4, 5, 6, 7], [9, 10, 11, 12]]),
        sids=('s0', 's4', 's9'),
        cnt=[1, 1, 1],
    )
    candidate_ag = candidate_g
Пример #6
0
def test_candidate_not_equal(b):
    a = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    assert a != b
Пример #7
0
def test_candidate_equal():
    a = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    b = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    assert a == b
Пример #8
0
])
def test_sysconf_merge(i, j, expected):
    res = planning.SystemConfig.merge(i, j)
    if expected is i:
        assert res is expected
    assert res == expected


def test_candidate_equal():
    a = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    b = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    assert a == b


@pytest.mark.parametrize('b', [
    planning.Candidate('b', {'1': 0}, np.array(1), [1], 2),
    planning.Candidate('a', {'1': 1}, np.array(1), [1], 2),
    planning.Candidate('a', {'1': 0}, np.array(2), [1], 2),
    planning.Candidate('a', {'1': 0}, np.array(1), [2], 2),
    planning.Candidate('a', {'1': 0}, np.array(1), [1], 3),
])
def test_candidate_not_equal(b):
    a = planning.Candidate('a', {'1': 0}, np.array(1), [1], 2)
    assert a != b


def test_candidate_update():
    agent = '0'
    idx = {str(i): i for i in range(3)}
    cs = np.arange(12).reshape(3, 4)
    sids = ['s0'] * 3