Generated on Mon Sep 22 2014 12:49:27 for Gecode by doxygen 1.8.7
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: 2012-10-08 03:57:23 +0200 (Mon, 08 Oct 2012) $ by $Author: tack $
11  * $Revision: 13143 $
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  if (es == ES_NOFIX && View::me(modeventdelta()) == ME_INT_VAL)
374  return es;
375 
376  const int n = x.size();
377 
378  if ((n > 2*y.size()) && (n > 6)) {
379  // If there are many assigned views, try to eliminate them
380  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
381  if (d > 2*static_cast<unsigned int>(n)) {
382  MinInc<View> min_inc;
383  Support::quicksort<View,MinInc<View> >(&x[0], n, min_inc);
384  } else {
385  Region r(home);
386  int* minbucket = r.alloc<int>(d);
387  View* minsorted = r.alloc<View>(n);
388 
389  for (unsigned int i=d; i--; )
390  minbucket[i]=0;
391  for (int i=n; i--; )
392  minbucket[x[i].min() - min_x]++;
393 
394  int c_min = 0;
395  for (unsigned int i=0; i<d; i++) {
396  int t_min = minbucket[i];
397  minbucket[i] = c_min; c_min += t_min;
398  }
399  assert(c_min == n);
400 
401  for (int i=n; i--;)
402  minsorted[minbucket[x[i].min() - min_x]++] = x[i];
403  for (int i=n; i--;)
404  x[i] = minsorted[i];
405  }
406 
407  int i = 0;
408  int j = 0;
409  int max = x[0].max()-1;
410  while (i < n-1) {
411  if (!x[i].assigned() ||
412  (x[i].val() <= max) || (x[i].val() > x[i+1].min())) {
413  // Keep view x[i]
414  max = std::max(max,x[i].max());
415  x[j++]=x[i];
416  }
417  i++;
418  }
419  if (!x[i].assigned() || (x[i].val() <= max))
420  x[j++]=x[i];
421  x.size(j);
422  }
423 
424  if (x.size() < 2)
425  return home.ES_SUBSUMED(*this);
426 
427  if (x.size() == 2)
428  GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),x[0],x[1]));
429 
430  return es;
431  }
432 
433  template<class View>
434  ExecStatus
436  if (x.size() == 2)
437  return Rel::Nq<View>::post(home,x[0],x[1]);
438  if (x.size() > 2)
439  (void) new (home) Bnd<View>(home,x);
440  return ES_OK;
441  }
442 
443 }}}
444 
445 // STATISTICS: int-prop
446 
Rank information
Definition: bnd.hpp:91
Sort-order by increasing maximum (by index)
Definition: bnd.hpp:98
void pathset_h(HallInfo hall[], int start, int end, int to)
Definition: bnd.hpp:149
#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:3955
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:3964
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 pathmin_t(const HallInfo hall[], int i)
Definition: bnd.hpp:164
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
ExecStatus prop_bnd(Space &home, ViewArray< View > &x, int &min_x, int &max_x)
Perform bounds consistent distinct propagation.
Definition: bnd.hpp:186
Handle to region.
Definition: region.hpp:61
int pathmax_t(const HallInfo hall[], int i)
Definition: bnd.hpp:178
Propagation has computed fixpoint.
Definition: core.hpp:528
Bnd(Home home, ViewArray< View > &x)
Constructor for posting.
Definition: bnd.hpp:42
Computation spaces.
Definition: core.hpp:1325
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: bnd.hpp:355
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
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:435
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
int pathmin_h(const HallInfo hall[], int i)
Definition: bnd.hpp:157
ViewArray< View > y
Views on which to perform value-propagation (subset of x)
Definition: distinct.hh:134
int pathmax_h(const HallInfo hall[], int i)
Definition: bnd.hpp:171
Execution has resulted in failure.
Definition: core.hpp:525
Information on Hall intervals.
Definition: bnd.hpp:135
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
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
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
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2800
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:129
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
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: bnd.hpp:82
void pathset_t(HallInfo hall[], int start, int end, int to)
Definition: bnd.hpp:141
Sort-order by increasing minimum (direct)
Definition: bnd.hpp:126
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