NextAction: pacman

A small, auditable classifier that predicts the next move of Pac-Man in a grid maze. Given the current state, it returns one action from a fixed vocabulary with a probability for each, so an agent can act alone when it is confident and hand the decision to a person when it is not. It ships with a decision policy (config.toml) that blocks impossible actions and sets a minimum confidence per action.

Demo

pacman demo

Over 300 unseen games, the model wins 39% against the expert's 35% (overlapping 95% intervals) and hands 3.3% of moves to the expert when its confidence is below 0.6. Sending more moves to the expert does not win more games, because the expert is no stronger than the model it taught; every loss ends with Pac-Man trapped, every open move next to a ghost. Scripts, benchmark and analysis: demos/pacman.

Model details

Developed by Luiz Araujo
Model type TF-IDF features + multinomial logistic regression (scikit-learn)
Task next-action prediction for task-oriented agents (text classification)
Language English
License mit
Size 48 kB
Code github.com/leduardoaraujo/NextAction

Uses

Direct use. Choose Pac-Man's next move in the NextAction demo, as a worked example of imitation learning with a decision policy: state-based action masks, a confidence threshold and fallback to an expert.

Out of scope. Other mazes or game rules without retraining, and any claim of strong play: the model imitates a greedy rule-based expert and inherits its mistakes.

How to get started

from huggingface_hub import hf_hub_download
import skops.io as sio

model = sio.load(hf_hub_download("ludolua/nextaction-pacman", "model.skops"))  # no types outside skops' trusted list

state = "[blocked up down] [danger none] [features wall_up wall_down food_left_0 ghost_left_close food_right_0 ghost_right_close closest_food_left safest_left threat_mid power_no]"
probabilities = dict(zip(model.classes_, model.predict_proba([state])[0]))
print(max(probabilities, key=probabilities.get), probabilities)

Dependencies: scikit-learn, skops and huggingface_hub. Use a scikit-learn version compatible with the training one. The full toolkit (CLI, REST API, decision log, evaluation, policy) is in the NextAction repository.

Input format

A free-text description of the current situation in English, for example "[blocked up down] [danger none] [features wall_up wall_down food_left_0 ghost_left_close food_right_0 ghost_right_close closest_food_left safest_left threat_mid power_no]".

States are built by features.describe() in the demos/pacman folder; use it to turn a position into the text this model expects.

Actions

Action Meaning
down move one cell down
left move one cell left
right move one cell right
up move one cell up

Training details

Data. 50775 moves of a rule-based expert in games generated by demos/pacman/generate_data.py. The training data is not redistributed here; download it from the original source.

Preprocessing. States are normalized to the English tag format, then vectorized with TF-IDF over word unigrams and bigrams with accents stripped.

Hyperparameter Value
ngram_range (1, 2)
min_df 2
C 1.0
class_weight None
max_iter 1000
calibration none

The solver is L-BFGS, run in warm-started chunks so training progress can be reported. Hyperparameters live in config.toml and can be searched with nextaction tune.

Evaluation

Method. separate test split (pacman_test.csv), with 12734 examples evaluated.

Results. Accuracy 0.973 and macro F1 0.974; always choosing the most frequent action would give 0.251. Expected calibration error: 0.029.

Action Precision Recall F1 Support
down 0.969 0.979 0.974 3138
left 0.972 0.971 0.972 3211
right 0.974 0.963 0.969 3193
up 0.978 0.98 0.979 3192

Review thresholds. Decisions with confidence below the threshold go to a person:

Threshold Decisions automated Accuracy on automated
0.3 100.0% 97.3%
0.4 99.9% 97.4%
0.5 99.1% 97.7%
0.6 97.6% 98.3%
0.7 95.3% 99.0%
0.8 92.2% 99.4%
0.9 84.5% 99.7%

Decision policy

config.toml holds the decision policy used with this model: rules that block impossible actions, a minimum confidence per action, risk levels and error costs, in units of one human review (review_cost = 1). nextaction calibrate-policy re-fits the thresholds from the costs.

Action Risk Min. confidence Unreviewed error cost Confirm with user
down low 0.6 1 no
left low 0.6 1 no
right low 0.6 1 no
up low 0.6 1 no

Rules:

  • no_walls: deny when
  • avoid_adjacent_ghosts: deny when

Simulated on the evaluation data above, the policy automates 96.8% of decisions at 98.3% accuracy, with an expected cost of 0.0483 reviews per decision.

Apply it with the NextAction package (pip install git+https://github.com/leduardoaraujo/NextAction):

import tomllib
from nextaction.policy import apply, from_dict

with open(hf_hub_download("ludolua/nextaction-pacman", "config.toml"), "rb") as file:
    policy = from_dict(tomllib.load(file)["policy"])
decision = apply(probabilities, state, policy)  # probabilities and state from the snippet above
print(decision["action"], decision["needs_review"], decision["reasons"])

Bias, risks and limitations

  • The model matches word patterns. It does not understand business rules or verify facts, so the calling application must check preconditions before acting.
  • Logistic regression probabilities are not calibrated; pick thresholds from the tables above, and re-evaluate on your own traffic before automating.
  • The expert wins only about a third of the games, so the model learns its mistakes too; per-move features cannot see dead ends several moves ahead.
  • The action vocabulary is fixed; new actions require new labeled data.

Citation

@software{araujo2026nextaction,
  author = {Araujo, Luiz},
  title  = {NextAction: a small, auditable next-action model for AI agents},
  year   = {2026},
  url    = {https://github.com/leduardoaraujo/NextAction}
}

Model card contact

Open an issue in the NextAction repository.

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Evaluation results