def test_pull_returns_a_valid_arm(self, k, e, rounds): egreedy = EpsilonGreedy(num_arms=k, epsilon=e) for _ in range(rounds): arm = egreedy.pull() self.assertGreaterEqual(arm, 0) self.assertLess(arm, k)
def test_pull_does_not_change_state(self, k, e, rounds): egreedy = EpsilonGreedy(num_arms=k, epsilon=e) for _ in range(rounds): egreedy.pull() self.assertEqual(0, sum(egreedy.pull_counts)) self.assertEqual(0, sum(egreedy.rewards))
def test_the_best_arm_based_on_feedback(self, k, e, rounds): p = np.random.random(k) p = p / np.sum(p) egreedy = EpsilonGreedy.create(e, rewards=p) pulls = np.zeros(k, dtype=np.uint64) for _ in range(rounds): arm = egreedy.pull() pulls[arm] += 1 self.assertEqual(np.argmax(p), np.argmax(pulls))
def test_pull_does_not_change_state_after_any_updates( self, k, e, rounds, updates): egreedy = EpsilonGreedy(num_arms=k, epsilon=e) for _ in range(updates): egreedy.update(chosen_arm=np.random.randint(0, k), reward=np.random.random()) pulls = sum(egreedy.pull_counts) rewards = sum(egreedy.rewards) for _ in range(rounds): egreedy.pull() self.assertEqual(pulls, sum(egreedy.pull_counts)) self.assertEqual(rewards, sum(egreedy.rewards))
def test_init_fails_with_epsilon_above_one(self, k, e): assume(e > 1.0) with self.assertRaises(AssertionError, msg="epsilon should be between [0, 1]"): _ = EpsilonGreedy(num_arms=k, epsilon=e)
def test_init_fails_with_invalid_nums_arms(self, k, e): with self.assertRaises(AssertionError, msg="there should be more than one arm"): _ = EpsilonGreedy(num_arms=k, epsilon=e)
def test_init_set_config_correctly(self, k, e): egreedy = EpsilonGreedy(num_arms=k, epsilon=e) self.assertEqual(k, egreedy.num_arms) self.assertEqual(e, egreedy.epsilon)