Ejemplo n.º 1
0
def test_accepts_equal():
    simulated_annealing = SimulatedAnnealing(2, 1, 1)

    for _ in range(100):
        # This results in an acceptance probability of exp{0}, that is, one.
        # Thus, the candidate state should always be accepted.
        assert_(
            simulated_annealing.accept(rnd.RandomState(), One(), One(), One()))
Ejemplo n.º 2
0
def test_exponential_threshold_update():
    record_travel = RecordToRecordTravel(5, 0, 0.1, "exponential")

    # The relative worsening is plus one, so this should be accepted initially,
    # as the threshold is 5, resp. 0.5. In the second case, 1 > 0.5, so the
    # second should be rejected.
    assert_(record_travel.accept(rnd.RandomState(), Zero(), Zero(), One()))
    assert_(not record_travel.accept(rnd.RandomState(), Zero(), Zero(), One()))
Ejemplo n.º 3
0
def test_linear_threshold_update():
    record_travel = RecordToRecordTravel(5, 0, 1)

    # For the first five, the threshold is, resp., 5, 4, 3, 2, 1. The relative
    # worsening is plus one, so this should be accepted (lower or equal to
    # threshold).
    for _ in range(5):
        assert_(record_travel.accept(rnd.RandomState(), Zero(), Zero(), One()))

    # Threshold is now zero, so this should no longer be accepted.
    assert_(not record_travel.accept(rnd.RandomState(), Zero(), Zero(), One()))
Ejemplo n.º 4
0
def test_exponential_random_solutions():
    """
    Checks if the exponential ``accept`` method correctly decides in two known
    cases for a fixed seed. This is the exponential equivalent to the linear
    random solutions test above.
    """
    simulated_annealing = SimulatedAnnealing(2, 1, 0.5, "exponential")

    state = rnd.RandomState(0)

    assert_(simulated_annealing.accept(state, Zero(), Zero(), One()))
    assert_(not simulated_annealing.accept(state, Zero(), Zero(), One()))
Ejemplo n.º 5
0
def test_linear_random_solutions():
    """
    Checks if the linear ``accept`` method correctly decides in two known cases
    for a fixed seed.
    """
    simulated_annealing = SimulatedAnnealing(2, 1, 1)

    state = rnd.RandomState(0)

    # Using the above seed, the first two random numbers are 0.55 and .72,
    # respectively. The acceptance probability is 0.61 first, so the first
    # should be accepted (0.61 > 0.55). Thereafter, it drops to 0.37, so the
    # second should not (0.37 < 0.72).
    assert_(simulated_annealing.accept(state, Zero(), Zero(), One()))
    assert_(not simulated_annealing.accept(state, Zero(), Zero(), One()))
Ejemplo n.º 6
0
def test_accepts_better():
    for _ in range(1, 100):
        simulated_annealing = SimulatedAnnealing(2, 1, 1)

        assert_(
            simulated_annealing.accept(rnd.RandomState(), One(), Zero(),
                                       Zero()))
Ejemplo n.º 7
0
def test_accepts_generator_and_random_state():
    """
    Tests if SimulatedAnnealing works with both Generator and RandomState
    randomness classes.

    See also https://numpy.org/doc/1.18/reference/random/index.html#quick-start
    """
    class Old:  # old RandomState interface mock
        def random_sample(self):  # pylint: disable=no-self-use
            return 0.5

    simulated_annealing = SimulatedAnnealing(2, 1, 1)
    assert_(simulated_annealing.accept(Old(), One(), One(), Zero()))

    class New:  # new Generator interface mock
        def random(self):  # pylint: disable=no-self-use
            return 0.5

    simulated_annealing = SimulatedAnnealing(2, 1, 1)
    assert_(simulated_annealing.accept(New(), One(), One(), Zero()))
Ejemplo n.º 8
0
def test_rejects_worse():
    record_travel = RecordToRecordTravel(0.5, 0.2, 0.1)

    # This results in a relative worsening of plus one, which is bigger than
    # the threshold (0.5).
    assert_(not record_travel.accept(rnd.RandomState(), Zero(), Zero(), One()))
Ejemplo n.º 9
0
def test_accepts_better():
    record_travel = RecordToRecordTravel(1, 0, 0.1)
    assert_(record_travel.accept(rnd.RandomState(), One(), Zero(), Zero()))
Ejemplo n.º 10
0
def test_accepts_better():
    """
    Tests if the hill climbing method accepts a better solution.
    """
    hill_climbing = HillClimbing()
    assert_(hill_climbing.accept(rnd.RandomState(), One(), One(), Zero()))
Ejemplo n.º 11
0
def test_rejects_worse():
    """
    Tests if the hill climbing method accepts a worse solution.
    """
    hill_climbing = HillClimbing()
    assert_(not hill_climbing.accept(rnd.RandomState(), Zero(), Zero(), One()))