Пример #1
0
    def test_add_state(self) -> None:
        """Test adding state to the beam - respect beam width."""
        beam = Beam(width=2)
        assert beam.width == 2

        state1 = State(score=1.0)
        beam.add_state(state1)
        assert beam.width == 2
        assert len(list(beam.iter_states())) == 1
        assert state1 in list(beam.iter_states())

        state2 = State(score=2.0)
        beam.add_state(state2)
        assert beam.width == 2
        assert len(list(beam.iter_states())) == 2
        assert state2 in list(beam.iter_states())

        state3 = State(score=3.0)
        beam.add_state(state3)
        assert beam.width == 2
        assert len(list(beam.iter_states())) == 2
        assert state3 in list(beam.iter_states())
        assert state2 in list(beam.iter_states())
        assert state1 not in list(beam.iter_states())

        state0 = State(score=0.0)
        beam.add_state(state0)
        assert beam.width == 2
        assert len(list(beam.iter_states())) == 2
        assert state3 in list(beam.iter_states())
        assert state2 in list(beam.iter_states())
        assert state1 not in list(beam.iter_states())
        assert state0 not in list(beam.iter_states())
Пример #2
0
    def test_new_iteration(self) -> None:
        """Test marking a new iteration in a resolution round."""
        beam = Beam(width=2)

        assert list(beam.iter_states()) == []
        assert list(beam.iter_new_added_states()) == []

        state01 = State(score=0.0)
        beam.add_state(state01)

        assert list(beam.iter_states()) == [state01]
        assert list(beam.iter_new_added_states()) == [state01]

        beam.new_iteration()

        assert list(beam.iter_states()) == [state01]
        assert list(beam.iter_new_added_states()) == []

        state02 = State(score=0.1)
        beam.add_state(state02)

        assert state01 in beam.iter_states()
        assert state02 in beam.iter_states()
        assert list(beam.iter_new_added_states()) == [state02]

        beam.new_iteration()

        state03 = State(score=0.2)
        beam.add_state(state03)

        state04 = State(score=0.3)
        beam.add_state(state04)

        state05 = State(score=0.4)
        beam.add_state(state05)

        assert beam.size == 2
        assert state04 in beam.iter_states()
        assert state05 in beam.iter_states()

        new_added = list(beam.iter_new_added_states())
        assert len(new_added) == 2
        assert state04 in new_added
        assert state05 in new_added

        assert list(beam.iter_new_added_states_sorted()) == [state05, state04]
        assert list(beam.iter_new_added_states_sorted(reverse=True)) == [state05, state04]
        assert list(beam.iter_new_added_states_sorted(reverse=False)) == [state04, state05]

        beam.new_iteration()

        state06 = State(score=1.0)
        beam.add_state(state06)

        state07 = State(score=1.0)
        beam.add_state(state07)

        assert list(beam.iter_new_added_states_sorted()) == [state06, state07]
        assert list(beam.iter_new_added_states_sorted(reverse=True)) == [state06, state07]
        assert list(beam.iter_new_added_states_sorted(reverse=False)) == [state06, state07]
Пример #3
0
    def _test_new_iteration(self) -> None:
        """Test marking a new iteration in a resolution round."""
        beam = Beam(width=2)

        assert list(beam.iter_states()) == []
        assert beam.get_last() is None

        state01 = State(score=0.0)
        beam.add_state(state01.score, state01)

        assert list(beam.iter_states()) == [state01]
        assert beam.get_last() is state01

        beam.new_iteration()

        # New iterations do not interleave.
        assert list(beam.iter_states()) == [state01]
        assert beam.get_last() is state01

        state02 = State(score=0.1)
        beam.add_state(state02.score, state02)

        assert state01 in beam.iter_states()
        assert state02 in beam.iter_states()
        assert beam.get_last() is state02
