/usr/include/gecode/int/gcc/bnd-sup.hpp is in libgecode-dev 5.1.0-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Patrick Pekczynski <pekczynski@ps.uni-sb.de>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Patrick Pekczynski, 2004
* Christian Schulte, 2009
* Guido Tack, 2009
*
* Last modified:
* $Date: 2017-03-08 11:58:56 +0100 (Wed, 08 Mar 2017) $ by $Author: schulte $
* $Revision: 15562 $
*
* 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.
*
*/
namespace Gecode { namespace Int { namespace GCC {
/**
* \defgroup GCCBndSup Support for GCC bounds propagation
*
* \ingroup FuncIntProp
*/
/**
* \brief Class for computing unreachable values in the value %GCC propagator
*
* \ingroup GCCBndSup
*/
class UnReachable {
public:
/// Number of variables with lower bound
int minb;
/// Number of variables with upper bound
int maxb;
/// Number of equal variables
int eq;
/// Number of smaller variables
int le;
/// Number of greater variables
int gr;
};
/**
* \brief Bounds consistency check for cardinality variables.
* \ingroup GCCBndSup
*/
template<class Card>
ExecStatus
prop_card(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k) {
int n = x.size();
int m = k.size();
Region r(home);
UnReachable* rv = r.alloc<UnReachable>(m);
for(int i = m; i--; )
rv[i].minb=rv[i].maxb=rv[i].le=rv[i].gr=rv[i].eq=0;
for (int i = n; i--; ) {
int min_idx;
if (!lookupValue(k,x[i].min(),min_idx))
return ES_FAILED;
if (x[i].assigned()) {
rv[min_idx].minb++;
rv[min_idx].maxb++;
rv[min_idx].eq++;
} else {
// count the number of variables
// with lower bound k[min_idx].card()
rv[min_idx].minb++;
int max_idx;
if (!lookupValue(k,x[i].max(),max_idx))
return ES_FAILED;
// count the number of variables
// with upper bound k[max_idx].card()
rv[max_idx].maxb++;
}
}
rv[0].le = 0;
int c_min = 0;
for (int i = 1; i < m; i++) {
rv[i].le = c_min + rv[i - 1].maxb;
c_min += rv[i - 1].maxb;
}
rv[m-1].gr = 0;
int c_max = 0;
for (int i = m-1; i--; ) {
rv[i].gr = c_max + rv[i + 1].minb;
c_max += rv[i + 1].minb;
}
for (int i = m; i--; ) {
int reachable = x.size() - rv[i].le - rv[i].gr;
if (!k[i].assigned()) {
GECODE_ME_CHECK(k[i].lq(home, reachable));
GECODE_ME_CHECK(k[i].gq(home, rv[i].eq));
} else {
// check validity of the cardinality value
if ((rv[i].eq > k[i].max()) || (k[i].max() > reachable))
return ES_FAILED;
}
}
return ES_OK;
}
/** \brief Consistency check, whether the cardinality values are feasible.
* \ingroup GCCBndSup
*/
template<class Card>
forceinline bool
card_consistent(ViewArray<IntView>& x, ViewArray<Card>& k) {
int smin = 0;
int smax = 0;
for (int i = k.size(); i--; ) {
smax += k[i].max();
smin += k[i].min();
}
// Consistent if number of variables within cardinality bounds
return (smin <= x.size()) && (x.size() <= smax);
}
/**
* \brief Maps domain bounds to their position in hall[].bounds.
* \ingroup GCCBndSup
*/
class Rank {
public:
/**
* \brief \f$ rank[i].min = z
* \Leftrightarrow min(x_i) = hall[z].bounds \f$
*/
int min;
/**
* \brief \f$ rank[i].max = z
* \Leftrightarrow max(x_i) = hall[z].bounds \f$
*/
int max;
};
/**
* \brief Compares two indices \a i, \a j of two views
* \a \f$ x_i \f$ \f$ x_j\f$ according to the
* ascending order of the views upper bounds
*
* \ingroup GCCBndSup
*/
template<class View>
class MaxInc {
protected:
/// View array for comparison
ViewArray<View> x;
public:
/// Constructor
MaxInc(const ViewArray<View>& x0) : x(x0) {}
/// Order
forceinline bool
operator ()(const int i, const int j) {
return x[i].max() < x[j].max();
}
};
/**
* \brief Compares two indices \a i, \a j of two views
* \a \f$ x_i \f$ \f$ x_j\f$ according to the
* ascending order of the views lower bounds
*
* \ingroup GCCBndSup
*/
template<class View>
class MinInc {
protected:
/// View array for comparison
ViewArray<View> x;
public:
/// Constructor
MinInc(const ViewArray<View>& x0) : x(x0) {}
/// Comparison
forceinline bool
operator ()(const int i, const int j) {
return x[i].min() < x[j].min();
}
};
/**
* \brief Compares two cardinality views
* \a \f$ x_i \f$ \f$ x_j\f$ according to the index
*
* \ingroup GCCBndSup
*/
template<class Card>
class MinIdx {
public:
/// Comparison
forceinline bool
operator ()(const Card& x, const Card& y) {
return x.card() < y.card();
}
};
/**
* \brief Partial sum structure for constant
* time computation of the maximal capacity of an interval.
*
* \ingroup GCCBndSup
*/
template<class Card>
class PartialSum {
private:
/// sum[i] contains the partial sum from 0 to i
int* sum;
/// the size of the sum
int size;
public:
/// Sentinels indicating whether an end of sum is reached
int firstValue, lastValue;
/// \name Initialization
//@{
/// Constructor
PartialSum(void);
/// Initialization
void init(Space& home, ViewArray<Card>& k, bool up);
/// Mark datstructure as requiring reinitialization
void reinit(void);
/// Test whether already initialized
operator bool(void) const;
//@}
/// \name Access
//@{
/// Compute the maximum capacity of an interval
int sumup(int from, int to) const;
/// Return smallest bound of variables
int minValue(void) const;
/// Return largest bound of variables
int maxValue(void) const;
/// Skip neigbouring array entries if their values do not differ
int skipNonNullElementsRight(int v) const;
/// Skip neigbouring array entries if their values do not differ
int skipNonNullElementsLeft(int v) const;
//@}
/// \name Update
//@{
/**
* \brief Check whether the values in the
* partial sum structure containting
* the lower cardinality bounds differ
* from the actual lower bounds of the
* cardinalities.
*/
bool check_update_min(ViewArray<Card>& k);
/**
* \brief Check whether the values in the
* partial sum structure containting
* the upper cardinality bounds differ
* from the actual upper bounds of the
* cardinalities.
*/
bool check_update_max(ViewArray<Card>& k);
//@}
};
template<class Card>
forceinline
PartialSum<Card>::PartialSum(void) : sum(NULL), size(-1) {}
template<class Card>
forceinline
PartialSum<Card>::operator bool(void) const {
return size != -1;
}
template<class Card>
inline void
PartialSum<Card>::init(Space& home, ViewArray<Card>& elements, bool up) {
int i = 0;
int j = 0;
// Determine number of holes in the index set
int holes = 0;
for (i = 1; i < elements.size(); i++) {
if (elements[i].card() != elements[i-1].card() + 1)
holes += elements[i].card()-elements[i-1].card()-1;
}
// we add three elements at the beginning and two at the end
size = elements.size() + holes + 5;
// memory allocation
if (sum == NULL) {
sum = home.alloc<int>(2*size);
}
int* ds = &sum[size];
int first = elements[0].card();
firstValue = first - 3;
lastValue = first + elements.size() + holes + 1;
// the first three elements
for (i = 3; i--; )
sum[i] = i;
/*
* copy the bounds into sum, filling up holes with zeroes
*/
int prevCard = elements[0].card()-1;
i = 0;
for (j = 2; j < elements.size() + holes + 2; j++) {
if (elements[i].card() != prevCard + 1) {
sum[j + 1] = sum[j];
} else if (up) {
sum[j + 1] = sum[j] + elements[i].max();
i++;
} else {
sum[j + 1] = sum[j] + elements[i].min();
i++;
}
prevCard++;
}
sum[j + 1] = sum[j] + 1;
sum[j + 2] = sum[j + 1] + 1;
// Compute distances, eliminating zeroes
i = elements.size() + holes + 3;
j = i + 1;
for ( ; i > 0; ) {
while(sum[i] == sum[i - 1]) {
ds[i] = j;
i--;
}
ds[j] = i;
i--;
j = ds[j];
}
ds[j] = 0;
ds[0] = 0;
}
template<class Card>
forceinline void
PartialSum<Card>::reinit(void) {
size = -1;
}
template<class Card>
forceinline int
PartialSum<Card>::sumup(int from, int to) const {
if (from <= to) {
return sum[to - firstValue] - sum[from - firstValue - 1];
} else {
assert(to - firstValue - 1 >= 0);
assert(to - firstValue - 1 < size);
assert(from - firstValue >= 0);
assert(from - firstValue < size);
return sum[to - firstValue - 1] - sum[from - firstValue];
}
}
template<class Card>
forceinline int
PartialSum<Card>::minValue(void) const {
return firstValue + 3;
}
template<class Card>
forceinline int
PartialSum<Card>::maxValue(void) const {
return lastValue - 2;
}
template<class Card>
forceinline int
PartialSum<Card>::skipNonNullElementsRight(int value) const {
value -= firstValue;
int* ds = &sum[size];
return (ds[value] < value ? value : ds[value]) + firstValue;
}
template<class Card>
forceinline int
PartialSum<Card>::skipNonNullElementsLeft(int value) const {
value -= firstValue;
int* ds = &sum[size];
return (ds[value] > value ? ds[ds[value]] : value) + firstValue;
}
template<class Card>
inline bool
PartialSum<Card>::check_update_max(ViewArray<Card>& k) {
int j = 0;
for (int i = 3; i < size - 2; i++) {
int max = 0;
if (k[j].card() == i+firstValue)
max = k[j++].max();
if ((sum[i] - sum[i - 1]) != max)
return true;
}
return false;
}
template<class Card>
inline bool
PartialSum<Card>::check_update_min(ViewArray<Card>& k) {
int j = 0;
for (int i = 3; i < size - 2; i++) {
int min = 0;
if (k[j].card() == i+firstValue)
min = k[j++].min();
if ((sum[i] - sum[i - 1]) != min)
return true;
}
return false;
}
/**
* \brief Container class provding information about the Hall
* structure of the problem variables.
*
* This class is used to
* keep the number of different arrays small, that is
* an array of type %HallInfo replaces integer arrays for each
* of the class members.
*
* \ingroup GCCBndSup
*/
class HallInfo {
public:
/// Represents the union of all lower and upper domain bounds
int bounds;
/**
* \brief Critical capacity pointer
* \a t represents a predecessor function where \f$ t_i \f$ denotes the
* predecessor of i in bounds
*/
int t;
/**
* \brief Difference between critical capacities
*
* \a d_i is the difference between the capacities of hall[i].bounds
* and its predecessor in bounds hall[t[i]].bounds
*
*/
int d;
/**
* \brief Hall set pointer
*
* If hall[i].h < i then the half-open interval
* [hall[h[i]].bounds,hall[i].bounds) is containd in a Hall
* set.
* Otherwise holds a pointer to the Hall intervall it belongs to.
*/
int h;
/// Stable Set pointer
int s;
/// Potentially Stable Set pointer
int ps;
/**
* \brief Bound update
*
* \a newBound contains either a narrowed domain bound
* or is stores the old domain bound of a variable.
*/
int newBound;
};
/**
* \name Path compression
*
* Each of the nodes on the path from \a start to \a end
* becomes a direct child of \a to.
* \ingroup GCCBndSup
*/
//@{
/// Path compression for potentially stable set structure
forceinline void
pathset_ps(HallInfo hall[], int start, int end, int to) {
int k, l;
for (l=start; (k=l) != end; hall[k].ps=to) {
l = hall[k].ps;
}
}
/// Path compression for stable set structure
forceinline void
pathset_s(HallInfo hall[], int start, int end, int to) {
int k, l;
for (l=start; (k=l) != end; hall[k].s=to) {
l = hall[k].s;
}
}
/// Path compression for capacity pointer structure
forceinline void
pathset_t(HallInfo hall[], int start, int end, int to) {
int k, l;
for (l=start; (k=l) != end; hall[k].t=to) {
l = hall[k].t;
}
}
/// Path compression for hall pointer structure
forceinline void
pathset_h(HallInfo hall[], int start, int end, int to) {
int k, l;
for (l=start; (k=l) != end; hall[k].h=to) {
l = hall[k].h;
assert(l != k);
}
}
//@}
/**
* \name Path minimum
*
* Returns the smalles reachable index starting from \a i.
* \ingroup GCCBndSup
*/
//@{
/// Path minimum for hall pointer structure
forceinline int
pathmin_h(const HallInfo hall[], int i) {
while (hall[i].h < i)
i = hall[i].h;
return i;
}
/// Path minimum for capacity pointer structure
forceinline int
pathmin_t(const HallInfo hall[], int i) {
while (hall[i].t < i)
i = hall[i].t;
return i;
}
//@}
/**
* \name Path maximum
*
* Returns the greatest reachable index starting from \a i.
* \ingroup GCCBndSup
*/
//@{
/// Path maximum for hall pointer structure
forceinline int
pathmax_h(const HallInfo hall[], int i) {
while (hall[i].h > i)
i = hall[i].h;
return i;
}
/// Path maximum for capacity pointer structure
forceinline int
pathmax_t(const HallInfo hall[], int i) {
while (hall[i].t > i) {
i = hall[i].t;
}
return i;
}
/// Path maximum for stable set pointer structure
forceinline int
pathmax_s(const HallInfo hall[], int i) {
while (hall[i].s > i)
i = hall[i].s;
return i;
}
/// Path maximum for potentially stable set pointer structure
forceinline int
pathmax_ps(const HallInfo hall[], int i) {
while (hall[i].ps > i)
i = hall[i].ps;
return i;
}
//@}
}}}
// STATISTICS: int-prop
|