work on planner templates in progress

This commit is contained in:
2019-02-07 21:58:38 +01:00
parent f220c6c960
commit 29b2f076bf
10 changed files with 209 additions and 108 deletions

View File

@@ -7,6 +7,7 @@ use self::cookbook::*;
use self::cookbook::recipes::Recipe;
use self::planner::solver::{Variables, Domain, Problem};
type DomainValues<'a, V> = Vec<&'a V>;
/// We want a mapping of the week meals (matin, midi, soir)
/// Breakfast => RecipeCategory::Breakfast
/// Lunch => RecipeCategory::MainCourse
@@ -14,52 +15,87 @@ use self::planner::solver::{Variables, Domain, Problem};
mod template {
//! Exports the [`Template`] struct.
use super::{Domain, DomainValues, Variables, Recipe};
type Day = String;
const DAYS: &[&str] = &["Lundi", "Mardi", "Mercredi"];
const DAYS: &[&str] = &[
"Lundi", "Mardi", "Mercredi",
"Jeudi", "Vendredi", "Samedi",
"Dimanche"];
/// An enum to discriminate every meals
#[allow(dead_code)]
enum Meals {
pub enum Meals {
Breakfast(Day),
Lunch(Day),
Dinner(Day)
}
impl From<Meals> for String {
fn from(item: Meals) -> String {
match item {
Meals::Breakfast(d) => format!("{}_Breakfast", d),
Meals::Lunch(d) => format!("{}_Lunch", d),
Meals::Dinner(d) => format!("{}_Dinner", d),
}
}
}
/// A fixed template of meals
///
/// TODO: mask -> disabled choosen meals
/// initials -> fixed values
pub struct Template;
impl Template {
fn empty() {
fn keys() -> Vec<Meals> {
let mut keys = Vec::new();
for day in DAYS {
for meal in &[Meals::Breakfast, Meals::Lunch, Meals::Dinner] {
keys.push(meal(day.to_string()))
}
}
keys
}
/// Build a [`Template`] from a variables assignment map,
/// usually a solution returned by solver
pub(super) fn from_variables<'a, V>(_vars: Variables<'a,V>) -> Template{
Template
}
/// Builds a vector of variables, to be used with
/// [`ProblemBuilder`].
pub(super) fn generate_variables(domain: &Domain<Recipe>)
-> Vec<(String, DomainValues<Recipe>, Option<&Recipe>)>
{
use cookbook::recipes::RecipeCategory;
let mut vars = Vec::new();
for key in Self::keys().into_iter() {
//TODO: Use key variants to set filters on domain
//TODO: Initial values
let _filter: fn(&&Recipe) -> bool = match key {
Meals::Breakfast(_) => |r: &&Recipe| {
r.category == RecipeCategory::Breakfast // Breakfast
},
Meals::Lunch(_) => |r: &&Recipe| {
r.category as i16 == 2i16 // MainCourse
},
Meals::Dinner(_) => |r: &&Recipe| {
r.category as i16 == 1i16 // Starter
}
};
vars.push((key.into(), domain.filter(_filter), None));
}
vars
}
}
}
impl Into<String> for Meals {
fn into(self) -> String {
match self {
Meals::Breakfast(d) => format!("{}_Breakfast", d),
Meals::Lunch(d) => format!("{}_Lunch", d),
Meals::Dinner(d) => format!("{}_Dinner", d),
}
}
}
/// It may also contains an initial value for each variable
fn generate_variables<V>(domain: &Domain<V>) -> Vec<(String, &Domain<V>, Option<&V>)> {
let mut vars = Vec::new();
for day in DAYS {
vars.push((Meals::Lunch(day.to_string()).into(), domain, None));
vars.push((Meals::Dinner(day.to_string()).into(), domain, None));
}
vars
}
fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool {
let id = 0;
assign.get("Lundi_Lunch").unwrap().unwrap().ingredients.contains(&id)
&& !assign.get("Mardi_Lunch").unwrap().unwrap().ingredients.contains(&id)
fn ingredients_contains<'a>(_assign: &Variables<'a,Recipe>) -> bool {
true
}
@@ -80,7 +116,7 @@ fn get_planning_all_results() -> String {
let possible_values = recipes::load_all(&conn);
let domain = Domain::new(possible_values);
let mut problem = Problem::build();
for (var, dom, ini) in generate_variables(&domain) {
for (var, dom, ini) in template::Template::generate_variables(&domain) {
problem = problem.add_variable(var, dom, ini);
}
let mut problem = problem

View File

@@ -14,7 +14,7 @@ enum Assignment<'a, V> {
}
type Domains<'a, V> = HashMap<String, &'a Domain<V>>;
pub type DomainValues<'a, V> = Vec<&'a V>;
/// The domain of values that can be assigned to variables
#[derive(Clone)]
pub struct Domain<V> {
@@ -26,10 +26,11 @@ impl<V> Domain<V> {
Domain { values }
}
pub fn values(&self) -> &Vec<V> {
&self.values
/// Returns all values of a Domain instance
pub fn all(&self) -> DomainValues<V> {
self.values.iter().collect()
}
/// Returns a new domain with the given filter applied to inner values
/// Returns a Filter filter applied to inner values
///
/// # Examples
///
@@ -40,12 +41,10 @@ impl<V> Domain<V> {
/// fn even(i: &i32) -> bool { i % 2 == 0 };
/// assert_eq!(&domain.filter(even).values, &vec![2]);
/// ```
pub fn filter(&self, f: fn(&V) -> bool) -> Domain<V>
pub fn filter(&self, f: fn(&&V) -> bool) -> DomainValues<V>
where V: std::clone::Clone
{
Domain {
values: self.values.iter().cloned().filter(f).collect(),
}
self.values.iter().filter(f).collect()
}
}
@@ -62,7 +61,7 @@ pub struct Problem<'a, V> {
/// The initial assignements map
variables: Variables<'a, V>,
/// Each variable has its associated domain
domains: Domains<'a,V>,
domains: HashMap<String, DomainValues<'a,V>>,
/// Set of constraints to validate
constraints: Vec<Constraint<'a,V>>,
}
@@ -78,14 +77,14 @@ impl<'a,V> Problem<'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 !");
let domain_values = 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 !"); }
if domain_values.is_empty() { panic!("No value in domain !"); }
// TODO: should be able to filter domain values (inference, pertinence)
for value in domain.values.iter() {
updates.push(Assignment::Update(key.clone(), value));
for value in domain_values.into_iter() {
updates.push(Assignment::Update(key.clone(), *value));
}
Some(updates)
} else { // End of assignements
@@ -147,7 +146,7 @@ impl<'a, V> ProblemBuilder<'a, V> {
})
}
pub fn add_variable<S>(mut self, name: S, domain: &'a Domain<V>, value: Option<&'a V>) -> Self
pub fn add_variable<S>(mut self, name: S, domain: Vec<&'a V>, value: Option<&'a V>) -> Self
where S: Into<String>
{
let name = name.into();
@@ -174,8 +173,8 @@ mod tests {
use super::*;
let domain = Domain::new(vec![1,2,3]);
let mut problem: Problem<_> = Problem::build()
.add_variable(String::from("Left"), &domain, None)
.add_variable(String::from("Right"), &domain, None)
.add_variable(String::from("Left"), domain.all(), None)
.add_variable(String::from("Right"), domain.all(), None)
.add_constraint(|assign: &Variables<i32>| {
assign.get("Left").unwrap() == assign.get("Right").unwrap()
})
@@ -195,8 +194,8 @@ mod tests {
use super::*;
let domain = Domain::new(vec![1,2,3]);
let mut problem: Problem<_> = Problem::build()
.add_variable("Left".to_string(), &domain, None)
.add_variable("Right".to_string(), &domain, Some(&2))
.add_variable("Left".to_string(), domain.all(), None)
.add_variable("Right".to_string(), domain.all(), Some(&2))
.add_constraint( |assign: &Variables<i32>| {
assign.get("Left").unwrap() == assign.get("Right").unwrap()
})