Esempio n. 1
0
    def test_temperature_function(
        self,
        t0: float,
        accepted_final_states_count: int,
        limit: int,
        iteration: int,
        count: int,
    ) -> None:
        """Test the temperature function never drops bellow 0."""
        context = flexmock(
            accepted_final_states_count=accepted_final_states_count,
            limit=limit,
            iteration=iteration,
            count=count,
            beam=flexmock(size=96),
        )

        predictor = TemporalDifference()
        assert predictor._temperature_function(
            t0=t0,
            context=context) >= 0.0, "Temperature dropped bellow 0 or is NaN"
Esempio n. 2
0
    def test_run_exploitation(self, context: Context) -> None:
        """Tests run when exploitation is performed."""
        flexmock(TemporalDifference)
        flexmock(AdaptiveSimulatedAnnealing)

        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.99).once(
        )  # *higher* than acceptance_probability that is 0.75 so we do exploitation
        TemporalDifference.should_receive("_do_exploitation").with_args(
            max_state).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._temperature = 1.0
        predictor._steps_taken = 0
        with predictor.assigned_context(context):
            assert predictor.run() == (max_state, unresolved_dependency)
            assert predictor._steps_taken == 1
Esempio n. 3
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)
Esempio n. 4
0
    def test_n_step_td_step_adjust(self, context: Context) -> None:
        """Test adjusting steps taken on reward signal propagation."""
        predictor = TemporalDifference(step=1)
        predictor._temperature = 1.0
        predictor._steps_taken = 1
        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) == [0.33, 1]
        assert predictor._steps_taken == 0
Esempio n. 5
0
    def test_do_exploitation(self) -> None:
        """Tests on exploitation computation."""
        predictor = TemporalDifference()
        predictor._policy = {
            ("tensorflow", "2.1.0", "https://thoth-station.ninja"):
            [2020.21, 666],
            ("tensorflow", "2.0.0", "https://thoth-station.ninja"):
            [16.61, 1992],
            ("numpy", "1.0.0", "https://pypi.org/simple"): [30.30, 92],
        }

        state = flexmock()
        state.should_receive("iter_unresolved_dependencies").and_return([
            ("spacy", "2.2.4", "https://pypi.org/simple"),
            ("numpy", "1.0.0", "https://pypi.org/simple"),
            ("tensorflow", "2.1.0", "https://thoth-station.ninja"),
        ]).once()

        state.should_receive("get_random_unresolved_dependency").times(0)
        assert predictor._do_exploitation(state) == (
            "tensorflow",
            "2.1.0",
            "https://thoth-station.ninja",
        )
Esempio n. 6
0
    def test_set_reward_signal_unseen(self) -> None:
        """Test keeping the reward signal for an unseen step."""
        reward = 42.24
        package_tuple = ("tensorflow", "2.0.0", "https://thoth-station.ninja")

        state = flexmock()
        state.should_receive("iter_resolved_dependencies").and_return([package_tuple]).once()

        predictor = TemporalDifference()
        predictor._policy = {
            ("numpy", "1.0.0", "https://pypi.org/simple"): [30.30, 92],
        }

        predictor._steps_taken = 1
        predictor.set_reward_signal(state, None, reward)

        assert predictor._policy == {
            package_tuple: [42.24, 1],
            ("numpy", "1.0.0", "https://pypi.org/simple"): [30.30, 92],
        }
Esempio n. 7
0
 def test_init(self) -> None:
     """Test instantiation."""
     predictor = TemporalDifference()
     assert predictor._policy == {}
     assert predictor._temperature_history == []
     assert predictor._temperature == 0.0
Esempio n. 8
0
 def test_step_default(self) -> None:
     """Test default parameter for step."""
     predictor = TemporalDifference()
     assert predictor.step == 1
     assert predictor._steps_taken == 0