cambrian.utils¶
This module defines the Cambrian utilities.
Submodules¶
Classes¶
Helper class which stores information about a Mujoco actuator. |
|
Helper class which stores information about a Mujoco joint. |
|
Helper class which stores information about a Mujoco geometry |
Functions¶
|
Evaluate a policy. |
|
Save the parsed data to a pickle file. |
|
Try to load the data from the pickle file. |
|
Generate a sequence of numbers from a range. If num is 1, the average of the |
|
Temporarily set attributes of an object. |
|
Make a number odd by adding 1 if it is even. If num is a float, it is cast to |
Round a number to the nearest integer, rounding half up. |
|
|
Safely get the index of a value in a list. If the value is not in the list, None |
|
Return a reduce tuple: (unpickle_callable, (args,)) |
|
This will be called automatically by Python when unpickling. |
|
Check if the agent is selected. |
Package Contents¶
- evaluate_policy(env, model, num_runs, *, record_kwargs=None, step_callback=lambda _: ..., done_callback=lambda _: ...)[source]¶
Evaluate a policy.
- Parameters:
env (gym.Env) – The environment to evaluate the policy on. Assumed to be a VecEnv wrapper around a MjCambrianEnv.
model (MjCambrianModel) – The model to evaluate.
num_runs (int) – The number of runs to evaluate the policy on.
- Keyword Arguments:
record_kwargs (Dict[str, Any]) – The keyword arguments to pass to the save method of the environment. If None, the environment will not be recorded.
step_callback (Callable[[], bool]) – The callback function to call at each step. If the function returns False, the evaluation will stop.
done_callback (Callable[[int], bool]) – The callback function to call when a run is done. If the function returns False, the evaluation will stop.
- Returns:
float – The cumulative reward of the evaluation.
- generate_sequence_from_range(range, num, endpoint=True)[source]¶
Generate a sequence of numbers from a range. If num is 1, the average of the range is returned. Otherwise, a sequence of numbers is generated using np.linspace.
- Parameters:
range (Tuple[float, float]) – The range of the sequence.
num (int) – The number of elements in the sequence.
- Keyword Arguments:
endpoint (bool) – Whether to include the endpoint in the sequence.
- make_odd(num)[source]¶
Make a number odd by adding 1 if it is even. If num is a float, it is cast to an int.
- safe_index(arr, value, *, default=None)[source]¶
Safely get the index of a value in a list. If the value is not in the list, None is returned.
- class MjCambrianActuator[source]¶
Helper class which stores information about a Mujoco actuator.
- Variables:
adr (int) – The Mujoco actuator ID (index into model.actuator_* arrays).
trnadr (int) – The index of the actuator’s transmission in the model.
ctrlrange (Tuple[float, float]) – The control range of the actuator.
ctrllimited (bool) – Whether the actuator is control-limited.
- class MjCambrianJoint[source]¶
Helper class which stores information about a Mujoco joint.
- Variables:
type (int) – The Mujoco joint type (mj.mjtJoint).
adr (int) – The Mujoco joint ID (index into model.jnt_* arrays).
qposadr (int) – The index of the joint’s position in the qpos array.
numqpos (int) – The number of positions in the joint.
qveladr (int) – The index of the joint’s velocity in the qvel array.
numqvel (int) – The number of velocities in the joint.
- property qposadrs: List[int]¶
Get the indices of the joint’s positions in the qpos array.
- property qveladrs: List[int]¶
Get the indices of the joint’s velocities in the qvel array.
- class MjCambrianGeometry[source]¶
Helper class which stores information about a Mujoco geometry
- Variables:
id (int) – The Mujoco geometry ID (index into model.geom_* arrays).
rbound (float) – The radius of the geometry’s bounding sphere.
pos (np.ndarray) – The position of the geometry relative to the body.