From f7d846f18354e254353bc417ed1a666c59ef3ea2 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 9 Jul 2012 03:36:11 +1000 Subject: Better implementation: smarter strategy iteration Also add the beginnings of some log stuff, mayhaps. --- impl/Expression.hpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'impl/Expression.hpp') diff --git a/impl/Expression.hpp b/impl/Expression.hpp index 7fc4542..857064f 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -10,11 +10,18 @@ template struct VariableAssignment; +template +struct MaxStrategy; + template struct Expression { virtual ~Expression() { } virtual Domain eval(const VariableAssignment&) const = 0; + virtual Domain eval(const VariableAssignment& rho, + const MaxStrategy&) const { + return eval(rho); + } virtual void print(std::ostream&) const = 0; }; @@ -76,6 +83,16 @@ struct OperatorExpression : public Expression { return _operator.eval(argumentValues); } + virtual Domain eval(const VariableAssignment& rho, const MaxStrategy& strat) const { + std::vector argumentValues; + for (typename std::vector*>::const_iterator it = _arguments.begin(); + it != _arguments.end(); + ++it) { + argumentValues.push_back((*it)->eval(rho, strat)); + } + return _operator.eval(argumentValues); + } + const std::vector*> arguments() const { return _arguments; } @@ -98,13 +115,36 @@ struct OperatorExpression : public Expression { private: const Operator& _operator; + protected: const std::vector*> _arguments; }; template struct MaxExpression : public OperatorExpression { MaxExpression(const unsigned int& id, const Maximum& op, const std::vector*>& arguments) - : OperatorExpression(op, arguments), _id(id) { } + : OperatorExpression(op, arguments), _id(id) { + log::debug << "Op id" << id; + } + + virtual Domain eval(const VariableAssignment& rho, const MaxStrategy& strat) const { + return this->_arguments[strat.get(*this)]->eval(rho, strat); + } + + unsigned int bestStrategy(const VariableAssignment& rho, const MaxStrategy& strat) const { + Domain bestValue = this->eval(rho, strat); + unsigned int bestIndex = strat.get(*this); + + for (unsigned int i = 0, length = this->_arguments.size(); + i < length; + ++i) { + const Domain value = this->_arguments[i]->eval(rho, strat); + if (bestValue < value) { + bestValue = value; + bestIndex = i; + } + } + return bestIndex; + } unsigned int id() const { return _id; -- cgit v1.2.3