gale.ai.goap module

This file contains support for goal-oriented action planning (GOAP): GoapAction describes one thing an agent can do (its preconditions, its effects, and its cost), and plan finds the cheapest sequence of actions that turns a starting world state into one satisfying a goal. It works by reusing gale.ai.pathfinding.a_star_to_predicate (an open-goal A*: any world state satisfying the goal, not one fixed in advance) over the space of possible world states – the same idea gale.ai.graph.StateGraph’s docstring already uses for the Towers of Hanoi puzzle, applied here to states generated on the fly from a list of actions instead of a pre-built graph.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.ai.goap.GoapAction(name, preconditions, effects, cost=1.0)[source]

Bases: object

One action an agent can take: applicable when its preconditions are a subset of the current world state, and, once taken, applies its effects on top of it.

Usage example:

chop_wood = GoapAction(

“chop_wood”, preconditions={“has_axe”: True}, effects={“has_wood”: True}, cost=2.0,

)

Parameters:
  • name (str)

  • preconditions (Dict[str, Any])

  • effects (Dict[str, Any])

  • cost (float)

is_applicable(state)[source]
Parameters:

state (Dict[str, Any]) – The world state to check.

Returns:

Whether every one of this action’s preconditions holds in state.

Return type:

bool

apply(state)[source]
Parameters:

state (Dict[str, Any]) – The world state to apply this action’s effects on top of.

Returns:

A new world state with this action’s effects merged in, leaving state unmodified.

Return type:

Dict[str, Any]

gale.ai.goap.plan(world_state, goal, actions)[source]

Find the cheapest sequence of actions that turns world_state into one satisfying every key-value pair in goal.

Parameters:
  • world_state (Dict[str, Any]) – The starting world state.

  • goal (Dict[str, Any]) – Key-value pairs the final world state must satisfy.

  • actions (Sequence[GoapAction]) – Every action the agent may take.

Returns:

The cheapest list of actions achieving goal from world_state, in order, or None if no combination of actions does.

Return type:

List[GoapAction] | None