From c065ae2bd1176b17d137e0f52df6ef1d9af9e757 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 26 Oct 2012 16:29:52 +1100 Subject: Try to make the correct solver into a local solver As far as I can tell, it's worked! Hooray! --- impl/MaxStrategy.hpp | 96 ++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 44 deletions(-) (limited to 'impl/MaxStrategy.hpp') 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 struct MaxStrategy { virtual ~MaxStrategy() { } - virtual unsigned int get(const MaxExpression& e) const = 0; - virtual unsigned int get(const MaxExpression& e) { - return const_cast(this)->get(e); - } + virtual unsigned int get(const MaxExpression& e) = 0; }; unsigned int stack_depth = 1; @@ -61,10 +58,6 @@ struct DynamicMaxStrategy : public MaxStrategy { _rho = ρ } - unsigned int get(const MaxExpression& e) const { - return _values[e]; - } - unsigned int get(const MaxExpression& 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 >::iterator @@ -132,16 +132,25 @@ private: const MaxExpression& expr) : _strat(strat), _rho(rho), - _expr(expr) { + _expr(expr), + _current(strat._system.variableCount()) { } - const Domain& operator[](const Variable& var) const { + const Domain operator[](const Variable& 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& _rho; const MaxExpression& _expr; + IdSet > _current; }; struct DependencyStrategy : public MaxStrategy { @@ -149,7 +158,7 @@ private: : _strat(strat), _expr(expr) { } - unsigned int get(const MaxExpression& e) const { + unsigned int get(const MaxExpression& e) { _strat.solve(e); if (&_expr != &e) { _strat._influence[e].insert(_expr); @@ -172,40 +181,39 @@ private: }; -template -IdMap,T> solve_for(const EquationSystem& system) { - IdMap,T> result(system.variableCount(), infinity()); +template +struct Solver { + Solver(const EquationSystem& system) + : _system(system), + _strategy(system), + _rho(_system, _strategy, -infinity()) { + _strategy.setRho(_rho); + } - DynamicMaxStrategy strategy(system); - DynamicVariableAssignment rho(system, strategy, -infinity()); - strategy.setRho(rho); + Domain solve(Variable& var) { + MaxExpression& 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& 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& _system; + DynamicMaxStrategy _strategy; + DynamicVariableAssignment _rho; +}; /*template -- cgit v1.2.3