Skip to main content

Crate width_search

Crate width_search 

Source
Expand description

crates.io docs.rs CI codecov MSRV License unsafe forbidden

Width-based search (Lipovetzky & Geffner): novelty tables, IW(k), iterated width, serialized IW (SIW), BFWS(⟨w_{#g}, #g⟩), BFWS with relaxed-plan counts #r, and Rollout IW(1).

Anything that can enumerate successors and describe states as integer features implements WidthProblem: planning atoms, grid cells, RAM bytes. The sas-plus feature adds an adapter for sas_plus::Task.

§Usage

Implement WidthProblem and run a search. Here, walking from 0 to 5 on a line has width 1:

use width_search::{WidthProblem, iw};

/// Walk along the integers 0..=5, one step at a time.
struct Line;

impl WidthProblem for Line {
    type State = u32;
    type Action = i8;

    fn initial_state(&self) -> u32 {
        0
    }

    fn successors(&self, s: &u32, out: &mut Vec<(i8, u32)>) {
        if *s < 5 {
            out.push((1, s + 1));
        }
        if *s > 0 {
            out.push((-1, s - 1));
        }
    }

    // One feature per position.
    fn features(&self, s: &u32, out: &mut Vec<u32>) {
        out.push(*s);
    }

    fn num_features(&self) -> usize {
        6
    }

    fn is_goal(&self, s: &u32) -> bool {
        *s == 5
    }
}

let plan = iw(&Line, 1).plan.expect("IW(1) solves it");
assert_eq!(plan, [1, 1, 1, 1, 1]);

A grid with a key and a door needs width 2, because the agent revisits cells after picking up the key:

cargo run --example key_door         # IW(1) fails, IW(2) succeeds

§What “width” means

A state’s novelty is the size of the smallest set of its features that no earlier state in the search had all at once. A state that reaches a never-seen cell has novelty 1. A state whose cell and key flag were each seen before, but never together, has novelty 2. A state whose every feature combination was seen before has no novelty value; it counts as greater than any k.

IW(k) is breadth-first search that drops every non-goal state with novelty greater than k. Goal states are always kept. So it remembers only feature combinations of size up to k, and with n features there are at most O(n^k) of those.

A problem’s width is, roughly, the smallest k for which IW(k) finds a shortest plan. Formally (Lipovetzky & Geffner, 2012), it is the smallest k such that a chain of feature sets of size at most k leads from the initial state to the goal. Each set in the chain is reached optimally by extending an optimal plan for the previous set by one action. Intuitively, width counts how many facts you must track jointly to avoid losing your way:

  • Walking a line or reaching a cell on an open grid has width 1. Position alone tells you whether a state is new.
  • The key_door example has width 2. After taking the key, the agent walks back over cells it has visited. Position alone calls those states old and IW(1) prunes them. The pair (cell, has key) shows they are new.

Width is a property of the problem and of the features you choose. Better features can lower it. Many puzzle and planning domains have width 1 or 2 for a single goal fact, which is why low-k searches go so far. Conjunctive goals (“collect every key”) usually have higher width, which is the case BFWS handles by also counting satisfied goals (#g).

§How it compares

Classic searches rank states by distance: the cost so far (BFS, Dijkstra), plus an estimate of the cost to go (A*). Width-based search ranks them by novelty instead. A state is worth keeping only if it makes some feature value, or some combination of up to k feature values, true for the first time. You don’t need a heuristic or a distance metric. You only need features, and the search explores “new things” first.

AlgorithmNeedsOptimal?Memory / costShines when
BFSsuccessorsyes, unit costswhole reachable spacesmall state spaces
Dijkstrasuccessors, non-negative edge costsyeswhole reachable spaceweighted graphs, no good heuristic
A*successors, non-negative costs, admissible heuristic (consistent when it keeps a closed set)yesgrows with heuristic errorpathfinding on maps, where distance heuristics are good
Greedy best-firstsuccessors, heuristicnoheuristic-dependentfast “any plan” with an informative heuristic
MCTSsimulator, reward / rollout policyno (anytime)tree of visited nodesadversarial or stochastic games, large branching
IW(k)successors, featuresshortest plan (unit costs) if the problem’s width is ≤ kat most O(n^k) expansions for n featureshuge state spaces with low width, no heuristic available
SIWsuccessors, features, goal countno; incompleteone IW(k) per subgoalgoals that can be reached one at a time
BFWSsuccessors, features, goal countnonovelty tables plus open listconjunctive goals (collect all keys, reach all switches)
BFWS #ras BFWS, plus relaxed-plan featuresnoas BFWS, plus a relaxed plan per drop in #gplanning tasks where a relaxed plan says which facts matter
Rollout IW(1)successors, featuresnotree of rolloutssimulators where breadth-first layers are too wide (Atari)

Pros

  • No heuristic to design. Features (atoms, cell positions, inventory flags, RAM bytes) are usually easy to write down.
  • Bounded effort. IW(k) expands at most O(n^k) states however big the raw state space is, so it is predictable on combinatorial spaces where BFS blows up.
  • Many benchmark and puzzle domains have width 1 or 2 per goal atom, so IW(1) and IW(2) solve them quickly.
  • It works on black-box simulators: it never inspects the transition model, only the features of the states it generates.

Cons

  • IW(k) is incomplete when the problem’s width exceeds k. It prunes the state that leads to the goal and reports failure. iterated_width retries with larger k, at a cost that grows steeply with k.
  • It ignores action costs. Use Dijkstra or A* when you need the cheapest path on a weighted graph.
  • Results depend on your features. Coarse features prune too much, and redundant ones waste effort.
  • It is not built for adversarial or stochastic settings. Use minimax or MCTS there.

§In games

Good fits:

  • Puzzle and level validation. Check that a generated level is solvable (keys, doors, switches, block pushing) and get a witness plan.
  • Goal-oriented NPC planning (GOAP-style). Features are world-state facts, and you don’t need a hand-tuned heuristic per goal.
  • Automated playtesting on a simulator. Width-based search plays Atari games from the emulator RAM bytes alone (Lipovetzky, Ramirez & Geffner, IJCAI 2015).
  • Exploration bots. Novelty pruning visits new rooms, items and states before it revisits old ones.

Poor fits:

  • Shortest paths on a weighted grid or navmesh. A* is faster and optimal there (or JPS on uniform-cost grids).
  • Opponent moves, chance, or continuous physics without a discrete feature encoding.

§Features

  • sas-plus: sas::SasProblem, an adapter that runs width-based search on a sas_plus::Task, with goal counts and relaxed plans for BFWS.

Tested: end to end on a Fast Downward output.sas (gripper: IW needs width 3 for the conjunctive goal, finding the optimal 5-step plan). Also: IW with k = #variables is complete and optimal on unit-cost random tasks; plans of every search are validated on random tasks, and both BFWS variants find one exactly when the task is solvable.

§Searches

  • iw(problem, k) and iterated_width(problem, max_k): IW(k), and IW(1), IW(2), … until one succeeds.
  • siw(problem, max_k): a simplified serialized IW. Runs iterated width from the current state until #g drops, appends that subplan, and repeats. LAPKT’s SIW tracks goal atoms instead: it keeps every achieved goal, and accepts a new one only if the full goal stays reachable without deleting achieved goals. This version accepts any drop in #g, so its plans can be longer. SIW is fast when goals can be reached one at a time. It gives up when no subgoal is reachable within max_k, either in a dead end left by an earlier subplan or because the next subgoal needs more width.
  • bfws(problem, k): BFWS(⟨w_{#g}, #g⟩). Novelty is computed per value of #g.
  • bfws_r(problem, k): BFWS(⟨w_{#g,#r}, #g⟩), LAPKT’s BFWS-f5. #r counts how many facts of a relaxed plan the path to a state has reached. The relaxed plan is recomputed at every state where #g drops. Implement RelaxedPlan to use it; SasProblem does, with h_add best supporters.
  • rollout_iw(problem, seed): Rollout IW(1) (Bandres, Bonet & Geffner, AAAI 2018). It runs depth-first rollouts and prunes a node unless it reaches some feature at a smaller depth than before. The paper uses it to pick actions within a time budget. This version runs until it finds a goal or the root is solved. It reaches every width-1 goal, but its plans need not be shortest.

§Compared with LAPKT

Nodes expanded on the test fixtures, this crate / LAPKT 0.2.1 (Tarski grounder). The searches after IW(1) use width bound 2, the LAPKT default:

taskIW(1)IW(1), IW(2)SIWBFWSBFWS #r / f5
gripper-26 / 613 / 1313 / 137 / 47 / 4
sussman-anomaly7 / 716 / 1617 / 128 / 811 / 6
gripper-410 / 1072 / 7271 / 7129 / 4327 / 33
bw-large-a49 / 491913 / 191474 / 1670 / 4866 / 102
logistics-e46 / 4612033 / 1203340238 / 3954714915 / 179994416 / 4399

LAPKT planners: IW_Planner --iw_bound 1 and --iw_bound 2 (the latter runs IW(1), then IW(2)), SIW_Planner, and BFWS with --search_type BFWS-goalcount-only and BFWS-f5. scripts/lapkt-compare.sh reruns them, and tests/it/lapkt_counts.rs pins this crate’s side.

IW agrees almost exactly, although LAPKT computes novelty over STRIPS atoms and this crate over Fast Downward facts. The other searches differ for three reasons. LAPKT’s SIW keeps achieved goals and rejects goals whose achievement would block the rest, so it finds 3- and 7-step plans on sussman-anomaly and bw-large-a where this crate’s finds 5 and 14. Tie-breaking differs: LAPKT breaks ties by path cost, this crate by generation order. And LAPKT’s relaxed plans come from its own heuristic code, so #r counts different facts. LAPKT reports pruned nodes separately; its generated count plus pruned count is close to this crate’s generated.

References: Lipovetzky & Geffner, ECAI 2012 and AAAI 2017; Lipovetzky, Ramirez & Geffner, IJCAI 2015; Bandres, Bonet & Geffner, AAAI 2018.

§MSRV

Rust 1.86. Raising it is a minor release.

§Contributing

See CONTRIBUTING.md.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. The examples in examples/ are public domain (CC0-1.0), so you can copy them into anything. Test fixtures carry their own licenses; see REUSE.toml.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you shall be dual licensed as above, without any additional terms or conditions.

Modules§

sas
Adapter that runs width-based search on a sas_plus::Task. Adapter: run width-based search directly on a sas_plus::Task. Features are the task’s facts var = value; plans are operator ids.

Structs§

NoveltyTable
Records every feature tuple of size <= k seen so far.
SearchResult
The outcome of a search: a plan, if one was found, and node counts.

Traits§

GoalCount
Goal-directed information used by BFWS.
RelaxedPlan
Relaxed-plan information used by bfws_r to compute #r.
WidthProblem
A search problem described by successors and integer features.

Functions§

bfws
BFWS(f) with f = ⟨w_{#g}, #g⟩: best-first search ordered by novelty (computed separately for each goal-count value #g, capped at k + 1), breaking ties by #g. Complete on finite state spaces.
bfws_r
BFWS(f5) with f = ⟨w_{#g,#r}, #g⟩ (Lipovetzky & Geffner, AAAI 2017): like bfws, but novelty is computed separately for each pair of #g and #r.
iterated_width
Runs IW(1), IW(2), … up to max_k; returns the width that succeeded (if any) and the accumulated statistics.
iw
IW(k): breadth-first search that prunes every generated state whose novelty exceeds k. Runs in O(n^k) expansions for n features.
rollout_iw
Rollout IW(1) (Bandres, Bonet & Geffner, AAAI 2018), goal-directed.
siw
Serialized IW (SIW): reaches the goal one subgoal at a time.