refactors Problem, adds Solution
This commit is contained in:
@@ -5,20 +5,19 @@ extern crate cookbook;
|
|||||||
|
|
||||||
use std::time;
|
use std::time;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fmt::Display;
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use cookbook::*;
|
use cookbook::*;
|
||||||
use planner::{
|
use planner::{
|
||||||
*, Value,
|
*, Value,
|
||||||
solver::{
|
solver::{
|
||||||
Variables,
|
Solution,
|
||||||
Domain,
|
Domain,
|
||||||
Problem
|
Problem
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
fn pretty_output<K: Eq + Hash + Debug>(res: &Variables<Value, K>) -> String {
|
fn pretty_output<K: Eq + Hash + Debug>(res: &Solution<Value, K>) -> String {
|
||||||
let mut repr = String::new();
|
let mut repr = String::new();
|
||||||
for (var,value) in res {
|
for (var,value) in res {
|
||||||
let value = match value {
|
let value = match value {
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ use std::clone::Clone;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// An assignments map of variables
|
/// An assignments map of variables
|
||||||
pub type Variables<'a, V> = Vec<Option<&'a V>>;
|
type Variables<'a, V> = Vec<Option<&'a V>>;
|
||||||
|
/// A solution returned by [`Solver`]
|
||||||
|
pub type Solution<'a, V, K> = HashMap<K, Option<&'a V>>;
|
||||||
|
|
||||||
enum Assignment<'a, V> {
|
enum Assignment<'a, V> {
|
||||||
Update(usize, &'a V),
|
Update(usize, &'a V),
|
||||||
@@ -124,7 +126,7 @@ impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_template() -> Problem<'a, V, K> {
|
pub fn from_template() -> Problem<'a, V, K> {
|
||||||
let mut builder = Self::build();
|
let builder = Self::build();
|
||||||
|
|
||||||
builder.finish()
|
builder.finish()
|
||||||
}
|
}
|
||||||
@@ -169,12 +171,8 @@ impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all complete solutions, after visiting all possible outcomes using a stack (DFS).
|
fn _solve(&mut self, limit: Option<usize>) -> Vec<Solution<'a, V, K>> {
|
||||||
pub fn solve_all(&mut self) -> Vec<Variables<'a,V>>
|
let mut solutions: Vec<Solution<V, K>> = vec![];
|
||||||
where V: Clone + fmt::Debug,
|
|
||||||
K: Clone + fmt::Debug,
|
|
||||||
{
|
|
||||||
let mut solutions: Vec<Variables<V>> = vec![];
|
|
||||||
let mut stack: Vec<Assignment<'a, V>> = vec![];
|
let mut stack: Vec<Assignment<'a, V>> = vec![];
|
||||||
if let Some(mut init_updates) = self._push_updates() {
|
if let Some(mut init_updates) = self._push_updates() {
|
||||||
stack.append(&mut init_updates);
|
stack.append(&mut init_updates);
|
||||||
@@ -185,7 +183,15 @@ impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let node = stack.pop();
|
let node = stack.pop();
|
||||||
|
// There is no more combination to try out
|
||||||
if node.is_none() { break; };
|
if node.is_none() { break; };
|
||||||
|
// Exit early if we have enough solutions
|
||||||
|
if limit.is_some()
|
||||||
|
&& solutions.len() == limit.unwrap()
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
match node.unwrap() {
|
match node.unwrap() {
|
||||||
Assignment::Update(idx, val) => {
|
Assignment::Update(idx, val) => {
|
||||||
// Assign the variable and open new branches, if any.
|
// Assign the variable and open new branches, if any.
|
||||||
@@ -196,7 +202,12 @@ impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
} else {
|
} else {
|
||||||
// Assignements are completed
|
// Assignements are completed
|
||||||
if self._is_valid() {
|
if self._is_valid() {
|
||||||
solutions.push(self.variables.clone());
|
solutions.push(
|
||||||
|
// Builds Solution
|
||||||
|
self.keys.iter().cloned()
|
||||||
|
.zip(self.variables.iter().cloned())
|
||||||
|
.collect()
|
||||||
|
);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -207,44 +218,21 @@ impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
solutions
|
solutions
|
||||||
}
|
|
||||||
|
|
||||||
pub fn solve_one(&mut self) -> Option<Variables<'a,V>>
|
}
|
||||||
|
/// Returns all complete solutions, after visiting all possible outcomes using a stack (DFS).
|
||||||
|
pub fn solve_all(&mut self) -> Vec<Solution<'a,V,K>>
|
||||||
where V: Clone + fmt::Debug,
|
where V: Clone + fmt::Debug,
|
||||||
K: Clone + fmt::Debug,
|
K: Clone + fmt::Debug,
|
||||||
{
|
{
|
||||||
let mut stack: Vec<Assignment<'a, V>> = vec![];
|
self._solve(None) // No limit
|
||||||
if let Some(mut init_updates) = self._push_updates() {
|
|
||||||
stack.append(&mut init_updates);
|
|
||||||
} else {
|
|
||||||
panic!("Could not initialize !");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
pub fn solve_one(&mut self) -> Option<Solution<'a,V,K>>
|
||||||
let node = stack.pop();
|
where V: Clone + fmt::Debug,
|
||||||
if node.is_none() {
|
K: Clone + fmt::Debug,
|
||||||
return None;
|
{
|
||||||
};
|
self._solve(Some(1)).pop()
|
||||||
match node.unwrap() {
|
|
||||||
Assignment::Update(idx, val) => {
|
|
||||||
// Assign the variable and open new branches, if any.
|
|
||||||
self.variables[idx] = Some(val);
|
|
||||||
// TODO: handle case of empty domain.values
|
|
||||||
if let Some(mut nodes) = self._push_updates() {
|
|
||||||
stack.append(&mut nodes);
|
|
||||||
} else {
|
|
||||||
// Assignements are completed
|
|
||||||
if self._is_valid() {
|
|
||||||
return Some(self.variables.clone());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
},
|
|
||||||
Assignment::Clear(idx) => {
|
|
||||||
// We are closing this branch, unset the variable
|
|
||||||
self.variables[idx] = None;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,10 +282,10 @@ mod tests {
|
|||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let solutions: Vec<Variables<i32>> = vec![
|
let solutions: Vec<Solution<i32, _>> = vec![
|
||||||
[Some(&3), Some(&3)].iter().cloned().collect(),
|
[(String::from("Left"), Some(&3)), (String::from("Right"), Some(&3))].iter().cloned().collect(),
|
||||||
[Some(&2), Some(&2)].iter().cloned().collect(),
|
[(String::from("Left"), Some(&2)), (String::from("Right"), Some(&2))].iter().cloned().collect(),
|
||||||
[Some(&1), Some(&1)].iter().cloned().collect(),
|
[(String::from("Left"), Some(&1)), (String::from("Right"), Some(&1))].iter().cloned().collect(),
|
||||||
];
|
];
|
||||||
|
|
||||||
assert_eq!(problem.solve_all(), solutions);
|
assert_eq!(problem.solve_all(), solutions);
|
||||||
@@ -315,8 +303,8 @@ mod tests {
|
|||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let solutions: Vec<Variables<i32>> = vec![
|
let solutions: Vec<Solution<i32, String>> = vec![
|
||||||
[Some(&2), Some(&2)].iter().cloned().collect(),
|
[(String::from("Left"), Some(&2)), (String::from("Right"), Some(&2))].iter().cloned().collect(),
|
||||||
];
|
];
|
||||||
|
|
||||||
assert_eq!(problem.solve_all(), solutions);
|
assert_eq!(problem.solve_all(), solutions);
|
||||||
|
|||||||
Reference in New Issue
Block a user