updates solver.rs
This commit is contained in:
@@ -19,19 +19,21 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AssignMap<'a, V> = HashMap<String, Option<&'a V>>;
|
type Variables<'a, V> = HashMap<String, Option<&'a V>>;
|
||||||
|
|
||||||
/// Returns all possible Updates for next assignements.
|
/// Returns all possible Updates for next assignements, prepended with
|
||||||
/// They are added to the stack, just after a Clear for the next variable
|
/// a Clear to ensure the variable is unset before when leaving the branch.
|
||||||
fn assign_next<'a,'b, V>(assign: &'b AssignMap<'a, V>, domain: &'a Domain<V>)
|
fn assign_next<'a,'b, V>(assign: &'b Variables<'a, V>, domain: &'a Domain<V>)
|
||||||
-> Option<Vec<Assignment<'a, V>>>
|
-> Option<Vec<Assignment<'a, V>>>
|
||||||
where V: fmt::Debug
|
where V: fmt::Debug
|
||||||
{
|
{
|
||||||
// Panics on empty domain
|
// Panics on empty domain
|
||||||
if domain.values.is_empty() { panic!("No values in domain : {:?}", domain); };
|
if domain.values.is_empty() { panic!("No values in domain : {:?}", domain); };
|
||||||
|
|
||||||
|
// TODO: should be able to inject a choosing strategy
|
||||||
if let Some((key,_)) = assign.iter().find(|(_, val)| val.is_none()) {
|
if let Some((key,_)) = assign.iter().find(|(_, val)| val.is_none()) {
|
||||||
let mut updates = vec![Assignment::Clear(key.clone())];
|
let mut updates = vec![Assignment::Clear(key.clone())];
|
||||||
|
// TODO: should be able to filter domain values (inference, pertinence)
|
||||||
for value in domain.values.iter() {
|
for value in domain.values.iter() {
|
||||||
updates.push(Assignment::Update(key.clone(), value));
|
updates.push(Assignment::Update(key.clone(), value));
|
||||||
}
|
}
|
||||||
@@ -42,7 +44,7 @@ fn assign_next<'a,'b, V>(assign: &'b AssignMap<'a, V>, domain: &'a Domain<V>)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if any constraint is violated
|
/// Check if any constraint is violated
|
||||||
fn check_consistency<'a, V>(assign: &'a AssignMap<'a, V>, constraint: fn(&AssignMap<'a,V>) -> bool) -> bool {
|
fn check_consistency<'a, V>(assign: &'a Variables<'a, V>, constraint: fn(&Variables<'a,V>) -> bool) -> bool {
|
||||||
constraint(assign)
|
constraint(assign)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,15 +55,10 @@ enum Assignment<'a, V> {
|
|||||||
|
|
||||||
|
|
||||||
/// Visit all possible solutions, using a stack.
|
/// Visit all possible solutions, using a stack.
|
||||||
/// A single mutable map is used to lay out every outcome.
|
fn solve_all<'a, V: Clone + fmt::Debug>(mut assign: Variables<'a, V>, domain: &'a Domain<V>, is_valid: fn(&Variables<'a,V>) -> bool)
|
||||||
/// Using the stack to Update or Clear values :
|
-> Vec<Variables<'a, V>>
|
||||||
/// - After an Update : run assign_next to stack next updates. If there are none,
|
|
||||||
/// check for consistency, store in solutions if valid and close the branch (Clear)
|
|
||||||
/// - After a Clear : simply visit the next node on the stack.
|
|
||||||
fn solve_all<'a, V: Clone + fmt::Debug>(mut assign: AssignMap<'a, V>, domain: &'a Domain<V>, is_valid: fn(&AssignMap<'a,V>) -> bool)
|
|
||||||
-> Vec<AssignMap<'a, V>>
|
|
||||||
{
|
{
|
||||||
let mut solutions: Vec<AssignMap<V>> = vec![];
|
let mut solutions: Vec<Variables<V>> = vec![];
|
||||||
let mut stack: Vec<Assignment<'a, V>> = vec![];
|
let mut stack: Vec<Assignment<'a, V>> = vec![];
|
||||||
stack.append(&mut assign_next(&assign,domain).unwrap());
|
stack.append(&mut assign_next(&assign,domain).unwrap());
|
||||||
loop {
|
loop {
|
||||||
@@ -69,18 +66,20 @@ fn solve_all<'a, V: Clone + fmt::Debug>(mut assign: AssignMap<'a, V>, domain: &'
|
|||||||
if node.is_none() { break; };
|
if node.is_none() { break; };
|
||||||
match node.unwrap() {
|
match node.unwrap() {
|
||||||
Assignment::Update(key, val) => {
|
Assignment::Update(key, val) => {
|
||||||
|
// Assign the variable and open new branches, if any.
|
||||||
*assign.get_mut(&key).unwrap() = Some(val);
|
*assign.get_mut(&key).unwrap() = Some(val);
|
||||||
// Search for next assignments
|
|
||||||
// TODO: handle case of empty domain.values
|
// TODO: handle case of empty domain.values
|
||||||
if let Some(mut nodes) = assign_next(&assign, domain) {
|
if let Some(mut nodes) = assign_next(&assign, domain) {
|
||||||
stack.append(&mut nodes);
|
stack.append(&mut nodes);
|
||||||
} else {
|
} else {
|
||||||
|
// Assignements are completed
|
||||||
if is_valid(&assign) {
|
if is_valid(&assign) {
|
||||||
solutions.push(assign.clone());
|
solutions.push(assign.clone());
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
Assignment::Clear(key) => {
|
Assignment::Clear(key) => {
|
||||||
|
// We are closing this branch, unset the variable
|
||||||
*assign.get_mut(&key).unwrap() = None;
|
*assign.get_mut(&key).unwrap() = None;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -95,15 +94,15 @@ mod tests {
|
|||||||
fn test_solver_find_pairs() {
|
fn test_solver_find_pairs() {
|
||||||
use super::*;
|
use super::*;
|
||||||
// Find all pairs of two differents
|
// Find all pairs of two differents
|
||||||
let assign: AssignMap<i32> = [
|
let assign: Variables<i32> = [
|
||||||
("Left".to_string(), None),
|
("Left".to_string(), None),
|
||||||
("Right".to_string(), None),
|
("Right".to_string(), None),
|
||||||
].iter().cloned().collect();
|
].iter().cloned().collect();
|
||||||
let domain = Domain::new(vec![1,2,3]);
|
let domain = Domain::new(vec![1,2,3]);
|
||||||
let constraint = |assign: &AssignMap<i32>| {
|
let constraint = |assign: &Variables<i32>| {
|
||||||
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
||||||
};
|
};
|
||||||
let solutions: Vec<AssignMap<i32>> = vec![
|
let solutions: Vec<Variables<i32>> = vec![
|
||||||
[("Left".to_string(), Some(&3)), ("Right".to_string(), Some(&3)),].iter().cloned().collect(),
|
[("Left".to_string(), Some(&3)), ("Right".to_string(), Some(&3)),].iter().cloned().collect(),
|
||||||
[("Left".to_string(), Some(&2)), ("Right".to_string(), Some(&2)),].iter().cloned().collect(),
|
[("Left".to_string(), Some(&2)), ("Right".to_string(), Some(&2)),].iter().cloned().collect(),
|
||||||
[("Left".to_string(), Some(&1)), ("Right".to_string(), Some(&1)),].iter().cloned().collect(),
|
[("Left".to_string(), Some(&1)), ("Right".to_string(), Some(&1)),].iter().cloned().collect(),
|
||||||
|
|||||||
Reference in New Issue
Block a user