def test_should_update_an_arm_with_feedback(self): arm_names = ['arm1', 'arm2'] arm = [EpsilonGreedyArm(name, n=1, reward_sum=1) for name in arm_names] egreedy = EGreedy('my_bandit', arm, epsilon=0.1) feedback = Feedback('arm1', reward=1) egreedy.update(feedback) assert egreedy.arms_dict['arm1'].reward_sum == 2 and egreedy.arms_dict[ 'arm1'].n == 2
def test_should_run_for_several_iterations_and_sample_all_arms(self): arm_names = ['arm1', 'arm2', 'arm3'] arm = [EpsilonGreedyArm(name, n=1, reward_sum=1) for name in arm_names] egreedy = EGreedy('my_bandit', arm, epsilon=0.1) num_steps = 100 sampled_arm_ids = set() for _ in range(num_steps): arm = egreedy.choose() sampled_arm_ids.add(arm.arm_id) reward = 1.0 if random.random() < 0.2 else 0.0 egreedy.update(Feedback(arm.arm_id, reward)) assert sampled_arm_ids == set(arm_names)
def test_should_reset_value_to_default(self): arm_names = ['arm1', 'arm2', 'arm3'] arm = [EpsilonGreedyArm(name, n=1, reward_sum=1) for name in arm_names] egreedy = EGreedy('my_bandit', arm, epsilon=0.1) num_steps = 20 for _ in range(num_steps): arm = egreedy.choose() reward = 1.0 if random.random() < 0.2 else 0.0 egreedy.update(Feedback(arm_id=arm.arm_id, reward=reward)) egreedy.reset() assert all([ arm.reward_sum == 0 and arm.n == 0.001 for arm in egreedy.arms_dict.values() ])