diff options
Diffstat (limited to 'impl/MaxStrategy.hpp')
-rw-r--r-- | impl/MaxStrategy.hpp | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/impl/MaxStrategy.hpp b/impl/MaxStrategy.hpp index 7a37fb2..e52fd99 100644 --- a/impl/MaxStrategy.hpp +++ b/impl/MaxStrategy.hpp @@ -2,39 +2,89 @@ #define MAX_STRATEGY_HPP #include "Expression.hpp" +#include "EquationSystem.hpp" #include "VariableAssignment.hpp" template<typename T> struct MaxStrategy { + MaxStrategy() + : _length(0), _assignment(NULL) { } MaxStrategy(unsigned int length) - : _length(length), _assignment(new unsigned int[length]) { } + : _length(length), _assignment(new unsigned int[length]) { + for (unsigned int i = 0; i < length; ++i) { + _assignment[i] = 0; + } + } + MaxStrategy(const MaxStrategy& other) + : _length(other._length), _assignment(new unsigned int[other._length]) { + for (unsigned int i = 0; i < other._length; ++i) { + _assignment[i] = other._assignment[i]; + } + } + + MaxStrategy<T>& operator=(const MaxStrategy& other) { + if (_length != other._length) { + _length = other._length; + delete[] _assignment; + _assignment = new unsigned int[_length]; + } + for (unsigned int i = 0; i < _length; ++i) { + _assignment[i] = other._assignment[i]; + } + return *this; + } + virtual ~MaxStrategy() { delete[] _assignment; } const unsigned int& operator[] (const MaxExpression<T> x) const { - if (x.id() < 0 || x.id() >= _length) { + if (x.id() >= _length) { throw "Array out of bounds"; } return _assignment[x.id()]; } unsigned int& operator[] (const MaxExpression<T>& x) { - if (x.id() < 0 || x.id() >= _length) { + if (x.id() >= _length) { throw "Array out of bounds"; } return _assignment[x.id()]; } - T operator() (const EquationSystem<T>& eqns, const VariableAssignment<T>& rho) const { - return eqns.foreach((*this), rho); + VariableAssignment<T> operator() (const EquationSystem<T>& eqns, const VariableAssignment<T>& rho) const { + return eqns.foreach(*this, rho); } T operator() (const Expression<T>& expr, const VariableAssignment<T>& rho) const { const MaxExpression<T>* max = dynamic_cast<const MaxExpression<T>*>(&expr); if (max == NULL) { return expr(rho); } else { - return (*expr.arguments()[_assignment[max->id()]])(rho); + return (*max->argument(_assignment[max->id()]))(rho); + } + } + MaxStrategy<T> improve(const EquationSystem<T>& s, const VariableAssignment<T>& rho) const { + MaxStrategy<T> newStrategy(*this); + for (unsigned int i = 0; i < _length; ++i) { + const MaxExpression<T>* expr = s.getMax(i); + const T oldValue = (*this)(*expr, rho); + std::pair<const T,unsigned int> best = expr->bestStrategy(rho); + if (best.first > oldValue) + newStrategy[*expr] = best.second; + } + std::cout << "Strat improvement: " << newStrategy[*s.getMax(0)] << std::endl; + return newStrategy; + } + + bool operator== (const MaxStrategy& other) const { + if (_length != other._length) + return false; + for (unsigned int i = 0; i < _length; ++i) { + if (_assignment[i] != other._assignment[i]) { + return false; + } } + return true; } - MaxStrategy<T> improve() { + bool operator!= (const MaxStrategy& other) const { + return !(*this == other); } private: unsigned int _length; |