#ifndef IMPROVEMENT_OPERATOR_HPP #define IMPROVEMENT_OPERATOR_HPP #include "IdSet.hpp" #include "MaxStrategy.hpp" template struct ImprovementOperator { virtual ~ImprovementOperator() { } virtual bool improve( EquationSystem& system, ConcreteMaxStrategy& strat, const VariableAssignment& rho, IdSet >& changedIn, IdSet >& changedOut ) const = 0; }; template struct NaiveImprovementOperator : public ImprovementOperator { bool improve( EquationSystem& system, ConcreteMaxStrategy& strat, const VariableAssignment& rho, IdSet >&, IdSet >& ) const { bool changed = false; for (unsigned int i = 0, length = system.maxExpressionCount(); i < length; ++i) { MaxExpression& expr = system.maxExpression(i); // this relies on the fact that an expression will only be proessed after the expressions // it depends on (which should always be true, as they form a DAG) unsigned int index = expr.bestStrategy(rho, strat); if (index != strat.get(expr)) { changed = true; strat.set(expr, index); } } //log::strategy << strat; return changed; } }; template struct RepeatedImprovementOperator : public ImprovementOperator { RepeatedImprovementOperator(const ImprovementOperator& op) : _subImprovement(op) { } bool improve( EquationSystem& system, ConcreteMaxStrategy& strat, const VariableAssignment& rho, IdSet >& changedIn, IdSet >& changedOut ) const { if (_subImprovement.improve(system, strat, rho, changedIn, changedOut)) { VariableAssignment* rho2 = system.eval(rho, strat); improve(system, strat, *rho2, changedIn, changedOut); delete rho2; return true; } return false; } private: const ImprovementOperator& _subImprovement; }; template struct SmartImprovementOperator : public ImprovementOperator { SmartImprovementOperator(const EquationSystem& system) : _system(system), _influence(system.maxExpressionCount(), IdSet >(system.maxExpressionCount())), _first_run(true) { } struct DynamicMaxStrategy : public MaxStrategy { DynamicMaxStrategy( const EquationSystem& system, ConcreteMaxStrategy& strat, IdMap,IdSet>>& influence, const VariableAssignment& rho, IdSet>& stable, IdSet>& changed ) : _system(system), _rho(rho), _values(strat), _stable(stable), _changed(changed), _influence(influence) { } unsigned int get(const MaxExpression& e) const { solve(e); return _values.get(e); } private: void solve(const MaxExpression& x) const { if (!_stable.contains(x)) { _stable.insert(x); unsigned int val = x.bestStrategy(DependencyAssignment(*this, _rho, x), DependencyStrategy(*this, x)); if (val != _values.get(x)) { auto oldInfluence = _influence[x]; _influence[x].clear(); _values.set(x, val); _changed.insert(x); _stable.filter(oldInfluence); for (auto it = oldInfluence.begin(); it != oldInfluence.end(); ++it) { solve(_system.maxExpression(*it)); } _influence[x] = oldInfluence; } } } struct DependencyAssignment : public VariableAssignment{ DependencyAssignment(const DynamicMaxStrategy& strat, const VariableAssignment& rho, const MaxExpression& expr) : _strat(strat), _rho(rho), _expr(expr) { } const Domain& operator[](const Variable& var) const { _strat._influence[*_strat._system[var]].insert(_expr); return _rho[var]; } private: const DynamicMaxStrategy& _strat; const VariableAssignment& _rho; const MaxExpression& _expr; }; struct DependencyStrategy : public MaxStrategy { DependencyStrategy(const DynamicMaxStrategy& strat, const MaxExpression& expr) : _strat(strat), _expr(expr) { } unsigned int get(const MaxExpression& e) const { _strat.solve(e); _strat._influence[e].insert(_expr); return _strat._values.get(e); } private: const DynamicMaxStrategy& _strat; const MaxExpression& _expr; }; private: const EquationSystem& _system; const VariableAssignment& _rho; ConcreteMaxStrategy& _values; IdSet>& _stable; IdSet>& _changed; IdMap,IdSet>>& _influence; }; void invalidateSubexpressions(IdSet>& set, Expression& expr) const { MaxExpression* maxExpr = static_cast*>(&expr); if (maxExpr != NULL && maxExpr->id() < _system.maxExpressionCount()) { invalidateAll(set, *maxExpr); for (auto it = maxExpr->arguments().begin(), end = maxExpr->arguments().end(); it != end; ++it) { invalidateSubexpressions(set, **it); } } } void invalidateAll(IdSet>& set, MaxExpression& expr) const { if (!set.contains(expr)) { set.insert(expr); for (auto it = _influence[expr].begin(), end = _influence[expr].end(); it != end; ++it) { invalidateSubexpressions(set, _system.maxExpression(*it)); } } } void markChanged(IdSet>& set, MaxExpression& expr, IdSet>& seen) const { if (seen.contains(expr)) return; seen.insert(expr); auto var = _system.varFromExpr(expr); if (var == NULL || !set.contains(*var)) { if (var != NULL) { set.insert(*var); } for (auto it = _influence[expr].begin(), end = _influence[expr].end(); it != end; ++it) { markChanged(set, _system.maxExpression(*it), seen); } } } bool improve( EquationSystem&, ConcreteMaxStrategy& stratOut, const VariableAssignment& rho, IdSet >& changedIn, IdSet >& changedOut ) const { IdSet> invalidSet(_system.maxExpressionCount()); IdSet> changedSet(_system.maxExpressionCount()); if (_first_run) { _first_run = false; } else { for (auto it = changedIn.begin(), end = changedIn.end(); it != end; ++it) { invalidateSubexpressions(invalidSet, *_system[_system.variable(*it)]); } invalidSet.invert(); } log::debug << "stable: " << invalidSet; log::debug << "infl: " << _influence; DynamicMaxStrategy strat(_system, stratOut, _influence, rho, invalidSet, changedSet); auto inv = invalidSet; for (unsigned int i = 0, length = _system.maxExpressionCount(); i < length; ++i) { strat.get(_system.maxExpression(i)); } IdSet> seen; for (auto it = changedSet.begin(), end = changedSet.end(); it != end; ++it) { markChanged(changedOut, _system.maxExpression(*it), seen); } return changedOut.begin() != changedOut.end(); // true iff changedOut is non-empty } private: const EquationSystem& _system; mutable IdMap,IdSet>> _influence; mutable bool _first_run; }; #endif