k-Armed Bandit 1.0.0
A collection of k-armed bandits and assoicated agents for reinforcement learning
Loading...
Searching...
No Matches
test_greedy.py
Go to the documentation of this file.
1import numpy
2from agent import Greedy
3import unittest
4
5
6class TestGreedy(unittest.TestCase):
7 """
8 Test the Greedy agent solver.
9 """
10
11 def setUp(self) -> None:
12 """
13 Create an object to help with testing.
14 """
15 self.agent = Greedy(k=4, start_value=0.0)
16
18 """
19 Test that the algorithm always chooses exploit.
20
21 Since this is greedy, once a value is the best, it should always pick that option.
22 """
23 # Manually manipulate the table to be sure one value is better than the others.
24 ACTUAL_BEST = 2
25 self.agent._table[ACTUAL_BEST] = 100.0
26 # Test a number of times to be sure
27 for _ in range(100):
28 expected_best = self.agent.act()
29 self.assertEqual(ACTUAL_BEST, expected_best)
30
31 def test_update(self):
32 """
33 Test that the update works correctly.
34
35 The agent uses a weighted average, so use known values to ensure correct calculation.
36 """
37 # Working out the formula by hand produces the following values.
38 rewards = numpy.array(range(15, 26))
39 expected_results = numpy.array(
40 [15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20])
41 for i in range(expected_results.size):
42 # Apply the reward first, then check that the table updated correctly.
43 self.agent.update(action=0, reward=rewards[i])
44 self.assertEqual(self.agent.table[0], expected_results[i])
An agent that always exploits, never explores.
Definition greedy.py:4
Test the Greedy agent solver.
Definition test_greedy.py:6
test_update(self)
Test that the update works correctly.
None setUp(self)
Create an object to help with testing.
test_always_exploit(self)
Test that the algorithm always chooses exploit.