Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
transcendental.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Vincent Barichard, 2012
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36#include "test/float.hh"
37#include <gecode/minimodel.hh>
38
39#ifdef GECODE_HAS_MPFR
40
41#include <cmath>
42#include <algorithm>
43
44namespace Test { namespace Float {
45
47 namespace Transcendental {
48
50 class ExpXY : public Test {
51 public:
53 ExpXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
54 : Test("Transcendental::Exp::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
55
56 virtual MaybeType solution(const Assignment& x) const {
57 return eq(exp(x[0]), x[1]);
58 }
59
60 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
61 if (flip())
62 Gecode::exp(home, x[0], x[1]);
63 else
64 Gecode::rel(home, exp(x[0]) == x[1]);
65 }
66 };
67
69 class ExpXYSol : public Test {
70 public:
72 ExpXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
73 : Test("Transcendental::Exp::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
74
75 virtual MaybeType solution(const Assignment& x) const {
76 return eq(exp(x[0]), x[1]);
77 }
78
79 virtual bool extendAssignement(Assignment& x) const {
80 Gecode::FloatVal d = exp(x[0]);
81 if (Gecode::Float::subset(d, dom)) {
82 x.set(1, d);
83 return true;
84 } else {
85 return false;
86 }
87 }
88
89 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
90 Gecode::exp(home, x[0], x[1]);
91 }
92 };
93
95 class ExpXX : public Test {
96 public:
98 ExpXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
99 : Test("Transcendental::Exp::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
100
101 virtual MaybeType solution(const Assignment& x) const {
102 return eq(exp(x[0]), x[0]);
103 }
104
105 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
106 Gecode::exp(home, x[0], x[0]);
107 }
108 };
109
111 class LogXY : public Test {
112 public:
114 LogXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
115 : Test("Transcendental::Log::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
116
117 virtual MaybeType solution(const Assignment& x) const {
118 if (x[0].max() < 0.0)
119 return MT_FALSE;
120 return eq(log(x[0]), x[1]);
121 }
122
123 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
124 if (flip())
125 Gecode::log(home, x[0], x[1]);
126 else
127 Gecode::rel(home, log(x[0]) == x[1]);
128 }
129 };
130
132 class LogXYSol : public Test {
133 public:
135 LogXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
136 : Test("Transcendental::Log::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
137
138 virtual MaybeType solution(const Assignment& x) const {
139 if (x[0].max() < 0.0)
140 return MT_FALSE;
141 return eq(log(x[0]), x[1]);
142 }
143
144 virtual bool extendAssignement(Assignment& x) const {
145 if (x[0].max() < 0.0) return false;
146 Gecode::FloatVal d = log(x[0]);
147 if (Gecode::Float::subset(d, dom)) {
148 x.set(1, d);
149 return true;
150 } else {
151 return false;
152 }
153 }
154
155 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
156 Gecode::log(home, x[0], x[1]);
157 }
158 };
159
161 class LogXX : public Test {
162 public:
164 LogXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
165 : Test("Transcendental::Log::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
166
167 virtual MaybeType solution(const Assignment& x) const {
168 if (x[0].max() < 0.0)
169 return MT_FALSE;
170 return eq(log(x[0]), x[0]);
171 }
172
173 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
174 Gecode::log(home, x[0], x[0]);
175 }
176 };
177
179 class LogNXY : public Test {
180 Gecode::FloatNum base;
181 public:
183 LogNXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
184 : Test("Transcendental::Log::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
185
186 virtual MaybeType solution(const Assignment& x) const {
187 if ((x[0].max() <= 0.0) || (base <= 0.0))
188 return MT_FALSE;
189 return eq(log(x[0]) / log(base), x[1]);
190 }
191
192 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
193 Gecode::log(home, base, x[0], x[1]);
194 }
195 };
196
198 class LogNXYSol : public Test {
199 Gecode::FloatNum base;
200 public:
202 LogNXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
203 : Test("Transcendental::Log::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {}
204
205 virtual MaybeType solution(const Assignment& x) const {
206 if ((x[0].max() <= 0.0) || (base <= 0.0))
207 return MT_FALSE;
208 return eq(log(x[0]) / log(base), x[1]);
209 }
210
211 virtual bool extendAssignement(Assignment& x) const {
212 if ((x[0].max() <= 0.0) || (base <= 0.0))
213 return false;
214 Gecode::FloatVal d = log(x[0])/log(base);
215 if (Gecode::Float::subset(d, dom)) {
216 x.set(1, d);
217 return true;
218 } else {
219 return false;
220 }
221 }
222
223 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
224 Gecode::log(home, base, x[0], x[1]);
225 }
226 };
227
229 class LogNXX : public Test {
230 Gecode::FloatNum base;
231 public:
233 LogNXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
234 : Test("Transcendental::Log::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
235
236 virtual MaybeType solution(const Assignment& x) const {
237 if ((x[0].max() <= 0.0) || (base <= 0.0))
238 return MT_FALSE;
239 return eq(log(x[0]) / log(base), x[0]);
240 }
241
242 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
243 Gecode::log(home, base, x[0], x[0]);
244 }
245 };
246
248 class PowXY : public Test {
249 Gecode::FloatNum base;
250 public:
252 PowXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
253 : Test("Transcendental::Pow::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
254
255 virtual MaybeType solution(const Assignment& x) const {
256 if (base <= 0.0)
257 return MT_FALSE;
258 return eq(exp(x[0] * log(base)), x[1]);
259 }
260
261 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
262 Gecode::pow(home, base, x[0], x[1]);
263 }
264 };
265
267 class PowXYSol : public Test {
268 Gecode::FloatNum base;
269 public:
271 PowXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
272 : Test("Transcendental::Pow::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {}
273
274 virtual MaybeType solution(const Assignment& x) const {
275 if (base <= 0.0)
276 return MT_FALSE;
277 return eq(exp(x[0] * log(base)), x[1]);
278 }
279
280 virtual bool extendAssignement(Assignment& x) const {
281 if (base <= 0.0) return false;
282 Gecode::FloatVal d = exp(x[0]*log(base));
283 if (Gecode::Float::subset(d, dom)) {
284 x.set(1, d);
285 return true;
286 } else {
287 return false;
288 }
289 }
290
291 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
292 Gecode::pow(home, base, x[0], x[1]);
293 }
294 };
295
297 class PowXX : public Test {
298 Gecode::FloatNum base;
299 public:
301 PowXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
302 : Test("Transcendental::Pow::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
303
304 virtual MaybeType solution(const Assignment& x) const {
305 if ((x[0].max() <= 0.0) || (base <= 0.0))
306 return MT_FALSE;
307 return eq(exp(x[0] * log(base)), x[0]);
308 }
309
310 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
311 Gecode::pow(home, base, x[0], x[0]);
312 }
313 };
314
315 const Gecode::FloatNum step = 0.15;
320
324
328
332
336
340
344
348
352
356
360
364
368
372
376
380
384
388
392
396
400
404
408
412
416
418
419 }
420}}
421
422#endif
423// STATISTICS: test-float
Float value type.
Definition float.hh:334
Float variable array.
Definition float.hh:1033
Computation spaces.
Definition core.hpp:1744
Base class for assignments
Definition float.hh:80
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:268
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:194
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:273
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:249
Test for exponent constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
ExpXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for exponent constraint where solution is ensured
ExpXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for exponent constraint
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ExpXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for logarithm base n constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
LogNXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for logarithm base n constraint where solution is ensured
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
LogNXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Test for logarithm base n constraint
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
LogNXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for logarithm constraint with shared variables
LogXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for logarithm constraint where solution is ensured
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
LogXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for logarithm constraint
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
LogXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for pow exponent n constraint with shared variables
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
PowXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for pow exponent n constraint where solution is ensured
PowXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Test for pow exponent n constraint
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
PowXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
double FloatNum
Floating point number base type.
Definition float.hh:106
bool subset(const FloatVal &x, const FloatVal &y)
Definition val.hpp:490
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n\geq 0$.
Tests for transcendental constraints
LogNXYSol logn_xy_sol_b_1("B", b,-1.5, step)
LogNXYSol logn_xy_sol_c_1("C", c,-1.5, step)
PowXX pow_xx_b_3("B", b, 0, step)
LogNXX logn_xx_b_1("B", b,-1.5, step)
ExpXYSol exp_xy_sol_a("A", a, step)
LogNXY logn_xy_a_2("A", a, 1.5, step)
LogXY log_xy_c("C", c, step)
ExpXY exp_xy_b("B", b, step)
PowXY pow_xy_a_2("A", a, 1.5, step)
PowXX pow_xx_b_2("B", b, 1.5, step)
LogNXYSol logn_xy_sol_b_3("B", b, 0, step)
LogXYSol log_xy_sol_c("C", c, step)
PowXX pow_xx_c_2("C", c, 1.5, step)
PowXYSol pow_xy_sol_a_1("A", a,-1.5, step)
ExpXYSol exp_xy_sol_c("C", c, step)
LogNXY logn_xy_b_3("B", b, 0, step)
PowXY pow_xy_a_1("A", a,-1.5, step)
LogNXY logn_xy_a_3("A", a, 0, step)
ExpXX exp_xx_c("C", c, step)
ExpXX exp_xx_b("B", b, step)
LogXY log_xy_b("B", b, step)
LogNXYSol logn_xy_sol_a_3("A", a, 0, step)
PowXY pow_xy_b_3("B", b, 0, step)
LogNXX logn_xx_a_2("A", a, 1.5, step)
LogXYSol log_xy_sol_a("A", a, step)
ExpXX exp_xx_a("A", a, step)
LogNXYSol logn_xy_sol_b_2("B", b, 1.5, step)
LogNXY logn_xy_c_1("C", c,-1.5, step)
PowXYSol pow_xy_sol_c_1("C", c,-1.5, step)
PowXYSol pow_xy_sol_c_2("C", c, 1.5, step)
LogNXY logn_xy_b_2("B", b, 1.5, step)
LogNXY logn_xy_c_3("C", c, 0, step)
PowXY pow_xy_b_1("B", b,-1.5, step)
LogNXY logn_xy_b_1("B", b,-1.5, step)
PowXYSol pow_xy_sol_c_3("C", c, 0, step)
PowXYSol pow_xy_sol_b_2("B", b, 1.5, step)
LogNXX logn_xx_c_1("C", c,-1.5, step)
PowXY pow_xy_c_3("C", c, 0, step)
ExpXYSol exp_xy_sol_b("B", b, step)
LogXX log_xx_b("B", b, step)
PowXX pow_xx_c_1("C", c,-1.5, step)
LogNXX logn_xx_c_2("C", c, 1.5, step)
LogNXYSol logn_xy_sol_a_1("A", a,-1.5, step)
LogNXYSol logn_xy_sol_a_2("A", a, 1.5, step)
LogNXYSol logn_xy_sol_c_2("C", c, 1.5, step)
PowXX pow_xx_b_1("B", b,-1.5, step)
PowXYSol pow_xy_sol_a_2("A", a, 1.5, step)
LogNXX logn_xx_b_2("B", b, 1.5, step)
LogNXY logn_xy_c_2("C", c, 1.5, step)
LogNXYSol logn_xy_sol_c_3("C", c, 0, step)
LogNXX logn_xx_c_3("C", c, 0, step)
PowXX pow_xx_a_2("A", a, 1.5, step)
LogNXX logn_xx_a_1("A", a,-1.5, step)
ExpXY exp_xy_c("C", c, step)
PowXY pow_xy_b_2("B", b, 1.5, step)
PowXYSol pow_xy_sol_a_3("A", a, 0, step)
LogNXX logn_xx_a_3("A", a, 0, step)
PowXYSol pow_xy_sol_b_3("B", b, 0, step)
Gecode::FloatVal c(-8, 8)
PowXX pow_xx_a_1("A", a,-1.5, step)
LogNXY logn_xy_a_1("A", a,-1.5, step)
Gecode::FloatVal a(-8, 5)
PowXX pow_xx_c_3("C", c, 0, step)
PowXY pow_xy_c_2("C", c, 1.5, step)
LogNXX logn_xx_b_3("B", b, 0, step)
Gecode::FloatVal b(9, 12)
const Gecode::FloatNum step2
LogXYSol log_xy_sol_b("B", b, step)
LogXX log_xx_c("C", c, step)
PowXY pow_xy_c_1("C", c,-1.5, step)
PowXYSol pow_xy_sol_b_1("B", b,-1.5, step)
const Gecode::FloatNum step
ExpXY exp_xy_a("A", a, step)
LogXX log_xx_a("A", a, step)
PowXY pow_xy_a_3("A", a, 0, step)
LogXY log_xy_a("A", a, step)
PowXX pow_xx_a_3("A", a, 0, step)
Testing domain floats.
Definition float.cpp:43
@ EXTEND_ASSIGNMENT
Definition float.hh:64
@ CPLT_ASSIGNMENT
Definition float.hh:62
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
General test support.
Definition afc.cpp:39