Metrics

Every simulation run reports three groups of metrics for each environment and planner pair: standard ones computed for every environment (return, CVaR, timings), environment ones (task completion, collisions), and planner ones (tree depth, visit counts). Each comes as a value with a confidence interval. When two planners’ intervals do not overlap, the gap between them is larger than the spread from which episodes happened to be drawn.

Environment metrics are declared, not hand-computed. The environment reports raw numbers each step through step_info, and lists in get_metric_specs how each number becomes a metric. The shared code then does the per-episode reduction, the averaging and the confidence interval.

Why per-step channels

Episodes run in worker processes, and metrics are computed later in the parent process on a different copy of the environment. A count kept on self during the episode never reaches the parent. step_info returns a {name: float} dict that the simulator stores on each StepData as info, so the values travel back with the episode.

step_info is also called once more at the end of a terminated episode, with action and next_state set to None. Implementations must accept that. Base Environment Class documents both methods on the Environment class.

Example

The Tiger environment declares two metrics:

from POMDPPlanners.environments.tiger_pomdp import TigerPOMDP

env = TigerPOMDP(discount_factor=0.95)
print(env.get_metric_names())  # ['task_completion_rate', 'average_listens']
print(env.step_info(state="tiger_left", action="open_right", next_state="tiger_left"))
# {'correct_door_opened': 1.0, 'listened': 0.0}

This is how two specs turn per-step channels into metrics. Here the per-step values are written by hand; in a run they come from step_info:

from POMDPPlanners.core.simulation.step_info_metrics import (
    EpisodeReduction,
    StepInfoMetric,
    aggregate_step_info_metrics,
)

specs = [
    # Did the episode ever reach the goal? Averaged over episodes, a rate.
    StepInfoMetric(name="task_completion_rate", channel="at_goal",
                   per_episode=EpisodeReduction.ANY),
    # How many collisions per episode, averaged over episodes.
    StepInfoMetric(name="average_collisions", channel="collided",
                   per_episode=EpisodeReduction.SUM),
]

# What step_info returned on each step of three short episodes.
episodes = [
    [{"at_goal": 0.0, "collided": 1.0}, {"at_goal": 1.0, "collided": 0.0}],
    [{"at_goal": 0.0, "collided": 0.0}, {"at_goal": 0.0, "collided": 1.0}],
    [{"at_goal": 1.0, "collided": 0.0}],
]
for metric in aggregate_step_info_metrics(episodes, specs):
    print(metric.name, metric.value, metric.lower_confidence_bound,
          metric.upper_confidence_bound)

With three episodes the 95% interval is wide, about [-0.77, 2.10] around 0.67. The t-interval is that wide because it rests on three episodes; it narrows as the episode count grows.

Use the names in CommonMetricName for concepts that every environment shares, such as task completion, so one idea is not reported under several spellings.

Metric specs

class POMDPPlanners.core.simulation.step_info_metrics.StepInfoMetric(name, channel, per_episode, scale=1.0, default=None)[source]

Declares one metric derived from one per-step channel.

Parameters:
name

The metric name, as it will appear in results and tuning configs. Name it after what is measured, including the unit where the quantity is physical, so two environments cannot silently report different quantities under one name.

channel

The step_info key to read.

per_episode

How the channel’s values collapse within one episode.

scale

Multiplied into the per-episode value before averaging, for unit conversion. Defaults to 1.0.

default

Value contributed by a step that reported no such channel. None (the default) means such steps are skipped entirely. Use 0.0 only when a missing channel genuinely means zero — treating “not measured” as “did not happen” silently biases the metric.

Note

An episode that reported the channel on no step at all contributes nothing: it is dropped from the average rather than filled with a stand-in. What an unmeasured episode “should” have measured is not a property of the metric, so there is no per-spec knob for it. An episode that ran but was never measured is a different matter, and is rejected upstream by require_measured_episodes() rather than scored as zero.

class POMDPPlanners.core.simulation.step_info_metrics.EpisodeReduction(*values)[source]

How a channel’s per-step values collapse to one number per episode.

ANY

