Generated on Fri Aug 24 2012 04:52:16 for Gecode by doxygen 1.8.1.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: 2011-10-10 00:06:06 +1100 (Mon, 10 Oct 2011) $ by $Author: schulte $
11  * $Revision: 12431 $
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  class HallInfo {
136  public:
137  int bounds, t, d, h;
138  };
139 
140  forceinline void
141  pathset_t(HallInfo hall[], int start, int end, int to) {
142  int k, l;
143  for (l=start; (k=l) != end; hall[k].t=to) {
144  l = hall[k].t;
145  }
146  }
147 
148  forceinline void
149  pathset_h(HallInfo hall[], int start, int end, int to) {
150  int k, l;
151  for (l=start; (k=l) != end; hall[k].h=to) {
152  l = hall[k].h;
153  }
154  }
155 
156  forceinline int
157  pathmin_h(const HallInfo hall[], int i) {
158  while (hall[i].h < i)
159  i = hall[i].h;
160  return i;
161  }
162 
163  forceinline int
164  pathmin_t(const HallInfo hall[], int i) {
165  while (hall[i].t < i)
166  i = hall[i].t;
167  return i;
168  }
169 
170  forceinline int
171  pathmax_h(const HallInfo hall[], int i) {
172  while (hall[i].h > i)
173  i = hall[i].h;
174  return i;
175  }
176 
177  forceinline int
178  pathmax_t(const HallInfo hall[], int i) {
179  while (hall[i].t > i)
180  i = hall[i].t;
181  return i;
182  }
183 
184  template<class View>
185  ExecStatus
186  prop_bnd(Space& home, ViewArray<View>& x, int& min_x, int& max_x) {
187  const int n = x.size();
188 
189  Region r(home);
190 
191  int* minsorted = r.alloc<int>(n);
192  int* maxsorted = r.alloc<int>(n);
193 
194  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
195 
196  if (d < static_cast<unsigned int>(n))
197  return ES_FAILED;
198 
199  if (d > 2*static_cast<unsigned int>(n)) {
200  for (int i = n; i--; )
201  minsorted[i]=maxsorted[i]=i;
202 
203  MinIncIdx<View> min_inc(x);
204  Support::quicksort<int,MinIncIdx<View> >(minsorted, n, min_inc);
205  MaxIncIdx<View> max_inc(x);
206  Support::quicksort<int,MaxIncIdx<View> >(maxsorted, n, max_inc);
207  } else {
208 
209  int* minbucket = r.alloc<int>(d);
210  int* maxbucket = r.alloc<int>(d);
211  for (unsigned int i=d; i--; )
212  minbucket[i]=maxbucket[i]=0;
213 
214  for (int i=n; i--; ) {
215  minbucket[x[i].min() - min_x]++;
216  maxbucket[x[i].max() - min_x]++;
217  }
218 
219  int c_min = 0, c_max = 0;
220  for (unsigned int i=0; i<d; i++) {
221  int t_min = minbucket[i];
222  int t_max = maxbucket[i];
223  minbucket[i] = c_min; c_min += t_min;
224  maxbucket[i] = c_max; c_max += t_max;
225  }
226  assert((c_min == n) && (c_max == n));
227 
228  for (int i=n; i--; ) {
229  minsorted[minbucket[x[i].min() - min_x]++] = i;
230  maxsorted[maxbucket[x[i].max() - min_x]++] = i;
231  }
232  }
233 
234  // Update minimum and maximum information
235  min_x = x[minsorted[0]].min();
236  max_x = x[maxsorted[n-1]].max();
237 
238 
239  // Setup rank and bounds info
240  HallInfo* hall = r.alloc<HallInfo>(2*n+2);
241  Rank* rank = r.alloc<Rank>(n);
242 
243  int nb = 0;
244  {
245  int min = x[minsorted[0]].min();
246  int max = x[maxsorted[0]].max() + 1;
247  int last = min - 2;
248 
249  hall[0].bounds = last;
250 
251  int i = 0;
252  int j = 0;
253  while (true) {
254  if ((i < n) && (min < max)) {
255  if (min != last)
256  hall[++nb].bounds = last = min;
257  rank[minsorted[i]].min = nb;
258  if (++i < n)
259  min = x[minsorted[i]].min();
260  } else {
261  if (max != last)
262  hall[++nb].bounds = last = max;
263  rank[maxsorted[j]].max = nb;
264  if (++j == n)
265  break;
266  max = x[maxsorted[j]].max() + 1;
267  }
268  }
269  hall[nb+1].bounds = hall[nb].bounds + 2;
270  }
271 
272  // If tells cross holes, we do not compute a fixpoint
273  ExecStatus es = ES_FIX;
274 
275  // Propagate lower bounds
276  for (int i=nb+2; --i;) {
277  hall[i].t = hall[i].h = i-1;
278  hall[i].d = hall[i].bounds - hall[i-1].bounds;
279  }
280  for (int i=0; i<n; i++) { // visit intervals in increasing max order
281  int x0 = rank[maxsorted[i]].min;
282  int z = pathmax_t(hall, x0+1);
283  int j = hall[z].t;
284  if (--hall[z].d == 0)
285  hall[z = pathmax_t(hall, hall[z].t=z+1)].t = j;
286  pathset_t(hall, x0+1, z, z); // path compression
287  int y = rank[maxsorted[i]].max;
288  if (hall[z].d < hall[z].bounds-hall[y].bounds)
289  return ES_FAILED;
290  if (hall[x0].h > x0) {
291  int w = pathmax_h(hall, hall[x0].h);
292  int m = hall[w].bounds;
293  ModEvent me = x[maxsorted[i]].gq(home,m);
294  if (me_failed(me))
295  return ES_FAILED;
296  if ((me == ME_INT_VAL) ||
297  ((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
298  es = ES_NOFIX;
299  pathset_h(hall, x0, w, w); // path compression
300  }
301  if (hall[z].d == hall[z].bounds-hall[y].bounds) {
302  pathset_h(hall, hall[y].h, j-1, y); // mark hall interval
303  hall[y].h = j-1;
304  }
305  }
306 
307  // Propagate upper bounds
308  for (int i=nb+1; i--;) {
309  hall[i].t = hall[i].h = i+1;
310  hall[i].d = hall[i+1].bounds - hall[i].bounds;
311  }
312  for (int i=n; --i>=0; ) { // visit intervals in decreasing min order
313  int x0 = rank[minsorted[i]].max;
314  int z = pathmin_t(hall, x0-1);
315  int j = hall[z].t;
316  if (--hall[z].d == 0)
317  hall[z = pathmin_t(hall, hall[z].t=z-1)].t = j;
318  pathset_t(hall, x0-1, z, z);
319  int y = rank[minsorted[i]].min;
320  if (hall[z].d < hall[y].bounds-hall[z].bounds)
321  return ES_FAILED;
322  if (hall[x0].h < x0) {
323  int w = pathmin_h(hall, hall[x0].h);
324  int m = hall[w].bounds - 1;
325  ModEvent me = x[minsorted[i]].lq(home,m);
326  if (me_failed(me))
327  return ES_FAILED;
328  if ((me == ME_INT_VAL) ||
329  ((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
330  es = ES_NOFIX;
331  pathset_h(hall, x0, w, w);
332  }
333  if (hall[z].d == hall[y].bounds-hall[z].bounds) {
334  pathset_h(hall, hall[y].h, j+1, y);
335  hall[y].h = j+1;
336  }
337  }
338 
339  return es;
340  }
341 
342  template<class View>
343  ExecStatus
345  int min = x[x.size()-1].min(), max = x[x.size()-1].max();
346  for (int i=x.size()-1; i--; ) {
347  min = std::min(min,x[i].min());
348  max = std::max(max,x[i].max());
349  }
350  return prop_bnd(home, x, min, max);
351  }
352 
353  template<class View>
354  ExecStatus
356  assert(x.size() > 1);
357 
358  if (View::me(med) == ME_INT_VAL) {
359  ExecStatus es = prop_val<View,false>(home,y);
360  GECODE_ES_CHECK(es);
361  if (y.size() < 2)
362  return home.ES_SUBSUMED(*this);
363  if (es == ES_FIX)
364  return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_BND));
365  }
366 
367  if (y.size() == 2)
368  GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),y[0],y[1]));
369 
370  ExecStatus es = prop_bnd<View>(home,x,min_x,max_x);
371 
372  GECODE_ES_CHECK(es);
373 
374  const int n = x.size();
375 
376  if ((n > 2*y.size()) && (n > 6)) {
377  // If there are many assigned views, try to eliminate them
378  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
379  if (d > 2*static_cast<unsigned int>(n)) {
380  MinInc<View> min_inc;
381  Support::quicksort<View,MinInc<View> >(&x[0], n, min_inc);
382  } else {
383  Region r(home);
384  int* minbucket = r.alloc<int>(d);
385  View* minsorted = r.alloc<View>(n);
386 
387  for (unsigned int i=d; i--; )
388  minbucket[i]=0;
389  for (int i=n; i--; )
390  minbucket[x[i].min() - min_x]++;
391 
392  int c_min = 0;
393  for (unsigned int i=0; i<d; i++) {
394  int t_min = minbucket[i];
395  minbucket[i] = c_min; c_min += t_min;
396  }
397  assert(c_min == n);
398 
399  for (int i=n; i--;)
400  minsorted[minbucket[x[i].min() - min_x]++] = x[i];
401  for (int i=n; i--;)
402  x[i] = minsorted[i];
403  }
404 
405  int i = 0;
406  int j = 0;
407  int max = x[0].max()-1;
408  while (i < n-1) {
409  if (!x[i].assigned() ||
410  (x[i].val() <= max) || (x[i].val() > x[i+1].min())) {
411  // Keep view x[i]
412  max = std::max(max,x[i].max());
413  x[j++]=x[i];
414  }
415  i++;
416  }
417  if (!x[i].assigned() || (x[i].val() <= max))
418  x[j++]=x[i];
419  x.size(j);
420  }
421 
422  if (x.size() < 2)
423  return home.ES_SUBSUMED(*this);
424 
425  if (x.size() == 2)
426  GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),x[0],x[1]));
427 
428  return es;
429  }
430 
431  template<class View>
432  ExecStatus
434  if (x.size() == 2)
435  return Rel::Nq<View>::post(home,x[0],x[1]);
436  if (x.size() > 2)
437  (void) new (home) Bnd<View>(home,x);
438  return ES_OK;
439  }
440 
441 }}}
442 
443 // STATISTICS: int-prop
444