Пример #1
0
    def test_n_step_td_step_no_adjust(self, context: Context) -> None:
        """Test adjusting steps taken on reward signal propagation."""
        predictor = TemporalDifference(step=1)
        predictor._temperature = 1.0
        predictor._steps_taken = 0
        package_tuple = ("tensorflow", "2.3.1", "https://pypi.org/simple")
        state = State()
        state.add_resolved_dependency(package_tuple)
        with predictor.assigned_context(context):
            predictor.set_reward_signal(state, package_tuple, 0.33)

        assert predictor._policy.get(package_tuple) is None

        predictor._steps_taken = 1

        with predictor.assigned_context(context):
            predictor.set_reward_signal(state, package_tuple, 0.2)

        assert predictor._policy.get(package_tuple) == [0.53, 1]
        assert predictor._steps_taken == 0
Пример #2
0
    def test_pre_run(self) -> None:
        """Test initialization done before running."""
        predictor = TemporalDifference()

        predictor._policy = {("tensorflow", "2.0.0", "https://pypi.org/simple"): [1.0, 2]}
        predictor._temperature_history = [(0.212, True, 0.23, 100)]
        predictor._temperature = 12.3

        context = flexmock(limit=42)
        with predictor.assigned_context(context):
            predictor.pre_run()

        assert predictor._policy == {}
        assert predictor._temperature_history == []
        assert isinstance(predictor._temperature, float)
        assert predictor._temperature == float(context.limit)
Пример #3
0
    def test_run_exploration(self, context: Context) -> None:
        """Tests run when exploration is performed."""
        flexmock(TemporalDifference)
        flexmock(TemporalDifference)
        flexmock(AdaptiveSimulatedAnnealing)

        flexmock(State)
        max_state = State(score=3.0)
        probable_state = State(score=2.0)

        context.beam.add_state(max_state)
        context.beam.add_state(probable_state)

        unresolved_dependency = (
            "pytorch",
            "1.0.0",
            "https://thoth-station.ninja/simple",
        )

        flexmock(random)
        random.should_receive("randrange").with_args(1, 2).and_return(0).once()
        random.should_receive("random").and_return(0.50).once(
        )  # *lower* than acceptance_probability that is 0.75 so we do exploitation
        probable_state.should_receive(
            "get_random_unresolved_dependency").with_args(
                prefer_recent=True).and_return(unresolved_dependency).once()
        TemporalDifference.should_receive("_temperature_function").with_args(
            1.0, context).and_return(0.9).once()
        AdaptiveSimulatedAnnealing.should_receive(
            "_compute_acceptance_probability").with_args(
                max_state.score, probable_state.score,
                0.9).and_return(0.75).once()
        context.beam.should_receive("max").with_args().and_return(
            max_state).once()

        predictor = TemporalDifference(step=1)
        predictor._steps_taken = 0
        predictor._temperature = 1.0
        with predictor.assigned_context(context):
            assert predictor.run() == (probable_state, unresolved_dependency)
            assert predictor._steps_taken == 1