Пример #4
0
    def test_run(
        self,
        state_factory: Callable[[], State],
        state_count: int,
        limit: int,
        count: int,
        iteration: int,
        accepted_final_states: int,
    ) -> None:
        """Test running the annealing."""
        beam = Beam()
        state = state_factory()
        for _ in range(state_count):
            cloned_state = state.clone()
            cloned_state.iteration = state.iteration + 1
            beam.add_state(cloned_state)

        predictor = AdaptiveSimulatedAnnealing()
        context = flexmock(
            accepted_final_states_count=accepted_final_states,
            count=count,
            iteration=iteration,
            limit=limit,
            beam=beam,
        )
        with predictor.assigned_context(context):
            next_state, package_tuple = predictor.run()
            assert next_state in beam.iter_states()
            assert package_tuple is not None
            assert package_tuple[0] in next_state.unresolved_dependencies
            assert package_tuple in next_state.unresolved_dependencies[
                package_tuple[0]].values()
Пример #5
0
    def test_add_state_order_single(self) -> None:
        """Test adding states to beam and order during addition when score is same - iteration is relevant."""
        beam = Beam(width=1)

        state01 = State(
            score=0.0,
            latest_version_offset=0,
            iteration=1,
            iteration_states_added=0,
        )
        state01.add_justification([{"state": "01"}])
        beam.add_state(state01)

        state02 = State(
            score=0.0,
            latest_version_offset=0,
            iteration=0,
            iteration_states_added=0,
        )
        state02.add_justification([{"state": "02"}])
        beam.add_state(state02)

        assert list(beam.iter_states()) == [state01]
        assert list(beam.iter_states_sorted(reverse=True)) == [state01]
        assert list(beam.iter_states_sorted(reverse=False)) == [state01]
Пример #6
0
    def test_remove(self) -> None:
        """Test removal of a state from beam."""
        beam = Beam(width=2)

        state1 = State(score=0.0)
        beam.add_state(state1)
        beam.remove(state1)

        state2 = State(score=1.0)
        beam.add_state(state2)
        beam.remove(state2)

        state3 = State(score=2.0)
        beam.add_state(state3)
        beam.remove(state3)

        assert beam.size == 0

        beam.add_state(state1)
        beam.add_state(state2)
        beam.add_state(state3)

        assert state1 not in beam.iter_states()
        assert state2 in beam.iter_states()
        assert state3 in beam.iter_states()

        # TODO: uncomment once fixed https://github.com/thoth-station/adviser/issues/1541
        # with pytest.raises(ValueError):
        #     beam.remove(state1)

        assert beam.max() is state3

        beam.remove(state2)

        assert beam.max() is state3
        assert state2 not in beam.iter_states()
        assert state3 in beam.iter_states()
        assert beam.size == 1

        beam.remove(state3)

        assert beam.size == 0
        assert state3 not in beam.iter_states()
Пример #7
0
    def test_remove(self) -> None:
        """Test removal of a state from beam."""
        beam = Beam(width=2)

        state1 = State(score=0.0)
        beam.add_state(state1)
        beam.remove(state1)

        state2 = State(score=1.0)
        beam.add_state(state2)
        beam.remove(state2)

        state3 = State(score=2.0)
        beam.add_state(state3)
        beam.remove(state3)

        assert beam.size == 0

        beam.add_state(state1)
        beam.add_state(state2)
        beam.add_state(state3)

        assert state1 not in beam.iter_states()
        assert state2 in beam.iter_states()
        assert state3 in beam.iter_states()

        with pytest.raises(ValueError):
            beam.remove(state1)

        assert beam.max() is state3

        beam.remove(state2)

        assert beam.max() is state3
        assert state2 not in beam.iter_states()
        assert state3 in beam.iter_states()
        assert beam.size == 1

        beam.remove(state3)

        assert beam.size == 0
        assert state3 not in beam.iter_states()
Пример #8
0
    def test_wipe(self) -> None:
        """Test wiping out beam states."""
        beam = Beam()

        state1 = State(score=1.0)
        beam.add_state(state1)

        state2 = State(score=0.0)
        beam.add_state(state2)

        assert list(beam.iter_states_sorted()) == [state1, state2]
        assert list(beam.iter_states()) == [state2, state1]

        assert beam.wipe() is None
        assert list(beam.iter_states_sorted()) == []
        assert list(beam.iter_states()) == []

        beam.add_state(state1)
        beam.add_state(state2)
        assert list(beam.iter_states_sorted()) == [state1, state2]
        assert list(beam.iter_states()) == [state2, state1]
Пример #9
0
    def test_run(self, state: State, state_count: int) -> None:
        """Test running the random walk method."""
        beam = Beam()
        for _ in range(state_count):
            cloned_state = state.clone()
            cloned_state.score = random.random()
            cloned_state.iteration = state.iteration + 1
            beam.add_state(cloned_state)

        predictor = RandomWalk()
        context = flexmock(accepted_final_states_count=33, beam=beam)

        with predictor.assigned_context(context):
            next_state, package_tuple = predictor.run()
            assert next_state in beam.iter_states()
            assert package_tuple is not None
            assert package_tuple[0] in next_state.unresolved_dependencies
            assert package_tuple in next_state.unresolved_dependencies[package_tuple[0]].values()
Пример #10
0
    def test_run(self, state: State, state_count: int) -> None:
        """Test running the approximating latest method."""
        beam = Beam()
        for _ in range(state_count):
            cloned_state = state.clone()
            cloned_state.iteration = state.iteration + 1
            beam.add_state(cloned_state)

        predictor = ApproximatingLatest()
        # Add an item to the heat up entry so that assume the heat-up process was already done.
        predictor._packages_heated_up = {"some-package"}
        context = flexmock(accepted_final_states_count=33, beam=beam)
        with predictor.assigned_context(context):
            next_state, package_tuple = predictor.run()
            assert next_state is not None
            assert next_state in beam.iter_states()
            assert package_tuple[0] in next_state.unresolved_dependencies
            assert package_tuple in next_state.unresolved_dependencies[
                package_tuple[0]].values()
Пример #11
0
    def test_run(self, state_factory: Callable[[], State], state_count: int) -> None:
        """Test running the sampling method."""
        state = state_factory()
        beam = Beam()
        for _ in range(state_count):
            cloned_state = state.clone()
            cloned_state.iteration = state.iteration + 1
            beam.add_state(cloned_state)

        predictor = Sampling()
        context = flexmock(accepted_final_states_count=10, beam=beam)

        with predictor.assigned_context(context):
            next_state, package_tuple = predictor.run()
            assert next_state is not None
            assert next_state in beam.iter_states()
            assert package_tuple is not None
            assert package_tuple[0] in next_state.unresolved_dependencies
            assert package_tuple in next_state.unresolved_dependencies[package_tuple[0]].values()
Пример #12
0
    def test_add_state_order_single(self) -> None:
        """Test adding states to beam and order during addition when score is same - iteration is relevant."""
        beam = Beam(width=1)

        state01 = State(
            score=0.0,
            iteration=1,
        )
        state01.add_justification(self.JUSTIFICATION_SAMPLE_1)
        beam.add_state(state01)

        state02 = State(
            score=0.0,
            iteration=0,
        )
        state02.add_justification(self.JUSTIFICATION_SAMPLE_2)
        beam.add_state(state02)

        assert list(beam.iter_states()) == [state01]
        assert list(beam.iter_states_sorted(reverse=True)) == [state01]
        assert list(beam.iter_states_sorted(reverse=False)) == [state01]
Пример #13
0
 def test_initialization_positive(self, width: int) -> None:
     """Test initialization of beam."""
     beam = Beam(width=width)
     assert beam.width == width
     assert list(beam.iter_states()) == []