blob: 87e4eea5331a187eb0782b71d04c695a3a11e919 (
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
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
112
113
114
115
116
117
118
119
120
121
|
#ifndef OPERATOR_HPP
#define OPERATOR_HPP
template<typename T>
T infinity() { }
template<>
float infinity() {
return INFINITY;
}
#include <vector>
#include "IdSet.hpp"
template<typename T>
struct Variable;
template<typename T>
struct VariableAssignment;
template<typename T>
struct Expression;
template<typename T>
struct Operator {
Operator(const std::string& name) : op_name(name) { }
virtual ~Operator() { }
virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& rho) const = 0;
const std::string op_name;
};
template<typename T>
struct Maximum : public Operator<T> {
Maximum() : Operator<T>("max") { }
virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const {
T value = -infinity<T>();
for (typename std::vector< Expression<T>* >::const_iterator it = args.begin();
it != args.end();
++it) {
T temporary = (**it)(assignment);
value = (value < temporary ? value : temporary);
//if (value == infinity<T>()) break;
}
return value;
}
};
template<typename T>
struct Minimum : public Operator<T> {
Minimum() : Operator<T>("min") { }
virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const {
T value = infinity<T>();
for (typename std::vector< Expression<T>* >::const_iterator it = args.begin();
it != args.end();
++it) {
T temporary = (**it)(assignment);
value = (temporary < value ? temporary : value);
//if (value == -infinity<T>()) break;
}
return value;
}
};
template<typename T>
struct Constant : public Operator<T> {
Constant(const std::string& value, const T& val)
: Operator<T>(value), _value(val) { }
T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
return _value;
}
private:
const T _value;
};
template<typename T>
struct Addition: public Operator<T> {
Addition() : Operator<T>("add") { }
T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
T sum = (*args[0])(ass);
for (unsigned int i = 1, size = args.size(); i < size; ++i) {
sum += (*args[i])(ass);
}
return sum;
}
};
template<typename T>
struct Negate: public Operator<T> {
Negate() : Operator<T>("neg") { }
T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
// assert(args.size() == 1);
return -(*args[0])(ass);
}
};
template<typename T>
struct Comma: public Operator<T> {
Comma() : Operator<T>("comma") { }
T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
if ((*args[0])(ass) == -infinity<T>()) {
std::cout << "Comma - neg inf" << std::endl;
return -infinity<T>();
} else {
std::cout << "Comma - finite" << std::endl;
return (*args[1])(ass);
}
}
};
template<typename T>
struct Guard: public Operator<T> {
Guard() : Operator<T>("guard") { }
T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
if ((*args[0])(ass) < (*args[1])(ass)) {
return -infinity<T>();
} else {
return (*args[2])(ass);
}
}
};
#include "VariableAssignment.hpp"
#include "Expression.hpp"
#endif
|