k-Armed Bandit 1.0.0
A collection of k-armed bandits and assoicated agents for reinforcement learning
Loading...
Searching...
No Matches
test_base_bandit.py
Go to the documentation of this file.
1from bandit import BaseBandit
2import unittest
3
4
6 """
7 This is a fake child class.
8
9 Since Bandit is an abstract class, it can't be instantiated directly. So
10 it's methods can't be called, and therefore tested. So create a fake class
11 that defines the required methods in the simplist terms and use that
12 instead.
13 """
14
15 def __init__(self, k):
16 super().__init__(k)
17
18 def select(self, index):
19 pass
20
21 def trueValues(self):
22 pass
23
24
25class TestBaseBandit(unittest.TestCase):
26 """
27 Tests the base class that all bandits rely on. Since this is an abstract
28 class, it uses a simple inheriting class to allow testing of the
29 defined functionality.
30 """
31
33 """
34 Test that the bandit properly sets the number of arms. Any positive
35 integer is permitted and everything else is rejected. This mimics real
36 life where you can't have a non-natural number of arms. Zero is
37 explicitly excluded, since it makes the object trivial.
38 """
39 # These values should all work
40 for k in (1, 2, 100):
41 bandit = FakeBandit(k)
42 self.assertEqual(
43 bandit.k, k, msg='Static bandit did not create the correct number of arms.')
44 # These should all fail
45 for k in (0, -1, 0.5, '1', 'the', None):
46 with self.assertRaises(ValueError, msg='Static bandit did not reject invalid k input.'):
47 FakeBandit(k)
A base class for the various bandit implementations.
Definition base_bandit.py:4
select(self, index)
The method to select one of the arms of the bandit.
__init__(self, k)
Initialize the object with a set number of arms.
trueValues(self)
Return the true reward values of the bandit.
test_instantiate_k(self)
Test that the bandit properly sets the number of arms.