-
Notifications
You must be signed in to change notification settings - Fork 0
/
testMethod.py
157 lines (132 loc) · 4.51 KB
/
testMethod.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from method import trustRegionPolicyOptimization, policyGradient, proximalPolicyOptimization
from model import simpleDense
from absl import app
import numpy as np
class GridWorld(object):
"""
docstring for GridWorld.
"""
def __init__(self, dim = 2):
super(GridWorld, self).__init__()
self.dim = dim
self.rewards = []
self.position = []
for i in range(self.dim):
self.rewards.append([
[0, 0, 0, 0, -1, 0, 0],
[0, -1, -1, 0, -1, 0, 0],
[0, -1, -1, 1, -1, 0, 0],
[0, -1, -1, 0, -1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
])
self.position.append([6, 6]) # y, x
def gen_state(self):
# Generate a state given the current position of the agent
state = []
for i in range(self.dim):
s = np.zeros((7, 7))
s[self.position[i][0]][self.position[i][1]] = 1
state.append(s)
return state
def step(self, action):
state = []
reward = []
done = []
for i in range(self.dim):
if action[i] == 0: # Top
self.position[i] = [(self.position[i][0] - 1) % 7, self.position[i][1]]
elif action[i] == 1: # Left
self.position[i] = [self.position[i][0], (self.position[i][1] - 1) % 7]
elif action[i] == 2: # Right
self.position[i] = [self.position[i][0], (self.position[i][1] + 1) % 7]
elif action[i] == 3: # Down
self.position[i] = [(self.position[i][0] + 1) % 7, self.position[i][1]]
reward.append(self.rewards[i] [self.position[i][0]] [self.position[i][1]])
done.append(False) if reward[i] == 0 else done.append(True)
dones = True
for e in done:
if e != True:
dones = False
if dones: # The agent is dead, reset the game
for i in range(self.dim):
self.position[i] = [6, 6]
state = self.gen_state()
reward = np.sum(reward)
return state, reward, dones
def display(self):
print("="*14)
for i in range(self.dim):
y = 0
print('.'*10)
for line in self.rewards[i]:
x = 0
for case in line:
if case == -1:
c = "0"
elif (y == self.position[i][0] and x == self.position[i][1]):
c = "A"
elif case == 1:
c = "T"
else:
c = "-"
print(c, end=" ")
x += 1
y += 1
print()
def main(_):
grid = GridWorld(dim = 2)
buffer_size = 1000
# Create the NET class
agent = proximalPolicyOptimization.ProximalPolicyOptimization(
input_dim=[(7, 7),(7, 7)],
output_dim=[4,4],
pi_lr=0.0001,
buffer_size=buffer_size,
gamma=0.99,
clipping_range=0.2,
beta=1e-3,
model=simpleDense.SimpleDense()
)
rewards = []
b = 0
for epoch in range(10000):
done = False
state = grid.gen_state()
while not done:
action = agent.get_action(state)
n_state, reward, done = grid.step(action)
agent.store(state, action, reward)
b += 1
state = n_state
if done:
agent.finish_path(reward)
if len(rewards) > 100000:
for i in range(1000):
rewards.pop(0)
rewards.append(reward)
if b >= buffer_size:
if not done:
agent.finish_path(0)
done = True
agent.train()
b = 0
if epoch % 1000 == 0:
print("Rewards mean:%s" % np.mean(rewards))
if epoch % 100 == 0:
print(1/10 * epoch / 100)
for epoch in range(10):
import time
print("=========================TEST=================================")
done = False
state = grid.gen_state()
while not done:
time.sleep(1)
action = agent.get_action(state)
n_state, reward, done = grid.step(action)
grid.display()
state = n_state
print("reward=>", reward)
if __name__ == '__main__':
app.run(main)