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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Vincent Barichard <Vincent.Barichard@univ-angers.fr>
*
* Copyright:
* Christian Schulte, 2006
* Vincent Barichard, 2012
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/minimodel.hh>
#ifdef GECODE_HAS_FLOAT_VARS
namespace Gecode { namespace MiniModel {
/// Non-linear float arithmetic expressions
class GECODE_MINIMODEL_EXPORT ArithNonLinFloatExpr :
public NonLinFloatExpr {
public:
/// The expression type
enum ArithNonLinFloatExprType {
ANLFE_ABS, ///< Absolute value expression
ANLFE_MIN, ///< Minimum expression
ANLFE_MAX, ///< Maximum expression
ANLFE_MULT, ///< Multiplication expression
ANLFE_DIV, ///< Division expression
ANLFE_SQR, ///< Square expression
ANLFE_SQRT, ///< Square root expression
#ifdef GECODE_HAS_MPFR
ANLFE_EXP, ///< Exponential expression
ANLFE_LOG, ///< Logarithm root expression
ANLFE_ASIN, ///< expression
ANLFE_SIN, ///< expression
ANLFE_ACOS, ///< expression
ANLFE_COS, ///< expression
ANLFE_ATAN, ///< expression
ANLFE_TAN, ///< expression
#endif
ANLFE_POW, ///< Pow expression
ANLFE_NROOT ///< Nth root expression
} t;
/// Expressions
LinFloatExpr* a;
/// Size of variable array
int n;
/// Integer argument (used in nroot for example)
int aInt;
/// Constructors
ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0)
: t(t0), a(heap.alloc<LinFloatExpr>(n0)), n(n0), aInt(-1) {}
ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0, int a0)
: t(t0), a(heap.alloc<LinFloatExpr>(n0)), n(n0), aInt(a0) {}
/// Destructor
~ArithNonLinFloatExpr(void) { heap.free<LinFloatExpr>(a,n); }
/// Post expression
virtual FloatVar post(Home home, FloatVar* ret) const {
FloatVar y;
switch (t) {
case ANLFE_ABS:
{
FloatVar x = a[0].post(home);
if (x.min() >= 0)
y = result(home,ret,x);
else {
y = result(home,ret);
abs(home, x, y);
}
}
break;
case ANLFE_MIN:
if (n==1) {
y = result(home,ret, a[0].post(home));
} else if (n==2) {
FloatVar x0 = a[0].post(home);
FloatVar x1 = a[1].post(home);
if (x0.max() <= x1.min())
y = result(home,ret,x0);
else if (x1.max() <= x0.min())
y = result(home,ret,x1);
else {
y = result(home,ret);
min(home, x0, x1, y);
}
} else {
FloatVarArgs x(n);
for (int i=n; i--;)
x[i] = a[i].post(home);
y = result(home,ret);
min(home, x, y);
}
break;
case ANLFE_MAX:
if (n==1) {
y = result(home,ret,a[0].post(home));
} else if (n==2) {
FloatVar x0 = a[0].post(home);
FloatVar x1 = a[1].post(home);
if (x0.max() <= x1.min())
y = result(home,ret,x1);
else if (x1.max() <= x0.min())
y = result(home,ret,x0);
else {
y = result(home,ret);
max(home, x0, x1, y);
}
} else {
FloatVarArgs x(n);
for (int i=n; i--;)
x[i] = a[i].post(home);
y = result(home,ret);
max(home, x, y);
}
break;
case ANLFE_MULT:
{
assert(n == 2);
FloatVar x0 = a[0].post(home);
FloatVar x1 = a[1].post(home);
if (x0.assigned() && (x0.val() == 0.0))
y = result(home,ret,x0);
else if (x0.assigned() && (x0.val() == 1.0))
y = result(home,ret,x1);
else if (x1.assigned() && (x1.val() == 0.0))
y = result(home,ret,x1);
else if (x1.assigned() && (x1.val() == 1.0))
y = result(home,ret,x0);
else {
y = result(home,ret);
mult(home, x0, x1, y);
}
}
break;
case ANLFE_DIV:
{
assert(n == 2);
FloatVar x0 = a[0].post(home);
FloatVar x1 = a[1].post(home);
if (x1.assigned() && (x1.val() == 1.0))
y = result(home,ret,x0);
else if (x0.assigned() && (x0.val() == 0.0))
y = result(home,ret,x0);
else {
y = result(home,ret);
div(home, x0, x1, y);
}
}
break;
case ANLFE_SQR:
{
assert(n == 1);
FloatVar x = a[0].post(home);
if (x.assigned() && ((x.val() == 0.0) || (x.val() == 1.0)))
y = x;
else {
y = result(home,ret);
sqr(home, x, y);
}
}
break;
case ANLFE_SQRT:
{
assert(n == 1);
FloatVar x = a[0].post(home);
if (x.assigned() && ((x.val() == 0.0) || (x.val() == 1.0)))
y = result(home,ret,x);
else {
y = result(home,ret);
sqrt(home, x, y);
}
}
break;
case ANLFE_POW:
{
assert(n == 1);
FloatVar x = a[0].post(home);
if (x.assigned() && ((x.val() == 0.0) || (x.val() == 1.0)) &&
(aInt >= 0))
y = result(home,ret,x);
else {
y = result(home,ret);
pow(home, x, aInt, y);
}
}
break;
case ANLFE_NROOT:
{
assert(n == 1);
FloatVar x = a[0].post(home);
if (x.assigned() && ((x.val() == 0.0) || (x.val() == 1.0)) &&
(aInt >= 0))
y = result(home,ret,x);
else {
y = result(home,ret);
nroot(home, x, aInt, y);
}
}
break;
#ifdef GECODE_HAS_MPFR
case ANLFE_EXP:
{
assert(n == 1);
FloatVar x = a[0].post(home);
if (x.assigned() && (x.val() == 0.0)) {
FloatVar one(home,1.0,1.0);
y = result(home,ret,one);
} else {
y = result(home,ret);
exp(home, x, y);
}
}
break;
case ANLFE_LOG:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
log(home, x, y);
}
break;
case ANLFE_ASIN:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
asin(home, x, y);
}
break;
case ANLFE_SIN:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
sin(home, x, y);
}
break;
case ANLFE_ACOS:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
acos(home, x, y);
}
break;
case ANLFE_COS:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
cos(home, x, y);
}
break;
case ANLFE_ATAN:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
atan(home, x, y);
}
break;
case ANLFE_TAN:
{
assert(n == 1);
FloatVar x = a[0].post(home);
y = result(home,ret);
tan(home, x, y);
}
break;
#endif
default:
GECODE_NEVER;
}
return y;
}
virtual void post(Home home, FloatRelType frt, FloatVal c) const {
if ((t == ANLFE_MIN && frt == FRT_GQ) ||
(t == ANLFE_MAX && frt == FRT_LQ)) {
FloatVarArgs x(n);
for (int i=n; i--;)
x[i] = a[i].post(home);
rel(home, x, frt, c);
} else {
rel(home, post(home,nullptr), frt, c);
}
}
virtual void post(Home home, FloatRelType frt, FloatVal c,
BoolVar b) const {
rel(home, post(home,nullptr), frt, c, b);
}
};
/// Check if \a e is of type \a t
bool hasType(const LinFloatExpr& e, ArithNonLinFloatExpr::ArithNonLinFloatExprType t) {
return e.nlfe() &&
dynamic_cast<ArithNonLinFloatExpr*>(e.nlfe()) != nullptr &&
dynamic_cast<ArithNonLinFloatExpr*>(e.nlfe())->t == t;
}
}}
namespace Gecode {
LinFloatExpr
abs(const LinFloatExpr& e) {
using namespace MiniModel;
if (hasType(e, ArithNonLinFloatExpr::ANLFE_ABS))
return e;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_ABS,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
min(const LinFloatExpr& e0, const LinFloatExpr& e1) {
using namespace MiniModel;
int n = 0;
if (hasType(e0, ArithNonLinFloatExpr::ANLFE_MIN))
n += static_cast<ArithNonLinFloatExpr*>(e0.nlfe())->n;
else
n += 1;
if (hasType(e1, ArithNonLinFloatExpr::ANLFE_MIN))
n += static_cast<ArithNonLinFloatExpr*>(e1.nlfe())->n;
else
n += 1;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MIN,n);
int i=0;
if (hasType(e0, ArithNonLinFloatExpr::ANLFE_MIN)) {
ArithNonLinFloatExpr* e0e = static_cast<ArithNonLinFloatExpr*>(e0.nlfe());
for (; i<e0e->n; i++)
ae->a[i] = e0e->a[i];
} else {
ae->a[i++] = e0;
}
if (hasType(e1, ArithNonLinFloatExpr::ANLFE_MIN)) {
ArithNonLinFloatExpr* e1e = static_cast<ArithNonLinFloatExpr*>(e1.nlfe());
int curN = i;
for (; i<curN+e1e->n; i++)
ae->a[i] = e1e->a[i-curN];
} else {
ae->a[i++] = e1;
}
return LinFloatExpr(ae);
}
LinFloatExpr
min(const FloatVarArgs& x) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MIN,x.size());
for (int i=x.size(); i--;)
ae->a[i] = x[i];
return LinFloatExpr(ae);
}
LinFloatExpr
max(const LinFloatExpr& e0, const LinFloatExpr& e1) {
using namespace MiniModel;
int n = 0;
if (hasType(e0, ArithNonLinFloatExpr::ANLFE_MAX))
n += static_cast<ArithNonLinFloatExpr*>(e0.nlfe())->n;
else
n += 1;
if (hasType(e1, ArithNonLinFloatExpr::ANLFE_MAX))
n += static_cast<ArithNonLinFloatExpr*>(e1.nlfe())->n;
else
n += 1;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MAX,n);
int i=0;
if (hasType(e0, ArithNonLinFloatExpr::ANLFE_MAX)) {
ArithNonLinFloatExpr* e0e = static_cast<ArithNonLinFloatExpr*>(e0.nlfe());
for (; i<e0e->n; i++)
ae->a[i] = e0e->a[i];
} else {
ae->a[i++] = e0;
}
if (hasType(e1, ArithNonLinFloatExpr::ANLFE_MAX)) {
ArithNonLinFloatExpr* e1e = static_cast<ArithNonLinFloatExpr*>(e1.nlfe());
int curN = i;
for (; i<curN+e1e->n; i++)
ae->a[i] = e1e->a[i-curN];
} else {
ae->a[i++] = e1;
}
return LinFloatExpr(ae);
}
LinFloatExpr
max(const FloatVarArgs& x) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MAX,x.size());
for (int i=x.size(); i--;)
ae->a[i] = x[i];
return LinFloatExpr(ae);
}
LinFloatExpr
operator *(const FloatVar& e0, const FloatVar& e1) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MULT,2);
ae->a[0] = e0;
ae->a[1] = e1;
return LinFloatExpr(ae);
}
LinFloatExpr
operator *(const LinFloatExpr& e0, const FloatVar& e1) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MULT,2);
ae->a[0] = e0;
ae->a[1] = e1;
return LinFloatExpr(ae);
}
LinFloatExpr
operator *(const FloatVar& e0, const LinFloatExpr& e1) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MULT,2);
ae->a[0] = e0;
ae->a[1] = e1;
return LinFloatExpr(ae);
}
LinFloatExpr
operator *(const LinFloatExpr& e0, const LinFloatExpr& e1) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_MULT,2);
ae->a[0] = e0;
ae->a[1] = e1;
return LinFloatExpr(ae);
}
LinFloatExpr
operator /(const LinFloatExpr& e0, const LinFloatExpr& e1) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_DIV,2);
ae->a[0] = e0;
ae->a[1] = e1;
return LinFloatExpr(ae);
}
LinFloatExpr
sqr(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_SQR,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
sqrt(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_SQRT,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
pow(const LinFloatExpr& e, int exp) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_POW,1,exp);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
nroot(const LinFloatExpr& e, int exp) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_NROOT,1,exp);
ae->a[0] = e;
return LinFloatExpr(ae);
}
#ifdef GECODE_HAS_MPFR
LinFloatExpr
exp(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_EXP,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
log(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_LOG,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
asin(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_ASIN,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
sin(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_SIN,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
acos(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_ACOS,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
cos(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_COS,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
atan(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_ATAN,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
LinFloatExpr
tan(const LinFloatExpr& e) {
using namespace MiniModel;
ArithNonLinFloatExpr* ae =
new ArithNonLinFloatExpr(ArithNonLinFloatExpr::ANLFE_TAN,1);
ae->a[0] = e;
return LinFloatExpr(ae);
}
#endif
}
#endif
// STATISTICS: minimodel-any
|