summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
blob: 7b157c229261f21f7a4873b772ac40a08a2f3097 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP

//#include <pair>
#include "VariableAssignment.hpp"
#include "Operator.hpp"

template<typename T>
struct Expression {
  Expression(Operator<T>* op, const std::vector< Expression<T>* >& args)
    : _operator(op), _arguments(args) { }
  virtual ~Expression() {
    /*delete _operator;
    for (typename std::vector< Expression<T>* >::iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      delete *it;
    }*/
  }
  virtual T operator() (const VariableAssignment<T>& assignment) const {
    return (*_operator)(_arguments, assignment);
  }
  protected:
  Operator<T>* _operator;
  std::vector< Expression<T>* > _arguments;
};

template<typename T>
struct MaxExpression : public Expression<T> {
  MaxExpression(const std::vector< Expression<T>* >& args)
    : Expression<T>(new Maximum<T>, args) { }
  unsigned int id() const {
    return _id;
  }
  unsigned int id(unsigned int id) {
    return (_id = id);
  }
  Expression<T>* argument(unsigned int i) const {
    if (i >= Expression<T>::_arguments.size()) {
      throw "Error";
    }
    return Expression<T>::_arguments[i];
  }
  std::pair<T, unsigned int> bestStrategy(const VariableAssignment<T>& rho) const {
    std::pair<T, unsigned int> best = std::pair<T, unsigned int>(-infinity<T>(), 0);
    for (unsigned int i = 0, size = Expression<T>::_arguments.size(); i < size; ++i) {
      T value = (*Expression<T>::_arguments[i])(rho);
      if (value > best.first)
        best = std::pair<T, unsigned int>(value, i);
    }
    std::cout << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl;
    return best;
  }
  private:
  unsigned int _id;
};

#endif