diff options
Diffstat (limited to 'impl/MaxStrategy.hpp')
-rw-r--r-- | impl/MaxStrategy.hpp | 96 |
1 files changed, 52 insertions, 44 deletions
diff --git a/impl/MaxStrategy.hpp b/impl/MaxStrategy.hpp index f4026dd..d908c7b 100644 --- a/impl/MaxStrategy.hpp +++ b/impl/MaxStrategy.hpp @@ -9,10 +9,7 @@ template<typename Domain> struct MaxStrategy { virtual ~MaxStrategy() { } - virtual unsigned int get(const MaxExpression<Domain>& e) const = 0; - virtual unsigned int get(const MaxExpression<Domain>& e) { - return const_cast<const MaxStrategy*>(this)->get(e); - } + virtual unsigned int get(const MaxExpression<Domain>& e) = 0; }; unsigned int stack_depth = 1; @@ -61,10 +58,6 @@ struct DynamicMaxStrategy : public MaxStrategy<Domain> { _rho = ρ } - unsigned int get(const MaxExpression<Domain>& e) const { - return _values[e]; - } - unsigned int get(const MaxExpression<Domain>& e) { if (!_frozen) solve(e); @@ -93,9 +86,10 @@ private: _stable.insert(x); log::strategy << indent() << "Stabilise " << x << std::endl; - stack_depth++; - unsigned int val = x.bestStrategy(DependencyAssignment(*this, *_rho, x), - DependencyStrategy(*this, x)); + stack_depth++; + DependencyAssignment assignment(*this, *_rho, x); + DependencyStrategy depStrat(*this, x); + unsigned int val = x.bestStrategy(assignment, depStrat); stack_depth--; if (val != _values[x]) { @@ -107,6 +101,12 @@ private: _rho->invalidate(*_system.varFromExpr(x)); + _rho->thaw(); + this->freeze(); + _rho->stabilise(); + _rho->freeze(); + this->thaw(); + _stable.filter(oldInfluence); for (typename IdSet<MaxExpression<Domain> >::iterator @@ -132,16 +132,25 @@ private: const MaxExpression<Domain>& expr) : _strat(strat), _rho(rho), - _expr(expr) { + _expr(expr), + _current(strat._system.variableCount()) { } - const Domain& operator[](const Variable<Domain>& var) const { + const Domain operator[](const Variable<Domain>& var) { _strat._var_influence[var].insert(_expr); - return _rho[var]; + if (_current.contains(var)) { + return _rho[var]; + } else { + _current.insert(var); + Domain val = _strat._system[var]->eval(_rho, _strat); + _current.remove(var); + return val; + } } private: DynamicMaxStrategy& _strat; VariableAssignment<Domain>& _rho; const MaxExpression<Domain>& _expr; + IdSet<Variable<Domain> > _current; }; struct DependencyStrategy : public MaxStrategy<Domain> { @@ -149,7 +158,7 @@ private: : _strat(strat), _expr(expr) { } - unsigned int get(const MaxExpression<Domain>& e) const { + unsigned int get(const MaxExpression<Domain>& e) { _strat.solve(e); if (&_expr != &e) { _strat._influence[e].insert(_expr); @@ -172,40 +181,39 @@ private: }; -template<typename T> -IdMap<Variable<T>,T> solve_for(const EquationSystem<T>& system) { - IdMap<Variable<T>,T> result(system.variableCount(), infinity<T>()); +template<typename Domain> +struct Solver { + Solver(const EquationSystem<Domain>& system) + : _system(system), + _strategy(system), + _rho(_system, _strategy, -infinity<Domain>()) { + _strategy.setRho(_rho); + } - DynamicMaxStrategy<T> strategy(system); - DynamicVariableAssignment<T> rho(system, strategy, -infinity<T>()); - strategy.setRho(rho); + Domain solve(Variable<Domain>& var) { + MaxExpression<Domain>& rhs = *_system[var]; - bool changed; - do { - changed = false; + do { + _rho.has_changed(false); - // improve strategy - rho.freeze(); - strategy.thaw(); - for (unsigned int i = 0; i < system.variableCount(); ++i) { - strategy.get(*system[system.variable(i)]); - } + // improve strategy + _rho.freeze(); + _strategy.thaw(); + _strategy.get(rhs); - // iterate fixpoint - strategy.freeze(); - rho.thaw(); - for (unsigned int i = 0; i < system.variableCount(); ++i) { - Variable<T>& var = system.variable(i); - T val = rho[var]; - if (result[var] != val) { - result[var] = val; - changed = true; - } - } - } while(changed); + // iterate fixpoint + _strategy.freeze(); + _rho.thaw(); + _rho.stabilise(); + } while (_rho.has_changed()); - return result; -} + return _rho[var]; + } +private: + const EquationSystem<Domain>& _system; + DynamicMaxStrategy<Domain> _strategy; + DynamicVariableAssignment<Domain> _rho; +}; /*template<typename Domain> |