-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
76 lines (57 loc) · 1.67 KB
/
environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import json
class Arm:
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __str__(self):
return "Arm: mean {}, std: {}".format(self.mean, self.std)
def to_json(self):
return self.__dict__
def pull(self):
return np.random.normal(self.mean, self.std)
class KArmedBanditEnv:
"""N-armed bandit environment
"""
def __init__(self, k, mean_fn, std_fn):
"""Initialize bandit with k arms
Inputs:
k: number of bandit arms
mean_fn: a function that, when called without arguments, returns float
that will be used as the mean for an arm
std_fn: a function that, when called without arguments, returns float
that will be used as the standard deviation for an arm
"""
self.k = k
self.mean_fn = mean_fn
self.std_fn = std_fn
self.reset()
def __str__(self):
return "\n".join([
"{}: {}".format(i, str(arm))
for i, arm in enumerate(self.arms)])
def get_optimal_arm(self):
return int(np.argmax([arm.mean for arm in self.arms]))
def to_json(self):
return {
"k": self.k,
"arms": self.arms
}
def reset(self):
"""Reset bandit"""
self.arms = [
Arm(self.mean_fn(), self.std_fn())
for _ in range(self.k)
]
def pull(self, a=None):
"""Environment step function
Inputs:
- action: the index of the arm to pull, defaults to a random arm
Returns:
- reward from the pulled arm
"""
if a is None: a = np.random.choice(len(self.arms))
return self.arms[a].pull()
def step(self, *args):
"""Step function for the environment, which just calls self.pull"""
return self.pull(*args)