dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
ExpressionEvaluator.cpp
1
6#include "ExpressionEvaluator.h"
7
8#include <map>
9#include <numeric>
10#include <ostream>
11#include <utility>
12
13#include "dlinear/libs/libgmp.h"
14#include "dlinear/symbolic/literal.h"
15#include "dlinear/util/exception.h"
16
17namespace dlinear {
18
20 : GenericExpressionVisitor<Interval, const Box&>{config, "ExpressionEvaluator"}, e_{std::move(e)} {}
21
23 const TimerGuard timer_guard(&stats_.m_timer(), stats_.enabled());
25 return VisitExpression(e_, box);
26}
27Interval ExpressionEvaluator::operator()(const Box& box) const { return Process(box); }
28
29Interval ExpressionEvaluator::VisitVariable(const Expression& e, const Box& box) const {
30 const Variable& var{get_variable(e)};
31 return box[var];
32}
33
34Interval ExpressionEvaluator::VisitConstant(const Expression& e, const Box&) const {
35 return Interval{get_constant_value(e)};
36}
37
38Interval ExpressionEvaluator::VisitRealConstant(const Expression&, const Box&) const {
39 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
40}
41
42Interval ExpressionEvaluator::VisitAddition(const Expression& e, const Box& box) const {
43 const mpq_class& c{get_constant_in_addition(e)};
44 const auto& expr_to_coeff_map = get_expr_to_coeff_map_in_addition(e);
45 return std::accumulate(expr_to_coeff_map.begin(), expr_to_coeff_map.end(), Interval{c},
46 [this, &box](const Interval& init, const std::pair<const Expression, mpq_class>& p) {
47 return init + VisitExpression(p.first, box) * p.second;
48 });
49}
50
51Interval ExpressionEvaluator::VisitMultiplication(const Expression& e, const Box& box) const {
52 const mpq_class& c{get_constant_in_multiplication(e)};
53 const auto& base_to_exponent_map = get_base_to_exponent_map_in_multiplication(e);
54 return accumulate(base_to_exponent_map.begin(), base_to_exponent_map.end(), Interval{c},
55 [this, &box](const Interval& init, const std::pair<const Expression, Expression>& p) {
56 return init * VisitPow(p.first, p.second, box);
57 });
58}
59
60Interval ExpressionEvaluator::VisitDivision(const Expression&, const Box&) const {
61 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
62}
63
64Interval ExpressionEvaluator::VisitLog(const Expression&, const Box&) const {
65 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
66}
67
68Interval ExpressionEvaluator::VisitAbs(const Expression&, const Box&) const {
69 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
70}
71
72Interval ExpressionEvaluator::VisitExp(const Expression&, const Box&) const {
73 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
74}
75
76Interval ExpressionEvaluator::VisitSqrt(const Expression&, const Box&) const {
77 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
78}
79
80Interval ExpressionEvaluator::VisitPow(const Expression& e, const Box& box) const {
81 return VisitPow(get_first_argument(e), get_second_argument(e), box);
82}
83
84Interval ExpressionEvaluator::VisitPow(const Expression&, const Expression&, const Box&) const {
85 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
86#if 0
87 const Interval first{Visit(e1, box)};
88 const Interval second{Visit(e2, box)};
89 if (second.is_degenerated() && !second.is_empty()) {
90 DLINEAR_ASSERT(second.lb() == second.ub(), "Interval must be a point.");
91 const double point{second.lb()};
92 if (is_integer(point)) {
93 if (point == 2.0) {
94 return sqr(first);
95 } else {
96 return pow(first, static_cast<int>(point));
97 }
98 } else {
99 return pow(first, point);
100 }
101 } else {
102 return pow(first, second);
103 }
104#endif
105}
106
107Interval ExpressionEvaluator::VisitSin(const Expression&, const Box&) const {
108 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
109}
110
111Interval ExpressionEvaluator::VisitCos(const Expression&, const Box&) const {
112 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
113}
114
115Interval ExpressionEvaluator::VisitTan(const Expression&, const Box&) const {
116 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
117}
118
119Interval ExpressionEvaluator::VisitAsin(const Expression&, const Box&) const {
120 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
121}
122
123Interval ExpressionEvaluator::VisitAcos(const Expression&, const Box&) const {
124 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
125}
126
127Interval ExpressionEvaluator::VisitAtan(const Expression&, const Box&) const {
128 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
129}
130
131Interval ExpressionEvaluator::VisitAtan2(const Expression&, const Box&) const {
132 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
133}
134
135Interval ExpressionEvaluator::VisitSinh(const Expression&, const Box&) const {
136 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
137}
138
139Interval ExpressionEvaluator::VisitCosh(const Expression&, const Box&) const {
140 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
141}
142
143Interval ExpressionEvaluator::VisitTanh(const Expression&, const Box&) const {
144 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
145}
146
147Interval ExpressionEvaluator::VisitMin(const Expression&, const Box&) const {
148 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
149}
150
151Interval ExpressionEvaluator::VisitMax(const Expression&, const Box&) const {
152 DLINEAR_RUNTIME_ERROR("Operation is not supported yet.");
153}
154
155Interval ExpressionEvaluator::VisitIfThenElse(const Expression& /* unused */, const Box& /* unused */) const {
156 DLINEAR_RUNTIME_ERROR("If-then-else expression is not supported yet.");
157}
158
159Interval ExpressionEvaluator::VisitUninterpretedFunction(const Expression& /* unused */,
160 const Box& /* unused */) const {
161 DLINEAR_RUNTIME_ERROR("Uninterpreted function is not supported.");
162}
163
164std::ostream& operator<<(std::ostream& os, const ExpressionEvaluator& expression_evaluator) {
165 return os << "ExpressionEvaluator(" << expression_evaluator.expression() << ")";
166}
167
168} // namespace dlinear
Collection of variables with associated intervals.
Definition Box.h:31
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
ExpressionEvaluator(Expression e, const Config &config)
Construct a new ExpressionEvaluator object with the given expression and configuration.
Interval Process(const Box &box) const
Evaluates the expression with box.
Generic expression visitor implementing the visitor pattern.
void Increase()
Increase the iteration counter by one.
Definition Stats.cpp:39
Timer & m_timer()
Get read-write access to the timer of the stats.
Definition Stats.h:50
bool enabled() const
Check whether the stats is enabled.
Definition Stats.h:48
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Definition Timer.h:129
Represents a symbolic form of an expression.
Global namespace for the dlinear library.