-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
75 lines (59 loc) · 2.33 KB
/
app.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
import gym
import gym_chrome_dino
import constants
from model import get_model, save_model
from agent import choose_next_action, train
from preprocessing import process_image
import numpy as np
import random
from collections import deque
#print(model.summary())
def play():
experience_replay = deque(maxlen = constants.MAX_LENGTH_MEMORY_REPLACE)
env = gym.make("ChromeDinoNoBrowser-v0")
model = get_model(env)
total_reward = 0
episode = 1
time_step = 0
epsilon_prob = constants.EPSILON_VALUE
episode_lengths = list()
done = True
while True:
if done:
time_step = 0
last_state = process_image(env.reset())
if episode % constants.EPSIODES_TO_SAVE_MODEL == 0:
save_model(model)
# Select next action.
last_action = choose_next_action(env, model, last_state, epsilon_prob)
# Get info from the environment.
current_state, reward, done, info = env.step(last_action)
current_state = process_image(current_state)
total_reward += reward
time_step += 1
# If terminal
if done:
episode += 1
episode_lengths.append(time_step)
print("Episose: %s; Steps before fail %s; Epsilon: %.2f reward %s" %
(episode, time_step, epsilon_prob, total_reward))
total_reward = 0
# Store the transition in previous observations.
observation = (last_state, last_action, reward, current_state, done)
experience_replay.append(observation)
# If the size of the experience replay is enough.
if len(experience_replay) >= constants.MIN_EXPERIENCE_REPLAY_SIZE:
# Get mini batch from the experience replay.
mini_batch = random.sample(experience_replay, constants.MINI_BATCH_SAMPLE_SIZE)
# Train the network.
model = train(model, mini_batch)
# Increase time step by 1.
time_step += 1
# Last state now is current state.
last_state = current_state
# Gradully reduce the probability of a random action
# Starting from 1 and going to 0
if epsilon_prob > 0 and len(experience_replay) > constants.MIN_EXPERIENCE_REPLAY_SIZE:
epsilon_prob *= constants.EPSILON_DECAY
if __name__ == "__main__":
play()