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 the evaluations npz file and return the rewards. |
|
Parse the monitor csv file and return the timesteps and rewards. |
|
Calculate the mean of the top percent of the data, optionally excluding |
|
Calculate the fitness of the agent based on evaluation results. The fitness is |
|
Calculate the fitness of the agent based on monitor data. The fitness is determined |
|
Calculate the fitness of the agent. Uses the 3rd quartile of the cumulative |
|
This fitness function will return higher rewards generally for agents with more |
|
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.