-
Notifications
You must be signed in to change notification settings - Fork 31
/
QLearner.py
48 lines (41 loc) · 1.12 KB
/
QLearner.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
"""
Template for implementing QLearner
"""
import numpy as np
import random as rand
class QLearner(object):
def __init__(self,
num_states=100,
num_actions = 4,
alpha = 0.2,
gamma = 0.9,
rar = 0.5,
radr = 0.99,
verbose = False):
#TODO
self.verbose = verbose
self.num_actions = num_actions
self.s = 0
self.a = 0
def querysetstate(self, s):
"""
@summary: Update the state without updating the Q-table
@param s: The new state
@returns: The selected action
"""
#TODO
self.s = s
action = rand.randint(0, self.num_actions-1)
if self.verbose: print("s =", s,"a =",action)
return action
def query(self,s_prime,r):
"""
@summary: Update the Q table and return an action
@param s_prime: The new state
@param r: The ne state
@returns: The selected action
"""
#TODO
action = rand.randint(0, self.num_actions-1)
if self.verbose: print("s =", s_prime,"a =",action,"r =",r)
return action