Skip to main content

Factoring

Struct Factoring 

Source
pub struct Factoring { /* private fields */ }
Expand description

A partition of a task’s variables into one center and any number of leaves.

Every partition is valid: operators that would break the star topology (for example by reading one leaf and writing another) are searched explicitly as global actions. The factoring only decides how much of the task is priced in the leaves instead of searched.

Implementations§

Source§

impl Factoring

Source

pub fn generalized(task: &Task) -> Self

A generalized factoring: leaves chosen among the effect variable sets of the operators (the paper’s potential leaves), as many mutually disjoint mobile ones as a greedy pass finds; every other variable is the center, which may end up empty.

Gnad, Torralba and Fišer (ICAPS 2022) select the leaves with an integer program maximizing the number of mobile leaves; this is a greedy approximation of that objective. Candidates go smallest first, then by how few other candidates they overlap, then by how many operators set exactly that variable set. A leaf is mobile if the search would compile some operator as its leaf action: the operator sets only the leaf’s variables and reads only the leaf and the center, with each derived variable counted in the leaf it depends on. A candidate is kept if it is disjoint from the leaves so far, the search accepts the leaves (no derived variable depends on two of them), and it and every earlier leaf stay mobile (a new leaf takes its variables out of the center, which can strand another leaf), so every leaf of the result is mobile. Candidates with a derived variable or more than crate::MAX_LEAF_STATES states are skipped.

Leaves may read and write each other: those operators become global actions, which the search branches over (their Theorem 1 shows that this is decoupled search on a star factoring of a slightly extended task, so it stays sound, complete and optimal).

§Examples
use decoupled_search::{Factoring, search};
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// Two counters that each step on their own, and a swap that touches
// both: no star factoring has two leaves, a generalized one has.
let op = |name: &str, pre: Vec<Fact>, eff: Vec<Fact>| Operator {
    name: name.into(),
    pre,
    eff: eff.into_iter().map(Effect::new).collect(),
    cost: 1,
};
let task = Task {
    variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
    operators: vec![
        op("a", vec![Fact::new(0, 0)], vec![Fact::new(0, 1)]),
        op("b", vec![Fact::new(1, 0)], vec![Fact::new(1, 1)]),
        op("swap", vec![Fact::new(0, 1)], vec![Fact::new(0, 0), Fact::new(1, 0)]),
    ],
    initial: State(vec![0, 0]),
    goal: vec![Fact::new(0, 1), Fact::new(1, 1)],
    axioms: vec![],
};

let factoring = Factoring::generalized(&task);
assert!(factoring.center().is_empty());
assert_eq!(factoring.leaves(), &[vec![0], vec![1]]);
assert_eq!(search(&task, &factoring)?.map(|p| p.cost), Some(2));
Source§

impl Factoring

Source

pub fn with_strategy(task: &Task, strategy: Strategy) -> Self

The factoring that strategy computes for task.

Source

pub fn with_free_leaf_actions(&self, task: &Task) -> Result<Task, Error>

task with every operator that is a leaf action under this factoring at cost 0: the task whose heuristics are root heuristics for crate::Config::anytime_astar (Gnad and Hoffmann’s leaves-for-free root estimate, ICAPS 2015, Definition 6).

§Errors

As Factoring::score.

§Examples
use decoupled_search::Factoring;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 2,
};
let task = Task {
    variables: vec![Variable::new("truck", 2), Variable::new("pkg", 2)],
    operators: vec![
        op(vec![Fact::new(0, 0)], Fact::new(0, 1)),
        op(vec![Fact::new(0, 1)], Fact::new(1, 1)),
    ],
    initial: State(vec![0, 0]),
    goal: vec![Fact::new(1, 1)],
    axioms: vec![],
};

// The package moves by a leaf action, the truck by a global one.
let free = Factoring::fork(&task).with_free_leaf_actions(&task)?;
let costs: Vec<u32> = free.operators.iter().map(|o| o.cost).collect();
assert_eq!(costs, [2, 0]);
Source

pub fn score(&self, task: &Task) -> Result<FactoringScore, Error>

Scores this factoring on task, classifying operators exactly as the search does.

§Errors

