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

@@ -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()
})