-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
82 lines (69 loc) · 2.81 KB
/
Copy pathtrain.py
File metadata and controls
82 lines (69 loc) · 2.81 KB
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
from unityagents import UnityEnvironment
import numpy as np
import random
import time
import copy
from collections import namedtuple, deque
import torch
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
#%matplotlib inline
env = UnityEnvironment(file_name='Reacher_Linux_NoVis-V20/Reacher.x86_64')
# 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 agents
num_agents = len(env_info.agents)
print('Number of agents:', num_agents)
# size of each action
action_size = brain.vector_action_space_size
print('Size of each action:', action_size)
# examine the state space
states = env_info.vector_observations
state_size = states.shape[1]
print('There are {} agents. Each observes a state with length: {}'.format(states.shape[0], state_size))
print('The state for the first agent looks like:', states[0])
from agent import Agent
agent = Agent(state_size=state_size, action_size=action_size, random_seed=0)
def ddpg(n_episodes=2000, max_steps=1000):
scores_mean = deque(maxlen=100)
scores = []
best_score = 0
best_average_score = 0
for i_episode in range(1, n_episodes+1):
average_score = 0
env_info = env.reset(train_mode=True)[brain_name]
states = env_info.vector_observations
scores_agents = np.zeros(num_agents)
score = 0
agent.reset()
for step in range(max_steps):
actions = agent.act(states)
env_info = env.step(actions)[brain_name]
next_states = env_info.vector_observations
rewards = env_info.rewards
dones = env_info.local_done
agent.step(states, actions, rewards, next_states, dones, step)
states = next_states
scores_agents += rewards
if np.any(dones):
break
score = np.mean(scores_agents)
scores_mean.append(score)
average_score = np.mean(scores_mean)
scores.append(score)
if score > best_score:
best_score = score
if average_score > best_average_score:
best_average_score = average_score
print("Episode:{}, Low Score:{:.2f}, High Score:{:.2f}, Score:{:.2f}, Best Score:{:.2f}, Average Score:{:.2f}, Best Avg Score:{:.2f}".format(i_episode, scores_agents.min(), scores_agents.max(), score, best_score, average_score, best_average_score))
if average_score > 30:
torch.save(agent.actor_local.state_dict(), 'checkpoint_actor.pth')
torch.save(agent.critic_local.state_dict(), 'checkpoint_critic.pth')
print("Average score of 30 achieved")
break
return scores
scores = ddpg()