Every error of crate::search() except Error::ReachabilityNotOptimal: the factoring must be searchable.

Source

pub fn auto(task: &Task) -> Self

The best factoring of every Strategy for task with at least two mobile leaves, by score: the largest share of leaf actions, then more mobile leaves, then the smaller largest leaf, then the earlier strategy in Strategy::ALL. Strategy::Generalized comes last there, so it wins only when it scores strictly better than every star strategy; its leaves may then interact directly.

Factorings that Factoring::score rejects (for example because an axiom spans two of their leaves) are skipped. If no candidate has two mobile leaves, the result is the factoring without leaves: explicit search over the whole task. Gnad, Poser and Hoffmann (2017) abstain in that case too, since decoupling gains by not combining the states of several leaves; they rank candidates by short sample searches, this ranking is static (see the module docs).

§Examples
use decoupled_search::{Factoring, search};
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A hub that both spokes read and that reads both spokes.
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let task = Task {
    variables: vec![Variable::new("hub", 2), Variable::new("a", 2), Variable::new("b", 2)],
    operators: vec![
        op(vec![Fact::new(1, 1), Fact::new(2, 1)], Fact::new(0, 1)),
        op(vec![Fact::new(0, 0)], Fact::new(1, 1)),
        op(vec![Fact::new(0, 0)], Fact::new(2, 1)),
    ],
    initial: State(vec![0, 0, 0]),
    goal: vec![Fact::new(0, 1)],
    axioms: vec![],
};

// The fork factoring finds no sink component; the star strategies do.
let factoring = Factoring::auto(&task);
assert_eq!(factoring.leaves(), &[vec![1], vec![2]]);
assert_eq!(search(&task, &factoring)?.map(|p| p.cost), Some(3));
Source

pub fn auto_by_sample( task: &Task, objective: Objective, expansions: u64, ) -> Self

The factoring of the strategy whose short sample search does best, among those Factoring::auto would consider (two mobile leaves, accepted by the search): Gnad, Poser and Hoffmann’s (2017) per-instance selection, with an expansion budget instead of a time limit.

Each distinct candidate runs a sample search of objective (see Objective) for at most expansions expansions. A sample that finishes (cost-optimal: proves its result; satisficing: finds a plan) wins, the cheapest such one first; the paper would return its plan, here the caller searches again. Otherwise, as in the paper, satisficing planning prefers the larger heuristic improvement (the initial state’s h^FF over the least one seen), and cost-optimal planning the faster expansion rate. With a budget of expansions rather than of time, the rate is measured in deterministic work units (per generated decoupled state, one plus the leaf states its leaf closures range over), our proxy for time. Ties go to the earlier strategy in Strategy::ALL. Without a candidate, the result is the factoring without leaves; with one, no sample is run.

§Examples
use decoupled_search::{Factoring, Objective, search};
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A truck (0) and two packages that ride along.
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let task = Task {
    variables: vec![Variable::new("truck", 2), Variable::new("p", 3), Variable::new("q", 3)],
    operators: vec![
        op(vec![Fact::new(0, 0)], Fact::new(0, 1)),
        op(vec![Fact::new(0, 0), Fact::new(1, 0)], Fact::new(1, 2)),
        op(vec![Fact::new(0, 1), Fact::new(1, 2)], Fact::new(1, 1)),
        op(vec![Fact::new(0, 0), Fact::new(2, 0)], Fact::new(2, 2)),
        op(vec![Fact::new(0, 1), Fact::new(2, 2)], Fact::new(2, 1)),
    ],
    initial: State(vec![0, 0, 0]),
    goal: vec![Fact::new(1, 1), Fact::new(2, 1)],
    axioms: vec![],
};

let factoring = Factoring::auto_by_sample(&task, Objective::Optimal, 100);
assert_eq!(factoring.leaves().len(), 2);
assert_eq!(search(&task, &factoring)?.map(|p| p.cost), Some(5));
Source§

impl Factoring

Source

pub fn incident_arcs(task: &Task) -> Self

The incident-arcs star factoring (Gnad, Poser and Hoffmann 2017, Figure 2): orders the variables by decreasing number of incident causal-graph arcs, moves them into the center one at a time, and keeps the candidate with the most mobile leaves.

