-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
51 lines (39 loc) · 908 Bytes
/
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
# /usr/bin/env python 3.6
# -*-coding:utf-8-*-
'''
main program
'''
import gym
env = gym.make("CartPole-v0").unwrapped
maxEps = 400
numFeatures = 4
numActions = 2
lr = 0.01
gamma = 0.95
pg = Cart_Pytorch(numFeatures, numActions, lr, gamma)
rewardList_pg = []
for t in range(maxEps):
state = env.reset()
done = False
rewardSum = 0
while not done:
action = pg._selectAction(state)
newState, reward, done, _ = env.step(action)
pg.storeTrasition(state, action, reward)
state = newState
rewardSum += reward
discountedReward = pg.learn()
rewardList.append(rewardSum)
if t % 100 == 0:
print("Finish epsilon {}".format(t))
plt.plot(list(range(maxEps)), rewardList, 'r-', linewidth = 2)
plt.xlabel("episode")
plt.ylabel("rewards")
plt.show()
state = env.reset()
done = False
while not done:
action = pg.greedy(state)
state, reward, done, _ = env.step(action)
env.render()
env.close()