def test_no_workspace_throws_error(): config = BonsaiClientConfig() config.access_key = "1" try: BonsaiClient(config) assert False except RuntimeError: pass
def __init__(self, config: BonsaiClientConfig, *, log_dispatch: bool = True): self._config = config self._registered = None self._client = BonsaiClient(config) self._sequence_id = 1 self._log_dispatch = log_dispatch
def test_404_err_registration(): config = BonsaiClientConfig() config.server = "http://127.0.0.1:9000" config.workspace = "notfound" config.access_key = "111" client = BonsaiClient(config) try: interface = SimulatorInterface(name="a", timeout=1) client.session.create(config.workspace, interface) except HttpResponseError as err: assert err.status_code == 404
def test_flaky_sim(patched_sleep: Mock): config = BonsaiClientConfig() config.server = "http://127.0.0.1:9000" config.workspace = "flaky" config.access_key = "111" client = BonsaiClient(config) interface = SimulatorInterface(name="a", timeout=1) client.session.create(config.workspace, interface) counter = 0 while counter < 100: sim_state = SimulatorState(session_id=1, sequence_id=1, state={}, halted=False) client.session.advance(config.workspace, 1, body=sim_state) counter += 1
def __init__( self, model_name: str, render: bool = False, log_data: bool = False, ): """ Parameters ---------- model_name: str Name of the model to use. Must be an existing subfolder inside the "models" folder render: bool, optional Whether to visualize episodes during training, by default False log_data: bool, optional Whether to log data, by default False """ self.model_name = model_name self.render = render self.log_data = log_data self.sim = SimulatorSession(model_name=model_name, render=render, log_data=log_data) # The SIM_WORKSPACE and SIM_ACCESS_KEY are used internally by the BonsaiClient. So even if we don't use them, we check for their existence. get_environment_variable("SIM_WORKSPACE") get_environment_variable("SIM_ACCESS_KEY") sim_prefix = get_environment_variable("SIM_PREFIX") # Configure client to interact with Bonsai service self.config_client = BonsaiClientConfig() self.client = BonsaiClient(self.config_client) # Create simulator session self.registration_info = SimulatorInterface( name=f"{sim_prefix}_{self.sim.model_name}", timeout=60, simulator_context=self.config_client.simulator_context, ) self.registered_session = None self.sequence_id = None
def main(render=False): # Grab standardized way to interact with sim API sim = TemplateSimulatorSession(render=render) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Load json file as simulator integration config type file with open('moab_interface.json', "r") as file: template_str = file.read() # render the template with our constants template = Template(template_str) interface_str = template.render( initial_pitch=sim.simulator.pitch, initial_roll=sim.simulator.roll, initial_height_z=sim.simulator.height_z, time_delta=sim.simulator.time_delta, gravity=sim.simulator.time_delta, plate_radius=sim.simulator.plate_radius, plate_theta_vel_limit=sim.simulator.plate_theta_vel_limit, plate_theta_acc=sim.simulator.plate_theta_acc, plate_theta_limit=sim.simulator.plate_theta_limit, plate_z_limit=sim.simulator.plate_z_limit, ball_mass=sim.simulator.ball_mass, ball_radius=sim.simulator.ball_radius, ball_shell=sim.simulator.ball_shell, obstacle_radius=sim.simulator.obstacle_radius, obstacle_x=sim.simulator.obstacle_x, obstacle_y=sim.simulator.obstacle_y, target_x=sim.simulator.target_x, target_y=sim.simulator.target_y, initial_x=sim.simulator.ball.x, initial_y=sim.simulator.ball.y, initial_z=sim.simulator.ball.z, initial_vel_x=sim.simulator.ball_vel.x, initial_vel_y=sim.simulator.ball_vel.y, initial_vel_z=sim.simulator.ball_vel.z, initial_speed=0, initial_direction=0, ball_noise=sim.simulator.ball_noise, plate_noise=sim.simulator.plate_noise, ) interface = json.loads(interface_str) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=interface["name"], timeout=interface["timeout"], simulator_context=config_client.simulator_context, description=interface["description"], ) registered_session = client.session.create( workspace_name=config_client.workspace, body=registration_info) print("Registered simulator.") sequence_id = 1 try: while True: # Advance by the new state depending on the event type sim_state = SimulatorState(sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted()) event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state) sequence_id = event.sequence_id print("[{}] Last Event: {}".format(time.strftime('%H:%M:%S'), event.type)) # Event loop if event.type == 'Idle': time.sleep(event.idle.callback_time) print('Idling...') elif event.type == 'EpisodeStart': sim.episode_start(event.episode_start.config) elif event.type == 'EpisodeStep': sim.episode_step(event.episode_step.action) elif event.type == 'EpisodeFinish': print('Episode Finishing...') elif event.type == 'Unregister': client.session.delete(workspace_name=config_client.workspace, session_id=registered_session.session_id) print("Unregistered simulator.") else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete(workspace_name=config_client.workspace, session_id=registered_session.session_id) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete(workspace_name=config_client.workspace, session_id=registered_session.session_id) print("Unregistered simulator because: {}".format(err))
def main(render=False): # Grab standardized way to interact with sim API sim = TemplateSimulatorSession(render=render) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Load json file as simulator integration config type file with open('interface.json') as file: interface = json.load(file) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=interface['name'], timeout=interface['timeout'], simulator_context=config_client.simulator_context, description=interface['description'] ) def CreateSession(registration_info: SimulatorInterface, config_client: BonsaiClientConfig): """Creates a new Simulator Session and returns new session, sequenceId """ try: print("config: {}, {}".format(config_client.server, config_client.workspace)) registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info ) print("Registered simulator. {}".format(registered_session.session_id)) return registered_session, 1 except HttpResponseError as ex: print("HttpResponseError in Registering session: StatusCode: {}, Error: {}, Exception: {}".format(ex.status_code, ex.error.message, ex)) raise ex except Exception as ex: print("UnExpected error: {}, Most likely, It's some network connectivity issue, make sure, you are able to reach bonsai platform from your PC.".format(ex)) raise ex registered_session, sequence_id = CreateSession(registration_info, config_client) sequence_id = 1 try: while True: # Advance by the new state depending on the event type sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted() ) try: event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state ) sequence_id = event.sequence_id print("[{}] Last Event: {}".format(time.strftime('%H:%M:%S'), event.type)) except HttpResponseError as ex: print("HttpResponseError in Advance: StatusCode: {}, Error: {}, Exception: {}".format(ex.status_code, ex.error.message, ex)) # This can happen in network connectivity issue, though SDK has retry logic, but even after that request may fail, # if your network has some issue, or sim session at platform is going away.. # So let's re-register sim-session and get a new session and continue iterating. :-) registered_session, sequence_id = CreateSession(registration_info, config_client) continue except Exception as err: print("Unexpected error in Advance: {}".format(err)) # Ideally this shouldn't happen, but for very long-running sims It can happen with various reasons, let's re-register sim & Move on. # If possible try to notify Bonsai team to see, if this is platform issue and can be fixed. registered_session, sequence_id = CreateSession(registration_info, config_client) continue # Event loop if event.type == 'Idle': time.sleep(event.idle.callback_time) print('Idling...') elif event.type == 'EpisodeStart': sim.episode_start(event.episode_start.config) elif event.type == 'EpisodeStep': sim.episode_step(event.episode_step.action) elif event.type == 'EpisodeFinish': print('Episode Finishing...') elif event.type == 'Unregister': print("Simulator Session unregistered by platform because '{}', Registering again!".format(event.unregister.details)) registered_session, sequence_id = CreateSession(registration_info, config_client) continue else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id ) print("Unregistered simulator because: {}".format(err))
def main( render: bool = False, log_iterations: bool = False, config_setup: bool = False ): """Main entrypoint for running simulator connections Parameters ---------- render : bool, optional visualize steps in environment, by default True, by default False log_iterations: bool, optional log iterations during training to a CSV file """ # workspace environment variables if config_setup: env_setup() load_dotenv(verbose=True, override=True) # Grab standardized way to interact with sim API sim = TemplateSimulatorSession(render=render, log_data=log_iterations) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # # Load json file as simulator integration config type file with open("cartpole_description.json") as file: interface = json.load(file) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=sim.env_name, timeout=60, simulator_context=config_client.simulator_context, ) def CreateSession( registration_info: SimulatorInterface, config_client: BonsaiClientConfig ): """Creates a new Simulator Session and returns new session, sequenceId """ try: print( "config: {}, {}".format(config_client.server, config_client.workspace) ) registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info ) print("Registered simulator. {}".format(registered_session.session_id)) return registered_session, 1 except HttpResponseError as ex: print( "HttpResponseError in Registering session: StatusCode: {}, Error: {}, Exception: {}".format( ex.status_code, ex.error.message, ex ) ) raise ex except Exception as ex: print( "UnExpected error: {}, Most likely, it's some network connectivity issue, make sure you are able to reach bonsai platform from your network.".format( ex ) ) raise ex registered_session, sequence_id = CreateSession(registration_info, config_client) episode = 0 iteration = 0 try: while True: # Advance by the new state depending on the event type # TODO: it's risky not doing doing `get_state` without first initializing the sim sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted(), ) try: event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print( "[{}] Last Event: {}".format(time.strftime("%H:%M:%S"), event.type) ) except HttpResponseError as ex: print( "HttpResponseError in Advance: StatusCode: {}, Error: {}, Exception: {}".format( ex.status_code, ex.error.message, ex ) ) # This can happen in network connectivity issue, though SDK has retry logic, but even after that request may fail, # if your network has some issue, or sim session at platform is going away.. # So let's re-register sim-session and get a new session and continue iterating. :-) registered_session, sequence_id = CreateSession( registration_info, config_client ) continue except Exception as err: print("Unexpected error in Advance: {}".format(err)) # Ideally this shouldn't happen, but for very long-running sims It can happen with various reasons, let's re-register sim & Move on. # If possible try to notify Bonsai team to see, if this is platform issue and can be fixed. registered_session, sequence_id = CreateSession( registration_info, config_client ) continue # Event loop if event.type == "Idle": time.sleep(event.idle.callback_time) print("Idling...") elif event.type == "EpisodeStart": print(event.episode_start.config) sim.episode_start(event.episode_start.config) episode += 1 elif event.type == "EpisodeStep": iteration += 1 sim.episode_step(event.episode_step.action) if sim.log_data: sim.log_iterations( episode=episode, iteration=iteration, state=sim.get_state(), action=event.episode_step.action, ) elif event.type == "EpisodeFinish": print("Episode Finishing...") iteration = 0 elif event.type == "Unregister": print("Simulator Session unregistered by platform because '{}', Registering again!".format(event.unregister.details)) registered_session, sequence_id = CreateSession( registration_info, config_client ) continue else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator because: {}".format(err))
def main(cfg: DictConfig): save_path = cfg["model"]["saver"]["filename"] if cfg["data"]["full_or_relative"] == "relative": save_path = os.path.join(dir_path, save_path) model_name = cfg["model"]["name"] states = cfg["simulator"]["states"] actions = cfg["simulator"]["actions"] configs = cfg["simulator"]["configs"] initial_states = cfg["simulator"]["initial_states"] policy = cfg["simulator"]["policy"] # logflag = cfg["simulator"]["logging"] # logging not yet implemented scale_data = cfg["model"]["build_params"]["scale_data"] diff_state = cfg["data"]["diff_state"] workspace_setup = cfg["simulator"]["workspace_setup"] episode_inits = cfg["simulator"]["episode_inits"] input_cols = cfg["data"]["inputs"] output_cols = cfg["data"]["outputs"] augmented_cols = cfg["data"]["augmented_cols"] if type(input_cols) == ListConfig: input_cols = list(input_cols) if type(output_cols) == ListConfig: output_cols = list(output_cols) if type(augmented_cols) == ListConfig: augmented_cols = list(augmented_cols) input_cols = input_cols + augmented_cols logger.info(f"Using DDM with {policy} policy") if model_name.lower() == "pytorch": from all_models import available_models else: from model_loader import available_models Model = available_models[model_name] model = Model() model.load_model(filename=save_path, scale_data=scale_data) # model.build_model(**cfg["model"]["build_params"]) if not initial_states: logger.warn( "No initial values provided, using randomly initialized states which is probably NOT what you want" ) initial_states = {k: random.random() for k in states} signal_builder = cfg["simulator"]["signal_builder"] # Grab standardized way to interact with sim API sim = Simulator( model, states, actions, configs, input_cols, output_cols, episode_inits, initial_states, signal_builder, diff_state, ) # do a random action to get initial state sim.episode_start() if policy == "random": random_policy_from_keys = partial(random_policy, action_keys=sim.action_keys) test_policy( sim=sim, config={ **episode_inits, **initial_states }, policy=random_policy_from_keys, ) elif isinstance(policy, int): # If docker PORT provided, set as exported brain PORT port = policy url = f"http://localhost:{port}" print(f"Connecting to exported brain running at {url}...") trained_brain_policy = partial(brain_policy, exported_brain_url=url) test_policy( sim=sim, config={ **episode_inits, **initial_states }, policy=trained_brain_policy, ) elif policy == "bonsai": if workspace_setup: logger.info(f"Loading workspace information form .env") env_setup() load_dotenv(verbose=True, override=True) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=env_name, timeout=60, simulator_context=config_client.simulator_context, ) def CreateSession(registration_info: SimulatorInterface, config_client: BonsaiClientConfig): """Creates a new Simulator Session and returns new session, sequenceId""" try: print("config: {}, {}".format(config_client.server, config_client.workspace)) registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info) print("Registered simulator. {}".format( registered_session.session_id)) return registered_session, 1 # except HttpResponseError as ex: # print( # "HttpResponseError in Registering session: StatusCode: {}, Error: {}, Exception: {}".format( # ex.status_code, ex.error.message, ex # ) # ) # raise ex except Exception as ex: print( "UnExpected error: {}, Most likely, it's some network connectivity issue, make sure you are able to reach bonsai platform from your network." .format(ex)) raise ex registered_session, sequence_id = CreateSession( registration_info, config_client) episode = 0 iteration = 0 try: while True: # Advance by the new state depending on the event type sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted(), ) try: event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print("[{}] Last Event: {}".format( time.strftime("%H:%M:%S"), event.type)) # UPDATE #comment-out-azure-cli: # - commented out the HttpResponseError since it relies on azure-cli-core which has # - conflicting dependencies with microsoft-bonsai-api # - the catch-all exception below should still re-connect on disconnects # except HttpResponseError as ex: # print( # "HttpResponseError in Advance: StatusCode: {}, Error: {}, Exception: {}".format( # ex.status_code, ex.error.message, ex # ) # ) # # This can happen in network connectivity issue, though SDK has retry logic, but even after that request may fail, # # if your network has some issue, or sim session at platform is going away.. # # So let's re-register sim-session and get a new session and continue iterating. :-) # registered_session, sequence_id = CreateSession( # registration_info, config_client # ) # continue except Exception as err: print("Unexpected error in Advance: {}".format(err)) # Ideally this shouldn't happen, but for very long-running sims It can happen with various reasons, let's re-register sim & Move on. # If possible try to notify Bonsai team to see, if this is platform issue and can be fixed. registered_session, sequence_id = CreateSession( registration_info, config_client) continue # Event loop if event.type == "Idle": time.sleep(event.idle.callback_time) print("Idling...") elif event.type == "EpisodeStart": print(event.episode_start.config) sim.episode_start(event.episode_start.config) episode += 1 elif event.type == "EpisodeStep": iteration += 1 sim.episode_step(event.episode_step.action) elif event.type == "EpisodeFinish": print("Episode Finishing...") iteration = 0 elif event.type == "Unregister": print( "Simulator Session unregistered by platform because '{}', Registering again!" .format(event.unregister.details)) registered_session, sequence_id = CreateSession( registration_info, config_client) continue else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator because: {}".format(err))
def main( render: bool = False, log_iterations: bool = False, config_setup: bool = False, sim_speed: int = 0, sim_speed_variance: int = 0, env_file: Union[str, bool] = ".env", workspace: str = None, accesskey: str = None, ): """Main entrypoint for running simulator connections Parameters ---------- render : bool, optional visualize steps in environment, by default True, by default False log_iterations: bool, optional log iterations during training to a CSV file config_setup: bool, optional if enabled then uses a local `.env` file to find sim workspace id and access_key sim_speed: int, optional the average delay to use, default = 0 sim_speed_variance: int, optional the variance for sim delay env_file: str, optional if config_setup True, then where the environment variable for lookup exists workspace: str, optional optional flag from CLI for workspace to override accesskey: str, optional optional flag from CLI for accesskey to override """ # check if workspace or access-key passed in CLI use_cli_args = all([workspace, accesskey]) # check for accesskey and workspace id in system variables if all( [ not use_cli_args, "SIM_WORKSPACE" in os.environ, "SIM_ACCESS_KEY" in os.environ, ] ): workspace = os.environ["SIM_WORKSPACE"] accesskey = os.environ["SIM_ACCESS_KEY"] elif use_cli_args: # Use workspace and access key from CLI args passed into main pass elif config_setup or env_file: print( f"No system variables for workspace-id or access-key found, checking in env-file (.env by default)" ) workspace, accesskey = env_setup(env_file) load_dotenv(verbose=True, override=True) else: pass # Grab standardized way to interact with sim API sim = TemplateSimulatorSession(render=render, log_data=log_iterations) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # # Load json file as simulator integration config type file with open("cartpole_description.json") as file: interface = json.load(file) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=sim.env_name, timeout=interface["timeout"], simulator_context=config_client.simulator_context, description=interface["description"], ) def CreateSession( registration_info: SimulatorInterface, config_client: BonsaiClientConfig ): """Creates a new Simulator Session and returns new session, sequenceId """ try: print( "config: {}, {}".format(config_client.server, config_client.workspace) ) registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info ) print("Registered simulator. {}".format(registered_session.session_id)) return registered_session, 1 except HttpResponseError as ex: print( "HttpResponseError in Registering session: StatusCode: {}, Error: {}, Exception: {}".format( ex.status_code, ex.error.message, ex ) ) raise ex except Exception as ex: print( "UnExpected error: {}, Most likely, it's some network connectivity issue, make sure you are able to reach bonsai platform from your network.".format( ex ) ) raise ex registered_session, sequence_id = CreateSession(registration_info, config_client) episode = 0 iteration = 0 try: while True: # Advance by the new state depending on the event type # TODO: it's risky not doing doing `get_state` without first initializing the sim sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted(), ) try: event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print( "[{}] Last Event: {}".format(time.strftime("%H:%M:%S"), event.type) ) except HttpResponseError as ex: print( "HttpResponseError in Advance: StatusCode: {}, Error: {}, Exception: {}".format( ex.status_code, ex.error.message, ex ) ) # This can happen in network connectivity issue, though SDK has retry logic, but even after that request may fail, # if your network has some issue, or sim session at platform is going away.. # So let's re-register sim-session and get a new session and continue iterating. :-) registered_session, sequence_id = CreateSession( registration_info, config_client ) continue except Exception as err: print("Unexpected error in Advance: {}".format(err)) # Ideally this shouldn't happen, but for very long-running sims It can happen with various reasons, let's re-register sim & Move on. # If possible try to notify Bonsai team to see, if this is platform issue and can be fixed. registered_session, sequence_id = CreateSession( registration_info, config_client ) continue # Event loop if event.type == "Idle": time.sleep(event.idle.callback_time) print("Idling...") elif event.type == "EpisodeStart": print(event.episode_start.config) sim.episode_start(event.episode_start.config) episode += 1 elif event.type == "EpisodeStep": iteration += 1 delay = 0.0 if sim_speed > 0: if ( sim_speed_variance > 0 ): # stochastic delay, truncated normal distribution mu = sim_speed sigma = sim_speed_variance lower = np.max( [0, sim_speed - 3 * sim_speed_variance] ) # truncating at min +/- 3*variance upper = sim_speed + 3 * sim_speed_variance delay = truncnorm.rvs( (lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma, ) print("stochastic sim delay: {}s".format(delay)) time.sleep(delay) else: # fixed delay delay = sim_speed print("sim delay: {}s".format(delay)) time.sleep(delay) sim.episode_step(event.episode_step.action) if sim.log_data: sim.log_iterations( episode=episode, iteration=iteration, state=sim.get_state(), action=event.episode_step.action, sim_speed_delay=delay, ) elif event.type == "EpisodeFinish": print("Episode Finishing...") iteration = 0 elif event.type == "Unregister": print( "Simulator Session unregistered by platform because '{}', Registering again!".format( event.unregister.details ) ) registered_session, sequence_id = CreateSession( registration_info, config_client ) continue else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator because: {}".format(err))
action.get("reproduction") or 0, action.get("hunting") or 0) def halted(self) -> bool: """ Should return True if the simulator cannot continue""" # If there are no lions, there's no use in continuing. return self.ecosystem._lion_population == 0 if __name__ == "__main__": # Grab standardized way to interact with sim API sim = TemplateSimulatorSession() # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Load json file as simulator integration config type file with open('predator_control_interface.json') as file: interface = json.load(file) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=interface['name'], timeout=interface['timeout'], simulator_context=config_client.simulator_context) registered_session = client.session.create( workspace_name=config_client.workspace, body=registration_info) print("Registered simulator.") sequence_id = 1
def main(cfg: DictConfig): save_path = cfg["model"]["saver"][0]["filename"] if cfg["data"]["full_or_relative"] == "relative": save_path = os.path.join(dir_path, save_path) model_name = cfg["model"]["name"] states = cfg["simulator"]["states"] actions = cfg["simulator"]["actions"] configs = cfg["simulator"]["configs"] policy = cfg["simulator"]["policy"] scale_data = cfg["model"]["build_params"][7]["scale_data"] logger.info(f"Training with a new {policy} policy") Model = available_models[model_name] model = Model() model.build_model(model_type=model_name) model.load_model(filename=save_path, scale_data=scale_data) # Grab standardized way to interact with sim API sim = Simulator(model, states, actions, configs) # do a random action to get initial state sim.episode_start() if policy == "random": test_random_policy(1000, 250, sim) elif policy == "bonsai": env_setup() load_dotenv(verbose=True, override=True) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=env_name, timeout=60, simulator_context=config_client.simulator_context, ) def CreateSession(registration_info: SimulatorInterface, config_client: BonsaiClientConfig): """Creates a new Simulator Session and returns new session, sequenceId """ try: print("config: {}, {}".format(config_client.server, config_client.workspace)) registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info) print("Registered simulator. {}".format( registered_session.session_id)) return registered_session, 1 except HttpResponseError as ex: print( "HttpResponseError in Registering session: StatusCode: {}, Error: {}, Exception: {}" .format(ex.status_code, ex.error.message, ex)) raise ex except Exception as ex: print( "UnExpected error: {}, Most likely, it's some network connectivity issue, make sure you are able to reach bonsai platform from your network." .format(ex)) raise ex registered_session, sequence_id = CreateSession( registration_info, config_client) episode = 0 iteration = 0 try: while True: # Advance by the new state depending on the event type sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted(), ) try: event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print("[{}] Last Event: {}".format( time.strftime("%H:%M:%S"), event.type)) except HttpResponseError as ex: print( "HttpResponseError in Advance: StatusCode: {}, Error: {}, Exception: {}" .format(ex.status_code, ex.error.message, ex)) # This can happen in network connectivity issue, though SDK has retry logic, but even after that request may fail, # if your network has some issue, or sim session at platform is going away.. # So let's re-register sim-session and get a new session and continue iterating. :-) registered_session, sequence_id = CreateSession( registration_info, config_client) continue except Exception as err: print("Unexpected error in Advance: {}".format(err)) # Ideally this shouldn't happen, but for very long-running sims It can happen with various reasons, let's re-register sim & Move on. # If possible try to notify Bonsai team to see, if this is platform issue and can be fixed. registered_session, sequence_id = CreateSession( registration_info, config_client) continue # Event loop if event.type == "Idle": time.sleep(event.idle.callback_time) print("Idling...") elif event.type == "EpisodeStart": print(event.episode_start.config) sim.episode_start(event.episode_start.config) episode += 1 elif event.type == "EpisodeStep": iteration += 1 sim.episode_step(event.episode_step.action) elif event.type == "EpisodeFinish": print("Episode Finishing...") iteration = 0 elif event.type == "Unregister": print( "Simulator Session unregistered by platform because '{}', Registering again!" .format(event.unregister.details)) registered_session, sequence_id = CreateSession( registration_info, config_client) continue else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator because: {}".format(err))
def test_default_client_construction(): config = BonsaiClientConfig() config.access_key = "1" config.workspace = "2" BonsaiClient(config)
def main(render: bool = False, config_setup: bool = False): """Main entrypoint for running simulator connections Parameters ---------- render : bool, optional visualize steps in environment, by default True, by default False """ # workspace environment variables if config_setup: env_setup() load_dotenv(verbose=True, override=True) # Grab standardized way to interact with sim API sim = TemplateSimulatorSession(render=render) # Configure client to interact with Bonsai service config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # # Load json file as simulator integration config type file # with open("interface.json") as file: # interface = json.load(file) # Create simulator session and init sequence id registration_info = SimulatorInterface( name=sim.env_name, timeout=60, simulator_context=config_client.simulator_context, ) registered_session = client.session.create( workspace_name=config_client.workspace, body=registration_info) print("Registered simulator.") sequence_id = 1 try: while True: # Advance by the new state depending on the event type sim_state = SimulatorState( sequence_id=sequence_id, state=sim.get_state(), halted=sim.halted(), ) event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print("[{}] Last Event: {}".format(time.strftime("%H:%M:%S"), event.type)) # Event loop if event.type == "Idle": time.sleep(event.idle.callback_time) print("Idling...") elif event.type == "EpisodeStart": sim.episode_start(event.episode_start.config) elif event.type == "EpisodeStep": sim.episode_step(event.episode_step.action) elif event.type == "EpisodeFinish": print("Episode Finishing...") elif event.type == "Unregister": print( "Simulator Session unregistered by platform because '{}', Registering again!" .format(event.unregister.details)) client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name=config_client.workspace, session_id=registered_session.session_id, ) print("Unregistered simulator because: {}".format(err))
def run(self): """ Connects to the Bonsai service processes the command and passes them to the simulator """ config_client = BonsaiClientConfig() client = BonsaiClient(config_client) # Load json file as simulator integration config type file interface = self.get_interface() simulator_interface = SimulatorInterface( name = interface['name'], timeout = interface['timeout'], simulator_context = config_client.simulator_context, ) # Registers a simulator with Bonsai platform session = client.session.create( workspace_name = config_client.workspace, body = simulator_interface ) log.info("Registered simulator.") sequence_id = 1 try: while True: # Advance by the new state depending on the event type simulator_state = SimulatorState( sequence_id =sequence_id, state = self.get_state(), halted = self.halted() ) event = client.session.advance( workspace_name = config_client.workspace, session_id = session.session_id, body = simulator_state ) sequence_id = event.sequence_id log.debug("[{}] Last Event: {}".format(time.strftime('%H:%M:%S'), event.type)) # Event loop if event.type == 'Idle': time.sleep(event.idle.callback_time) log.info('Idling...') elif event.type == 'EpisodeStart': self.episode_start(event.episode_start.config) elif event.type == 'EpisodeStep': self.episode_step(event.episode_step.action) elif event.type == 'EpisodeFinish': self.episode_finish("") elif event.type == 'Unregister': client.session.delete( workspace_name = config_client.workspace, session_id = session.session_id ) log.info("Unregistered simulator.") else: pass except KeyboardInterrupt: # Gracefully unregister with keyboard interrupt client.session.delete( workspace_name =config_client.workspace, session_id = session.session_id ) log.info("Unregistered simulator.") except Exception as err: # Gracefully unregister for any other exceptions client.session.delete( workspace_name = config_client.workspace, session_id = session.session_id ) log.info("Unregistered simulator because: {}".format(err))
def main(): """ Creates a Bonsai simulator session and executes Bonsai episodes. """ workspace = os.getenv("SIM_WORKSPACE") accesskey = os.getenv("SIM_ACCESS_KEY") config_client = BonsaiClientConfig() client = BonsaiClient(config_client) registration_info = SimulatorInterface( name="SAMPLE_NAME-sim", timeout=60, simulator_context=config_client.simulator_context, description=None, ) print(f"config: {config_client.server}, {config_client.workspace}") registered_session: SimulatorSessionResponse = client.session.create( workspace_name=config_client.workspace, body=registration_info) print(f"Registered simulator. {registered_session.session_id}") sequence_id = 1 sim_model = SimulatorModel() sim_model_state = {'sim_halted': False} try: while True: sim_state = SimulatorState(sequence_id=sequence_id, state=sim_model_state, halted=sim_model_state.get( 'sim_halted', False)) event = client.session.advance( workspace_name=config_client.workspace, session_id=registered_session.session_id, body=sim_state, ) sequence_id = event.sequence_id print(f'[{time.strftime("%H:%M:%S")}] Last Event: {event.type}') if event.type == "Idle": time.sleep(event.idle.callback_time) elif event.type == "EpisodeStart": print(f"config {event.episode_start.config}") sim_model_state = sim_model.reset(event.episode_start.config) print(f"state {sim_model_state}") elif event.type == "EpisodeStep": print(f"action {event.episode_step.action}") sim_model_state = sim_model.step(event.episode_step.action) print(f"state {sim_model_state}") elif event.type == "EpisodeFinish": sim_model_state = {'sim_halted': False} elif event.type == "Unregister": print( f"Simulator Session unregistered by platform because '{event.unregister.details}'" ) return except BaseException as err: client.session.delete(workspace_name=config_client.workspace, session_id=registered_session.session_id) print(f"Unregistered simulator because {type(err).__name__}: {err}")