-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqlearning.py
More file actions
109 lines (83 loc) · 2.62 KB
/
Copy pathqlearning.py
File metadata and controls
109 lines (83 loc) · 2.62 KB
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
#! python
def main():
n = input("Enter a value for n >= 0: ")
while n < 0:
n=input("Nice try, go again!: ")
global gamma
gamma = input("Enter a value for G where 0<G<1: ")
while gamma >= 1 or gamma <= 0:
gamma = input("Nice try ;) Go again: ")
global the_cache
the_cache = []
# pretty printing
print "\nFor n = {0}, G = {1}.\n".format(n, gamma)
fit_exercise = q(n,'fit','exercise')
fit_relax = q(n,'fit','relax')
print "fit:\tExercise: {0}\tRelax: {1}\tpi: {2}".format( fit_exercise, fit_relax, "exercise" if fit_exercise >= fit_relax else "relax" )
unfit_exercise = q(n,'unfit','exercise')
unfit_relax = q(n,'unfit','relax')
print "unfit:\tExercise: {0}\tRelax: {1}\tpi: {2}\n".format( unfit_exercise, unfit_relax, "exercise" if unfit_exercise >= unfit_relax else "relax")
def get_val (key):
for item in the_cache:
if item[0] == key:
return item[1]
def get_key (n,s,a):
return str(n) + s + a
def insert(key, value):
the_cache.append((key, value))
def in_cache(key):
for item in the_cache:
if item[0] == key:
return True
return False
def v (n,s):
return max( q(n,s,'exercise'), q(n,s,'relax') )
def q (n,s,a):
key = get_key(n,s,a)
if in_cache(key):
return get_val(key)
if n == 0:
return q0(s,a)
result = q0(s,a) + ((gamma)*(p(s,a,'fit') * v(n-1,'fit') + p(s,a,'unfit') * v(n-1,'unfit')))
insert(key,result)
return result
def q0 (s,a):
p_fit = p(s,a,'fit')
p_unfit = p(s,a,'unfit')
r_fit = r(s,a,'fit')
r_unfit = r(s,a,'unfit')
return (p_fit * r_fit) + (p_unfit * r_unfit)
def p (r,half,c):
# both matrices are now in row major order in p_arr
# one half of the array (index from 0 to 3) is the exercise matrix
# and the other half (index 4 to 7) is the relax matrix
p_arr = [0.99, 0.01, 0.2, 0.8, 0.7, 0.3, 0.0, 1.0]
row_size = 2
row = 0
column = 0
if r == 'unfit':
row = 1
if c == 'unfit':
column = 1
index = (row_size*row) + column
# move to corresponding value in second half of matrix if searching for 'relax'
if half == 'relax':
index += 4
return p_arr[index]
def r (r,half,c):
# same concept as p_arr above
r_arr = [8.0, 8.0, 0.0, 0.0, 10.0, 10.0, 5.0, 5.0]
row_size = 2
row = 0
column = 0
if r == 'unfit':
row = 1
if c == 'unfit':
column = 1
index = (row_size*row) + column
if half == 'relax':
index += 4
return r_arr[index]
# just nice having the main function at the top
if __name__== "__main__":
main()