1.0 if any step reported a non-zero value, else 0.0. Across episodes this yields a rate — the natural shape for “did it ever happen”, e.g. reaching a goal.

ALL

1.0 only if every step reported a non-zero value. The correct shape for a “never failed” predicate such as a legged robot staying upright: under ANY a robot that falls at the last step would still count as a success because earlier steps were fine.

MAX

The largest value seen. The natural shape for severity, where the worst moment characterizes the episode rather than the average.

SUM

The total over the episode, for counting occurrences.

MEAN

The average over the episode’s steps.

LAST

The final reported value, for a terminal condition.

POMDPPlanners.core.simulation.step_info_metrics.aggregate_step_info_metrics(episodes, specs)[source]

Reduce per-step channels into metrics with confidence intervals.

Each spec is applied independently: its channel is reduced within every episode that reported it, and those per-episode numbers are averaged across episodes with a 95% t-interval.

Parameters:
Return type:

List[MetricValue]

Returns:

One MetricValue per spec whose channel was reported by at least one episode, in spec order. A spec whose channel never appears is omitted rather than reported as zero, so a measurement that was never taken is never mistaken for a measurement of zero. An individual episode that reported the channel on no step is dropped from that spec’s average for the same reason.

Example

Task completion rate over three episodes:

from POMDPPlanners.core.simulation.step_info_metrics import (
    EpisodeReduction,
    StepInfoMetric,
    aggregate_step_info_metrics,
)

episodes = [
    [{"success": 0.0}, {"success": 1.0}],
    [{"success": 0.0}],
    [{"success": 0.0}, {"success": 0.0}],
]
spec = StepInfoMetric(
    name="task_completion_rate",
    channel="success",
    per_episode=EpisodeReduction.ANY,
)
metrics = aggregate_step_info_metrics(episodes, [spec])
round(metrics[0].value, 4)  # 0.3333
class POMDPPlanners.core.simulation.metrics.MetricValue(name, value, lower_confidence_bound, upper_confidence_bound)[source]
Parameters:

Built-in metric names

The standard metrics are computed for every pair. return_cvar is skipped when the environment has no reward_range, because its confidence interval needs bounds on the return.

class POMDPPlanners.simulations.simulation_statistics.StandardMetrics(*values)[source]

Standard metric names computed for all environment-policy pairs.

AVERAGE_ACTION_TIME = 'average_action_time'
AVERAGE_ACTUAL_NUM_STEPS = 'average_actual_num_steps'
AVERAGE_BELIEF_UPDATE_TIME = 'average_belief_update_time'
AVERAGE_OBSERVATION_TIME = 'average_observation_time'
AVERAGE_REACH_TERMINAL_STATE = 'average_reach_terminal_state'
AVERAGE_RETURN = 'average_return'
AVERAGE_REWARD_TIME = 'average_reward_time'
AVERAGE_STATE_SAMPLING_TIME = 'average_state_sampling_time'
RETURN_CVAR = 'return_cvar'
RETURN_VALUE_AT_RISK = 'return_value_at_risk'
class POMDPPlanners.core.simulation.metrics.CommonMetricName(*values)[source]

Metric names that mean the same thing in every environment.

Environments take these names from here instead of writing the string, so one concept cannot drift into several spellings again. It had: task completion alone was success_rate, goal_reaching_rate, win_rate and exit_success_rate depending on the environment. An environment’s own metrics enum points its member at the shared value, e.g. TASK_COMPLETION_RATE = CommonMetricName.TASK_COMPLETION_RATE.value.

TASK_COMPLETION_RATE

Fraction of episodes in which the task was completed.

ENDED_BY_GOAL_RATE

Fraction of episodes that ended because the task was completed.

ENDED_BY_FAILURE_RATE

Fraction of episodes that ended in a failure.

ENDED_BY_TIMEOUT_RATE

Fraction of episodes that ran out of steps.

AVERAGE_EPISODE_LENGTH

Steps per episode, averaged over episodes.

COLLISION_RATE

Fraction of episodes with a collision.

AVERAGE_COLLISIONS

Collisions per episode, averaged over episodes.

AVERAGE_DANGEROUS_AREA_STEPS

