Source code for langgoap.planner.agents
"""Agent-modelling primitives for stochastic planning.
:class:`AgentModel` is the library-level protocol for predicting the
actions of *external* agents — opponents in adversarial games,
other LLM agents in a multi-agent plan, or any entity whose choices
a :class:`~langgoap.planner.transitions.TransitionModel` must
sample to simulate rollouts and chance-node expansions. The shape
mirrors :class:`~langgoap.planner.transitions.TransitionModel`:
* :meth:`AgentModel.expected` returns a deterministic point
estimate, consumed by A*, CSP, and MCTS chance-node expansion.
* :meth:`AgentModel.sample` draws one action from the agent's
distribution, consumed by MCTS rollouts and stochastic
simulations.
:class:`UniformRandomAgentModel` is the baseline implementation
suitable for adversaries whose policy is unknown or intentionally
random (e.g. CS188 ``RandomGhost``). Smarter agent models —
shortest-path pursuers, learned policies, per-agent personality
profiles — plug in through the same protocol without touching
the :class:`TransitionModel`.
"""
from __future__ import annotations
from random import Random
from typing import (
Any,
Callable,
Generic,
Mapping,
Protocol,
Sequence,
TypeVar,
runtime_checkable,
)
A = TypeVar("A")
# Covariant variant for the read-only Protocol surface (action only appears
# in return position). Concrete classes keep using the invariant ``A``
# because Generic[...] cannot accept variant TypeVars.
A_co = TypeVar("A_co", covariant=True)
[docs]
@runtime_checkable
class AgentModel(Protocol[A_co]):
"""Predicts an external agent's next action given a world state.
Generic over the action type ``A``. Conventional values include
direction strings (``"North"``, ``"South"``, ...),
:class:`~langgoap.ActionSpec` references, or any domain-specific
hashable action token.
"""
def expected(self, state: Mapping[str, Any]) -> A_co:
"""Return the deterministic point-estimate action for ``state``."""
...
def sample(self, state: Mapping[str, Any], rng: Random) -> A_co:
"""Draw one action from this agent's distribution for ``state``."""
...
__all__ = ["AgentModel", "UniformRandomAgentModel"]