How to use the mlagents.envs.UnityEnvironment function in mlagents

To help you get started, we’ve selected a few mlagents examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Sohojoe / MarathonEnvsBaselines / gym-unity / gym_unity / envs / unity_env.py View on Github external
def __init__(self, environment_filename: str, worker_id=0, use_visual=False, multiagent=False):
        """
        Environment initialization
        :param environment_filename: The UnityEnvironment path or file to be wrapped in the gym.
        :param worker_id: Worker number for environment.
        :param use_visual: Whether to use visual observation or vector observation.
        :param multiagent: Whether to run in multi-agent mode (lists of obs, reward, done).
        """
        self._env = UnityEnvironment(environment_filename, worker_id)
        self.name = self._env.academy_name
        self.visual_obs = None
        self._current_state = None
        self._n_agents = None
        self._multiagent = multiagent

        # Check brain configuration
        if len(self._env.brains) != 1:
            raise UnityGymException(
                "There can only be one brain in a UnityEnvironment "
                "if it is wrapped in a gym.")
        self.brain_name = self._env.external_brain_names[0]
        brain = self._env.brains[self.brain_name]

        if use_visual and brain.number_visual_observations == 0:
            raise UnityGymException("`use_visual` was set to True, however there are no"
github reinforcement-learning-kr / rl_bootcamp / codes / dqn_Unity / train_dqn.py View on Github external
def main():
    # Initialize environment
    env = UnityEnvironment(file_name='../../env/Pong/Pong')

    default_brain = env.brain_names[0]
    brain = env.brains[default_brain]

    env_info = env.reset(train_mode=True)[default_brain]

    obs_dim = env_info.vector_observations[0].shape[0]
    act_num = brain.vector_action_space_size[0]
    print('State dimension:', obs_dim)
    print('Action number:', act_num)

    # Set a random seed
    np.random.seed(0)
    torch.manual_seed(0)

    # Create a SummaryWriter object by TensorBoard
github StepNeverStop / RLwithUnity / simple_run.py View on Github external
def main():
    if sys.platform.startswith('win'):
        win32api.SetConsoleCtrlHandler(_win_handler, True)

    if train_config['unity_mode']:
        env = UnityEnvironment()
    else:
        env = UnityEnvironment(
            file_name=train_config['unity_file'],
            no_graphics=True if train_config['train'] else False,
            base_port=train_config['port']
        )
    brain_name = env.external_brain_names[0]
    brain = env.brains[brain_name]
    # set the memory use proportion of GPU
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    # tf_config.gpu_options.per_process_gpu_memory_fraction = 0.5
    tf.reset_default_graph()
    graph = tf.Graph()
    with graph.as_default() as g:
        with tf.Session(graph=g, config=tf_config) as sess:
github reinforcement-learning-kr / Unity_ML_Agents / algorithm / 5_BehavioralCloning.py View on Github external
losses.append(loss)
                accuracies.append(accuracy)
            
            # 게임 진행 상황 출력 및 텐서 보드에 loss, accuracy값 기록
            if epoch % print_interval == 0 and epoch != 0:
                print("epoch({}) - loss: {:.4f} / accuracy: {:.4f}".format(
                       epoch, np.mean(losses), np.mean(accuracies)))
                agent.Write_Summray(np.mean(losses), np.mean(accuracies), epoch)
                losses = []
                accuracies = []

        # 네트워크 모델 저장
        agent.save_model()

    else:
        env = UnityEnvironment(file_name=env_name)
        default_brain = env.brain_names[0]
        brain = env.brains[default_brain]

        env_info = env.reset(train_mode=train_mode, config=env_config)[default_brain]
        
        for episode in range(test_episode):
            done = False
            episode_rewards = 0

            while not done:
                action = agent.get_action(np.array([env_info.vector_observations[0]]))
                env_info = env.step(action)[default_brain]
                episode_rewards += env_info.rewards[0]
                done = env_info.local_done[0]

            print("Total reward this episode: {}".format(episode_rewards))
github StepNeverStop / RLs / client.py View on Github external
def initialize_env_model(filepath, algo, name, port):
    env = UnityEnvironment(
        file_name=filepath,
        base_port=port,
        no_graphics=True
    )
    if algo == 'pg':
        algorithm_config = Algorithms.pg_config
        model = Algorithms.PG
        policy_mode = 'ON'
    elif algo == 'ppo':
        algorithm_config = Algorithms.ppo_config
        model = Algorithms.PPO
        policy_mode = 'ON'
    elif algo == 'ddpg':
        algorithm_config = Algorithms.ddpg_config
        model = Algorithms.DDPG
        policy_mode = 'OFF'
github reinforcement-learning-kr / rl_bootcamp / 2-Intermediate / 6_ML-Agents / sac_Unity / train.py View on Github external
def main():
    # Initialize environment
    env = UnityEnvironment(file_name='../env/Hopper/Hopper')

    default_brain = env.brain_names[0]
    brain = env.brains[default_brain]

    env_info = env.reset(train_mode=True)[default_brain]

    obs_dim = env_info.vector_observations[0].shape[0]
    act_dim = brain.vector_action_space_size[0]
    print('State dimension:', obs_dim)
    print('Action dimension:', act_dim)

    # Set a random seed
    np.random.seed(0)
    torch.manual_seed(0)

    # Create a SummaryWriter object by TensorBoard
github danielnbarbosa / angela / libs / environments / unity.py View on Github external
def __init__(self, name, seed=0):
        from mlagents import envs
        self.seed = seed
        print('SEED: {}'.format(self.seed))
        self.env = envs.UnityEnvironment(file_name=name, seed=seed)
        self.brain_name = self.env.brain_names[0]
github Unity-Technologies / marathon-envs / gym-unity / gym_unity / envs / unity_env.py View on Github external
no_graphics: bool = False,
        allow_multiple_visual_obs: bool = False,
    ):
        """
        Environment initialization
        :param environment_filename: The UnityEnvironment path or file to be wrapped in the gym.
        :param worker_id: Worker number for environment.
        :param use_visual: Whether to use visual observation or vector observation.
        :param uint8_visual: Return visual observations as uint8 (0-255) matrices instead of float (0.0-1.0).
        :param multiagent: Whether to run in multi-agent mode (lists of obs, reward, done).
        :param flatten_branched: If True, turn branched discrete action spaces into a Discrete space rather than
            MultiDiscrete.
        :param no_graphics: Whether to run the Unity simulator in no-graphics mode
        :param allow_multiple_visual_obs: If True, return a list of visual observations instead of only one.
        """
        self._env = UnityEnvironment(
            environment_filename, worker_id, no_graphics=no_graphics
        )
        self.name = self._env.academy_name
        self.visual_obs = None
        self._current_state = None
        self._n_agents = None
        self._multiagent = multiagent
        self._flattener = None
        self.game_over = (
            False
        )  # Hidden flag used by Atari environments to determine if the game is over
        self._allow_multiple_visual_obs = allow_multiple_visual_obs

        # Check brain configuration
        if len(self._env.brains) != 1:
            raise UnityGymException(
github StepNeverStop / RLwithUnity / run.py View on Github external
sth.check_or_create(config_dir, 'config')

    logger = create_logger(
        name='logger',
        console_level=logging.INFO,
        console_format='%(levelname)s : %(message)s',
        logger2file=record_config['logger2file'],
        file_name=log_dir + '\log.txt',
        file_level=logging.WARNING,
        file_format='%(lineno)d - %(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s'
    )
    if train_config['train']:
        sth.save_config(config_dir, config)

    if train_config['unity_mode']:
        env = UnityEnvironment()
    else:
        env = UnityEnvironment(
            file_name=train_config['unity_file'],
            no_graphics=True if train_config['train'] else False,
            base_port=train_config['port']
        )
    brain_name = env.external_brain_names[0]
    brain = env.brains[brain_name]
    # set the memory use proportion of GPU
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    # tf_config.gpu_options.per_process_gpu_memory_fraction = 0.5
    tf.reset_default_graph()
    graph = tf.Graph()
    with graph.as_default() as g:
        with tf.Session(graph=g, config=tf_config) as sess: