From e00c1e3f71bb1840e10a85af53469a81c73c7ef1 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 30 Apr 2012 21:17:07 +1000 Subject: Functional algorithm. Unoptimised. --- impl/MaxStrategy.hpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) (limited to 'impl/MaxStrategy.hpp') 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 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& 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 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& 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& eqns, const VariableAssignment& rho) const { - return eqns.foreach((*this), rho); + VariableAssignment operator() (const EquationSystem& eqns, const VariableAssignment& rho) const { + return eqns.foreach(*this, rho); } T operator() (const Expression& expr, const VariableAssignment& rho) const { const MaxExpression* max = dynamic_cast*>(&expr); if (max == NULL) { return expr(rho); } else { - return (*expr.arguments()[_assignment[max->id()]])(rho); + return (*max->argument(_assignment[max->id()]))(rho); + } + } + MaxStrategy improve(const EquationSystem& s, const VariableAssignment& rho) const { + MaxStrategy newStrategy(*this); + for (unsigned int i = 0; i < _length; ++i) { + const MaxExpression* expr = s.getMax(i); + const T oldValue = (*this)(*expr, rho); + std::pair 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 improve() { + bool operator!= (const MaxStrategy& other) const { + return !(*this == other); } private: unsigned int _length; -- cgit v1.2.3