A candidate’s leaves are the weakly connected components of the causal graph without the center that have at most crate::MAX_LEAF_STATES states; the other variables form the center. No arc connects two leaves, so the result always passes Factoring::check_star. A variable’s incident arcs are its incoming plus its outgoing arcs, counted once on the full causal graph. Ties are ours, the paper leaves them open: in the order, the smaller variable first; between candidates, the smaller center. As in the paper, the center has at least one variable. The paper abstains below two mobile leaves; this returns the best candidate anyway, possibly without leaves.

§Examples
use decoupled_search::Factoring;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A hub (variable 0) that both spokes read and that reads both spokes.
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let task = Task {
    variables: vec![Variable::new("hub", 2), Variable::new("a", 2), Variable::new("b", 2)],
    operators: vec![
        op(vec![Fact::new(1, 1), Fact::new(2, 1)], Fact::new(0, 1)),
        op(vec![Fact::new(0, 0)], Fact::new(1, 1)),
        op(vec![Fact::new(0, 0)], Fact::new(2, 1)),
    ],
    initial: State(vec![0, 0, 0]),
    goal: vec![Fact::new(0, 1)],
    axioms: vec![],
};

let factoring = Factoring::incident_arcs(&task);
assert_eq!(factoring.center(), &[0]);
assert_eq!(factoring.leaves(), &[vec![1], vec![2]]);
assert_eq!(factoring.check_star(&task), Ok(()));
Source

pub fn max_leaves(task: &Task) -> Self

The independent-set star factoring, which maximises the number of leaves (Gnad, Poser and Hoffmann 2017, Figure 1): every maximum independent set of the causal graph seeds one leaf per variable; then variables adjacent to exactly one leaf join it while that gains mobile leaves, frozen (immobile) leaves go back to the center, and leaves grow once more. The result is the candidate with the most mobile leaves.

Variables that cannot be leaves (derived, or with more than crate::MAX_LEAF_STATES values) are left out of the independent sets. The paper enumerates maximum independent sets for up to ten seconds; this enumerates at most 16 of them with a deterministic search budget, after which the best set found so far is used. Always passes Factoring::check_star. The paper abstains below two mobile leaves; this returns the best candidate anyway.

§Examples
use decoupled_search::Factoring;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A chain a - b - c: putting b into the center leaves a and c apart.
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let var = |name: &str| Variable::new(name, 2);
let task = Task {
    variables: vec![var("a"), var("b"), var("c")],
    operators: vec![
        op(vec![Fact::new(0, 1)], Fact::new(1, 1)),
        op(vec![Fact::new(1, 1)], Fact::new(2, 1)),
        op(vec![], Fact::new(0, 1)),
    ],
    initial: State(vec![0, 0, 0]),
    goal: vec![Fact::new(2, 1)],
    axioms: vec![],
};

let factoring = Factoring::max_leaves(&task);
assert_eq!(factoring.center(), &[1]);
assert_eq!(factoring.leaves(), &[vec![0], vec![2]]);
Source§

impl Factoring

Source

pub fn new(task: &Task, leaves: Vec<Vec<VarId>>) -> Result<Self, Error>

A factoring with the given leaves; all other variables form the center.

§Errors

Leaves of any size are accepted; searching with explicit leaves rejects one with more than MAX_LEAF_STATES states (Error::LeafTooLarge), symbolic leaves (crate::Config::symbolic_leaves) do not.

Source

pub fn fork(task: &Task) -> Self

The fork factoring: every sink strongly connected component of the causal graph (one no arc leaves) with at most MAX_LEAF_STATES states is a leaf, everything else is the center.

Operators on a sink component read only the center and that component, so all of them are priced in the leaf. A task whose causal graph is one small component becomes one leaf with an empty center.

Source

pub fn inverted_fork(task: &Task) -> Self

The inverted-fork factoring: every source strongly connected component of the causal graph (one no arc enters) with at most MAX_LEAF_STATES states is a leaf, everything else is the center.

Operators on a source component read and write only that component, and the center may read it. Such operators are leaf actions unless they also touch the center.

