-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (67 loc) · 1.96 KB
/
main.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
import square_env
from curious_agent import CuriousAgent
import gym
import numpy as np
import time
def loc_to_scalar(loc):
scales = []
for i in loc:
if (i[0] == 10 and (i[1] == 10 or i[1] == 0)) or (i[0] == 0 and (i[1] == 10 or i[1] == 0)):
scales.append(3)
elif i[0] == 10 or i[1] == 10 or i[0] == 0 or i[1] == 0:
scales.append(2)
else:
scales.append(1)
return scales
def info_to_location(info):
loc = []
for i in info:
loc.append(i["location"])
return loc
def main():
env = gym.make("square-v0")
state = env.reset()
agent = CuriousAgent()
error = 1.0
tds = []
errors = []
timesteps = []
rewards = []
costs = []
infos = []
for timestep in range(200):
state, error, info, td, reward, prediction = agent.take_step(env, state, error)
errors.append(error)
tds.append(td)
rewards.append(reward)
infos.append(info)
timesteps.append(timestep)
if timestep % 50 == 0:
print("state: " + str(state))
print("pred:" + str(np.round(prediction)))
#learner_c = agent.train(300)
#costs.append(np.sqrt(learner_c))
env.render()
print(agent.learner_alpha)
print(agent.value_alpha)
print(agent.epsilon)
locs = info_to_location(infos)
scales = loc_to_scalar(locs)
print(scales)
import matplotlib.pyplot as plt
plt.figure(timesteps, scales)
plt.plot(timesteps,tds)
plt.title("TDS")
plt.axis([min(timesteps),max(timesteps),min(tds),max(tds)])
fig, ax = plt.subplots(1,1)
ax.plot(timesteps, errors)
ax.plot(timesteps)
ax.set_title("Errors")
ax([min(timesteps), max(timesteps), min(errors), max(errors)])
plt.figure()
plt.plot(timesteps, rewards)
plt.title("Rewards")
plt.axis([min(timesteps), max(timesteps), min(rewards), max(rewards)])
plt.show()
if __name__ == "__main__":
main()