def _create_env_from_random_state(self): # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) if self.saved_state is not None: seed2=self.saved_state[0] seed3=self.saved_state[1] seed4=self.saved_state[2] hour=self.saved_state[3] print('Using state', seed2, seed3, seed4, hour) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) if self.harrison_benedict: vpatient_params = pd.read_csv(PATIENT_PARA_FILE) self.kind = self.patient_name.split('#')[0] self.bw = vpatient_params.query('Name=="{}"'.format(self.patient_name))['BW'].item() scenario=RandomBalancedScenario(start_time=start_time, seed=seed3, harrison_benedict=self.harrison_benedict, bw=self.bw, kind=self.kind) else: scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnv(patient, sensor, pump, scenario,noise=self.noise) return env, seed2, seed3, seed4, hour
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed=seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 return [seed1, seed2, seed3]
def set_global_seeds(seed): import tensorflow as tf from gym.utils.seeding import hash_seed seed = hash_seed(seed, max_bytes=4) tf.set_random_seed(seed) np.random.seed(seed) random.seed(seed)
def seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) if self.game_mode is not None: modes = self.ale.getAvailableModes() assert self.game_mode in modes, ( "Invalid game mode \"{}\" for game {}.\nAvailable modes are: {}" ).format(self.game_mode, self.game, modes) self.ale.setMode(self.game_mode) if self.game_difficulty is not None: difficulties = self.ale.getAvailableDifficulties() assert self.game_difficulty in difficulties, ( "Invalid game difficulty \"{}\" for game {}.\nAvailable difficulties are: {}" ).format(self.game_difficulty, self.game, difficulties) self.ale.setDifficulty(self.game_difficulty) return [seed1, seed2]
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) self._action_set = self.ale.getMinimalActionSet() self.action_space = spaces.Discrete(len(self._action_set), np_random=self.np_random) (screen_width, screen_height) = self.ale.getScreenDims() if self._obs_type == 'ram': self.observation_space = spaces.Box(low=np.zeros(128), high=np.zeros(128) + 255, np_random=self.np_random) elif self._obs_type == 'image': self.observation_space = spaces.Box(low=0, high=255, shape=(screen_height, screen_width, 3), np_random=self.np_random) else: raise error.Error('Unrecognized observation type: {}'.format( self._obs_type)) return [seed1, seed2]
def __call__(self, env_maker, seed=None, monitor_file=None): """ :param env_maker: instance of roam_learning.robot_env.EnvMaker :param seed: int that is used to generate seeds for vectorized envs :param monitor_file: path to a .csv file to log episode rewards, lengths etc,. of the vectorized envs :return: instance of either DummyVecEnv, SubprocVecEnv or ShmemVecEnv """ # Create a list of env makers if seed is not None: assert isinstance(seed, int) env_makers = [] for i in range(self.nenvs): env_makers += [deepcopy(env_maker)] if seed is not None: seed = hash_seed(seed) env_makers[i].set_seed(seed + i) # Create the vectorized envs envs = self.vec_env_wrapper(env_makers) # Monitor the envs before normalization if monitor_file is not None: envs = VecMonitor(envs, filename=monitor_file) if self.normalize_obs or self.normalize_ret: envs = VecNormalize(envs, ob=self.normalize_obs, ret=self.normalize_ret, use_tf=True) return envs
def _create_env_from_random_state(self): # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnv(patient, sensor, pump, scenario) return env, seed2, seed3, seed4
def seed(self, seed=None): """ @brief Specify the seed of the environment. @details Note that it also resets the low-level Jiminy engine. One must call the `reset` method manually afterward. @param[in] seed non-negative integer. Optional: A strongly random seed will be generated by gym if omitted. @return Updated seed of the environment """ # Generate a 8 bytes (uint64) seed using gym utils self.rg, self._seed = seeding.np_random(seed) # Convert it into a 4 bytes uint32 seed. # Note that hashing is used to get rid off possible # correlation in the presence of concurrency. self._seed = np.uint32( seeding._int_list_from_bigint(seeding.hash_seed(self._seed))[0]) # Reset the seed of Jiminy Engine, if available if self.engine_py is not None: self.engine_py.seed(self._seed) return [self._seed]
def seed(self, seed=None): if seed is None: self.seed_ = create_seed() random.seed(self.seed_) else: self.seed_ = hash_seed(seed) random.seed(self.seed_)
def seed(self, seed=None): if self.benchmark_mode: initial_seed = 42 else: initial_seed = seeding.hash_seed(seed) % 2 ** 32 self._random_state = np.random.RandomState(seed=initial_seed) return [initial_seed]
def seed(self, seed=None): """ Derives a random seed. """ self.np_random, seed1 = seeding.np_random(seed) seed2 = seeding.hash_seed(seed1 + 1) % 2**31 return [seed1, seed2]
def seed(self, seed=None): print('_seed called') self.np_random, seed1 = seeding.np_random(seed=seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) self.env = _T1DSimEnv(patient, sensor, pump, scenario) return [seed1, seed2, seed3]
def seed(self, seed=None): """Set seed for the environment random generations.""" self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. Takem from atari_env.py seed2 = seeding.hash_seed(seed1 + 1) % 2**31 self.sim.seed(seed2) return [seed1, seed2]
def _launch_fceux(self): # Making sure ROM file is valid if '' == self.rom_path or not os.path.isfile(self.rom_path): raise gym.error.Error('Unable to find ROM. Please download the game from the web and configure the rom path by ' + 'calling env.configure(rom_path=path_to_file)') # Creating pipes self._create_pipes() # Creating temporary lua file self.temp_lua_path = os.path.join('/tmp', str(seeding.hash_seed(None) % 2 ** 32) + '.lua') temp_lua_file = open(self.temp_lua_path, 'w', 1) for k, v in list(self.launch_vars.items()): temp_lua_file.write('%s = "%s";\n' % (k, v)) i = 0 for script in self.lua_path: temp_lua_file.write('f_%d = assert (loadfile ("%s"));\n' % (i, script)) temp_lua_file.write('f_%d ();\n' % i) i += 1 temp_lua_file.close() # Resetting variables self.last_frame = 0 self.reward = 0 self.episode_reward = 0 self.is_finished = False self.screen = np.zeros(shape=(self.screen_height, self.screen_width, 3), dtype=np.uint8) self._reset_info_vars() # Loading fceux args = [FCEUX_PATH] args.extend(self.cmd_args[:]) args.extend(['--loadlua', self.temp_lua_path]) args.append(self.rom_path) args.extend(['>/dev/null', '2>/dev/null', '&']) self.subprocess = subprocess.Popen("/bin/bash", shell=False, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = self.subprocess.communicate(' '.join(args) + "\necho $!") self.fceux_pid = int(stdout) if 0 == self.subprocess.returncode: self.is_initialized = 1 if not self.disable_out_pipe: with self.lock_out: try: self.pipe_out = open(self.path_pipe_out, 'w', 1) except IOError: self.pipe_out = None # Removing lua file sleep(1) # Sleeping to make sure fceux has time to load file before removing if os.path.isfile(self.temp_lua_path): try: os.remove(self.temp_lua_path) except OSError: pass else: self.is_initialized = 0 raise gym.error.Error('Unable to start fceux. Command: %s' % (' '.join(args)))
def _seed(self, seed=None): np_random, seed1 = seeding.np_random(seed) # Derive a random seed. seed2 = seeding.hash_seed(seed1 + 1) % 2**32 self.game.set_seed(seed2) # 3 allowed actions [12, 13, 14] (must match .cfg file) self.action_space = spaces.HighLow(np.matrix([[0, 1, 0]] * 3), np_random=np_random) self.observation_space = spaces.Box(low=0, high=255, shape=(self.screen_height, self.screen_width, 3), np_random=np_random) return [seed1, seed2]
def _seed(self, seed=None): np_random, seed1 = seeding.np_random(seed) # Derive a random seed. seed2 = seeding.hash_seed(seed1 + 1) % 2**32 self.game.set_seed(seed2) # action indexes are [0, 9, 10, 12, 13, 14] self.action_space = spaces.HighLow(np.matrix([[0, 1, 0]] * 6), np_random=np_random) self.observation_space = spaces.Box(low=0, high=255, shape=(self.screen_height, self.screen_width, 3), np_random=np_random) return [seed1, seed2]
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) return [seed1, seed2]
def seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) return [seed1, seed2]
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. seed2 = seeding.hash_seed(seed1 + 1) % 2**32 pachi_py.pachi_srand(seed2) shape = pachi_py.CreateBoard(self.board_size).encode().shape self.observation_space = spaces.Box(np.zeros(shape), np.ones(shape), np_random=self.np_random) # One action for each board position, pass, and resign self.action_space = spaces.Discrete(self.board_size**2 + 2, np_random=self.np_random) return [seed1, seed2]
def seed(self, seed=None, id=0): _, seed1 = seeding.np_random(seed) if id > 0: seed = seed * 100 + id self.np_random, _ = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2)
def seed(self, seed: Optional[int] = None): r"""Define random number generators for different aspects of the environment. """ seed = seeding.hash_seed(seed, max_bytes=2) self._rng = random.Random() self._rng_read = random.Random() self._rng_np = np.random.RandomState() self._rng.seed(seed) self._rng_read.seed(seed) self._rng_np.seed(seed)
def get_experiment_environment(**args): from utils import setup_mpi_gpus, setup_tensorflow_session from baselines.common import set_global_seeds from gym.utils.seeding import hash_seed process_seed = args["seed"] + 1000 * MPI.COMM_WORLD.Get_rank() process_seed = hash_seed(process_seed, max_bytes=4) set_global_seeds(process_seed) setup_mpi_gpus() tf_context = setup_tensorflow_session() return tf_context
def get_experiment_environment(**args): from utils import setup_tensorflow_session from baselines.common import set_global_seeds from gym.utils.seeding import hash_seed process_seed = args["seed"] + 1000 * 0 process_seed = hash_seed(process_seed, max_bytes=4) set_global_seeds(process_seed) logger_context = logger.scoped_configure( dir=None, format_strs=['stdout', 'log', 'csv']) tf_context = setup_tensorflow_session() return logger_context, tf_context
def seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. * 2 seed2 = seeding.hash_seed(seed1 + 1) # Empirically, we need to seed before loading the ROM (ignoring this for now in our case). # return [seed1, seed2] self.state = None self.last_pop = 0 assert self.micro.map.getMapState().shape
def _seed(self, seed=None): self.ale.setFloat(b'repeat_action_probability', options.repeat_action_probability) from gym.utils import seeding self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2 ** 31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) return [seed1, seed2]
def _seed(self, seed=None): self.ale.setFloat(b'repeat_action_probability', options.repeat_action_probability) from gym.utils import seeding self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) return [seed1, seed2]
def _create_pipes(self): # Creates named pipe for inter-process communication self.pipe_name = seeding.hash_seed(None) % 2 ** 32 self.launch_vars['pipe_name'] = self.pipe_name if not self.disable_out_pipe: self.path_pipe_out = '%s-out.%d' % (self.path_pipe_prefix, self.pipe_name) os.mkfifo(self.path_pipe_out) # Launching a thread that will listen to incoming pipe # Thread exits if self.is_exiting = 1 or pipe_in is closed if not self.disable_in_pipe: thread_incoming = Thread(target=self._listen_to_incoming_pipe, kwargs={'pipe_name': self.pipe_name}) thread_incoming.start()
def get_experiment_environment(**args): process_seed = 1234 + 1000 * MPI.COMM_WORLD.Get_rank() process_seed = hash_seed(process_seed, max_bytes=4) set_global_seeds(1234) setup_mpi_gpus() logger_context = logger.scoped_configure( dir='C:/Users/Elias/Desktop/savedunc/' + MODE + '_' + datetime.now().strftime('%Y_%m_%d_%H_%M_%S'), format_strs=['stdout', 'log', 'csv', 'tensorboard'] if MPI.COMM_WORLD.Get_rank() == 0 else ['log']) tf_context = setup_tensorflow_session() return logger_context, tf_context
def seed(self, mode=1, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) # set the mode, removing 1 from the number # 1 is removed so the user can use the number from the manual # mode 1 in the manual is mode 0 in the code self.ale.setMode(mode - 1) return [seed1, seed2]
def get_experiment_environment(**args): from curiosity.utils import setup_mpi_gpus, setup_tensorflow_session from baselines.common import set_global_seeds from gym.utils.seeding import hash_seed process_seed = args["seed"] + 1000 * MPI.COMM_WORLD.Get_rank() process_seed = hash_seed(process_seed, max_bytes=4) set_global_seeds(process_seed) setup_mpi_gpus() logger_context = logger.scoped_configure(dir=None, format_strs=['stdout', 'log', 'csv'] if MPI.COMM_WORLD.Get_rank() == 0 else ['log']) tf_context = setup_tensorflow_session() return logger_context, tf_context
def seed(self, seed=None): """ This is totally the implementation in AtariEnv in openai/gym. """ self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. # Toybox takes a uint seed, but we're copying the ALE seed for reasons above. # They're unclear who checks, so being safe here. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 self.toybox.set_seed(seed2) # Start a new game to ensure that the seed gets used!. self.toybox.new_game() return [seed1, seed2]
def _create_env_from_random_state(self): #print('_create_env_from_random_state simglucose extended observation') # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) if self.saved_state is not None: seed2=self.saved_state[0] seed3=self.saved_state[1] seed4=self.saved_state[2] hour=self.saved_state[3] print('Using state', seed2, seed3, seed4, hour) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnvExtendedObs(patient, sensor, pump, scenario,n_samples=self._n_samples, append_action=self.append_action ) self.time_per_step=int(sensor.sample_time) self.time_in_env=0 return env, seed2, seed3, seed4, hour
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 # Empirically, we need to seed before loading the ROM. self.ale.setInt(b'random_seed', seed2) self.ale.loadROM(self.game_path) self._action_set = self.ale.getMinimalActionSet() self.action_space = spaces.Discrete(len(self._action_set), np_random=self.np_random) (screen_width,screen_height) = self.ale.getScreenDims() if self._obs_type == 'ram': self.observation_space = spaces.Box(low=np.zeros(128), high=np.zeros(128)+255, np_random=self.np_random) elif self._obs_type == 'image': self.observation_space = spaces.Box(low=0, high=255, shape=(screen_height, screen_width, 3), np_random=self.np_random) else: raise error.Error('Unrecognized observation type: {}'.format(self._obs_type)) return [seed1, seed2]
def _seed(self, seed=None): seed = seed if seed else seeding.hash_seed(seed) % 2**32 random.seed(seed) return [seed]
def _seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) # Derive a random seed. seed2 = seeding.hash_seed(seed1 + 1) % 2 ** 32 pachi_py.pachi_srand(seed2) return [seed1, seed2]
def _seed(self, seed=None): seed = seeding.hash_seed(seed) % 2**32 self.game.set_seed(seed) return [seed]
def seed(self, seed=None): self.np_random, seed1 = seeding.np_random(seed) seed2 = seeding.hash_seed(seed1 + 1) % 2 ** 31 return [seed1, seed2]
def _seed(self, seed=None): # Derive a random seed. seed = seeding.hash_seed(seed) % 2**32 self.game.set_seed(seed) return [seed]
def _seed(self, seed=None): self.curr_seed = seeding.hash_seed(seed) % 2 ** 32 return [self.curr_seed]