pub struct Rules<D> { /* private fields */ }Expand description
A set of rules, kept in ascending order of cost.
Propagators make search fast; rules explain. Rules::step applies the cheapest rule
that makes progress, so a solve reads like a person’s and each step can serve as a hint.
Rules::solve repeats that until no rule fires; the most expensive rule it needed
grades the puzzle (Trace::grade).
flowchart LR
start([domains]) --> cheapest[try rules, cheapest first]
cheapest -->|a rule removed values| record[record the step] --> cheapest
cheapest -->|no rule fires| done([trace: steps, solved?])§Examples
use constraint_search::{Domain, Domains, ValueSubset, VariableSubset, Rules};
// One row of four cells over the values 0..4, with two cells already given.
let mut domains = Domains::<u16>::new();
let row: Vec<_> = (0..4).map(|_| domains.add(u16::range(4))).collect();
domains.assign(row[0], 0)?;
domains.assign(row[1], 1)?;
let mut rules = Rules::new();
rules.add(VariableSubset::new(1, 1, [row.clone()]));
rules.add(ValueSubset::new(1, 2, [row.clone()]));
let trace = rules.solve(&mut domains, false)?;
assert!(!trace.solved); // cells 2 and 3 can still swap values 2 and 3
assert_eq!(rules.get(trace.steps[0].rule).name(), "variable single");Implementations§
Source§impl<D: Domain> Rules<D>
impl<D: Domain> Rules<D>
Sourcepub fn add(&mut self, rule: impl Rule<D> + 'static) -> &mut Self
pub fn add(&mut self, rule: impl Rule<D> + 'static) -> &mut Self
Adds rule after every rule that costs no more than it.
Sourcepub fn step(
&self,
domains: &mut Domains<D>,
assume_unique: bool,
) -> Result<Option<Step<D>>, Inconsistent>
pub fn step( &self, domains: &mut Domains<D>, assume_unique: bool, ) -> Result<Option<Step<D>>, Inconsistent>
Applies the cheapest rule that removes a value, once; None if no rule does.
This is the next hint for a person solving the puzzle. Rules with
Rule::assumes_unique take part only if assume_unique is true.
§Errors
Returns Inconsistent if a rule finds the puzzle has no solution. Values removed by
that rule before it failed stay removed.
Sourcepub fn solve(
&self,
domains: &mut Domains<D>,
assume_unique: bool,
) -> Result<Trace<D>, Inconsistent>
pub fn solve( &self, domains: &mut Domains<D>, assume_unique: bool, ) -> Result<Trace<D>, Inconsistent>
Applies Rules::step until no rule makes progress.
§Errors
Returns Inconsistent if a rule finds the puzzle has no solution.