§Examples
use decoupled_search::Factoring;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A switch (variable 0) that a lamp (variable 1) depends on.
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let task = Task {
    variables: vec![Variable::new("switch", 2), Variable::new("lamp", 2)],
    operators: vec![
        op(vec![], Fact::new(0, 1)),
        op(vec![Fact::new(0, 1)], Fact::new(1, 1)),
    ],
    initial: State(vec![0, 0]),
    goal: vec![Fact::new(1, 1)],
    axioms: vec![],
};

let factoring = Factoring::inverted_fork(&task);
assert_eq!(factoring.leaves(), &[vec![0]]);
assert_eq!(factoring.center(), &[1]);
Source

pub fn x_shape(task: &Task) -> Self

The X-shape factoring (Gnad, Hoffmann and Domshlak, SoCS 2015): the fork factoring, plus every small source strongly connected component of the rest of the causal graph that has no arc into a fork leaf.

The paper runs the fork strategy and then the inverted-fork strategy on the fork center, putting back into the center every inverted-fork leaf with an arc into a fork leaf. Fork leaves have no outgoing arcs, so the source components of the fork center are source components of the whole causal graph. Every leaf then touches only the center: fork leaves receive arcs from it, inverted-fork leaves send arcs to it, so the result passes Factoring::check_star. Leaves keep the MAX_LEAF_STATES bound of the other strategies.

§Examples
use decoupled_search::Factoring;
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// A switch (0) enables a truck (1) that moves a package (2).
let op = |pre: Vec<Fact>, eff: Fact| Operator {
    name: "op".into(),
    pre,
    eff: vec![Effect::new(eff)],
    cost: 1,
};
let var = |name: &str| Variable::new(name, 2);
let task = Task {
    variables: vec![var("switch"), var("truck"), var("package")],
    operators: vec![
        op(vec![], Fact::new(0, 1)),
        op(vec![Fact::new(0, 1)], Fact::new(1, 1)),
        op(vec![Fact::new(1, 1)], Fact::new(2, 1)),
    ],
    initial: State(vec![0, 0, 0]),
    goal: vec![Fact::new(2, 1)],
    axioms: vec![],
};

let factoring = Factoring::x_shape(&task);
assert_eq!(factoring.center(), &[1]);
assert_eq!(factoring.leaves(), &[vec![2], vec![0]]);
assert_eq!(factoring.check_star(&task), Ok(()));
Source

pub fn check_star(&self, task: &Task) -> Result<(), Error>

Checks that the factoring is a strict star: no causal-graph arc connects variables of two different leaves, so leaves interact only through the center. A derived variable counts as the state variables it depends on (the search evaluates it with them), and those must lie in one factor.

Search does not require this, since an operator that couples two leaves becomes a global action. In a star factoring no operator writes one leaf while reading or writing another, which keeps the leaves independent. An operator may still read several leaves and write only the center. All strategies of this type except Factoring::new produce star factorings.

§Errors

Error::NotStar with the first offending arc from -> to (in variable order; through a derived variable, from is one of its inputs), and Error::FactoringMismatch if the factoring does not partition exactly the variables of task.

§Examples
use decoupled_search::{Error, Factoring};
use sas_plus::{Effect, Fact, Operator, State, Task, Variable};

// One operator reads variable 0 and writes variable 1.
let task = Task {
    variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
    operators: vec![Operator {
        name: "op".into(),
        pre: vec![Fact::new(0, 1)],
        eff: vec![Effect::new(Fact::new(1, 1))],
        cost: 1,
    }],
    initial: State(vec![0, 0]),
    goal: vec![Fact::new(1, 1)],
    axioms: vec![],
};

let split = Factoring::new(&task, vec![vec![0], vec![1]])?;
assert_eq!(split.check_star(&task), Err(Error::NotStar { from: 0, to: 1 }));
assert_eq!(Factoring::fork(&task).check_star(&task), Ok(()));
Source

pub fn center(&self) -> &[VarId]

The center variables, sorted ascending. May be empty.

Source

pub fn leaves(&self) -> &[Vec<VarId>]

The leaves, each a non-empty list of variables.

Trait Implementations§

Source§

impl Clone for Factoring

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Factoring

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Factoring

Source§

impl PartialEq for Factoring

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Factoring

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.