Skip to content

Commit

Permalink
Solver now depends on Event instead of Event being an associated type…
Browse files Browse the repository at this point in the history
… of VariableT.
  • Loading branch information
ptal committed Apr 12, 2015
1 parent 7e379b6 commit be05cf0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
24 changes: 13 additions & 11 deletions src/libmcp/solver/propagator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,31 @@ impl<Var> DeepClone<Vec<Rc<RefCell<Var>>>> for Rc<RefCell<Var>> where
}
}

pub trait BoxedDeepClone<V: VariableT>
pub trait BoxedDeepClone<E: EventIndex, V: VariableT>
{
fn boxed_deep_clone(&self, state: &Vec<Rc<RefCell<V>>>) -> Box<PropagatorErasure<V>>;
fn boxed_deep_clone(&self, state: &Vec<Rc<RefCell<V>>>) -> Box<PropagatorErasure<E, V>>;
}

impl<R, V> BoxedDeepClone<V> for R where
impl<E, R, V> BoxedDeepClone<E, V> for R where
E: EventIndex,
R: DeepClone<Vec<Rc<RefCell<V>>>>,
R: Propagator<<V as VariableT>::Event>,
R: Propagator<E>,
R: 'static,
V: VariableT
{
fn boxed_deep_clone(&self, state: &Vec<Rc<RefCell<V>>>) -> Box<PropagatorErasure<V>> {
fn boxed_deep_clone(&self, state: &Vec<Rc<RefCell<V>>>) -> Box<PropagatorErasure<E, V>> {
Box::new(self.deep_clone(state))
}
}

pub trait PropagatorErasure<V: VariableT>:
Propagator<<V as VariableT>::Event>
+ BoxedDeepClone<V>
pub trait PropagatorErasure<E: EventIndex, V: VariableT>:
Propagator<E>
+ BoxedDeepClone<E, V>
{}

impl<
E: EventIndex,
V: VariableT,
R: Propagator<<V as VariableT>::Event>
+ BoxedDeepClone<V>
> PropagatorErasure<V> for R {}
R: Propagator<E>
+ BoxedDeepClone<E, V>
> PropagatorErasure<E, V> for R {}
38 changes: 21 additions & 17 deletions src/libmcp/solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,32 @@ use std::cell::RefCell;
use std::fmt::{Formatter, Display, Error};
use std::result::fold;

pub struct Solver<V: VariableT, D, A> {
propagators: Vec<Box<PropagatorErasure<V> + 'static>>,
pub struct Solver<E: EventIndex, V: VariableT, D, A> {
propagators: Vec<Box<PropagatorErasure<E, V> + 'static>>,
variables: Vec<Rc<RefCell<V>>>,
deps: D,
agenda: A
}

impl<V, D, A> Space for Solver<V, D, A> where
impl<E, V, D, A> Space for Solver<E, V, D, A> where
E: EventIndex,
V: VariableT+Clone,
D: VarEventDependencies,
A: Agenda
{
type Constraint = Box<PropagatorErasure<V> + 'static>;
type Constraint = Box<PropagatorErasure<E, V> + 'static>;
type Variable = Rc<RefCell<V>>;
type Domain = <V as VariableT>::Domain;
type Label = Solver<V, D, A>;
type Label = Solver<E, V, D, A>;

fn newvar(&mut self, dom: <Solver<V, D, A> as Space>::Domain) ->
<Solver<V, D, A> as Space>::Variable {
fn newvar(&mut self, dom: <Solver<E, V, D, A> as Space>::Domain) ->
<Solver<E, V, D, A> as Space>::Variable {
let var_idx = self.variables.len();
self.variables.push(Rc::new(RefCell::new(VariableT::new(var_idx, dom))));
self.variables[var_idx].clone()
}

fn add(&mut self, p: <Solver<V, D, A> as Space>::Constraint) {
fn add(&mut self, p: <Solver<E, V, D, A> as Space>::Constraint) {
self.propagators.push(p);
}

Expand All @@ -57,7 +58,7 @@ impl<V, D, A> Space for Solver<V, D, A> where
self.propagation_loop()
}

fn mark(&self) -> <Solver<V, D, A> as Space>::Label {
fn mark(&self) -> <Solver<E, V, D, A> as Space>::Label {
let mut solver = Solver::new();
solver.variables = self.variables.iter()
.map(|v| Rc::new(RefCell::new(v.borrow().clone())))
Expand All @@ -68,18 +69,19 @@ impl<V, D, A> Space for Solver<V, D, A> where
solver
}

fn goto(&self, label: <Solver<V, D, A> as Space>::Label) -> Self
fn goto(&self, label: <Solver<E, V, D, A> as Space>::Label) -> Self
{
label
}
}

impl<V, D, A> Solver<V, D, A> where
impl<E, V, D, A> Solver<E, V, D, A> where
E: EventIndex,
V: VariableT+Clone,
D: VarEventDependencies,
A: Agenda
{
pub fn new() -> Solver<V, D, A> {
pub fn new() -> Solver<E, V, D, A> {
Solver {
propagators: vec![],
variables: vec![],
Expand All @@ -94,7 +96,7 @@ impl<V, D, A> Solver<V, D, A> where
}

fn init_deps(&mut self) {
self.deps = VarEventDependencies::new(self.variables.len(), <<V as VariableT>::Event as EventIndex>::size());
self.deps = VarEventDependencies::new(self.variables.len(), <E as EventIndex>::size());
for (p_idx, p) in self.propagators.iter().enumerate() {
let p_deps = p.dependencies();
for (v, ev) in p_deps.into_iter() {
Expand Down Expand Up @@ -141,13 +143,13 @@ impl<V, D, A> Solver<V, D, A> where
true
}

fn reschedule_prop(&mut self, events: &Vec<(usize, <V as VariableT>::Event)>, p_idx: usize) {
fn reschedule_prop(&mut self, events: &Vec<(usize, E)>, p_idx: usize) {
if !events.is_empty() {
self.agenda.schedule(p_idx);
}
}

fn react(&mut self, events: Vec<(usize, <V as VariableT>::Event)>) {
fn react(&mut self, events: Vec<(usize, E)>) {
for (v, ev) in events.into_iter() {
let reactions = self.deps.react(v, ev);
for p in reactions.into_iter() {
Expand All @@ -165,7 +167,8 @@ impl<V, D, A> Solver<V, D, A> where
}
}

impl<V, D, A> Display for Solver<V, D, A> where
impl<E, V, D, A> Display for Solver<E, V, D, A> where
E: EventIndex,
V: VariableT+Display
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
Expand All @@ -184,11 +187,12 @@ mod test {
use interval::ops::*;
use solver::variable::*;
use solver::space::*;
use solver::fd::event::*;
use solver::fd::propagator::*;
use solver::agenda::RelaxedFifoAgenda;
use solver::dependencies::VarEventDepsVector;

type FDSolver = Solver<Variable<Interval<i32>>, VarEventDepsVector, RelaxedFifoAgenda>;
type FDSolver = Solver<FDEvent, Variable<Interval<i32>>, VarEventDepsVector, RelaxedFifoAgenda>;

#[test]
fn basic_test() {
Expand Down
5 changes: 0 additions & 5 deletions src/libmcp/solver/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
// limitations under the License.

use solver::event::*;
use solver::fd::event::*;
use interval::ncollections::ops::*;
use interval::ops::*;
use std::rc::Rc;
use std::cell::RefCell;

use std::fmt::{Formatter, Display, Error};

Expand All @@ -26,7 +23,6 @@ use std::fmt::{Formatter, Display, Error};
pub trait VariableT
{
type Domain;
type Event: EventIndex;

fn new(id: usize, dom: Self::Domain) -> Self;
}
Expand All @@ -52,7 +48,6 @@ impl<Domain> VariableT for Variable<Domain> where
Domain: Cardinality
{
type Domain = Domain;
type Event = FDEvent;

fn new(idx: usize, dom: Domain) -> Variable<Domain> {
assert!(!dom.is_empty());
Expand Down

0 comments on commit be05cf0

Please sign in to comment.