<jupyter_start><jupyter_text>Demonstration  Installation<jupyter_code>!pip -q install ./python<jupyter_output><empty_output><jupyter_text>Import functionality<jupyter_code>from unityagents import UnityEnvironment
import numpy as np

# Invite our agent & import utils
from ddpg_agent import Agent
#from random import random as rnd
import torch<jupyter_output><empty_output><jupyter_text>Initialization<jupyter_code>vm_ = True
if vm_:
    # select version 1 (with a single agent) of the environment
    env = UnityEnvironment(file_name='/data/Reacher_One_Linux_NoVis/Reacher_One_Linux_NoVis.x86_64')
else:
    # Obtain from local path
    env = UnityEnvironment(file_name='Reacher_Linux/Reacher.x86')
    
# 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])<jupyter_output><empty_output><jupyter_text>Useful Functions<jupyter_code>def act(env, actions, brain_name="ReacherBrain") -> tuple:
    """Sends actions to the environment env and observes the results.
    Returns a tuple of rewards, next_states, dones (One per agent)"""
    action_result = env.step(actions)[brain_name] # Act on the environment and observe the result
    return (action_result.rewards,
            action_result.vector_observations, # next states
            action_result.local_done) # True if the episode ended
    
def reset(env, training=True, brain_name="ReacherBrain") -> np.ndarray:
    """Syntactic sugar for resetting the unity environment"""
    return env.reset(train_mode=training)[brain_name].vector_observations

def visualize(agent, env): 
    states = reset(env)
    score = 0
    done = False
    while not done:
        actions = agent.decide(states)      # Choose an action based on the state
        rewards, next_states, dones = act(env, actions)    # Send the action to the environment
        score += rewards[0]                                # Update the score
        states = next_states                             # Roll over the state to next time step
        done = any(dones)
    print("Score: {}".format(score))<jupyter_output><empty_output><jupyter_text>Simulate!<jupyter_code>agent = Agent(state_size, action_size, 0)
state_dict = torch.load('my_weights.pth')<jupyter_output><empty_output><jupyter_text>Obtain weights<jupyter_code>agent.actor_local.load_state_dict(state_dict['actor_state_dict'])
agent.critic_local.load_state_dict(state_dict['critic_state_dict'])<jupyter_output><empty_output><jupyter_text>Visualize<jupyter_code>visualize(agent, env)<jupyter_output><empty_output><jupyter_text>When finished, you can close the environment.<jupyter_code>env.close()
import torch
torch.cuda.current_device()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09680421967111387, 'fraction_numerical': 0.004343779087806391, 'mean_word_length': 5.06015037593985, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '313920', 'license': 'MIT', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1012, 'n_words': 241, 'path': 'Demonstration.ipynb', 'repo_name': 'BruceChanJianLe/drlnd-reacher-project2', 'size': '3223'}