POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils package
Submodules
POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.numba_kernels module
PacMan-specific Numba-JIT kernels.
Single-pass batched reward kernel that mirrors the numpy implementation in
PacManRewardModel.compute_reward_batch but fuses the
terminal / collision / pellet / win / dangerous-area work into one loop
over rows. The numpy path issues ~15-20 separate operator calls per
invocation; folding them into one @njit kernel eliminates the
per-call dispatch overhead and the temporary allocations.
Conventions
All array inputs are contiguous
float64/int32/int64/uint8as declared on the kernel signature.next_statesis always passed as a real array. When the caller has no realised next-state batch, pass a zero-row sentinel (np.empty((0, state_dim), dtype=np.float64)) and sethas_next_statestoFalse; the kernel skips the collision contribution in that case.No Python objects, no RNG: reward is deterministic given inputs.
- POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.numba_kernels.compute_reward_batch_kernel(states, action, next_states, has_next_states, neighbor_table, pellet_positions, dangerous_areas, num_ghosts, step_penalty, ghost_collision_penalty, pellet_reward, win_reward, dangerous_area_penalty, dangerous_area_radius, idx_pac_row, idx_pac_col, idx_ghosts_start, idx_pellets_start, idx_terminal)[source]
Fused per-row reward kernel.
Replicates the numpy implementation exactly: terminal -> 0.0, otherwise step_penalty + (optional) collision + dangerous-area + pellet + win-bonus contributions. The pellet-mask iteration bound is derived from
pellet_positions.shape[0]; no separateidx_pellets_endparameter is needed.- Return type:
- Parameters:
states (ndarray)
action (int)
next_states (ndarray)
has_next_states (bool)
neighbor_table (ndarray)
pellet_positions (ndarray)
dangerous_areas (ndarray)
num_ghosts (int)
step_penalty (float)
ghost_collision_penalty (float)
pellet_reward (float)
win_reward (float)
dangerous_area_penalty (float)
dangerous_area_radius (float)
idx_pac_row (int)
idx_pac_col (int)
idx_ghosts_start (int)
idx_pellets_start (int)
idx_terminal (int)
- POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.numba_kernels.compute_reward_scalar_kernel(state, next_state, dangerous_areas, num_ghosts, n_pellets, step_penalty, ghost_collision_penalty, pellet_reward, win_reward, dangerous_area_penalty, dangerous_area_radius, idx_pac_row, idx_pac_col, idx_ghosts_start, idx_pellets_start, idx_score, idx_terminal)[source]
Single-row scalar reward kernel mirroring the Python implementation.
Reads the realised pacman / ghost positions directly from
next_state(matching the Python scalar — pacman moves deterministically, so the neighbor-table lookup the batch kernel uses is redundant here). Pellet pickup is detected via thenext_state.score > state.scoreinvariant the transition kernel upholds; win-bonus fires only whennext_stateis terminal with no remaining pellets.- Return type:
- Parameters:
state (ndarray)
next_state (ndarray)
dangerous_areas (ndarray)
num_ghosts (int)
n_pellets (int)
step_penalty (float)
ghost_collision_penalty (float)
pellet_reward (float)
win_reward (float)
dangerous_area_penalty (float)
dangerous_area_radius (float)
idx_pac_row (int)
idx_pac_col (int)
idx_ghosts_start (int)
idx_pellets_start (int)
idx_score (int)
idx_terminal (int)
POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.pacman_reward_models module
Reward models for the PacMan POMDP.
Mirrors the abstract-base / concrete-subclass layout used by
POMDPPlanners.environments.light_dark_pomdp.light_dark_pomdp_utils.light_dark_reward_models
and
POMDPPlanners.environments.laser_tag_pomdp.laser_tag_pomdp_utils.laser_tag_reward_models,
so further PacMan reward variants can be added without growing the env class.
Three concrete variants are provided. They share all of the non-dangerous-
area scoring (step / collision / pellet / win) via the
PacManRewardModel base; each variant only customises the dangerous-
area contribution:
PacManRewardModel(CONSTANT_HAZARD_PENALTY): deterministic-dangerous_area_penaltywhenever the realised next pacman position lies inside any configured circular hazard zone (squared-distance check againstdangerous_area_radius).PacManZeroMeanHazardShockRewardModel(ZERO_MEAN_HAZARD_SHOCK):±dangerous_area_penalty50/50 in-zone — zero expected contribution, high variance.PacManDistanceDecayedHazardPenaltyRewardModel(DISTANCE_DECAYED_HAZARD_PENALTY):-dangerous_area_penaltyis applied with probabilityexp(-min_dist / penalty_decay)based on the closest zone centre — no radius cutoff.
The reward model owns all the parameters and pre-built buffers that the
reward computation needs (state-layout indices, pellet positions,
dangerous areas, scalar penalties / bonuses, and a callable that returns
the env’s lazily-built neighbor table). The environment retains its own
copies of these values for the transition / observation paths and
delegates reward() / reward_batch() to the model.
- class POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.pacman_reward_models.BasePacManRewardModel[source]
Bases:
ABCAbstract reward model for PacMan POMDP variants.
- class POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.pacman_reward_models.PacManDistanceDecayedHazardPenaltyRewardModel(*, num_ghosts, pellet_positions_arr, dangerous_areas, dangerous_areas_arr, dangerous_area_radius, dangerous_area_penalty, step_penalty, ghost_collision_penalty, pellet_reward, win_reward, idx_pac_row, idx_pac_col, idx_ghosts_start, idx_pellets_start, idx_pellets_end, idx_score, idx_terminal, neighbor_table_getter, penalty_decay)[source]
Bases:
PacManRewardModelDISTANCE_DECAYED_HAZARD_PENALTY variant.
Penalty is applied with probability
exp(-min_dist / penalty_decay)wheremin_distis the Euclidean distance from the realised next pacman position to the closest dangerous-area centre. No radius cutoff — every step risks some (vanishingly small at large distance) penalty. Each call draws one uniform regardless of distance, matching the existing decaying-prob kernel and the light-dark / laser-tag analogous reward models.dangerous_area_radiusis ignored in this model.- Parameters:
num_ghosts (int)
pellet_positions_arr (ndarray)
dangerous_areas_arr (ndarray)
dangerous_area_radius (float)
dangerous_area_penalty (float)
step_penalty (float)
ghost_collision_penalty (float)
pellet_reward (float)
win_reward (float)
idx_pac_row (int)
idx_pac_col (int)
idx_ghosts_start (int)
idx_pellets_start (int)
idx_pellets_end (int)
idx_score (int)
idx_terminal (int)
penalty_decay (float)
- class POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.pacman_reward_models.PacManRewardModel(*, num_ghosts, pellet_positions_arr, dangerous_areas, dangerous_areas_arr, dangerous_area_radius, dangerous_area_penalty, step_penalty, ghost_collision_penalty, pellet_reward, win_reward, idx_pac_row, idx_pac_col, idx_ghosts_start, idx_pellets_start, idx_pellets_end, idx_score, idx_terminal, neighbor_table_getter)[source]
Bases:
BasePacManRewardModelStandard PacMan reward model.
- Reward structure:
Per-step:
step_penaltybaseline applied to every non-terminal transition.Pellet collection:
+pellet_rewardwhennext_staterecords a score increase versusstate(scalar path) or when the realised next position lands on an active pellet cell (batch path).Ghost collision:
+ghost_collision_penaltywhen any ghost innext_stateoccupies PacMan’s realised next cell.Dangerous area:
-dangerous_area_penaltywhen PacMan’s realised next position lies inside any configured circular hazard zone (squared-distance check againstdangerous_area_radius).Win bonus:
+win_rewardwhen the transition consumes the last remaining pellet (terminal with empty pellet mask in the scalar path; final-pellet pickup in the batch path).
Terminal states return
0.0reward.Subclasses override
_dangerous_area_contribution_scalar()and_apply_dangerous_area_contribution_batch()to express different stochastic penalty models; the rest of the scoring is identical.- Parameters:
num_ghosts (int)
pellet_positions_arr (ndarray)
dangerous_areas_arr (ndarray)
dangerous_area_radius (float)
dangerous_area_penalty (float)
step_penalty (float)
ghost_collision_penalty (float)
pellet_reward (float)
win_reward (float)
idx_pac_row (int)
idx_pac_col (int)
idx_ghosts_start (int)
idx_pellets_start (int)
idx_pellets_end (int)
idx_score (int)
idx_terminal (int)
- class POMDPPlanners.environments.pacman_pomdp.pacman_pomdp_utils.pacman_reward_models.PacManZeroMeanHazardShockRewardModel(*, num_ghosts, pellet_positions_arr, dangerous_areas, dangerous_areas_arr, dangerous_area_radius, dangerous_area_penalty, step_penalty, ghost_collision_penalty, pellet_reward, win_reward, idx_pac_row, idx_pac_col, idx_ghosts_start, idx_pellets_start, idx_pellets_end, idx_score, idx_terminal, neighbor_table_getter)[source]
Bases:
PacManRewardModelZERO_MEAN_HAZARD_SHOCK variant.
Replaces the deterministic in-zone penalty with a 50/50 split between
+dangerous_area_penaltyand-dangerous_area_penaltywhenever the realised next pacman position is in any dangerous area. Expected contribution is0; variance isdangerous_area_penalty**2. MirrorsContinuousLDZeroMeanHazardShockRewardModelandLaserTagZeroMeanHazardShockRewardModel. All non-dangerous-area scoring (collision / pellet / win / step penalty) is identical to the standard model.- Parameters:
num_ghosts (int)
pellet_positions_arr (ndarray)
dangerous_areas_arr (ndarray)
dangerous_area_radius (float)
dangerous_area_penalty (float)
step_penalty (float)
ghost_collision_penalty (float)
pellet_reward (float)
win_reward (float)
idx_pac_row (int)
idx_pac_col (int)
idx_ghosts_start (int)
idx_pellets_start (int)
idx_pellets_end (int)
idx_score (int)
idx_terminal (int)