starts basic solver
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
mod solver;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
115
planner/src/solver.rs
Normal file
115
planner/src/solver.rs
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
use std::fmt;
|
||||||
|
use std::clone::Clone;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Domain<V> {
|
||||||
|
values: Vec<V>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V> Domain<V> {
|
||||||
|
fn new(values: Vec<V>) -> Domain<V> {
|
||||||
|
Domain { values }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Domain<{:?}>", self.values)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AssignMap<'a, V> = HashMap<String, Option<&'a V>>;
|
||||||
|
|
||||||
|
/// Returns all possible Updates for next assignements.
|
||||||
|
/// They are added to the stack, just after a Clear for the next variable
|
||||||
|
fn assign_next<'a,'b, V>(assign: &'b AssignMap<'a, V>, domain: &'a Domain<V>)
|
||||||
|
-> Option<Vec<Assignment<'a, V>>>
|
||||||
|
where V: fmt::Debug
|
||||||
|
{
|
||||||
|
// Panics on empty domain
|
||||||
|
if domain.values.is_empty() { panic!("No values in domain : {:?}", domain); };
|
||||||
|
|
||||||
|
if let Some((key,_)) = assign.iter().find(|(_, val)| val.is_none()) {
|
||||||
|
let mut updates = vec![Assignment::Clear(key.clone())];
|
||||||
|
for value in domain.values.iter() {
|
||||||
|
updates.push(Assignment::Update(key.clone(), value));
|
||||||
|
}
|
||||||
|
Some(updates)
|
||||||
|
} else { // End of assignements
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if any constraint is violated
|
||||||
|
fn check_consistency<'a, V>(assign: &'a AssignMap<'a, V>, constraint: fn(&AssignMap<'a,V>) -> bool) -> bool {
|
||||||
|
constraint(assign)
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Assignment<'a, V> {
|
||||||
|
Update(String, &'a V),
|
||||||
|
Clear(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Visit all possible solutions, using a stack.
|
||||||
|
/// A single mutable map is used to lay out every outcome.
|
||||||
|
/// Using the stack to Update or Clear values :
|
||||||
|
/// - 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 stack: Vec<Assignment<'a, V>> = vec![];
|
||||||
|
stack.append(&mut assign_next(&assign,domain).unwrap());
|
||||||
|
loop {
|
||||||
|
let node = stack.pop();
|
||||||
|
if node.is_none() { break; };
|
||||||
|
match node.unwrap() {
|
||||||
|
Assignment::Update(key, val) => {
|
||||||
|
*assign.get_mut(&key).unwrap() = Some(val);
|
||||||
|
// Search for next assignments
|
||||||
|
// TODO: handle case of empty domain.values
|
||||||
|
if let Some(mut nodes) = assign_next(&assign, domain) {
|
||||||
|
stack.append(&mut nodes);
|
||||||
|
} else {
|
||||||
|
if is_valid(&assign) {
|
||||||
|
solutions.push(assign.clone());
|
||||||
|
};
|
||||||
|
};
|
||||||
|
},
|
||||||
|
Assignment::Clear(key) => {
|
||||||
|
*assign.get_mut(&key).unwrap() = None;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
solutions
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn test_solver_find_pairs() {
|
||||||
|
use super::*;
|
||||||
|
// Find all pairs of two differents
|
||||||
|
let assign: AssignMap<i32> = [
|
||||||
|
("Left".to_string(), None),
|
||||||
|
("Right".to_string(), None),
|
||||||
|
].iter().cloned().collect();
|
||||||
|
let domain = Domain::new(vec![1,2,3]);
|
||||||
|
let constraint = |assign: &AssignMap<i32>| {
|
||||||
|
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
||||||
|
};
|
||||||
|
let solutions: Vec<AssignMap<i32>> = vec![
|
||||||
|
[("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(&1)), ("Right".to_string(), Some(&1)),].iter().cloned().collect(),
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(solve_all(assign, &domain, constraint), solutions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user