Works on ingredients

This commit is contained in:
2019-02-03 15:17:43 +01:00
parent 28afe8ece0
commit d3259c82b3
10 changed files with 151 additions and 61 deletions

View File

@@ -14,6 +14,7 @@ use self::planner::solver::{Variables, Domain, Problem};
type Day = String;
const DAYS: &[&str] = &["Lundi", "Mardi", "Mercredi"];
#[allow(dead_code)]
enum Meals {
Breakfast(Day),
Lunch(Day),
@@ -41,8 +42,9 @@ fn generate_variables<V>(domain: &Domain<V>) -> Vec<(String, &Domain<V>, Option<
}
fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool {
assign.get("Lundi_Lunch").unwrap().unwrap().ingredients.contains("Patates")
&& !assign.get("Mardi_Lunch").unwrap().unwrap().ingredients.contains("Patates")
let id = 0;
assign.get("Lundi_Lunch").unwrap().unwrap().ingredients.contains(&id)
&& !assign.get("Mardi_Lunch").unwrap().unwrap().ingredients.contains(&id)
}

View File

@@ -13,6 +13,7 @@ enum Assignment<'a, V> {
Clear(String)
}
type Domains<'a, V> = HashMap<String, &'a Domain<V>>;
/// The domain of values that can be assigned to variables
#[derive(Clone)]
@@ -71,10 +72,11 @@ impl<'a,V> Problem<'a, V> {
/// Returns all possible Updates for next assignements, prepended with
/// a Clear to ensure the variable is unset before when leaving the branch.
fn _assign_next(&self) -> Option<Vec<Assignment<'a,V>>> {
fn _push_updates(&self) -> Option<Vec<Assignment<'a,V>>> {
// TODO: should be able to inject a choosing strategy
if let Some((key,_)) = self.variables.iter().find(|(_, val)| val.is_none()) {
let domain = self.domains.get(key).expect("No domain for variable !");
// Push a clear assignment first, just before going up the stack.
let mut updates = vec![Assignment::Clear(key.clone())];
if domain.values.is_empty() { panic!("No value in domain !"); }
@@ -96,13 +98,13 @@ impl<'a,V> Problem<'a, V> {
return true;
}
/// Visit all possible solutions, using a stack.
/// Visit all possible solutions, using a stack (DFS).
pub fn solve_all(&mut self) -> Vec<Variables<'a,V>>
where V: Clone + fmt::Debug
{
let mut solutions: Vec<Variables<V>> = vec![];
let mut stack: Vec<Assignment<'a, V>> = vec![];
stack.append(&mut self._assign_next().unwrap());
stack.append(&mut self._push_updates().unwrap());
loop {
let node = stack.pop();
if node.is_none() { break; };
@@ -111,7 +113,7 @@ impl<'a,V> Problem<'a, V> {
// Assign the variable and open new branches, if any.
*self.variables.get_mut(&key).unwrap() = Some(val);
// TODO: handle case of empty domain.values
if let Some(mut nodes) = self._assign_next() {
if let Some(mut nodes) = self._push_updates() {
stack.append(&mut nodes);
} else {
// Assignements are completed