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 _return_action_result(self, index): info = {} try: res = self.queues[index]['action_response'].get(timeout=60) except queue.Empty: print('Scrimmage Environment: error getting action result') res = ExternalControl_pb2.ActionResult(done=True) info['scrimmage_err'] = "" obs = np.array(res.observations.value) return obs, res.reward, res.done, {}
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)
def main(): # Get params file, model:class, and port num from args list parser = argparse.ArgumentParser(description=None) parser.add_argument('--xml', default="/tmp/p", help='Parameters file path') parser.add_argument('--actor', type=str, help='what actor to use for acting. ' 'Input in the form module:Class (e.g., ' 'load_model:NNModel) and should have the same basic ' 'methods/properties as load_model:NNModel') parser.add_argument("--port-num", type=int, default=50052, help="port to start scrimmage interface on") args = parser.parse_args() # Initizialized variables num_actors = 1 port_offset = 1 queue_names = ['env', 'action', 'action_response'] ip = "localhost" queues = [] server_threads = [] port = args.port_num # Starting up GRPC link for i in range(num_actors): port = int(port) + i * port_offset address = ip + ":" + str(port) queues.append({s: queue.Queue() for s in queue_names}) server_threads.append( ServerThread(queues[-1], address)) server_threads[-1].start() environments = \ [queues[i]['env'].get() for i in range(num_actors)] if len(environments) == 1: action_space = _create_tuple_space(environments[0].action_spaces) observation_space = _create_tuple_space( environments[0].observation_spaces) else: action_space = [_create_tuple_space(e.action_spaces)for e in environments] observation_space = [_create_tuple_space(e.observation_spaces) for e in environments] action_space = gym.spaces.Tuple(action_space) observation_space = gym.spaces.Tuple(observation_space) # Set up actor model actor_module_str, actor_class_str = args.actor.split(":") actor_module = importlib.import_module(actor_module_str) actor_class = getattr(actor_module, actor_class_str) # Actor initialization actor = actor_class(action_space, observation_space, args.xml) while True: # get state from GRPC link try: res = queues[0]['action_response'].get(timeout=60) except queue.Empty: print('Not receiving sensor info from scrimmage') res = ExternalControl_pb2.ActionResult(done=True) if res.done: print("Received end of simulation") for s in server_threads: s.stop = True break obs = np.array(res.observations.value) # get action from actor action = actor.act(obs) # return action to GRPC link 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) if num_actors == 1: _step_single(0, action_space, action) else: for i, a in enumerate(action): _step_single(i, action_space.spaces[i], a)
def SendEnvironment(self, env, context): """Receive Environment proto and send back an action.""" self.queues['env'].put(env) return ExternalControl_pb2.Empty()