Hierarchical Task Network (HTN) Planning in AI (original) (raw)

Last Updated : 23 Jan, 2026

Hierarchical Task Network (HTN) Planning is an AI planning approach that solves complex problems by breaking them down into smaller, structured subtasks until they become primitive actions that an agent can execute. It relies on hierarchical task decomposition and domain-specific knowledge, making it effective for real-world scenarios like robotics, logistics, simulations and automated systems. Let's see few key terms,

Working

HTN planning begins with one or more high-level goals and systematically refines them into executable steps through decomposition. The planner selects an abstract task, applies a suitable method to break it into subtasks, checks constraints and continues refining until only primitive tasks remain.

**1. Start with High-Level Tasks: The planning process starts with a set of goals and the current world state, usually containing abstract tasks that cannot be executed directly.

**2. Apply Decomposition Methods: For each abstract task, the planner chooses a method that specifies how the task should be expanded into subtasks:

t_a \to (t_1, t_2,t_3 )

**3. Recursively Expand Subtasks: Each subtask is then examined,

This refinement continues until no abstract tasks remain.

**4. Validate Preconditions and Update State: Primitive actions must satisfy their preconditions before execution. When executed, their effects modify the state using:

s' = s \cup \mathrm{eff}(o) \setminus \mathrm{pre}(o)

This keeps the plan feasible at every step.

**5. Produce the Final Plan: After all tasks become primitive and consistent with state constraints, the final output is an ordered action sequence:

P = \langle o_1,\, o_2,\, \ldots,\, o_m \rangle

Implementation

Let's see the implementation,

Python `

operators = { "boil_water": {"pre": {"water_available"}, "eff": {"water_boiled"}}, "add_tea_leaves": {"pre": {"water_boiled"}, "eff": {"tea_infused"}}, "pour_tea": {"pre": {"tea_infused"}, "eff": {"tea_ready"}} } methods = { "make_tea": [ ["boil_water", "add_tea_leaves", "pour_tea"] ] } def htn_planner(state, tasks): plan = [] for task in tasks: if task in operators: if operators[task]["pre"].issubset(state): state = (state - operators[task] ["pre"]) | operators[task]["eff"] plan.append(task) else: raise Exception(f"Preconditions not satisfied for: {task}") elif task in methods: for method in methods[task]: subplan = htn_planner(state, method) plan.extend(subplan) for op in subplan: state = (state - operators[op] ["pre"]) | operators[op]["eff"] else: raise Exception(f"Unknown task: {task}") return plan

initial_state = {"water_available"} goal_tasks = ["make_tea"] print("Generated Plan:", htn_planner(initial_state, goal_tasks))

`

**Output:

Generated Plan: ['boil_water', 'add_tea_leaves', 'pour_tea']

Applications

Advantages

Limitations