def _step_single(i, space, a): if not isinstance(a, collections.Iterable): a = [a] if isinstance(action_space, gym.spaces.Box): action_pb =\ ExternalControl_pb2.Action(continuous=a, done=False) else: action_pb =\ ExternalControl_pb2.Action(discrete=a, done=False) queues[i]['action'].put(action_pb)
def SendActionResult(self, action_result, context): """Receive ActionResult proto and send back an action.""" self.queues['action_response'].put(action_result) if not action_result.done: try: action = self.queues['action'].get(timeout=10000.0) except queue.Empty: action = ExternalControl_pb2.Action(done=True) res = ExternalControl_pb2.ActionResult(done=True) self.queues['action_response'].put(res) else: action = ExternalControl_pb2.Action(done=True) res = ExternalControl_pb2.ActionResult(done=True) self.queues['action_response'].put(res) return action
def _terminate_scrimmage(self): """Terminates scrimmage instance held by the class. given how sigints are handled by scrimmage, we need to shutdown the network to the autonomy in addition to sending a sigint. """ for queues in self.queues: queues['action'].put(ExternalControl_pb2.Action(done=True)) self.scrimmage_process.poll() while self.scrimmage_process.returncode is None: for queues in self.queues: queues['action'].put(ExternalControl_pb2.Action(done=True)) time.sleep(0.1) self.scrimmage_process.poll()
def _terminate_scrimmage(self): """Terminates scrimmage instance held by the class. given how sigints are handled by scrimmage, we need to shutdown the network to the autonomy in addition to sending a sigint. """ if self.scrimmage_process.returncode is None: try: os.remove(self.temp_mission_file) except OSError: pass for queues in self.queues: queues['action'].put(ExternalControl_pb2.Action(done=True)) try: self.scrimmage_process.kill() self.scrimmage_process.poll() while self.scrimmage_process.returncode is None: self.scrimmage_process.poll() time.sleep(0.1) except OSError: print('could not terminate existing scrimmage process. ' 'It may have already shutdown.')
def __init__(self, enable_gui, mission_file, num_actors=1, port_offset=1, address="localhost:50051", gdb_args=""): """Create queues for multi-threading.""" self.enable_gui = enable_gui self.mission_file = mission_file self.address = address self.gdb_args = gdb_args self.num_actors = num_actors self.port_offset = port_offset self.rng = None self.seed = self._seed(None) queue_names = ['env', 'action', 'action_response'] ip, port = address.split(":") self.queues = [] self.server_threads = [] for i in range(num_actors): port = int(port) + i * port_offset address = ip + ":" + str(port) self.queues.append({s: queue.Queue() for s in queue_names}) self.server_threads.append(ServerThread(self.queues[-1], address)) self.server_threads[-1].start() # startup headless version of scrimmage to get the environment for queues in self.queues: queues['action'].put(ExternalControl_pb2.Action(done=True)) self.scrimmage_process = self._start_scrimmage(False, True) environments = \ [self.queues[i]['env'].get() for i in range(num_actors)] self._terminate_scrimmage() if len(environments) == 1: self.action_space, self.observation_space, self.reward_range = \ self._create_spaces(environments[0]) else: spaces = [self._create_spaces(e) for e in environments] action_space, observation_space, reward_range = zip(*spaces) self.action_space = gym.spaces.Tuple(action_space) self.observation_space = gym.spaces.Tuple(observation_space) min_rewards, max_rewards = zip(*reward_range) self.reward_range = (min(min_rewards), max(max_rewards)) signal.signal(signal.SIGINT, self._signal_handler)