Adds basic filter on Domain

This commit is contained in:
2019-01-30 15:13:36 +01:00
parent 5e92e30f51
commit 253045c742
2 changed files with 26 additions and 6 deletions

View File

@@ -5,7 +5,11 @@ extern crate planner;
use self::cookbook::*; use self::cookbook::*;
use self::cookbook::models::Recipe; use self::cookbook::models::Recipe;
use self::planner::solver::{Domain, Problem, Variables}; use self::planner::solver::{Domain, Problem, Constraint, Variables};
/// We want a mapping of the week meals (matin, midi, soir)
/// associated with filters for Domain, depending on recipes RecipeCategory
/// It may also contains an initial value for each variable
fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool { fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool {
assign.get("Lundi_midi").unwrap().unwrap().ingredients.contains("Patates") assign.get("Lundi_midi").unwrap().unwrap().ingredients.contains("Patates")
@@ -16,18 +20,15 @@ fn get_planning_all_results() -> String {
let conn = establish_connection(); let conn = establish_connection();
let possible_values = recipes::load_all(&conn); let possible_values = recipes::load_all(&conn);
let domain = Domain::new(possible_values); let domain = Domain::new(possible_values);
let mut problem = Problem::build() let mut problem = Problem::build()
.add_variable("Lundi_midi".to_string(), &domain, None) .add_variable("Lundi_midi".to_string(), &domain, None)
.add_variable("Mardi_midi".to_string(), &domain, None) .add_variable("Mardi_midi".to_string(), &domain, None)
.add_constraint(ingredients_contains) .add_constraint(ingredients_contains)
.finish(); .finish();
let results = problem.solve_all(); let results = problem.solve_all();
format!("{:#?}\nTotal = {}", &results, results.len()) format!("{:#?}\nTotal = {}", &results, results.len())
} }
fn main() { fn main() {
println!("{}", get_planning_all_results()); println!("{}", get_planning_all_results());
} }

View File

@@ -17,13 +17,32 @@ enum Assignment<'a, V> {
/// The domain of values that can be assigned to variables /// The domain of values that can be assigned to variables
#[derive(Clone)] #[derive(Clone)]
pub struct Domain<V> { pub struct Domain<V> {
values: Vec<V> pub values: Vec<V>
} }
impl<V> Domain<V> { impl<V> Domain<V> {
pub fn new(values: Vec<V>) -> Domain<V> { pub fn new(values: Vec<V>) -> Domain<V> {
Domain { values } Domain { values }
} }
/// Returns a new domain with the given filter applied to inner values
///
/// # Examples
///
/// ```
/// # extern crate planner;
/// # use planner::solver::Domain;
/// let domain = Domain::new(vec![1,2,3]);
/// 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>
where V: std::clone::Clone
{
Domain {
values: self.values.iter().cloned().filter(f).collect(),
}
}
} }
impl<V: fmt::Debug> fmt::Debug for Domain<V> { impl<V: fmt::Debug> fmt::Debug for Domain<V> {
@@ -32,7 +51,7 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
} }
} }
type Constraint<'a,V> = fn(&Variables<'a,V>) -> bool; pub type Constraint<'a,V> = fn(&Variables<'a,V>) -> bool;
pub struct Problem<'a, V> { pub struct Problem<'a, V> {
/// The initial assignements map /// The initial assignements map