def __init__(self, env_name, visual_observations=False, train_mode=True, *args, base_port=10000, **kwargs): self.env_name = env_name self.visual_observations = visual_observations self.train_mode = train_mode self._env = UnityEnvironment(env_name, *args, base_port=base_port, **kwargs, side_channels=[ self._create_engine_channel(), AuxRewardSideChannel() ]) self._env.reset() groups = self._env.get_agent_groups() self._agent_group = self.DEFAULT_AGENT_GROUP if self.DEFAULT_AGENT_GROUP in groups else groups[ 0] self.group_spec = self._env.get_agent_group_spec(self._agent_group) self.observation_space = self._get_obs_space() self.action_space = self._get_action_space()
def test_initialization(mock_communicator, mock_launcher): mock_communicator.return_value = MockCommunicator( discrete_action=False, visual_inputs=0 ) env = UnityEnvironment(" ") assert env.get_behavior_names() == ["RealFakeBrain"] env.close()
def __init__(self, environment_filename, no_graphics): engine_configuration_channel = EngineConfigurationChannel() self.env = UnityEnvironment( file_name=environment_filename, side_channels=[engine_configuration_channel], no_graphics=no_graphics) self.env.reset() self.brain_name = self.env.get_agent_groups() self.group_spec = self.env.get_agent_group_spec(self.brain_name[0]) engine_configuration_channel.set_configuration_parameters( width=640, height=480, time_scale=3.0) self.group_name = self.brain_name # Set observation and action spaces if self.group_spec.is_action_discrete(): self._action_space = [] branches = self.group_spec.discrete_action_branches # if self.group_spec.action_shape == 1: for _ in range(2): self._action_space.append([ spaces.Discrete(branches[i]) for i in range(len(branches)) ]) else: high = np.array([1] * self.group_spec.action_shape) self._action_space = spaces.Box(-high, high, dtype=np.float32) high = np.array([np.inf] * self._get_vec_obs_size()) self._observation_space = spaces.Box(-high, high, dtype=np.float32)
def shuffleEnv(env_name,plot_only=False,future_version=False,trial_num=0,args=None,include_occlusion=False,spawn_orange=True,exp=None): #Create environment #print("Create env") if exp is None: exp = trial_num if args is None: env = UnityEnvironment(file_name=env_name,worker_id=trial_num,seed=trial_num) else: #print(trial_num) #print(args.worker_id+trial_num) env = UnityEnvironment(file_name=env_name,worker_id=args.worker_id+trial_num,seed=args.seed+trial_num) #Baseline Positions #print("Init") quad_omega = (np.random.rand(1) * 2*np.pi) - np.pi quadR = 5 quad_x = quadR * np.cos(quad_omega) quad_y = quadR * np.sin(quad_omega) x0 = np.array((quad_x,quad_y,1)) xmult = np.array((0.5,0.5,0.1)) yaw0 = 0 ymult = np.pi treePosYaw = np.array((0,0,0,0)) treemult = np.array((0.5,0.5,0,180)) treeHeight = 1.6 orangePos = np.array((0,0,0.5)) orangeR = 0.6 #0.6 orangeRmult = 0.1 #0.3 orangeHmult = 0.5 #randomize environment xoffset = 2*xmult*(np.random.rand(3) - 0.5) x0_i = x0 + xoffset #yaw0_i = yaw0 + 2*ymult*(np.random.rand(1) - 0.5) treeoffset = treemult*(np.random.rand(4) - 0.5) treePos_i = treePosYaw + treeoffset theta = 2*np.pi*np.random.random_sample() orangeR_rand = orangeRmult*np.random.random_sample() orangeH_rand = orangeHmult*np.random.random_sample() R_i = orangeR + orangeR_rand orangeoffset = np.array((R_i*np.cos(theta), R_i*np.sin(theta), orangePos[2] + orangeH_rand)) cR = (0.1 * min(1.0,max(0.0, np.random.normal(0.5)))) + 0.2 orangePos_i = treePos_i[0:3] + orangeoffset orangePosTrue = orangePos_i yaw0_i = np.arctan2(treePos_i[1]-x0_i[1],treePos_i[0]-x0_i[0]) x0_i = np.hstack((x0_i,yaw0_i,0,0)) envAct = np.array((np.random.randint(6),np.random.randint(6),0)) if not plot_only: (camName, envName, orangePos_i, occlusion, orangePosTrue) = setUpEnv(env,x0_i,treePos_i,orangePos_i,envAct, orangeColor = np.random.randint(9),future_version=future_version,collisionR = cR,spawn_orange=spawn_orange, exp=exp) else: camName = "" #R_i = orangeR + orangeR_rand + 0.5 #orangeoffset = np.array((R_i*np.cos(theta), R_i*np.sin(theta), # orangePos[2] + orangeH_rand)) #orangePos_i = treePos_i[0:3] + orangeoffset if not include_occlusion or plot_only or occlusion is None: return (env,x0_i, camName, envName, orangePos_i,treePos_i) else: return (env,x0_i, camName, envName, orangePos_i,treePos_i, occlusion, orangePosTrue)
def __init__(self, worker_id, realtime_mode=False): self.reset_parameters = EnvironmentParametersChannel() self.engine_config = EngineConfigurationChannel() env_path = "C:/myDesktop/source/gridworld_imitation/food_collector_4" self._env = UnityEnvironment( env_path, worker_id, side_channels=[self.reset_parameters, self.engine_config]) self._env.reset() self.behavior_name = list(self._env.behavior_specs)[0] behavior_spec = self._env.behavior_specs[self.behavior_name] print(behavior_spec) if realtime_mode: self.engine_config.set_configuration_parameters(time_scale=1.0) self.reset_parameters.set_float_parameter("train-mode", 0.0) else: self.engine_config.set_configuration_parameters(time_scale=20.0) self.reset_parameters.set_float_parameter("train-mode", 1.0) self._flattener = ActionFlattener( behavior_spec.action_spec.discrete_branches)
def make_unity_env(self, env_name, float_params=dict(), time_scale=1, seed=time.time(), worker_id=None, **kwargs): """ creates a gym environment from a unity game env_name: str the path to the game float_params: dict or None this should be a dict of argument settings for the unity environment keys: varies by environment time_scale: float argument to set Unity's time scale. This applies less to gym wrapped versions of Unity Environments, I believe.. but I'm not sure seed: int the seed for randomness worker_id: int must specify a unique worker id for each unity process on this machine """ if float_params is None: float_params = dict() path = os.path.expanduser(env_name) channel = EngineConfigurationChannel() env_channel = EnvironmentParametersChannel() channel.set_configuration_parameters(time_scale=1) for k, v in float_params.items(): if k == "validation" and v >= 1: print("Game in validation mode") env_channel.set_float_parameter(k, float(v)) if worker_id is None: worker_id = seed % 500 + 1 env_made = False n_loops = 0 worker_id = 0 while not env_made and n_loops < 50: try: env = UnityEnvironment(file_name=path, side_channels=[channel, env_channel], worker_id=worker_id, seed=seed) env_made = True except: s = "Error encountered making environment, " s += "trying new worker_id" print(s) worker_id = (worker_id + 1 + int(np.random.random() * 100)) % 500 try: env.close() except: pass n_loops += 1 env = UnityToGymWrapper(env, allow_multiple_obs=True) return env
def test_close(mock_communicator, mock_launcher): comm = MockCommunicator(discrete_action=False, visual_inputs=0) mock_communicator.return_value = comm env = UnityEnvironment(" ") assert env._loaded env.close() assert not env._loaded assert comm.has_been_closed
def test_log_file_path_is_set(mock_communicator, mock_launcher): mock_communicator.return_value = MockCommunicator() env = UnityEnvironment(file_name="myfile", worker_id=0, log_folder="./some-log-folder-path") args = env._executable_args() log_file_index = args.index("-logFile") assert args[log_file_index + 1] == "./some-log-folder-path/Player-0.log"
def test_port_defaults(mock_communicator, mock_launcher, base_port, file_name, expected): mock_communicator.return_value = MockCommunicator(discrete_action=False, visual_inputs=0) env = UnityEnvironment(file_name=file_name, worker_id=0, base_port=base_port) assert expected == env._port env.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('trial_dir', nargs='+', help='pickle folder') args = parser.parse_args() for folder_name in args.trial_dir: sim_img_list = [] ext_img_list = [] if os.path.isfile(folder_name + "/metadata.pickle"): metadata = pickle.load(open(folder_name + "/metadata.pickle", "rb")) else: metadata = {} metadata['tree'] = (-0.2, 0, 0, 26) metadata['orangeTrue'] = (-0.5, -0, 1.14) trajdata = pickle.load(open(folder_name + "/traj_data.pickle", "rb")) env_name = None #'unity/env_v9' env = UnityEnvironment(file_name=env_name, worker_id=0, seed=0) tree = np.array(metadata['tree']) orange = np.array(metadata['orangeTrue']) camName = None print(len(trajdata)) for ii, state in enumerate(trajdata): #pos = np.array(state[0]) #rot = R.from_dcm(state[1]).as_euler('zyz') #x = np.hstack((pos,rot)) if camName is None: (camName, envName, orange, occ_frac, dummy) = setUpEnv(env, state, tree, orange) camAct = makeCamAct(state) (im_arr, ext_arr) = unity_image(env, camAct, camName, envName) im_arr = img.fromarray((im_arr * 255).astype('uint8')) im_arr.save(folder_name + 'sim_image' + str(ii) + '.png') ext_arr = img.fromarray((ext_arr * 255).astype('uint8')) ext_arr.save(folder_name + 'ext_image' + str(ii) + '.png') sim_img_list.append(im_arr) ext_img_list.append(ext_arr) #print(ii) #print(state) env.close() if len(sim_img_list) > 0: sim_img_list[0].save(folder_name + "/sim.gif", save_all=True, append_images=sim_img_list[1:], duration=10, loop=0, optimize=True, quality=100) if len(ext_img_list) > 0: ext_img_list[0].save(folder_name + "/ext.gif", save_all=True, append_images=ext_img_list[1:], duration=10, loop=0, optimize=True, quality=100)
def main(): """ file_name: is the name of the environment binary (located in the root directory of the python project) worker_id: indicates which port to use for communication with the environment. For use in parallel training regimes such as A3C. seed: indicates the seed to use when generating random numbers during the training process. In environments which are deterministic, setting the seed enables reproducible experimentation by ensuring that the environment and trainers utilize the same random seed. side_channels: provides a way to exchange data with the Unity simulation that is not related to the reinforcement learning loop. For example: configurations or properties. More on them in the "Modifying the environment from Python"(https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Python-API.md#modifying-the-environment-from-python) section. --- env.reset() env.step() env.close() """ channel = EngineConfigurationChannel() filename = "Mummy" env = UnityEnvironment(file_name=filename, seed=1, side_channels=[channel]) channel.set_configuration_parameters(time_scale=2.0) env.reset() behavior_names = env.behavior_specs.keys() for name in behavior_names: print('behavior_name:', name) # Mummy?team=0 decision_steps, terminal_steps = env.get_steps( behavior_name="Mummy?team=0") """ print('DecisionSteps') print('- observation:', decision_steps.obs) print('- reward:', decision_steps.reward) print('- agent_id:', decision_steps.agent_id) print('- action_mask:', decision_steps.action_mask) print('TerminalSteps') print('- observation:', terminal_steps.obs) print('- reward:', terminal_steps.reward) print('- agent_id:', terminal_steps.agent_id) print('- interrupted:', terminal_steps.interrupted) """ while True: for i in decision_steps.agent_id: if i in terminal_steps.agent_id: continue env.set_action_for_agent(behavior_name="Mummy?team=0", agent_id=i, action=np.random.uniform(-1.0, 1.0, size=(2, ))) env.step() decision_steps, terminal_steps = env.get_steps( behavior_name="Mummy?team=0")
def test_int_channel(): sender = IntChannel() receiver = IntChannel() sender.send_int(5) sender.send_int(6) data = UnityEnvironment._generate_side_channel_data( {sender.channel_id: sender}) UnityEnvironment._parse_side_channel_message( {receiver.channel_id: receiver}, data) assert receiver.list_int[0] == 5 assert receiver.list_int[1] == 6
class UnityWrapper(object): def __init__(self, env_args): self.engine_configuration_channel = EngineConfigurationChannel() if env_args['train_mode']: self.engine_configuration_channel.set_configuration_parameters( time_scale=env_args['train_time_scale']) else: self.engine_configuration_channel.set_configuration_parameters( width=env_args['width'], height=env_args['height'], quality_level=env_args['quality_level'], time_scale=env_args['inference_time_scale'], target_frame_rate=env_args['target_frame_rate']) self.float_properties_channel = EnvironmentParametersChannel() if env_args['file_path'] is None: self._env = UnityEnvironment(base_port=5004, seed=env_args['env_seed'], side_channels=[ self.engine_configuration_channel, self.float_properties_channel ]) else: unity_env_dict = load_yaml('/'.join( [os.getcwd(), 'rls', 'envs', 'unity_env_dict.yaml'])) self._env = UnityEnvironment( file_name=env_args['file_path'], base_port=env_args['port'], no_graphics=not env_args['render'], seed=env_args['env_seed'], side_channels=[ self.engine_configuration_channel, self.float_properties_channel ], additional_args=[ '--scene', str( unity_env_dict.get(env_args.get('env_name', 'Roller'), 'None')), '--n_agents', str(env_args.get('env_num', 1)) ]) self.reset_config = env_args['reset_config'] def reset(self, **kwargs): reset_config = kwargs.get('reset_config', None) or self.reset_config for k, v in reset_config.items(): self.float_properties_channel.set_float_parameter(k, v) self._env.reset() def __getattr__(self, name): if name.startswith('_'): raise AttributeError( "attempted to get missing private attribute '{}'".format(name)) return getattr(self._env, name)
def _make_unity_env( env_path: Optional[str] = None, port: int = UnityEnvironment.BASE_ENVIRONMENT_PORT, seed: int = -1, env_args: Optional[List[str]] = None, engine_config: Optional[EngineConfig] = None, side_channels: Optional[List[SideChannel]] = None) -> UnityEnvironment: """ Create a UnityEnvironment. """ # Use Unity Editor if env file is not provided. if env_path is None: port = UnityEnvironment.DEFAULT_EDITOR_PORT else: launch_string = UnityEnvironment.validate_environment_path(env_path) if launch_string is None: raise UnityEnvironmentException( f"Couldn't launch the {env_path} environment. Provided filename does not match any environments." ) logger.info(f"Starting environment from {env_path}.") # Configure Unity Engine. if engine_config is None: engine_config = EngineConfig.default_config() engine_configuration_channel = EngineConfigurationChannel() engine_configuration_channel.set_configuration(engine_config) if side_channels is None: side_channels = [engine_configuration_channel] else: side_channels.append(engine_configuration_channel) # Find an available port to connect to Unity environment. while True: try: env = UnityEnvironment( file_name=env_path, seed=seed, base_port=port, args=env_args, side_channels=side_channels, ) except UnityWorkerInUseException: logger.debug(f"port {port} in use.") port += 1 else: logger.info(f"Connected to environment using port {port}.") break return env
def main(config): env_name = "./unity_env/TwoResourceProblem" engine_configuration_channel = EngineConfigurationChannel() env = UnityEnvironment(file_name=env_name, seed=1, side_channels=[engine_configuration_channel]) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) experiment = Experiment(env=env, env_channel=engine_configuration_channel, config=config, device=device) experiment.start() env.close()
def get_environment(config: Config) -> BaseEnv: channel = EnvironmentParametersChannel() file_name = None if config.RuntimeArgs.run_in_unity: file_name = None print(Fore.CYAN + "Environment set. Press play within Unity" + Fore.RESET) elif sys.platform.startswith("win"): file_name = 'Build/GridWorld.exe' elif sys.platform.startswith("darwin"): file_name = 'Build.app/Contents/MacOS/GridWorld' elif sys.platform.startswith("linux"): raise Exception("Tell chris to support linux") else: raise Exception("Unable to find which executable to run for system:" + sys.platform) # Load env = UnityEnvironment(file_name=file_name, side_channels=[channel]) # Set the channel environment accordingly channel.set_float_parameter("num_targets", config.Game.num_targets) channel.set_float_parameter("num_fires", config.Game.num_fires) allow_light_source = 1.0 if config.Game.allow_light_source else 0.0 channel.set_float_parameter("allow_light_source", allow_light_source) channel.set_float_parameter("step_reward", config.Game.step_reward) channel.set_float_parameter("target_reward", config.Game.target_reward) channel.set_float_parameter("fire_reward", config.Game.fire_reward) channel.set_float_parameter("max_steps", config.Game.max_steps) channel.set_float_parameter("time_scale", config.RuntimeArgs.time_scale) return env
def test_run_environment(env_name): """ Run the gym test using the specified environment :param env_name: Name of the Unity environment binary to launch """ u_env = UnityEnvironment(env_name, worker_id=1, no_graphics=True) env = UnityToGymWrapper(u_env) try: # Examine environment parameters print(str(env)) # Reset the environment initial_observations = env.reset() if len(env.observation_space.shape) == 1: # Examine the initial vector observation print("Agent observations look like: \n{}".format( initial_observations)) for _episode in range(10): env.reset() done = False episode_rewards = 0 while not done: actions = env.action_space.sample() obs, reward, done, _ = env.step(actions) episode_rewards += reward print("Total reward this episode: {}".format(episode_rewards)) finally: env.close()
def __init__(self, env_config): self.worker_index = env_config.worker_index if 'SM_CHANNEL_TRAIN' in os.environ: env_name = os.environ['SM_CHANNEL_TRAIN'] + '/' + env_config[ 'env_name'] os.chmod(env_name, 0o755) print("Changed environment binary into executable mode.") # Try connecting to the Unity3D game instance. while True: try: unity_env = UnityEnvironment( env_name, no_graphics=False, worker_id=self.worker_index, additional_args=['-logFile', 'unity.log']) except UnityWorkerInUseException: self.worker_index += 1 else: break else: env_name = env_config['env_name'] while True: try: unity_env = default_registry[env_name].make( no_graphics=False, worker_id=self.worker_index, additional_args=['-logFile', 'unity.log']) except UnityWorkerInUseException: self.worker_index += 1 else: break self.env = UnityToGymWrapper(unity_env) self.action_space = self.env.action_space self.observation_space = self.env.observation_space
async def sim_create(self, data) -> None: """An actor method to create a sim environment.""" env_id = data['env_id'] # seed = data['seed'] print(f'Creating sim with value {env_id}', flush=True) print(f"Current dir: {os.getcwd()}", flush=True) try: print("[Server] Creating Unity Environment", flush=True) self.env = UnityEnvironment(f"{os.getcwd()}/src/Server/Unity/envs/{env_id}/{env_id}") print("[Server] Resetting environment already", flush=True) self.env.reset() # we need to reset first in Unity # self.unity_env = UnityEnvironment("./environments/GridWorld") # self.env = gym.make(env_id) # if seed: # self.env.seed(seed) except gym.error.Error as e: print(e) raise Exception("Attempted to look up malformed environment ID '{}'".format(env_id)) except Exception as e: print(e) raise Exception(e) except: print(sys.exc_info()) traceback.print_tb(sys.exc_info()[2]) raise
def train(): engine_configuration_channel = EngineConfigurationChannel() # 時間スケールを20倍に設定 engine_configuration_channel.set_configuration_parameters(time_scale=20.0) unity_env = UnityEnvironment("./ml-agents/Project/PushBlock", side_channels=[engine_configuration_channel]) env = UnityToGymWrapper(unity_env, 0, flatten_branched=True) logger.configure('./logs') # DQNで学習 model = deepq.learn( env, "mlp", seed=0, lr=2.5e-4, total_timesteps=400000, buffer_size=50000, exploration_fraction=0.05, exploration_final_eps=0.1, print_freq=20, train_freq=5, learning_starts=20000, target_network_update_freq=50, gamma=0.99, prioritized_replay=False, checkpoint_freq=1000, dueling=True, checkpoint_path=None, load_path="./model" ) # モデルを保存 save_path = "./model" ckpt = tf.train.Checkpoint(model=model) manager = tf.train.CheckpointManager(ckpt, save_path, max_to_keep=1) manager.save()
def make(self, **kwargs: Any) -> BaseEnv: """ Returns the UnityEnvironment that corresponds to the Unity executable found at the provided url. The arguments passed to this method will be passed to the constructor of the UnityEnvironment (except for the file_name argument) """ url = None if platform == "linux" or platform == "linux2": url = self._linux_url if platform == "darwin": url = self._darwin_url if platform == "win32": url = self._win_url if url is None: raise FileNotFoundError( f"The entry {self.identifier} does not contain a valid url for this " "platform") path = get_local_binary_path(self.identifier, url) if "file_name" in kwargs: kwargs.pop("file_name") args: List[str] = [] if "additional_args" in kwargs: if kwargs["additional_args"] is not None: args += kwargs["additional_args"] if self._add_args is not None: args += self._add_args kwargs["additional_args"] = args return UnityEnvironment(file_name=path, **kwargs)
def _thunk(): unity_env = UnityEnvironment(env_directory) env = UnityToGymWrapper(unity_env, rank, uint8_visual=False) env = Monitor( env, logger.get_dir() and os.path.join(logger.get_dir(), str(rank))) return env
def create_environment_factory( env_path: Optional[str], no_graphics: bool, seed: int, start_port: int, env_args: Optional[List[str]], ) -> Callable[[int, List[SideChannel]], BaseEnv]: if env_path is not None: launch_string = UnityEnvironment.validate_environment_path(env_path) if launch_string is None: raise UnityEnvironmentException( f"Couldn't launch the {env_path} environment. Provided filename does not match any environments." ) def create_unity_environment( worker_id: int, side_channels: List[SideChannel] ) -> UnityEnvironment: # Make sure that each environment gets a different seed env_seed = seed + worker_id return UnityEnvironment( file_name=env_path, worker_id=worker_id, seed=env_seed, no_graphics=no_graphics, base_port=start_port, args=env_args, side_channels=side_channels, ) return create_unity_environment
def __init__(self, fName): parser = jsonParser(fName) self.data = parser.loadParser() self.aData = parser.loadAgentParser() self.optimData = parser.loadOptParser() keyList = list(self.data.keys()) torch.manual_seed(self.data['seed']) self.uMode = False if 'unityEnv' not in keyList else self.data[ 'unityEnv'] == "True" self.sMode = False if 'sMode' not in keyList else self.data[ 'sMode'] == "True" if self.sMode: self.tau = self.data['tau'] else: self.udateP = self.data['updateP'] name = self.data['envName'] print(name) if self.uMode: id_ = np.random.randint(10, 100, 1)[0] self.env = UnityEnvironment(name, worker_id=id_) self.brain = self.env.brain_names[0] else: self.env = gym.make(name) self.evalEnv = gym.make(name) self.nReplayMemory = int(self.data['nReplayMemory']) self.gamma = self.data['gamma'] self.bSize = self.data['bSize'] self.rScaling = self.data['rScaling'] self.lrFreq = self.data['lrFreq'] self.runStep = self.data['runStep'] self.startStep = self.data['startStep'] self.episodeP = self.data['episodeP'] self.sPath = self.data['sPath'] self.writeTMode = \ True if 'writeTMode' not in keyList else self.data['writeTMode'] == "True" if self.writeTMode: time = datetime.datetime.now().strftime("%Y%m%d-%H-%M-%S") self.tPath = self.data['tPath'] + self.data['envName'] + time self.writer = SummaryWriter(self.tPath) self.lPath = self.data['lPath'] self.device = self.data['device'] self.inferMode = self.data['inferMode'] == "True" self.renderMode = self.data['renderMode'] == "True" self.keyList = keyList self.aSize = self.data['aSize'] self.sSize = self.data['sSize'] self.nAgent = self.data['nAgent'] self.evalObsSet = deque(maxlen=self.sSize[0]) self.best = self.data['best'] self.evalP = self.data['evalP'] assert ~(self.uMode is False and self.nAgent > 1), "nAgent must be 1,"
def load_environment(): unity_timeout = 1000000 unity_env = UnityEnvironment(file_name="./build/BreakoutMining", seed=1, timeout_wait=unity_timeout, side_channels=[], no_graphics=False) return UnityToGymWrapper(unity_env)
def test_closing(env_name): """ Run the gym test and closes the environment multiple times :param env_name: Name of the Unity environment binary to launch """ try: env1 = UnityToGymWrapper( UnityEnvironment(env_name, worker_id=1, no_graphics=True)) env1.close() env1 = UnityToGymWrapper( UnityEnvironment(env_name, worker_id=1, no_graphics=True)) env2 = UnityToGymWrapper( UnityEnvironment(env_name, worker_id=2, no_graphics=True)) env2.reset() finally: env1.close() env2.close()
def get_env(env_directory: str, visual: bool, start_index: int = 0): """ For loading the evironment """ unity_env = UnityEnvironment(env_directory, seed=0, no_graphics=not visual) env = UnityToGymWrapper(unity_env, uint8_visual=False) return env
def __init__(self, unity_env_name, networks=None, networks_shape=None, population_size=1000, generation_number = 100, crossover_rate=0.3, crossover_method='neuron', mutation_rate=0.7, mutation_method='weight', n_process=1): """ Initializes the genetic algorithm :param networks(list of NeuralNetwork): First generation networks :param networks_shape(list of int): List defining number of layers and number of neurons in each layer :param population_size(int): Number of networks for each generation :param generation_number(int): How many generations the algorithm will run :param crossover_rate(int): Proportion of children to be produced at each generation :param crossover_method(str): How children will be produced :param mutation_rate(int): Proportion of the population to mutate at each generation :param mutation_method(str): How mutation will be done Todo: Replace n_agent as it will describe the number of simulations Todo: use yaml for easier configuration, Todo: automate NeuralNetwork inner and outer shape (let hidden layers as free choice) """ self.networks_shape = networks_shape if self.networks_shape is None: # if no shape is provided self.networks_shape = [21,16,3] # default shape self.networks = networks if networks is None: # if no networks are provided self.networks = [] for i in range(population_size): # producing population self.networks.append(NeuralNetwork(self.networks_shape)) self.population_size = population_size self.generation_number = generation_number self.crossover_rate = crossover_rate self.crossover_method = crossover_method self.mutation_rate = mutation_rate self.mutation_method = mutation_method self.n_process = n_process self.unity_env_name = unity_env_name self.env = UnityEnvironment(base_port = 5006, file_name=unity_env_name, seed=0, no_graphics=True) self.env.reset() group_name = self.env.get_agent_groups()[0] self.n_agents = self.env.get_step_result(group_name).n_agents() self.generation_durations = [] self.generation_performances = []
def test_raw_bytes(): sender = RawBytesChannel() receiver = RawBytesChannel() sender.send_raw_data("foo".encode("ascii")) sender.send_raw_data("bar".encode("ascii")) data = UnityEnvironment._generate_side_channel_data( {sender.channel_type: sender}) UnityEnvironment._parse_side_channel_message( {receiver.channel_type: receiver}, data) messages = receiver.get_and_clear_received_messages() assert len(messages) == 2 assert messages[0].decode("ascii") == "foo" assert messages[1].decode("ascii") == "bar" messages = receiver.get_and_clear_received_messages() assert len(messages) == 0
def __init__(self, worker_id=0, file_name=None, port=5005, render=False, seed=42, timeout_wait=60, env_copies=12, env_name='3DBall', real_done=True, initialize_config={}, engine_config={ 'width': 84, 'height': 84, 'quality_level': 5, 'time_scale': 20, 'target_frame_rate': -1, 'capture_frame_rate': 60 }, **kwargs): self._n_copies = env_copies self._real_done = real_done self._side_channels = self.initialize_all_side_channels( initialize_config, engine_config) env_kwargs = dict(seed=seed, worker_id=worker_id, timeout_wait=timeout_wait, side_channels=list( self._side_channels.values())) # 注册所有初始化后的通讯频道 if file_name is not None: env_dict = load_config('rls/configs/unity/env_dict.yaml') env_kwargs.update(file_name=file_name, base_port=port, no_graphics=not render, additional_args=[ '--scene', str(env_dict.get(env_name, 'None')) ]) self.env = UnityEnvironment(**env_kwargs) self.env.reset() self.initialize_environment()