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
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]
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()
def test_run(self, state_count: int) -> None: """Test running the sampling method.""" beam = Beam() for _ in range(state_count): state = State(score=random.random()) beam.add_state(state) predictor = HillClimbing() context = flexmock(accepted_final_states_count=33) next_state = predictor.run(context, beam) assert next_state is not None assert next_state is beam.top()
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())
def test_heat_up_end(self) -> None: """Test the end of the heat up phase.""" flexmock(Beam) beam = Beam() dependency_tuple_1 = ("tensorflow", "2.1.0", "https://pypi.org/simple") dependency_tuple_2 = ("tensorflow", "2.0.0", "https://pypi.org/simple") dependency_tuple_3 = ("tensorflow", "1.15.0", "https://pypi.org/simple") dependency_tuple_4 = ("flask", "1.1.1", "https://pypi.org/simple") dependency_tuple_5 = ("flask", "1.0", "https://pypi.org/simple") state = State( score=0.999, resolved_dependencies={}, unresolved_dependencies={ "tensorflow": { hash(dependency_tuple_1): dependency_tuple_1, hash(dependency_tuple_2): dependency_tuple_2, hash(dependency_tuple_3): dependency_tuple_3, }, "flask": { hash(dependency_tuple_4): dependency_tuple_4, hash(dependency_tuple_5): dependency_tuple_5, }, }, ) beam.add_state(state) last_state = flexmock(score=100) first_unresolved_dependency = flexmock() last_state.should_receive( "get_first_unresolved_dependency").and_return( first_unresolved_dependency).once() beam.should_receive("get_last").and_return(last_state).once() flexmock(ApproximatingLatest) predictor = ApproximatingLatest() predictor._initial_state = state context = flexmock(accepted_final_states_count=0, beam=beam) predictor._packages_heated_up.add("tensorflow") predictor._packages_heated_up.add("flask") with predictor.assigned_context(context): next_state, package_tuple = predictor.run() assert package_tuple is first_unresolved_dependency assert next_state is last_state
def test_run(self, state: State, state_count: int) -> None: """Test running the hill climbing method.""" beam = Beam() for _ in range(state_count): cloned_state = state.clone() cloned_state.iteration = state.iteration + 1 beam.add_state(cloned_state) predictor = HillClimbing() 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 is beam.max() assert package_tuple[0] in next_state.unresolved_dependencies assert package_tuple in next_state.unresolved_dependencies[package_tuple[0]].values()
def test_heat_up(self) -> None: """Test the heat up phase.""" beam = Beam() dependency_tuple_1 = ("tensorflow", "2.1.0", "https://pypi.org/simple") dependency_tuple_2 = ("tensorflow", "2.0.0", "https://pypi.org/simple") dependency_tuple_3 = ("tensorflow", "1.15.0", "https://pypi.org/simple") dependency_tuple_4 = ("flask", "1.1.1", "https://pypi.org/simple") dependency_tuple_5 = ("flask", "1.0", "https://pypi.org/simple") state = State( score=0.999, resolved_dependencies={}, unresolved_dependencies={ "tensorflow": { hash(dependency_tuple_1): dependency_tuple_1, hash(dependency_tuple_2): dependency_tuple_2, hash(dependency_tuple_3): dependency_tuple_3, }, "flask": { hash(dependency_tuple_4): dependency_tuple_4, hash(dependency_tuple_5): dependency_tuple_5, }, }, ) beam.add_state(state) predictor = ApproximatingLatest() context = flexmock(accepted_final_states_count=0, beam=beam) assert not predictor._packages_heated_up with predictor.assigned_context(context): next_state, package_tuple = predictor.run() assert next_state is state assert "tensorflow" in predictor._packages_heated_up assert package_tuple is dependency_tuple_1 with predictor.assigned_context(context): next_state, package_tuple = predictor.run() assert next_state is state assert "tensorflow" in predictor._packages_heated_up assert "flask" in predictor._packages_heated_up assert package_tuple is dependency_tuple_4
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()
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()
def test_run(self, state_factory: Callable[[], State], state_count: int) -> None: """Test running the random walk method.""" state = state_factory() 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()
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()
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]
def test_get_random(self) -> None: """Test getting a random state.""" beam = Beam() with pytest.raises(IndexError): beam.get_random() state1 = State(score=1.0) beam.add_state(state1) assert beam.get_random() is state1 state2 = State(score=1.0) beam.add_state(state2) random_state = random.getstate() random.seed(3) try: assert beam.get_random() is state1 assert beam.get_random() is state1 assert beam.get_random() is state2 finally: random.setstate(random_state)
def test_iter_states_sorted(self) -> None: """Test asking for states returns a sorted list of states.""" beam = Beam(width=4) assert beam.width == 4 state1 = State(score=1.0) beam.add_state(state1) state3 = State(score=3.0) beam.add_state(state3) state0 = State(score=0.0) beam.add_state(state0) state2 = State(score=2.0) beam.add_state(state2) assert list(beam.iter_states_sorted()) == [state3, state2, state1, state0]
def test_max(self) -> None: """Test max element in beam.""" beam = Beam(width=2) assert beam.width == 2 state1 = State(score=1.0) beam.add_state(state1) assert beam.max() is state1 state2 = State(score=2.0) beam.add_state(state2) assert beam.max() is state2 state3 = State(score=0.5) beam.add_state(state3) assert beam.max() is state2
def test_add_state_order_multi(self) -> None: """Test adding states to beam and order during addition when score is same.""" beam = Beam(width=2) state01 = State(score=0.0, iteration=0, iteration_states_added=0) state01.add_justification([{"state": "01"}]) beam.add_state(state01) state02 = State(score=0.0, iteration=0, iteration_states_added=1) state02.add_justification([{"state": "02"}]) beam.add_state(state02) state03 = State(score=0.0, iteration=1, iteration_states_added=0) state03.add_justification([{"state": "03"}]) beam.add_state(state03) state04 = State(score=0.0, iteration=1, iteration_states_added=1) state04.add_justification([{"state": "04"}]) beam.add_state(state04) assert list(beam.iter_states_sorted()) == [state03, state04] assert list(beam.iter_states_sorted(reverse=True)) == [state03, state04] assert list(beam.iter_states_sorted(reverse=False)) == [state04, state03]
def test_add_state_order_multi(self) -> None: """Test adding states to beam and order during addition when score is same.""" beam = Beam(width=2) state01 = State(score=0.0) state01.add_justification(self.JUSTIFICATION_SAMPLE_1) beam.add_state(state01) state02 = State(score=0.0) state02.add_justification(self.JUSTIFICATION_SAMPLE_2) beam.add_state(state02) state03 = State(score=0.0) state03.add_justification(self.JUSTIFICATION_SAMPLE_3) beam.add_state(state03) assert list(beam.iter_states_sorted()) == [state01, state02] assert list(beam.iter_states_sorted(reverse=True)) == [state01, state02] assert list(beam.iter_states_sorted(reverse=False)) == [state01, state02]
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]
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]