Generated on Sat Feb 7 2015 02:01:22 for Gecode by doxygen 1.8.9.1
bnd.hpp
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  *
6  * Copyright:
7  * Christian Schulte, 2003
8  *
9  * Last modified:
10  * $Date: 2013-11-15 14:53:16 +0100 (Fri, 15 Nov 2013) $ by $Author: schulte $
11  * $Revision: 14077 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Int { namespace Distinct {
39 
40  template<class View>
43  : Propagator(home), x(x0), y(home,x0) {
44  // Both x and y initially contain the same variables
45  // - x is used for bounds propagation
46  // - y is used for performing singleton propagation
47  // They can not be shared as singleton propagation removes
48  // determined variables still required for bounds propagation.
49  y.subscribe(home,*this,PC_INT_BND);
50  int min = x[x.size()-1].min(), max = x[x.size()-1].max();
51  for (int i=x.size()-1; i--; ) {
52  min = std::min(min,x[i].min());
53  max = std::max(max,x[i].max());
54  }
55  min_x = min; max_x = max;
56  }
57 
58  template<class View>
59  forceinline size_t
61  y.cancel(home,*this,PC_INT_BND);
62  (void) Propagator::dispose(home);
63  return sizeof(*this);
64  }
65 
66  template<class View>
68  Bnd<View>::Bnd(Space& home, bool share, Bnd<View>& p)
69  : Propagator(home,share,p), min_x(p.min_x), max_x(p.max_x) {
70  x.update(home,share,p.x);
71  y.update(home,share,p.y);
72  }
73 
74  template<class View>
75  Actor*
76  Bnd<View>::copy(Space& home, bool share) {
77  return new (home) Bnd<View>(home,share,*this);
78  }
79 
80  template<class View>
81  PropCost
82  Bnd<View>::cost(const Space&, const ModEventDelta& med) const {
83  if (View::me(med) == ME_INT_VAL)
84  return PropCost::linear(PropCost::LO, y.size());
85  else
86  return PropCost::quadratic(PropCost::LO, x.size());
87  }
88 
89 
91  class Rank {
92  public:
93  int min, max;
94  };
95 
97  template<class View>
98  class MaxIncIdx {
99  protected:
101  public:
103  MaxIncIdx(const ViewArray<View>& x0) : x(x0) {}
104  forceinline bool
105  operator ()(const int i, const int j) {
106  return x[i].max() < x[j].max();
107  }
108  };
109 
111  template<class View>
112  class MinIncIdx {
113  protected:
115  public:
116  forceinline
117  MinIncIdx(const ViewArray<View>& x0) : x(x0) {}
118  forceinline bool
119  operator ()(const int i, const int j) {
120  return x[i].min() < x[j].min();
121  }
122  };
123 
125  template<class View>
126  class MinInc {
127  public:
128  forceinline bool
129  operator ()(const View& x, const View& y) {
130  return x.min() < y.min();
131  }
132  };
133 
135  template<class IntType>
136  class HallInfo {
137  public:
139  };
140 
141  template<class IntType>
142  forceinline void
144  IntType start, IntType end, IntType to) {
145  IntType k, l;
146  for (l=start; (k=l) != end; hall[k].t=to) {
147  l = hall[k].t;
148  }
149  }
150 
151  template<class IntType>
152  forceinline void
154  IntType start, IntType end, IntType to) {
155  IntType k, l;
156  for (l=start; (k=l) != end; hall[k].h=to) {
157  l = hall[k].h;
158  }
159  }
160 
161  template<class IntType>
164  while (hall[i].h < i)
165  i = hall[i].h;
166  return i;
167  }
168 
169  template<class IntType>
172  while (hall[i].t < i)
173  i = hall[i].t;
174  return i;
175  }
176 
177  template<class IntType>
180  while (hall[i].h > i)
181  i = hall[i].h;
182  return i;
183  }
184 
185  template<class IntType>
188  while (hall[i].t > i)
189  i = hall[i].t;
190  return i;
191  }
192 
193  template<class View, class IntType>
196  int* minsorted, int* maxsorted) {
197  const int n = x.size();
198 
199  Region r(home);
200 
201  // Setup rank and bounds info
202  HallInfo<IntType>* hall = r.alloc<HallInfo<IntType> >(2*n+2);
203  Rank* rank = r.alloc<Rank>(n);
204 
205  int nb = 0;
206  {
207  IntType min = x[minsorted[0]].min();
208  IntType max = x[maxsorted[0]].max() + 1;
209  IntType last = min - 2;
210 
211  hall[0].bounds = last;
212 
213  int i = 0;
214  int j = 0;
215  while (true) {
216  if ((i < n) && (min < max)) {
217  if (min != last)
218  hall[++nb].bounds = last = min;
219  rank[minsorted[i]].min = nb;
220  if (++i < n)
221  min = x[minsorted[i]].min();
222  } else {
223  if (max != last)
224  hall[++nb].bounds = last = max;
225  rank[maxsorted[j]].max = nb;
226  if (++j == n)
227  break;
228  max = x[maxsorted[j]].max() + 1;
229  }
230  }
231  hall[nb+1].bounds = hall[nb].bounds + 2;
232  }
233 
234  // If tells cross holes, we do not compute a fixpoint
235  ExecStatus es = ES_FIX;
236 
237  // Propagate lower bounds
238  for (int i=nb+2; --i;) {
239  hall[i].t = hall[i].h = i-1;
240  hall[i].d = hall[i].bounds - hall[i-1].bounds;
241  }
242  for (int i=0; i<n; i++) { // visit intervals in increasing max order
243  IntType x0 = rank[maxsorted[i]].min;
244  IntType z = pathmax_t(hall, x0+1);
245  IntType j = hall[z].t;
246  if (--hall[z].d == 0)
247  hall[z = pathmax_t(hall, hall[z].t=z+1)].t = j;
248  pathset_t(hall, x0+1, z, z); // path compression
249  IntType y = rank[maxsorted[i]].max;
250  if (hall[z].d < hall[z].bounds-hall[y].bounds)
251  return ES_FAILED;
252  if (hall[x0].h > x0) {
253  IntType w = pathmax_h(hall, hall[x0].h);
254  IntType m = hall[w].bounds;
255  ModEvent me = x[maxsorted[i]].gq(home,m);
256  if (me_failed(me))
257  return ES_FAILED;
258  if ((me == ME_INT_VAL) ||
259  ((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
260  es = ES_NOFIX;
261  pathset_h(hall, x0, w, w); // path compression
262  }
263  if (hall[z].d == hall[z].bounds-hall[y].bounds) {
264  pathset_h(hall, hall[y].h, j-1, y); // mark hall interval
265  hall[y].h = j-1;
266  }
267  }
268 
269  // Propagate upper bounds
270  for (int i=nb+1; i--;) {
271  hall[i].t = hall[i].h = i+1;
272  hall[i].d = hall[i+1].bounds - hall[i].bounds;
273  }
274  for (int i=n; --i>=0; ) { // visit intervals in decreasing min order
275  IntType x0 = rank[minsorted[i]].max;
276  IntType z = pathmin_t(hall, x0-1);
277  IntType j = hall[z].t;
278  if (--hall[z].d == 0)
279  hall[z = pathmin_t(hall, hall[z].t=z-1)].t = j;
280  pathset_t(hall, x0-1, z, z);
281  IntType y = rank[minsorted[i]].min;
282  if (hall[z].d < hall[y].bounds-hall[z].bounds)
283  return ES_FAILED;
284  if (hall[x0].h < x0) {
285  IntType w = pathmin_h(hall, hall[x0].h);
286  IntType m = hall[w].bounds - 1;
287  ModEvent me = x[minsorted[i]].lq(home,m);
288  if (me_failed(me))
289  return ES_FAILED;
290  if ((me == ME_INT_VAL) ||
291  ((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
292  es = ES_NOFIX;
293  pathset_h(hall, x0, w, w);
294  }
295  if (hall[z].d == hall[y].bounds-hall[z].bounds) {
296  pathset_h(hall, hall[y].h, j+1, y);
297  hall[y].h = j+1;
298  }
299  }
300 
301  return es;
302  }
303 
304  template<class View>
306  prop_bnd(Space& home, ViewArray<View>& x, int& min_x, int& max_x) {
307 
308  const int n = x.size();
309 
310  Region r(home);
311 
312  int* minsorted = r.alloc<int>(n);
313  int* maxsorted = r.alloc<int>(n);
314 
315  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
316 
317  if (d < static_cast<unsigned int>(n))
318  return ES_FAILED;
319 
320  if (d > 2*static_cast<unsigned int>(n)) {
321  for (int i = n; i--; )
322  minsorted[i]=maxsorted[i]=i;
323 
324  MinIncIdx<View> min_inc(x);
325  Support::quicksort<int,MinIncIdx<View> >(minsorted, n, min_inc);
326  MaxIncIdx<View> max_inc(x);
327  Support::quicksort<int,MaxIncIdx<View> >(maxsorted, n, max_inc);
328  } else {
329 
330  int* minbucket = r.alloc<int>(d);
331  int* maxbucket = r.alloc<int>(d);
332  for (unsigned int i=d; i--; )
333  minbucket[i]=maxbucket[i]=0;
334 
335  for (int i=n; i--; ) {
336  minbucket[x[i].min() - min_x]++;
337  maxbucket[x[i].max() - min_x]++;
338  }
339 
340  int c_min = 0, c_max = 0;
341  for (unsigned int i=0; i<d; i++) {
342  int t_min = minbucket[i];
343  int t_max = maxbucket[i];
344  minbucket[i] = c_min; c_min += t_min;
345  maxbucket[i] = c_max; c_max += t_max;
346  }
347  assert((c_min == n) && (c_max == n));
348 
349  for (int i=n; i--; ) {
350  minsorted[minbucket[x[i].min() - min_x]++] = i;
351  maxsorted[maxbucket[x[i].max() - min_x]++] = i;
352  }
353  }
354 
355  // Update minimum and maximum information
356  min_x = x[minsorted[0]].min();
357  max_x = x[maxsorted[n-1]].max();
358 
359 
360  if (d > (static_cast<unsigned int>(Int::Limits::max) / 2U))
361  return prop_bnd<View,long long int>(home,x,minsorted,maxsorted);
362  else
363  return prop_bnd<View,int>(home,x,minsorted,maxsorted);
364  }
365 
366  template<class View>
367  ExecStatus
369  int min = x[x.size()-1].min(), max = x[x.size()-1].max();
370  for (int i=x.size()-1; i--; ) {
371  min = std::min(min,x[i].min());
372  max = std::max(max,x[i].max());
373  }
374  return prop_bnd(home, x, min, max);
375  }
376 
377  template<class View>
378  ExecStatus
380  assert(x.size() > 1);
381 
382  if (View::me(med) == ME_INT_VAL) {
383  ExecStatus es = prop_val<View,false>(home,y);
384  GECODE_ES_CHECK(es);
385  if (y.size() < 2)
386  return home.ES_SUBSUMED(*this);
387  if (es == ES_FIX)
388  return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_BND));
389  }
390 
391  if (y.size() == 2)
392  GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),y[0],y[1]));
393 
394  ExecStatus es = prop_bnd<View>(home,x,min_x,max_x);
395 
396  GECODE_ES_CHECK(es);
397  if (es == ES_NOFIX && View::me(modeventdelta()) == ME_INT_VAL)
398  return es;
399 
400  const int n = x.size();
401 
402  if ((n > 2*y.size()) && (n > 6)) {
403  // If there are many assigned views, try to eliminate them
404  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
405  if (d > 2*static_cast<unsigned int>(n)) {
406  MinInc<View> min_inc;
407  Support::quicksort<View,MinInc<View> >(&x[0], n, min_inc);
408  } else {
409  Region r(home);
410  int* minbucket = r.alloc<int>(d);
411  View* minsorted = r.alloc<View>(n);
412 
413  for (unsigned int i=d; i--; )
414  minbucket[i]=0;
415  for (int i=n; i--; )
416  minbucket[x[i].min() - min_x]++;
417 
418  int c_min = 0;
419  for (unsigned int i=0; i<d; i++) {
420  int t_min = minbucket[i];
421  minbucket[i] = c_min; c_min += t_min;
422  }
423  assert(c_min == n);
424 
425  for (int i=n; i--;)
426  minsorted[minbucket[x[i].min() - min_x]++] = x[i];
427  for (int i=n; i--;)
428  x[i] = minsorted[i];
429  }
430 
431  int i = 0;
432  int j = 0;
433  int max = x[0].max()-1;
434  while (i < n-1) {
435  if (!x[i].assigned() ||
436  (x[i].val() <= max) || (x[i].val() > x[i+1].min())) {
437  // Keep view x[i]
438  max = std::max(max,x[i].max());
439  x[j++]=x[i];
440  }
441  i++;
442  }
443  if (!x[i].assigned() || (x[i].val() <= max))
444  x[j++]=x[i];
445  x.size(j);
446  }
447 
448  if (x.size() < 2)
449  return home.ES_SUBSUMED(*this);
450 
451  if (x.size() == 2)
452  GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),x[0],x[1]));
453 
454  return es;
455  }
456 
457  template<class View>
458  ExecStatus
460  if (x.size() == 2)
461  return Rel::Nq<View>::post(home,x[0],x[1]);
462  if (x.size() > 2)
463  (void) new (home) Bnd<View>(home,x);
464  return ES_OK;
465  }
466 
467 }}}
468 
469 // STATISTICS: int-prop
470 
Rank information
Definition: bnd.hpp:91
Sort-order by increasing maximum (by index)
Definition: bnd.hpp:98
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Propagator for negated equality
Definition: rel.hh:261
static PropCost quadratic(PropCost::Mod m, unsigned int n)
Quadratic complexity for modifier m and size measure n.
Definition: core.hpp:4032
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
int min_x
Minimum (approximation) of view in x.
Definition: distinct.hh:136
ViewArray< View > x
Views on which to perform bounds-propagation.
Definition: distinct.hh:132
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
int ModEvent
Type for modification events.
Definition: core.hpp:146
Base-class for propagators.
Definition: core.hpp:755
bool operator()(const View &x, const View &y)
Definition: bnd.hpp:129
Handle to region.
Definition: region.hpp:61
Propagation has computed fixpoint.
Definition: core.hpp:528
const int max
Largest allowed integer value.
Definition: int.hh:113
Bnd(Home home, ViewArray< View > &x)
Constructor for posting.
Definition: bnd.hpp:42
Computation spaces.
Definition: core.hpp:1362
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: bnd.hpp:379
Base-class for both propagators and branchers.
Definition: core.hpp:666
Gecode::IntSet d(v, 7)
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
IntType pathmin_h(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:163
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Gecode::IntArgs i(4, 1, 2, 3, 4)
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator for view array x.
Definition: bnd.hpp:459
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
ViewArray< View > y
Views on which to perform value-propagation (subset of x)
Definition: distinct.hh:134
Execution has resulted in failure.
Definition: core.hpp:525
Information on Hall intervals.
Definition: bnd.hpp:136
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: bnd.hpp:76
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:56
void pathset_t(HallInfo< IntType > hall[], IntType start, IntType end, IntType to)
Definition: bnd.hpp:143
ViewArray< View > x
Definition: bnd.hpp:100
const Gecode::ModEvent ME_INT_BND
Domain operation has changed the minimum or maximum of the domain.
Definition: var-type.hpp:65
ViewArray< View > x
Definition: bnd.hpp:114
IntType pathmax_h(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:179
View arrays.
Definition: array.hpp:234
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
MinIncIdx(const ViewArray< View > &x0)
Definition: bnd.hpp:117
IntType pathmin_t(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:171
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
void pathset_h(HallInfo< IntType > hall[], IntType start, IntType end, IntType to)
Definition: bnd.hpp:153
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
bool operator()(const int i, const int j)
Definition: bnd.hpp:105
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:132
int max_x
Maximum (approximation) of view in x.
Definition: distinct.hh:138
Binary disequality propagator.
Definition: rel.hh:432
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
MaxIncIdx(const ViewArray< View > &x0)
Definition: bnd.hpp:103
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Bounds consistent distinct propagator.
Definition: distinct.hh:129
Gecode toplevel namespace
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: bnd.hpp:82
IntType pathmax_t(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:187
Sort-order by increasing minimum (direct)
Definition: bnd.hpp:126
IntType
Description of integer types.
Definition: int-type.hpp:43
ExecStatus prop_bnd(Space &home, ViewArray< View > &x, int *minsorted, int *maxsorted)
Definition: bnd.hpp:195
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
virtual size_t dispose(Space &home)
Destructor.
Definition: bnd.hpp:60
Home class for posting propagators
Definition: core.hpp:717
bool operator()(const int i, const int j)
Definition: bnd.hpp:119
Sort-order by increasing minimum (by index)
Definition: bnd.hpp:112
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58