-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
92 lines (74 loc) · 3.46 KB
/
run.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
87
88
89
90
91
92
import torch
from unityagents import UnityEnvironment
import numpy as np
from agent import Agent
from collections import deque
import torch
from model import *
device = torch.device("cpu")
env = UnityEnvironment(file_name="Banana.app")
# get the default brain
brain_name = env.brain_names[0]
brain = env.brains[brain_name]
# reset the environment
env_info = env.reset(train_mode=True)[brain_name]
# number of actions
action_size = brain.vector_action_space_size
# examine the state space
state = env_info.vector_observations[0]
state_size = len(state)
def dqn(agent,
n_episodes=4000, max_t=1000,
eps_start=1.0, eps_end=0.01, eps_decay=0.999,
train=True, checkpoint_filename='checkpoint.pth'):
"""Deep Q-Learning.
Args
n_episodes (int): maximum number of training episodes
max_t (int): maximum number of timesteps per episode
eps_start (float): starting value of epsilon, for epsilon-greedy action selection
eps_end (float): minimum value of epsilon
eps_decay (float): multiplicative factor (per episode) for decreasing epsilon
train (bool): flag deciding if the agent will train or just play through the episode
"""
scores = [] # list containing scores from each episode
scores_window = deque(maxlen=100) # last 100 scores
eps = eps_start # initialize epsilon
for i_episode in range(1, n_episodes+1):
env_info = env.reset(train_mode=train)[brain_name]
state = env_info.vector_observations[0]
score = 0
for t in range(max_t):
action = agent.act(state, eps if train else 0.0)
env_info = env.step(action)[brain_name]
next_state = env_info.vector_observations[0] # get the next state
reward = env_info.rewards[0] # get the reward
done = env_info.local_done[0] # see if episode has finished
if train:
agent.step(state, action, reward, next_state, done)
score += reward # update the score
state = next_state # roll over the state to next time step
if done: # exit loop if episode finished
break
scores_window.append(score) # save most recent score
scores.append(score) # save most recent score
eps = max(eps_end, eps_decay*eps) # decrease epsilon
print('\rEpisode {}\tAverage Score: {:.2f}'.format(i_episode, np.mean(scores_window)), end="")
if i_episode % 100 == 0:
print('\rEpisode {}\tAverage Score: {:.2f}'.format(i_episode, np.mean(scores_window)))
if np.mean(scores_window) >= 13.0 and train:
print('\nEnvironment solved in {:d} episodes!\tAverage Score: {:.2f}'.format(
i_episode-100,
np.mean(scores_window)
))
torch.save(agent.qnetwork_local.state_dict(), checkpoint_filename)
break
return scores
if __name__ == '__main__':
agent = Agent(network_type=DuelingQNetwork.net_type,
state_size=state_size,
action_size=action_size,
seed=0,
device=device,
checkpoint_filename='duel_checkpoint.pth')
scores = dqn(agent, n_episodes=100, train=False)
env.close()