Agent

class gym_collision_avoidance.envs.agent.Agent(start_x, start_y, goal_x, goal_y, radius, pref_speed, initial_heading, policy, dynamics_model, sensors, id)

A disc-shaped object that has a policy, dynamics, sensors, and can move through the environment

Parameters:
  • start_x – (float or int) x position of agent in global frame at start of episode
  • start_y – (float or int) y position of agent in global frame at start of episode
  • goal_x – (float or int) desired x position of agent in global frame by end of episode
  • goal_y – (float or int) desired y position of agent in global frame by end of episode
  • radius – (float or int) radius of circle describing disc-shaped agent’s boundaries in meters
  • pref_speed – (float or int) maximum speed of agent in m/s
  • initial_heading – (float) angle of agent in global frame at start of episode
  • policy – (Policy) computes agent’s action from its state
  • dynamics_model – (Dynamics) computes agent’s new state from its state and action
  • sensors – (list) of Sensor measures the environment for use by the policy
  • id – (int) not sure how much it’s used, but uniquely identifies each agent
  • action_dim – (int) number of actions on each timestep (e.g., 2 because of speed, heading cmds)
  • num_actions_to_store – (int) number of past action vectors to remember (I think just used by CADRL to compute turning_dir?)
  • near_goal_threshold – (float) once within this distance to goal, say that agent has reached goal
  • dt_nominal – (float) time in seconds of each simulation step
ego_pos_to_global_pos(ego_pos)

Convert a position in the ego frame to the global frame.

This might be useful for plotting some of the perturbation stuff.

Parameters:ego_pos (np array) – if (2,), it represents one (x,y) position in ego frame if (n,2), it represents n (x,y) positions in ego frame
Returns:either (2,) (x,y) position in global frame or (n,2) n (x,y) positions in global frame
Return type:global_pos (np array)
get_agent_data(attribute)

Grab the value of self.attribute (useful to define which states sensor uses from config file).

Parameters:attribute (str) – which attribute of this agent to look up (e.g., “pos_global_frame”)
get_agent_data_equiv(attribute, value)

Grab the value of self.attribute and return whether it’s equal to value (useful to define states sensor uses from config file).

Parameters:
  • attribute (str) – which attribute of this agent to look up (e.g., “radius”)
  • value (anything) – thing to compare self.attribute to (e.g., 0.23)
Returns:

result of self.attribute and value comparison (bool)

get_ref()

Using current and goal position of agent in global frame, compute coordinate axes of ego frame.

Ego frame is defined as: origin at center of agent, x-axis pointing from agent’s center to agent’s goal (right-hand rule, z axis upward). This is a useful representation for goal-conditioned tasks, since many configurations of agent-pos-and-goal in the global frame map to the same ego setup.

Returns: 2-element tuple containing

  • ref_prll (np array): (2,) with vector corresponding to ego-x-axis (pointing from agent_position->goal)
  • ref_orth (np array): (2,) with vector corresponding to ego-y-axis (orthogonal to ref_prll)
get_sensor_data(sensor_name)

Extract the latest measurement from the sensor by looking up in the self.sensor_data dict (which is populated by the self.sense method.

Parameters:sensor_name (str) – name of the sensor (e.g., ‘laserscan’, I think from Sensor.str?)
global_pos_to_ego_pos(global_pos)

Convert a position in the global frame to the ego frame.

Parameters:global_pos (np array) – one (x,y) position in global frame
Returns:(2,) (x,y) position in ego frame
Return type:ego_pos (np array)
print_agent_info()

Print out a summary of the agent’s current state.

reset(px=None, py=None, gx=None, gy=None, pref_speed=None, radius=None, heading=None)

Reset an agent with different states/goal, delete history and reset timer (but keep its dynamics, policy, sensors)

Parameters:
  • px – (float or int) x position of agent in global frame at start of episode
  • py – (float or int) y position of agent in global frame at start of episode
  • gx – (float or int) desired x position of agent in global frame by end of episode
  • gy – (float or int) desired y position of agent in global frame by end of episode
  • pref_speed – (float or int) maximum speed of agent in m/s
  • radius – (float or int) radius of circle describing disc-shaped agent’s boundaries in meters
  • heading – (float) angle of agent in global frame at start of episode
sense(agents, agent_index, top_down_map)

Call the sense method of each Sensor in self.sensors, store in self.sensor_data dict keyed by sensor.name.

Parameters:
  • agents (list) – all Agent in the environment
  • agent_index (int) – index of this agent (the one with this sensor) in agents
  • top_down_map (2D np array) – binary image with 0 if that pixel is free space, 1 if occupied
set_state(px, py, vx=None, vy=None, heading=None)

Without doing a full reset, update the agent’s current state (pos, vel, heading).

This is useful in conjunction with (ExternalDynamics). For instance, if Agents in this environment should be aware of a real robot or one from a more realistic simulator (e.g., Gazebo), you could just call set_state each sim step based on what the robot/Gazebo tells you.

If vx, vy not provided, will interpolate based on new&old position. Same for heading.

Parameters:
  • px (float or int) – x position of agent in global frame right now
  • py (float or int) – y position of agent in global frame right now
  • vx (float or int) – x velocity of agent in global frame right now
  • vy (float or int) – y velocity of agent in global frame right now
  • heading (float) – angle of agent in global frame right now
take_action(action, dt)

If not yet done, take action for dt seconds, check if done.

Parameters:
  • action (list) – nominally a [delta heading angle, speed] command for this agent (but probably could be anything that the dynamics_model.step can handle)
  • dt (float) – time in seconds to execute action
to_vector()

Convert the agent’s attributes to a single global state vector.