Generated on Sun Aug 9 2020 05:34:08 for Gecode by doxygen 1.8.18
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 
44 namespace 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) {}
56  virtual MaybeType solution(const Assignment& x) const {
57  return eq(exp(x[0]), x[1]);
58  }
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) {}
75  virtual MaybeType solution(const Assignment& x) const {
76  return eq(exp(x[0]), x[1]);
77  }
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  }
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) {}
101  virtual MaybeType solution(const Assignment& x) const {
102  return eq(exp(x[0]), x[0]);
103  }
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) {}
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  }
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) {}
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  }
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  }
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) {}
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  }
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) {}
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  }
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) {}
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  }
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  }
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) {}
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  }
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) {}
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  }
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) {}
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  }
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  }
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) {}
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  }
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 
381  PowXY pow_xy_a_1("A",a,-1.5,step);
382  PowXY pow_xy_b_1("B",b,-1.5,step);
383  PowXY pow_xy_c_1("C",c,-1.5,step);
384 
388 
389  PowXX pow_xx_a_1("A",a,-1.5,step);
390  PowXX pow_xx_b_1("B",b,-1.5,step);
391  PowXX pow_xx_c_1("C",c,-1.5,step);
392 
393  PowXY pow_xy_a_2("A",a,1.5,step);
394  PowXY pow_xy_b_2("B",b,1.5,step);
395  PowXY pow_xy_c_2("C",c,1.5,step);
396 
400 
401  PowXX pow_xx_a_2("A",a,1.5,step);
402  PowXX pow_xx_b_2("B",b,1.5,step);
403  PowXX pow_xx_c_2("C",c,1.5,step);
404 
408 
412 
416 
418 
419  }
420 }}
421 
422 #endif
423 // STATISTICS: test-float
ExpXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
PowXYSol pow_xy_sol_c_3("C", c, 0, step)
ExpXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
PowXX pow_xx_c_3("C", c, 0, step)
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n\geq 0$.
Definition: arithmetic.cpp:109
void log(Home home, FloatNum base, FloatVar x0, FloatVar x1)
Post propagator for .
Gecode::FloatVal dom
Domain of variables.
Definition: float.hh:249
Test for logarithm constraint
LogNXY logn_xy_b_1("B", b,-1.5, step)
LogNXY logn_xy_c_3("C", c, 0, step)
LogNXY logn_xy_b_2("B", b, 1.5, step)
LogXX log_xx_a("A", a, step)
PowXYSol pow_xy_sol_b_3("B", b, 0, step)
ExpXX exp_xx_c("C", c, step)
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition: float.hpp:268
PowXY pow_xy_a_3("A", a, 0, step)
LogNXYSol logn_xy_sol_a_2("A", a, 1.5, step)
LogXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
PowXY pow_xy_b_1("B", b,-1.5, step)
PowXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
PowXX pow_xx_a_3("A", a, 0, step)
ExpXY exp_xy_b("B", b, step)
Test for exponent constraint where solution is ensured
PowXYSol pow_xy_sol_c_1("C", c,-1.5, step)
@ MT_FALSE
Definition: float.hh:52
PowXY pow_xy_a_2("A", a, 1.5, step)
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ExpXY exp_xy_a("A", a, step)
PowXYSol pow_xy_sol_b_1("B", b,-1.5, step)
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
LogNXYSol logn_xy_sol_b_3("B", b, 0, step)
LogNXYSol logn_xy_sol_b_2("B", b, 1.5, step)
Gecode::FloatVal c(-8, 8)
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
LogNXX logn_xx_a_3("A", a, 0, step)
PowXY pow_xy_b_2("B", b, 1.5, step)
LogNXYSol logn_xy_sol_a_3("A", a, 0, step)
PowXX pow_xx_b_2("B", b, 1.5, step)
Gecode::FloatVal a(-8, 5)
Computation spaces.
Definition: core.hpp:1742
LogNXX logn_xx_a_2("A", a, 1.5, step)
Test for exponent constraint with shared variables
PowXY pow_xy_b_3("B", b, 0, step)
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
LogNXYSol logn_xy_sol_c_3("C", c, 0, step)
PowXX pow_xx_c_1("C", c,-1.5, step)
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.
PowXYSol pow_xy_sol_b_2("B", b, 1.5, step)
LogNXYSol logn_xy_sol_a_1("A", a,-1.5, step)
Test for logarithm base n constraint where solution is ensured
LogNXX logn_xx_b_2("B", b, 1.5, step)
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
LogXX log_xx_c("C", c, step)
ExpXX exp_xx_a("A", a, step)
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
PowXX pow_xx_b_1("B", b,-1.5, step)
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
LogNXX logn_xx_c_2("C", c, 1.5, step)
Test for pow exponent n constraint with shared variables
Test for pow exponent n constraint where solution is ensured
ExpXY exp_xy_c("C", c, step)
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
LogNXY logn_xy_c_1("C", c,-1.5, step)
LogNXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
ExpXYSol exp_xy_sol_c("C", c, step)
LogXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
@ EXTEND_ASSIGNMENT
Definition: float.hh:64
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
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.
bool flip(void)
Flip a coin and return true or false randomly.
Definition: float.hpp:273
const Gecode::FloatNum step2
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
double FloatNum
Floating point number base type.
Definition: float.hh:106
@ CPLT_ASSIGNMENT
Definition: float.hh:62
LogXY log_xy_c("C", c, step)
bool subset(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:490
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ExpXYSol exp_xy_sol_a("A", a, step)
LogXYSol log_xy_sol_b("B", b, step)
PowXY pow_xy_c_2("C", c, 1.5, step)
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.
Test for logarithm constraint where solution is ensured
LogNXY logn_xy_b_3("B", b, 0, step)
LogXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
PowXY pow_xy_c_1("C", c,-1.5, step)
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for exponent constraint
Test for logarithm base n constraint
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
PowXYSol pow_xy_sol_c_2("C", c, 1.5, step)
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ExpXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for logarithm constraint with shared variables
Float value type.
Definition: float.hh:334
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
PowXX pow_xx_b_3("B", b, 0, step)
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
PowXYSol pow_xy_sol_a_3("A", a, 0, step)
LogNXYSol logn_xy_sol_c_2("C", c, 1.5, step)
Gecode::IntSet d(v, 7)
LogNXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition: float.hpp:194
Test for pow exponent n constraint
PowXYSol pow_xy_sol_a_2("A", a, 1.5, step)
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.
MaybeType
Type for comparisons and solutions.
Definition: float.hh:51
Float variable array.
Definition: float.hh:1030
ExpXYSol exp_xy_sol_b("B", b, step)
PowXY pow_xy_a_1("A", a,-1.5, step)
PowXX pow_xx_c_2("C", c, 1.5, step)
General test support.
Definition: afc.cpp:39
LogNXYSol logn_xy_sol_b_1("B", b,-1.5, step)
ExpXX exp_xx_b("B", b, step)
LogNXX logn_xx_c_1("C", c,-1.5, step)
LogXYSol log_xy_sol_c("C", c, step)
Base class for assignments
Definition: float.hh:80
PowXY pow_xy_c_3("C", c, 0, step)
PowXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
LogNXY logn_xy_a_2("A", a, 1.5, step)
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Gecode::FloatVal b(9, 12)
LogNXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum _base, Gecode::FloatNum st)
Create and register test.
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
const Gecode::FloatNum step
PowXX pow_xx_a_1("A", a,-1.5, step)
LogXX log_xx_b("B", b, step)
LogXY log_xy_a("A", a, step)
LogNXY logn_xy_a_3("A", a, 0, step)
LogXY log_xy_b("B", b, step)
LogNXX logn_xx_c_3("C", c, 0, step)
LogNXY logn_xy_a_1("A", a,-1.5, step)
LogNXYSol logn_xy_sol_c_1("C", c,-1.5, step)
LogNXX logn_xx_a_1("A", a,-1.5, step)
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
LogNXX logn_xx_b_3("B", b, 0, step)
LogNXY logn_xy_c_2("C", c, 1.5, step)
LogXYSol log_xy_sol_a("A", a, step)
PowXYSol pow_xy_sol_a_1("A", a,-1.5, step)
Test for logarithm base n constraint with shared variables
PowXX pow_xx_a_2("A", a, 1.5, step)
LogNXX logn_xx_b_1("B", b,-1.5, step)