1from agent
import BaseAgent
7 A fake child class to allow testing of BaseAgent.
9 BaseAgent is an abstract class, so can't be instantiated directly. This FakeChild class implements the bare minimum
10 to allow testing of the elements of the base class that can be tested.
13 def __init__(self, k: int, start_value: float = 0.0) ->
None:
14 super().
__init__(k, start_value=start_value)
19 def update(self, action: int, reward: float) ->
None:
25 Test the BaseAgent class.
27 This tests the creating of the Q-table, as well as the exploration/exploitation helper methods.
32 Verify that the table initializes with the right values.
35 for k
in (1, 10, 100):
37 self.assertEqual(agent.table.size, k,
38 msg=
'BaseAgent did not make a correct table size.')
40 for k
in (0, -1, 0.5,
'1',
'the',
None):
41 with self.assertRaises(Exception, msg=
'Static bandit did not reject invalid k input.'):
46 Test that the class picks the best action based on the table.
48 This tests that the class will reliable return the index associated with the best action. It should also
49 arbitrarily break ties in the case of multiple entries with an equivalent score.
55 agent._table[ACTUAL_BEST] = BEST_REWARD
56 expected_best = agent.exploit()
57 self.assertEqual(ACTUAL_BEST, expected_best,
58 'Exploitation picked an incorrect index.')
60 agent._table[ACTUAL_BEST + 1] = BEST_REWARD
63 expected_best = agent.exploit()
64 result = (expected_best == ACTUAL_BEST)
or (
65 expected_best == ACTUAL_BEST + 1)
67 result, msg=
'Exploitation picked an incorrect index when breaking a tie.')
71 Test that the class picks a random valid action from the table.
76 possible_actions = list(range(K))
78 action = agent.explore()
79 self.assertTrue(action
in possible_actions,
80 msg=
'Exploration produced an invalid index.')
A base class used to create a variety of bandit solving agents.
A fake child class to allow testing of BaseAgent.
None __init__(self, int k, float start_value=0.0)
Construct the agent.
int act(self)
Use a specific algorithm to determine which action to take.
None update(self, int action, float reward)
Update the Q-Table.
Test the BaseAgent class.
test_q_table_creation(self)
Verify that the table initializes with the right values.
test_exploitation(self)
Test that the class picks the best action based on the table.
test_exploration(self)
Test that the class picks a random valid action from the table.