forked from yandexdataschool/Practical_RL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlearning.py
134 lines (100 loc) · 3.57 KB
/
qlearning.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
# qlearningAgents.py
# ------------------
## based on http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
import random,math
import numpy as np
from collections import defaultdict
class QLearningAgent():
"""
Q-Learning Agent
Instance variables you have access to
- self.epsilon (exploration prob)
- self.alpha (learning rate)
- self.discount (discount rate aka gamma)
Functions you should use
- self.getLegalActions(state)
which returns legal actions for a state
- self.getQValue(state,action)
which returns Q(state,action)
- self.setQValue(state,action,value)
which sets Q(state,action) := value
!!!Important!!!
NOTE: please avoid using self._qValues directly to make code cleaner
"""
def __init__(self,alpha,epsilon,discount,getLegalActions):
"We initialize agent and Q-values here."
self.getLegalActions= getLegalActions
self._qValues = defaultdict(lambda:defaultdict(lambda:0))
self.alpha = alpha
self.epsilon = epsilon
self.discount = discount
def getQValue(self, state, action):
"""
Returns Q(state,action)
"""
return self._qValues[state][action]
def setQValue(self,state,action,value):
"""
Sets the Qvalue for [state,action] to the given value
"""
self._qValues[state][action] = value
#---------------------#start of your code#---------------------#
def getValue(self, state):
"""
Returns max_action Q(state,action)
where the max is over legal actions.
"""
possibleActions = self.getLegalActions(state)
#If there are no legal actions, return 0.0
if len(possibleActions) == 0:
return 0.0
"*** YOUR CODE HERE ***"
return max([self.getQValue(state, a) for a in possibleActions])
def getPolicy(self, state):
"""
Compute the best action to take in a state.
"""
possibleActions = self.getLegalActions(state)
#If there are no legal actions, return None
if len(possibleActions) == 0:
return None
best_action = None
"*** YOUR CODE HERE ***"
best_action = possibleActions[np.argmax([self.getQValue(state, a) for a in possibleActions])]
return best_action
def getAction(self, state):
"""
Compute the action to take in the current state, including exploration.
With probability self.epsilon, we should take a random action.
otherwise - the best policy action (self.getPolicy).
HINT: You might want to use util.flipCoin(prob)
HINT: To pick randomly from a list, use random.choice(list)
"""
# Pick Action
possibleActions = self.getLegalActions(state)
action = None
#If there are no legal actions, return None
if len(possibleActions) == 0:
return None
#agent parameters:
epsilon = self.epsilon
"*** YOUR CODE HERE ***"
if np.random.random()<=epsilon:
return random.choice(possibleActions)
else:
action = self.getPolicy(state)
return action
def update(self, state, action, nextState, reward):
"""
You should do your Q-Value update here
NOTE: You should never call this function,
it will be called on your behalf
"""
#agent parameters
gamma = self.discount
learning_rate = self.alpha
"*** YOUR CODE HERE ***"
reference_qvalue = reward + gamma * self.getValue(nextState)
updated_qvalue = (1-learning_rate) * self.getQValue(state,action) + learning_rate * reference_qvalue
self.setQValue(state,action,updated_qvalue)
#---------------------#end of your code#---------------------#