-
Notifications
You must be signed in to change notification settings - Fork 1
/
Agents.py
185 lines (143 loc) · 6.31 KB
/
Agents.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from pacman_env import *
from tqdm import tqdm
class QLearningAgent():
def agent_init(self, agent_init_info):
"""Setup for the agent called when the experiment first starts.
Args:
agent_init_info (dict), the parameters used to initialize the agent. The dictionary contains:
{
num_states (int): The number of states,
num_actions (int): The number of actions,
epsilon (float): The epsilon parameter for exploration,
step_size (float): The step-size,
discount (float): The discount factor,
}
"""
# Store the parameters provided in agent_init_info.
self.num_actions = agent_init_info["num_actions"]
self.epsilon = agent_init_info["epsilon"]
self.step_size = agent_init_info["step_size"]
self.discount = agent_init_info["discount"]
self.rand_generator = np.random.RandomState(agent_init_info["seed"])
# Create an array for action-value estimates and initialize it to zero.
self.q = {} # The array of action-value estimates.
def agent_start(self, state):
"""The first method called when the episode starts, called after
the environment starts.
Args:
state (int): the state from the
environment's evn_start function.
Returns:
action (int): the first action the agent takes.
"""
# Choose action using epsilon greedy.
current_q = self.q.setdefault(state,[0,0,0,0,0])
if self.rand_generator.rand() < self.epsilon:
action = self.rand_generator.randint(self.num_actions) # random action selection
else:
action = self.argmax(current_q) # greedy action selection
self.prev_state = state
self.prev_action = action
return action
def agent_step(self, reward, state):
"""A step taken by the agent.
Args:
reward (float): the reward received for taking the last action taken
state (int): the state from the
environment's step based on where the agent ended up after the
last step.
Returns:
action (int): the action the agent is taking.
"""
# Choose action using epsilon greedy.
current_q = self.q.setdefault(state,[0,0,0,0,0])
if self.rand_generator.rand() < self.epsilon:
action = self.rand_generator.randint(self.num_actions)
else:
action = self.argmax(current_q)
# Perform an update (1 line)
### START CODE HERE ###
#perform update :
self.q[self.prev_state][self.prev_action] += self.step_size*(reward + self.discount*np.max(self.q[state]) - self.q[self.prev_state][self.prev_action])
### END CODE HERE ###
self.prev_state = state
self.prev_action = action
return action
def agent_end(self, reward):
"""Run when the agent terminates.
Args:
reward (float): the reward the agent received for entering the
terminal state.
"""
# Perform the last update in the episode (1 line)
self.q[self.prev_state][self.prev_action] += self.step_size*(reward - self.q[self.prev_state][self.prev_action])
def argmax(self, q_values):
"""argmax with random tie-breaking
Args:
q_values (Numpy array): the array of action-values
Returns:
action (int): an action with the highest value
"""
top = float("-inf")
ties = []
for i in range(len(q_values)):
if q_values[i] > top:
top = q_values[i]
ties = []
if q_values[i] == top:
ties.append(i)
return self.rand_generator.choice(ties)
def set_epsilon(self, value):
self.epsilon = value
class SarsaAgent():
def agent_init(self, agent_init_info):
self.num_actions = agent_init_info["num_actions"]
self.epsilon = agent_init_info["epsilon"]
self.step_size = agent_init_info["step_size"]
self.discount = agent_init_info["discount"]
self.rand_generator = np.random.RandomState(agent_init_info["seed"])
self.q = {}
def agent_start(self, state):
current_q = self.q.setdefault(state,[0,0,0,0,0])
if self.rand_generator.rand() < self.epsilon:
action = self.rand_generator.randint(self.num_actions) # random action selection
else:
action = self.argmax(current_q) # greedy action selection
self.prev_state = state
self.prev_action = action
return action
def agent_step(self, reward, state):
# Choose action using epsilon greedy.
current_q = self.q.setdefault(state,[0,0,0,0,0])
if self.rand_generator.rand() < self.epsilon:
action = self.rand_generator.randint(self.num_actions)
else:
action = self.argmax(current_q)
best_q = np.max(current_q)
number_of_greedy_actions = np.sum(current_q==best_q)
proba_non_greedy = (self.epsilon / self.num_actions)
proba_greedy = ((1 - self.epsilon) / number_of_greedy_actions) + (self.epsilon / self.num_actions)
expected_q = 0
for a in range(self.num_actions):
if current_q[a] != best_q:
expected_q += current_q[a] * proba_non_greedy
else:
expected_q += current_q[a] * proba_greedy
self.q[self.prev_state][self.prev_action] += self.step_size*(reward + self.discount*expected_q - self.q[self.prev_state][self.prev_action])
self.prev_state = state
self.prev_action = action
return action
def agent_end(self, reward):
self.q[self.prev_state][self.prev_action] += self.step_size*(reward - self.q[self.prev_state][self.prev_action])
def argmax(self, q_values):
top = float("-inf")
ties = []
for i in range(len(q_values)):
if q_values[i] > top:
top = q_values[i]
ties = []
if q_values[i] == top:
ties.append(i)
return self.rand_generator.choice(ties)
def set_epsilon(self, value):
self.epsilon = value