-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbipedalwalker_ddpg.py
65 lines (56 loc) · 1.88 KB
/
bipedalwalker_ddpg.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
import gym
from agent.continuous.seperate.ddpg import DDPG
from agent import utils
from example_model.policy.mlp.continuous import MLPDDPGContinuousActor
from example_model.policy.mlp.continuous import MLPDDPGContinuousCritic
import tensorflow as tf
import numpy as np
from tensorboardX import SummaryWriter
writer = SummaryWriter()
state_size = 24
output_size = 4
env = gym.make('BipedalWalker-v2')
sess = tf.Session()
target_actor = MLPDDPGContinuousActor('target_actor',state_size,output_size)
target_critic = MLPDDPGContinuousCritic('target_critic',state_size,output_size)
actor = MLPDDPGContinuousActor('actor',state_size,output_size)
critic = MLPDDPGContinuousCritic('critic',state_size,output_size)
agent = DDPG(sess,state_size,output_size,1,1,target_actor,target_critic,actor,critic)
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
#saver.restore(sess,'bipedalwalker_ddpg/model')
ep = 0
clip = 1.0
epsilon = 1.0
scores = []
update_step = 0
while True:
ep += 1
state = env.reset()
done = False
score = 0
while not done:
#if ep % 10 == 0:
# env.render()
#env.render()
action = agent.get_action([state], epsilon)
action = action[0]
next_state, reward, done, _ = env.step(action)
score += reward
agent.get_sample(state,action,reward,next_state,done)
state = next_state
if len(agent.memory) >= 10000:
agent.train_model()
scores.append(score)
agent.noise_generator.reset()
if len(agent.memory) >= 10000 and epsilon >= 0.01:
epsilon *= 0.999
epsilon -= 0.000001
print(ep, score)
if ep % 10 == 0:
update_step += 1
print(update_step, np.mean(scores), epsilon)
if update_step < 600:
writer.add_scalar('data/reward', score, update_step)
saver.save(sess, 'bipedalwalker_ddpg/model')
scores = []