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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <ostream>
#include <map>
#include <string>
#include <vector>
template<typename T>
struct Expression;
#include "Operator.hpp"
template<typename T>
struct Maximum : public Operator<T> {
const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const {
//assert(v.size() == 1);
T value = -INFINITY;
for (typename std::vector<Expression<T> >::const_iterator it = v.begin();
it != v.end();
++it) {
const T result = it->eval(m);
value = (value < result ? result : value);
}
return value;
}
virtual std::string output() const {
return "max";
}
};
template<typename T>
struct Minimum : public Operator<T> {
const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const {
//assert(v.size() == 1);
T value = INFINITY;
for (typename std::vector<Expression<T> >::const_iterator it = v.begin();
it != v.end();
++it) {
const T result = it->eval(m);
value = (value < result ? value : result);
}
return value;
}
virtual std::string output() const {
return "min";
}
};
template<typename T>
struct Expression {
Expression(unsigned int id, Operator<T>* o, const std::vector< Expression<T> >& a)
: _id(id), _operator(o), _arguments(a) { }
virtual const T eval(std::map<std::string, T> m) const {
return _operator->eval(_arguments, m);
}
virtual Expression<T> maxStrategyEager(const std::map<std::string, T> assignment) const {
if (dynamic_cast<Maximum<T>*>(_operator) != NULL) {
T bestVal;
const Expression<T>* best = NULL;
for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
T nextVal = it->eval(assignment);
if (best == NULL || nextVal > bestVal) {
best = &(*it);
bestVal = nextVal;
}
}
return best->maxStrategyEager(assignment);
}
std::vector<Expression<T> > newArgs;
for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
newArgs.push_back(it->maxStrategyEager(assignment));
}
return Expression(_operator, newArgs);
}
std::string output() const {
std::string result = _operator->output() + "(";
for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
if (it != _arguments.begin()) {
result += ", ";
}
result += it->output();
}
return result + ")";
}
virtual ~Expression() {}
private:
unsigned int _id;
Operator<T>* _operator;
std::vector< Expression<T> > _arguments;
template<typename E>
friend std::ostream& operator<<(std::ostream&, const Expression<E>&);
};
template<typename T>
std::ostream& operator<<(std::ostream& cout, const Expression<T>& e) {
cout << e._operator->output() << "("
<< ")";
return cout;
}
#endif
|