Steps spent in a dangerous area per episode, averaged over episodes.

AVERAGE_DANGEROUS_ENCOUNTERS

Dangerous events of every kind per episode, averaged over episodes.

AVERAGE_NEAR_MISSES

Near-miss events per episode, averaged over episodes.

AVERAGE_SPEED_MPS

Mean speed within an episode in m/s, averaged over episodes.

Planner metrics

A planner returns a PolicyRunData with every action. Each of its info variables is averaged within an episode, then across episodes, and appears in the results as policy_info_<name>.

Both classes are documented on Base Planner Class, next to the planner interface that returns them.

Listing the metrics of a pair

Use these to find the exact names to optimize in a tuning study.

POMDPPlanners.simulations.simulation_statistics.get_metric_names_from_environment_policy_pair(environment, policy_class)[source]

Get all metric names returned by compute_statistics_environment_policy_pair.

This function returns the complete list of metric names that will be computed for a given environment-policy pair, matching the structure of metrics returned by compute_statistics_environment_policy_pair.

The metrics are returned in the same order as compute_statistics_environment_policy_pair: 1. Environment-specific metrics - Custom metrics from the environment 2. Policy-specific metrics - Info variables tracked by the policy (prefixed with "policy_info_") 3. Standard metrics - Always computed for all environment-policy pairs

Parameters:
  • environment (Environment) – Environment instance to get custom metrics from

  • policy_class (Type[Policy]) – Policy class (not instance) to get info variable names from

Return type:

List[str]

Returns:

Complete list of all metric names in the order they appear in compute_statistics_environment_policy_pair results

Example

Get metric names for TigerPOMDP with POMCP:

>>> from POMDPPlanners.environments.tiger_pomdp import TigerPOMDP
>>> from POMDPPlanners.planners.mcts_planners.pomcp import POMCP
>>> from POMDPPlanners.simulations.simulation_statistics import get_metric_names_from_environment_policy_pair
>>>
>>> env = TigerPOMDP(discount_factor=0.95)
>>> metric_names = get_metric_names_from_environment_policy_pair(env, POMCP)
>>>
>>> # Check for environment-specific metrics (first in order)
>>> "task_completion_rate" in metric_names
True
>>>
>>> # Check for policy-specific metrics (second, with prefix)
>>> "policy_info_min_actions_visit_count" in metric_names
True
>>>
>>> # Check for standard metrics (last in order)
>>> "average_return" in metric_names
True
>>> "return_cvar" in metric_names
True
POMDPPlanners.simulations.simulation_statistics.get_available_optimization_metrics(environment, policy_class)[source]

Get all metric names available for hyperparameter optimization.

This function aggregates metric names from three sources: 1. Standard metrics - Always computed for all environment-policy pairs 2. Environment-specific metrics - Custom metrics from the environment 3. Policy-specific metrics - Info variables tracked by the policy

Parameters:
  • environment (Environment) – Environment instance to get custom metrics from

  • policy_class (Type[Policy]) – Policy class (not instance) to get info variable names from

Return type:

List[str]

Returns:

Complete list of all metric names available for this environment-policy combination

Note

This function returns metrics in a different order than compute_statistics_environment_policy_pair. Use get_metric_names_from_environment_policy_pair if you need the exact order from compute_statistics_environment_policy_pair.

Example

Get available metrics for TigerPOMDP with POMCP:

>>> from POMDPPlanners.environments.tiger_pomdp import TigerPOMDP
>>> from POMDPPlanners.planners.mcts_planners.pomcp import POMCP
>>> from POMDPPlanners.simulations.simulation_statistics import get_available_optimization_metrics
>>>
>>> env = TigerPOMDP(discount_factor=0.95)
>>> available_metrics = get_available_optimization_metrics(env, POMCP)
>>>
>>> # Check for standard metrics
>>> "average_return" in available_metrics
True
>>> "return_cvar" in available_metrics
True
>>>
>>> # Check for environment-specific metrics
>>> "task_completion_rate" in available_metrics
True
>>>
>>> # Check for policy-specific metrics (with prefix)
>>> "policy_info_min_actions_visit_count" in available_metrics
True