skip to content

How does an HTN planner decompose a villager's compound task such as gathering firewood, and how does that differ from GOAP's search?

level: seniorimportance: should knowfreq 46%

answer

  1. top-down, not goal-backwards
  2. compound tasks, methods, primitives
  3. first method whose condition holds
  4. state simulated forward
  5. designers author the how

basics

~20 s

An HTN planner replaces a compound task with the subtasks of the first method whose condition holds, recursively, until only primitive actions remain, simulating state forward. GOAP instead searches a flat action set for any chain reaching goal facts.

solid answer

~50 s

In a **hierarchical task network** the designer writes **compound tasks** such as `GatherFirewood`, each with ordered **methods**: *if the villager has an axe*, `GoToTree, ChopTree, CarryLogs`; *otherwise*, `CollectFallenBranches, CarryLogs`. The planner keeps a task list and takes the first task. If it is compound, it picks the first method whose condition holds in the **simulated** world state and replaces the task with its subtasks; if it is **primitive**, it checks the preconditions and applies the effects to that simulated state. When a method's subtasks cannot be completed it backtracks to the next method. **GOAP** has no hierarchy: it searches a flat set of actions for the cheapest chain whose effects reach the goal facts. HTN gives designers control and usually plans faster because methods prune the choices, but it only produces plans the hierarchy describes; GOAP can find chains nobody wrote, at the cost of search and less predictable behaviour.

go deeper

for a junior

Recall the parts: compound tasks, the ordered methods that say how to do them, and primitive tasks that are real actions with preconditions and effects.

for a middle

Walk a decomposition: take the first task, choose the first method whose condition holds, replace it with subtasks, apply primitive effects to the simulated state and backtrack on failure.

for a senior

Compare HTN and GOAP for a given cast on control, planning cost, novelty and replanning, and explain how an HTN agent decides whether a new plan should interrupt the current one.

for a principal

Weigh whether designers should author how tasks are done or only what actions do: HTN keeps behaviour reviewable and cheap, GOAP buys emergence with tuning and debugging effort.

## Vocabulary A **hierarchical task network (HTN)** planner builds a plan by breaking work down, not by searching for any chain of actions that reaches a goal. - A **compound task** is something to accomplish that is not itself an action, such as `GatherFirewood` or `SurviveTheNight`. - A **method** is one way to accomplish a compound task: a condition saying when it applies, plus an ordered list of subtasks. A compound task has one or more methods, listed in priority order. - A **primitive task** is an action the agent can execute, with preconditions and effects on the world state, much like a GOAP action. - The **world state** is a set of facts, copied and updated as the planner simulates the plan. ## How decomposition runs The form games commonly use is total-order forward decomposition: 1. Start with a task list holding the root task, and a copy of the current world state. 2. Take the first task from the list. 3. If it is compound, find the first method whose condition holds in the simulated state, and replace the task with that method's subtasks, in order. 4. If it is primitive, check its preconditions in the simulated state, apply its effects to that state, and append it to the plan. 5. If a method's subtasks cannot be completed, backtrack and try the next method of the same compound task. 6. Stop when the task list is empty; the plan is the primitive tasks in order. Because state is simulated forward, every step sees the facts its predecessors will have produced, and a method's condition can be any test of that state. Backtracking has to restore the simulated world state and the task list to how they were when the failed method was chosen, so implementations either copy the state at each method choice or record the effects to undo. A deep hierarchy with many methods that fail late can therefore cost real time, which is one reason designers order methods so the common case succeeds first. ## Worked example: firewood A villager's `GatherFirewood` has two methods, in this order: - *Has an axe*: `GoToTree`, `ChopTree`, `CarryLogs`. - *Otherwise*: `CollectFallenBranches`, `CarryLogs`. With `hasAxe = false` the first method's condition fails, the second is chosen, and the plan is `CollectFallenBranches`, `CarryLogs`. If `GatherFirewood` sits under a root task whose first method is "flee if danger is near", danger outranks chores simply by being listed first. ## HTN versus GOAP | | HTN | GOAP | |---|---|---| | Input | A root task to perform | A goal: facts to make true | | Direction | Top-down decomposition, state simulated forward | Search over action chains, often backwards from the goal | | Authored knowledge | How tasks are done (methods) | What each action needs and changes | | Choice rule | First applicable method in priority order, with backtracking | Lowest total cost found by best-first search | | Novel plans | Only those the method library describes | Any chain the actions permit | | Planning cost | Usually low, since methods prune choices; deep backtracking can still grow | Grows with branching factor and depth | | Designer control | Direct | Indirect, through preconditions and costs | ## Replanning - Many game HTNs re-run decomposition from the root when the world state changes, instead of patching the running plan. - To avoid needless switches, the planner records which method it chose at each compound task. The two records are compared from the root down: a new plan replaces the running one only if, at the first compound task where they differ, the new plan chose an earlier, higher-priority method. A villager chopping wood switches to fleeing because the danger method outranks the chores, not merely because an equal-priority plan was recomputed. - Primitive tasks still validate their preconditions at run time, as in any planner. ## Choosing between them - Choose **HTN** when designers know how tasks should be done and want reviewable, predictable behaviour with cheap planning, for example villagers with routines. - Choose **GOAP** when the value lies in agents finding their own route through changing circumstances, for example combat NPCs improvising around a blocked door, and the team can pay for search and tuning. - Expect each one's typical failure: an HTN agent fails a task nobody wrote a method for, and a GOAP agent sometimes finds a chain designers never intended.

  • When the world changes mid-plan, how does a game HTN decide whether to replace the running plan?
    It re-runs decomposition from the root on the new state and records which method was chosen at each compound task. If, at the first compound task where the two records differ, the new one chose an earlier, higher-priority method, the new plan wins; otherwise the agent carries on. A villager chopping wood switches to fleeing only because the danger method outranks the chores.
  • What does an HTN lose by planning only through authored methods?
    It cannot discover a chain nobody wrote a method for. If the axe breaks and no method covers another source of wood, the HTN villager fails the task, while a GOAP domain whose actions happen to combine, say breaking a crate that yields wood, could still find a route. The trade is predictability and cheap planning for less emergent behaviour.

HTN is a cook following a recipe book whose recipes list alternatives (no butter, use oil); GOAP is a cook given a pantry and a target dish who works out the steps. The first is predictable and quick; the second can improvise a dish no recipe describes.

  • HTN is just GOAP with a better heuristic.
  • An HTN planner tries every action sequence and keeps the cheapest.
  • An HTN can find plans its method library does not describe.
  • HTN works backwards from the goal state, like regressive GOAP.
  • HTN primitive tasks have no preconditions or effects.