示例#1
0
def test_extend_add():

    res0 = AnnealResults(AnnealResult({}, 2, True) for _ in range(4))
    assert res0.best.value == 2
    res1 = AnnealResults(AnnealResult({}, 1, True) for _ in range(3))
    assert res1.best.value == 1

    assert (res0 + res1).best.value == 1
    assert type(res0 + res1) == AnnealResults

    temp = res0.copy()
    temp += res1
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    temp = res0.copy()
    temp.extend(res1)
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    assert (res1 + res0).best.value == 1
    assert type(res1 + res0) == AnnealResults

    temp = res1.copy()
    temp += res0
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    temp = res1.copy()
    temp.extend(res0)
    assert temp.best.value == 1
    assert type(temp) == AnnealResults
示例#2
0
def test_annealresults():

    states = [
        ({
            0: -1,
            1: 1,
            'a': -1
        }, 1),
        ({
            0: 1,
            1: 1,
            'a': -1
        }, 9),
        ({
            0: -1,
            1: -1,
            'a': -1
        }, -3),
    ]
    sorted_states = sorted(states, key=lambda x: x[1])
    anneal_states = [AnnealResult(*s, True) for s in states]
    anneal_sorted_states = [AnnealResult(*s, True) for s in sorted_states]

    res, boolean_res = AnnealResults(), AnnealResults()
    for s in states:
        res.add_state(*s, True)
        boolean_res.add_state(spin_to_boolean(s[0]), s[1], False)

    for s in states:
        assert AnnealResult(*s, True) in res
        assert AnnealResult(s[0], s[1] + 1, True) not in res

    assert len(res) == 3
    assert len(boolean_res) == 3
    assert res.best.state == states[2][0]
    assert res.best.value == states[2][1]
    assert res.copy() == res
    assert res.to_spin() == res
    assert res.to_boolean() == boolean_res
    assert boolean_res.to_spin() == res
    assert res == anneal_states
    assert boolean_res == [x.to_boolean() for x in anneal_states]
    str(res)
    str(boolean_res)
    repr(res)
    repr(boolean_res)

    count = 0
    for s in res:
        assert s == AnnealResult(*states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_states[i]

    assert res[:2] == anneal_states[:2]
    assert isinstance(res[1:3], AnnealResults)

    res.sort()
    count = 0
    for s in res:
        assert s == AnnealResult(*sorted_states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_sorted_states[i]

    assert res[:2] == anneal_sorted_states[:2]

    boolean_res.sort()
    assert res == anneal_sorted_states
    assert boolean_res == [x.to_boolean() for x in anneal_sorted_states]

    assert type(res * 2) == AnnealResults
    assert type(res + res) == AnnealResults
    res *= 2
    assert type(res) == AnnealResults
    res += res
    assert type(res) == AnnealResults
    res += anneal_states
    assert type(res) == AnnealResults
    res.extend(anneal_sorted_states)
    assert type(res) == AnnealResults

    res.clear()
    assert res.best is None
    assert not res