Skip to content

Instantly share code, notes, and snippets.

@pythonlessons
Created November 26, 2019 14:53
Show Gist options
  • Save pythonlessons/9e8c224050fa465d2b3b892543649bac to your computer and use it in GitHub Desktop.
Save pythonlessons/9e8c224050fa465d2b3b892543649bac to your computer and use it in GitHub Desktop.
1_Cartpole_DQN_run_fucntion.py
def run(self):
for e in range(self.EPISODES):
state = self.env.reset()
state = np.reshape(state, [1, self.state_size])
done = False
i = 0
while not done:
self.env.render()
action = self.act(state)
next_state, reward, done, _ = self.env.step(action)
next_state = np.reshape(next_state, [1, self.state_size])
if not done or i == self.env._max_episode_steps-1:
reward = reward
else:
reward = -100
self.remember(state, action, reward, next_state, done)
state = next_state
i += 1
if done:
print("episode: {}/{}, score: {}, e: {:.2}".format(e, self.EPISODES, i, self.epsilon))
if i == 500:
print("Saving trained model as cartpole-dqn.h5")
self.save("cartpole-dqn.h5")
return
self.replay()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment