Trying to organize it all :$
This commit is contained in:
@@ -5,28 +5,61 @@ extern crate planner;
|
||||
|
||||
use self::cookbook::*;
|
||||
use self::cookbook::models::Recipe;
|
||||
use self::planner::solver::{Domain, Problem, Constraint, Variables};
|
||||
use self::planner::solver::{Variables, Domain, Problem, Constraint};
|
||||
|
||||
/// We want a mapping of the week meals (matin, midi, soir)
|
||||
/// Breakfast => RecipeCategory::Breakfast
|
||||
/// Lunch => RecipeCategory::MainCourse
|
||||
/// Dinner => RecipeCategory::MainCourse
|
||||
type Day = String;
|
||||
|
||||
enum Meals {
|
||||
Breakfast(Day),
|
||||
Lunch(Day),
|
||||
Dinner(Day)
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
/// associated with filters for Domain, depending on recipes RecipeCategory
|
||||
enum RecipeCategory {
|
||||
Breakfast,
|
||||
MainCourse
|
||||
}
|
||||
/// 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 &["Lundi", "Mardi", "Mercredi"] {
|
||||
vars.push((Meals::Lunch(String::from(day)).into(), domain, None));
|
||||
vars.push((Meals::Dinner(String::from(day)).into(), domain, None));
|
||||
}
|
||||
vars
|
||||
}
|
||||
|
||||
fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool {
|
||||
assign.get("Lundi_midi").unwrap().unwrap().ingredients.contains("Patates")
|
||||
&& assign.get("Mardi_midi").unwrap().unwrap().ingredients.contains("Patates")
|
||||
assign.get("Lundi_Lunch").unwrap().unwrap().ingredients.contains("Patates")
|
||||
&& !assign.get("Mardi_Lunch").unwrap().unwrap().ingredients.contains("Patates")
|
||||
}
|
||||
|
||||
fn get_planning_all_results() -> String {
|
||||
let conn = establish_connection();
|
||||
let possible_values = recipes::load_all(&conn);
|
||||
let domain = Domain::new(possible_values);
|
||||
let mut problem = Problem::build()
|
||||
.add_variable("Lundi_midi".to_string(), &domain, None)
|
||||
.add_variable("Mardi_midi".to_string(), &domain, None)
|
||||
.add_constraint(ingredients_contains)
|
||||
.finish();
|
||||
let mut problem = Problem::build();
|
||||
for (var, dom, ini) in generate_variables(&domain) {
|
||||
problem = problem.add_variable(var, dom, ini);
|
||||
}
|
||||
let mut problem = problem.add_constraint(ingredients_contains)
|
||||
.finish();
|
||||
let results = problem.solve_all();
|
||||
format!("{:#?}\nTotal = {}", &results, results.len())
|
||||
format!("{:#?}\nTotal = {}", &results.first(), results.len())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user