cambrian.ml.fitness_fns

Fitness functions for evaluating the fitness of agents. These functions are used by the optimizers to evaluate the fitness of the agents. The fitness functions are responsible for loading the evaluations and monitor files and calculating the fitness of the agent based on the evaluations.

Functions

parse_evaluations_npz(evaluations_npz)

Parse the evaluations npz file and return the rewards.

parse_monitor_csv(monitor_csv)

Parse the monitor csv file and return the timesteps and rewards.

top_n_percent(data, percent, use_outliers[, max_zscore])

Calculate the mean of the top percent of the data, optionally excluding

fitness_from_evaluations(config, evaluations_npz, *[, ...])

Calculate the fitness of the agent based on evaluation results. The fitness is

fitness_from_monitor(config, monitor_csv, *[, ...])

Calculate the fitness of the agent based on monitor data. The fitness is determined

fitness_from_txt(config, txt_file)

Calculate the fitness of the agent. Uses the 3rd quartile of the cumulative

fitness_num_eyes(config, *, pattern[, mean, std, ...])

This fitness function will return higher rewards generally for agents with more

fitness_num_eyes_and_fov(config, *, num_eyes_pattern, ...)

This fitness function will return higher rewards generally for agents with more

Module Contents

parse_evaluations_npz(evaluations_npz)[source]

Parse the evaluations npz file and return the rewards.

parse_monitor_csv(monitor_csv)[source]

Parse the monitor csv file and return the timesteps and rewards.

top_n_percent(data, percent, use_outliers, max_zscore=3.0)[source]

Calculate the mean of the top percent of the data, optionally excluding outliers.

fitness_from_evaluations(config, evaluations_npz, *, return_data=False, use_outliers=False, percent=25.0, quartiles=(25, 75))[source]

Calculate the fitness of the agent based on evaluation results. The fitness is determined by taking the mean of the top percent of the evaluation rewards, optionally excluding outliers.

Parameters:
  • config (MjCambrianConfig) – Configuration for the evaluation.

  • evaluations_npz (Path) – Path to the .npz file containing evaluation data.

Keyword Arguments:
  • return_data (bool) – If True, returns the evaluation data along with the fitness.

  • use_outliers (bool) – If True, includes outliers in the fitness calculation. Defaults to False.

  • percent (float) – Defines the top ‘n’ percent of rewards to be used for calculating fitness. Defaults to 75.0.

Returns:

float | Tuple[float, np.ndarray] – The calculated fitness value. If return_data is True, returns a tuple containing the fitness value and the full evaluation data as a numpy array.

Notes

  • The rewards array should be a 2D array where each row represents an evaluation run and each column represents the rewards for each evaluation step.

  • If the evaluations file does not exist, returns negative infinity as the fitness value.

fitness_from_monitor(config, monitor_csv, *, return_data=False, percent=25.0, n_episodes=1)[source]

Calculate the fitness of the agent based on monitor data. The fitness is determined by taking the specified percent of the cumulative monitor rewards.

Parameters:
  • config (MjCambrianConfig) – Configuration for the evaluation.

  • monitor_csv (Path) – Path to the CSV file containing monitor data.

Keyword Arguments:
  • return_data (bool) – If True, returns the monitor data along with the fitness. Defaults to False.

  • percent (float) – Defines the top ‘n’ percent of rewards to be used for calculating fitness. Defaults to 75.0 (3rd quartile).

  • n_episodes (int) – The number of episodes to use sum rewards for. Defaults to 1. n_episodes must be a common factor of the number of episodes in the monitor data.

Returns:

float | Tuple[float, Tuple[np.ndarray, np.ndarray]] – The calculated fitness value. If return_data is True, returns a tuple containing the fitness value and the monitor data (timesteps, rewards) as numpy arrays.

Notes

  • If the rewards are empty, returns negative infinity as the fitness value.

  • The monitor data should contain timesteps and corresponding rewards.

fitness_from_txt(config, txt_file)[source]

Calculate the fitness of the agent. Uses the 3rd quartile of the cumulative monitor rewards.

fitness_num_eyes(config, *, pattern, mean=0, std=5, assume_one=True)[source]

This fitness function will return higher rewards generally for agents with more eyes.

Parameters:

pattern (str) – The path to the number of eyes in the config.

fitness_num_eyes_and_fov(config, *, num_eyes_pattern, fov_pattern, mean=0, std=5, assume_one=True, optimal_fov=45)[source]

This fitness function will return higher rewards generally for agents with more eyes and a fov closer to the optimal_fov.