-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCLearning.py
106 lines (82 loc) · 2.13 KB
/
MCLearning.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
import random
class Gridworld():
def __init__(self):
self.x = 0 # row
self.y = 0 # column
def step(self, a):
# 0: right, 1: left, 2: up, 3:down
if a == 0:
self.move_right()
elif a == 1:
self.move_left()
elif a == 2:
self.move_up()
elif a == 3:
self.move_down()
reward = -1
done = self.is_done()
return (self.x, self.y), reward, done
def move_right(self):
self.y += 1
if self.y > 3:
self.y = 3
def move_left(self):
self.y -= 1
if self.y < 0:
self.y = 0
def move_down(self):
self.x += 1
if self.x > 3:
self.x = 3
def move_up(self):
self.x -= 1
if self.x < 0:
self.x = 0
def is_done(self):
if self.x == 3 and self.y == 3:
return True
else:
return False
def get_state(self):
return self.x, self.y
def reset(self):
self.x = 0
self.y = 0
return self.x, self.y
class Agent():
def __init__(self):
pass
def select_action(self):
coin = random.random()
if coin < 0.25:
action = 0
elif coin < 0.5:
action = 1
elif coin < 0.75:
action = 2
else:
action = 3
return action
def main():
env = Gridworld()
agent = Agent()
data = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] # value
gamma = 1.0
alpha = 0.0001
for k in range(50000):
done = False
history = []
while not done:
action = agent.select_action()
(x, y), reward, done = env.step(action)
history.append((x, y, reward))
env.reset()
cum_reward = 0 # G_T = 0
for transition in history[::-1]: # backward
x, y, reward = transition
data[x][y] = data[x][y] + alpha*(cum_reward - data[x][y])
cum_reward = reward + gamma*cum_reward
for row in data:
print(row)
if __name__ == '__main__':
main()