-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lab10- problem 3
131 lines (115 loc) · 3.66 KB
/
Lab10- problem 3
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
import numpy as np
import random
'''==================================================
Initial set up
=================================================='''
SMALL_ENOUGH = 0.05
GAMMA = 0.99
NOISE = 0.1
#Define all states
all_states=[]
for i in range(3):
for j in range(4):
all_states.append((i,j))
#Define rewards for all states
rewards = {}
for i in all_states:
if i == (1,3):
rewards[i] = -1
elif i == (2,3):
rewards[i] = 1
else:
rewards[i] = -0.04
#Dictionnary of possible actions. We have two "end" states (1,2 and 2,2)
actions = {
(0,0):('D', 'R'),
(0,1):('D', 'R', 'L'),
(0,2):('D', 'L', 'R'),
(0,3):('D', 'L'),
(1, 2) : ('U', 'D', 'L', 'R'),
(1,0):('D', 'U', 'R'),
# (1,1):('D', 'R', 'L', 'U'),
(2,0):('U', 'R'),
(2,1):('U', 'L', 'R'),
(2,2) : ('U', 'L', 'R'),
}
#Define an initial policy
policy={}
for s in actions.keys():
policy[s] = np.random.choice(actions[s])
#Define initial value function
V={}
for s in all_states:
if s in actions.keys():
V[s] = 0
if s == (1,3):
V[s]=-1
if s == (2,3):
V[s]=1
if s == (1, 1):
V[s] = 0
'''==================================================
Value Iteration
=================================================='''
iteration = 0
while True:
biggest_change = 0
for s in all_states:
if s in policy:
old_v = V[s]
new_v = -1e6
for a in actions[s]:
if a == 'U':
nxt = [s[0]-1, s[1]]
if a == 'D':
nxt = [s[0]+1, s[1]]
if a == 'L':
nxt = [s[0], s[1]-1]
if a == 'R':
nxt = [s[0], s[1]+1]
#Choose a new random action to do (transition probability)
random_1=np.random.choice([i for i in actions[s] if i != a])
num = random.randint(0, 100)
if num > 80 and num <= 90:
if random_1 == 'U':
random_1 = 'L'
if random_1 == 'D':
random_1 = 'R'
if random_1 == 'L':
random_1 = 'D'
if random_1 == 'R':
random_1 = 'U'
if num > 90:
if random_1 == 'U':
random_1 = 'R'
if random_1 == 'D':
random_1 = 'L'
if random_1 == 'L':
random_1 = 'U'
if random_1 == 'R':
random_1 = 'D'
if random_1 == 'U':
act = [s[0]-1, s[1]]
if random_1 == 'D':
act = [s[0]+1, s[1]]
if random_1 == 'L':
act = [s[0], s[1]-1]
if random_1 == 'R':
act = [s[0], s[1]+1]
if act[0] < 3 and act[0] >= 0 and act[1] < 4 and act[1] >= 0:
#Calculate the value
nxt = tuple(nxt)
act = tuple(act)
v = rewards[s] + (GAMMA * ((1-NOISE)* V[nxt] + (NOISE * V[act])))
if v > new_v:
new_v = v
policy[s] = a
V[s] = new_v
biggest_change = max(biggest_change, np.abs(old_v - V[s]))
#See if the loop should stop now
if biggest_change < SMALL_ENOUGH:
break
iteration += 1
print(f"Number of iterations is {iteration}" )
for i in V:
print(i, V[i])