-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_trained_agent.py
64 lines (53 loc) · 2.14 KB
/
run_trained_agent.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
from unityagents import UnityEnvironment
import numpy as np
from collections import deque
import torch
from ddpg_agent import Agent
import datetime
import sys
import time
env = UnityEnvironment(file_name="Tennis.app")
# Environments contain brains which are responsible for deciding the actions of
# their associated agents. Here we check for the first brain available, and set
# it as the default brain we will be controlling from Python.
# get the default brain
brain_name = env.brain_names[0]
brain = env.brains[brain_name]
# reset the environment
env_info = env.reset(train_mode=False)[brain_name]
# Examine the State and Action Spaces
# 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
cstate_size = states.shape[1]
print(
"There are {} agents. Each observes a state with length: {}".format(
states.shape[0], cstate_size
)
)
print("The state for the first agent looks like:", states[0])
agent = Agent(state_size=cstate_size, action_size=action_size, random_seed=42)
agent.actor_local.load_state_dict(torch.load('checkpoint_actor.pth'))
agent.critic_local.load_state_dict(torch.load('checkpoint_critic.pth'))
env_info = env.reset(train_mode=False)[brain_name] # reset the environment
states = env_info.vector_observations # get the current state (for each agent)
scores = np.zeros(num_agents) # initialize the score (for each agent)
time.sleep(3)
for _ in range(500):
actions = agent.act(states, add_noise=False)
env_info = env.step(actions)[brain_name] # send all actions to the env
next_states = env_info.vector_observations # get next state for each agent
rewards = env_info.rewards # get reward (for each agent)
dones = env_info.local_done # see if episode finished
scores = np.add(
np.max(scores), env_info.rewards
) # update the score (for each agent)
states = next_states
if np.any(dones):
break
print("Total score (averaged over agents) this episode: {}".format(np.mean(scores)))