Generated on Thu Jul 25 2019 00:00:00 for Gecode by doxygen 1.8.15
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: 2016-11-08 17:23:24 +0100 (Tue, 08 Nov 2016) $ by $Author: schulte $
11  * $Revision: 15253 $
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  template<class View>
90  void
92  y.reschedule(home,*this,PC_INT_BND);
93  }
94 
95 
97  class Rank {
98  public:
99  int min, max;
100  };
101 
103  template<class View>
104  class MaxIncIdx {
105  protected:
107  public:
109  MaxIncIdx(const ViewArray<View>& x0) : x(x0) {}
110  forceinline bool
111  operator ()(const int i, const int j) {
112  return x[i].max() < x[j].max();
113  }
114  };
115 
117  template<class View>
118  class MinIncIdx {
119  protected:
121  public:
123  MinIncIdx(const ViewArray<View>& x0) : x(x0) {}
124  forceinline bool
125  operator ()(const int i, const int j) {
126  return x[i].min() < x[j].min();
127  }
128  };
129 
131  template<class View>
132  class MinInc {
133  public:
134  forceinline bool
135  operator ()(const View& x, const View& y) {
136  return x.min() < y.min();
137  }
138  };
139 
141  template<class IntType>
142  class HallInfo {
143  public:
145  };
146 
147  template<class IntType>
148  forceinline void
150  IntType start, IntType end, IntType to) {
151  IntType k, l;
152  for (l=start; (k=l) != end; hall[k].t=to) {
153  l = hall[k].t;
154  }
155  }
156 
157  template<class IntType>
158  forceinline void
160  IntType start, IntType end, IntType to) {
161  IntType k, l;
162  for (l=start; (k=l) != end; hall[k].h=to) {
163  l = hall[k].h;
164  }
165  }
166 
167  template<class IntType>
170  while (hall[i].h < i)
171  i = hall[i].h;
172  return i;
173  }
174 
175  template<class IntType>
178  while (hall[i].t < i)
179  i = hall[i].t;
180  return i;
181  }
182 
183  template<class IntType>
186  while (hall[i].h > i)
187  i = hall[i].h;
188  return i;
189  }
190 
191  template<class IntType>
194  while (hall[i].t > i)
195  i = hall[i].t;
196  return i;
197  }
198 
199  template<class View, class IntType>
202  int* minsorted, int* maxsorted) {
203  const int n = x.size();
204 
205  Region r(home);
206 
207  // Setup rank and bounds info
208  HallInfo<IntType>* hall = r.alloc<HallInfo<IntType> >(2*n+2);
209  Rank* rank = r.alloc<Rank>(n);
210 
211  int nb = 0;
212  {
213  IntType min = x[minsorted[0]].min();
214  IntType max = x[maxsorted[0]].max() + 1;
215  IntType last = min - 2;
216 
217  hall[0].bounds = last;
218 
219  int i = 0;
220  int j = 0;
221  while (true) {
222  if ((i < n) && (min < max)) {
223  if (min != last)
224  hall[++nb].bounds = last = min;
225  rank[minsorted[i]].min = nb;
226  if (++i < n)
227  min = x[minsorted[i]].min();
228  } else {
229  if (max != last)
230  hall[++nb].bounds = last = max;
231  rank[maxsorted[j]].max = nb;
232  if (++j == n)
233  break;
234  max = x[maxsorted[j]].max() + 1;
235  }
236  }
237  hall[nb+1].bounds = hall[nb].bounds + 2;
238  }
239 
240  // If tells cross holes, we do not compute a fixpoint
241  ExecStatus es = ES_FIX;
242 
243  // Propagate lower bounds
244  for (int i=nb+2; --i;) {
245  hall[i].t = hall[i].h = i-1;
246  hall[i].d = hall[i].bounds - hall[i-1].bounds;
247  }
248  for (int i=0; i<n; i++) { // visit intervals in increasing max order
249  IntType x0 = rank[maxsorted[i]].min;
250  IntType z = pathmax_t(hall, x0+1);
251  IntType j = hall[z].t;
252  if (--hall[z].d == 0)
253  hall[z = pathmax_t(hall, hall[z].t=z+1)].t = j;
254  pathset_t(hall, x0+1, z, z); // path compression
255  IntType y = rank[maxsorted[i]].max;
256  if (hall[z].d < hall[z].bounds-hall[y].bounds)
257  return ES_FAILED;
258  if (hall[x0].h > x0) {
259  IntType w = pathmax_h(hall, hall[x0].h);
260  IntType m = hall[w].bounds;
261  ModEvent me = x[maxsorted[i]].gq(home,m);
262  if (me_failed(me))
263  return ES_FAILED;
264  if ((me == ME_INT_VAL) ||
265  ((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
266  es = ES_NOFIX;
267  pathset_h(hall, x0, w, w); // path compression
268  }
269  if (hall[z].d == hall[z].bounds-hall[y].bounds) {
270  pathset_h(hall, hall[y].h, j-1, y); // mark hall interval
271  hall[y].h = j-1;
272  }
273  }
274 
275  // Propagate upper bounds
276  for (int i=nb+1; i--;) {
277  hall[i].t = hall[i].h = i+1;
278  hall[i].d = hall[i+1].bounds - hall[i].bounds;
279  }
280  for (int i=n; --i>=0; ) { // visit intervals in decreasing min order
281  IntType x0 = rank[minsorted[i]].max;
282  IntType z = pathmin_t(hall, x0-1);
283  IntType j = hall[z].t;
284  if (--hall[z].d == 0)
285  hall[z = pathmin_t(hall, hall[z].t=z-1)].t = j;
286  pathset_t(hall, x0-1, z, z);
287  IntType y = rank[minsorted[i]].min;
288  if (hall[z].d < hall[y].bounds-hall[z].bounds)
289  return ES_FAILED;
290  if (hall[x0].h < x0) {
291  IntType w = pathmin_h(hall, hall[x0].h);
292  IntType m = hall[w].bounds - 1;
293  ModEvent me = x[minsorted[i]].lq(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);
300  }
301  if (hall[z].d == hall[y].bounds-hall[z].bounds) {
302  pathset_h(hall, hall[y].h, j+1, y);
303  hall[y].h = j+1;
304  }
305  }
306 
307  return es;
308  }
309 
310  template<class View>
312  prop_bnd(Space& home, ViewArray<View>& x, int& min_x, int& max_x) {
313 
314  const int n = x.size();
315 
316  Region r(home);
317 
318  int* minsorted = r.alloc<int>(n);
319  int* maxsorted = r.alloc<int>(n);
320 
321  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
322 
323  if (d < static_cast<unsigned int>(n))
324  return ES_FAILED;
325 
326  if (d > 2*static_cast<unsigned int>(n)) {
327  for (int i = n; i--; )
328  minsorted[i]=maxsorted[i]=i;
329 
330  MinIncIdx<View> min_inc(x);
331  Support::quicksort<int,MinIncIdx<View> >(minsorted, n, min_inc);
332  MaxIncIdx<View> max_inc(x);
333  Support::quicksort<int,MaxIncIdx<View> >(maxsorted, n, max_inc);
334  } else {
335 
336  int* minbucket = r.alloc<int>(d);
337  int* maxbucket = r.alloc<int>(d);
338  for (unsigned int i=d; i--; )
339  minbucket[i]=maxbucket[i]=0;
340 
341  for (int i=n; i--; ) {
342  minbucket[x[i].min() - min_x]++;
343  maxbucket[x[i].max() - min_x]++;
344  }
345 
346  int c_min = 0, c_max = 0;
347  for (unsigned int i=0; i<d; i++) {
348  int t_min = minbucket[i];
349  int t_max = maxbucket[i];
350  minbucket[i] = c_min; c_min += t_min;
351  maxbucket[i] = c_max; c_max += t_max;
352  }
353  assert((c_min == n) && (c_max == n));
354 
355  for (int i=n; i--; ) {
356  minsorted[minbucket[x[i].min() - min_x]++] = i;
357  maxsorted[maxbucket[x[i].max() - min_x]++] = i;
358  }
359  }
360 
361  // Update minimum and maximum information
362  min_x = x[minsorted[0]].min();
363  max_x = x[maxsorted[n-1]].max();
364 
365 
366  if (d > (static_cast<unsigned int>(Int::Limits::max) / 2U))
367  return prop_bnd<View,long long int>(home,x,minsorted,maxsorted);
368  else
369  return prop_bnd<View,int>(home,x,minsorted,maxsorted);
370  }
371 
372  template<class View>
373  ExecStatus
375  int min = x[x.size()-1].min(), max = x[x.size()-1].max();
376  for (int i=x.size()-1; i--; ) {
377  min = std::min(min,x[i].min());
378  max = std::max(max,x[i].max());
379  }
380  return prop_bnd(home, x, min, max);
381  }
382 
383  template<class View>
384  ExecStatus
386  assert(x.size() > 1);
387 
388  if (View::me(med) == ME_INT_VAL) {
389  ExecStatus es = prop_val<View,false>(home,y);
390  GECODE_ES_CHECK(es);
391  if (y.size() < 2)
392  return home.ES_SUBSUMED(*this);
393  if (es == ES_FIX)
394  return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_BND));
395  }
396 
397  if (y.size() == 2)
398  GECODE_REWRITE(*this,(Rel::Nq<View,View>::post(home(*this),y[0],y[1])));
399 
400  ExecStatus es = prop_bnd<View>(home,x,min_x,max_x);
401 
402  GECODE_ES_CHECK(es);
403  if (es == ES_NOFIX && View::me(modeventdelta()) == ME_INT_VAL)
404  return es;
405 
406  const int n = x.size();
407 
408  if ((n > 2*y.size()) && (n > 6)) {
409  // If there are many assigned views, try to eliminate them
410  unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
411  if (d > 2*static_cast<unsigned int>(n)) {
412  MinInc<View> min_inc;
413  Support::quicksort<View,MinInc<View> >(&x[0], n, min_inc);
414  } else {
415  Region r(home);
416  int* minbucket = r.alloc<int>(d);
417  View* minsorted = r.alloc<View>(n);
418 
419  for (unsigned int i=d; i--; )
420  minbucket[i]=0;
421  for (int i=n; i--; )
422  minbucket[x[i].min() - min_x]++;
423 
424  int c_min = 0;
425  for (unsigned int i=0; i<d; i++) {
426  int t_min = minbucket[i];
427  minbucket[i] = c_min; c_min += t_min;
428  }
429  assert(c_min == n);
430 
431  for (int i=n; i--;)
432  minsorted[minbucket[x[i].min() - min_x]++] = x[i];
433  for (int i=n; i--;)
434  x[i] = minsorted[i];
435  }
436 
437  int i = 0;
438  int j = 0;
439  int max = x[0].max()-1;
440  while (i < n-1) {
441  if (!x[i].assigned() ||
442  (x[i].val() <= max) || (x[i].val() > x[i+1].min())) {
443  // Keep view x[i]
444  max = std::max(max,x[i].max());
445  x[j++]=x[i];
446  }
447  i++;
448  }
449  if (!x[i].assigned() || (x[i].val() <= max))
450  x[j++]=x[i];
451  x.size(j);
452  }
453 
454  if (x.size() < 2)
455  return home.ES_SUBSUMED(*this);
456 
457  if (x.size() == 2)
458  GECODE_REWRITE(*this,(Rel::Nq<View,View>::post(home(*this),x[0],x[1])));
459 
460  return es;
461  }
462 
463  template<class View>
464  ExecStatus
466  if (x.size() == 2)
467  return Rel::Nq<View,View>::post(home,x[0],x[1]);
468  if (x.size() > 2)
469  (void) new (home) Bnd<View>(home,x);
470  return ES_OK;
471  }
472 
473 }}}
474 
475 // STATISTICS: int-prop
476 
Rank information
Definition: bnd.hpp:97
Sort-order by increasing maximum (by index)
Definition: bnd.hpp:104
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:120
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Propagator for negated equality
Definition: rel.hh:267
static PropCost quadratic(PropCost::Mod m, unsigned int n)
Quadratic complexity for modifier m and size measure n.
Definition: core.hpp:4832
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:4841
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: bnd.hpp:82
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:53
int ModEvent
Type for modification events.
Definition: core.hpp:142
Base-class for propagators.
Definition: core.hpp:1092
bool operator()(const View &x, const View &y)
Definition: bnd.hpp:135
Handle to region.
Definition: region.hpp:61
Propagation has computed fixpoint.
Definition: core.hpp:545
const int max
Largest allowed integer value.
Definition: int.hh:116
Bnd(Home home, ViewArray< View > &x)
Constructor for posting.
Definition: bnd.hpp:42
Computation spaces.
Definition: core.hpp:1748
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: bnd.hpp:385
Base-class for both propagators and branchers.
Definition: core.hpp:696
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:95
IntType pathmin_h(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:169
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
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:465
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:542
Information on Hall intervals.
Definition: bnd.hpp:142
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:149
ViewArray< View > x
Definition: bnd.hpp:106
virtual void reschedule(Space &home)
Schedule function.
Definition: bnd.hpp:91
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:120
IntType pathmax_h(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:185
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition: set.hh:784
View arrays.
Definition: array.hpp:234
ExecStatus ES_FIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has computed partial fixpoint
Definition: core.hpp:3620
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
MinIncIdx(const ViewArray< View > &x0)
Definition: bnd.hpp:123
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:784
IntType pathmin_t(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:177
void pathset_h(HallInfo< IntType > hall[], IntType start, IntType end, IntType to)
Definition: bnd.hpp:159
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
bool operator()(const int i, const int j)
Definition: bnd.hpp:111
Propagation cost.
Definition: core.hpp:554
ExecStatus
Definition: core.hpp:540
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
int max_x
Maximum (approximation) of view in x.
Definition: distinct.hh:138
Binary disequality propagator.
Definition: rel.hh:464
Post propagator for SetVar x
Definition: set.hh:784
Execution is okay.
Definition: core.hpp:544
Propagation has not computed fixpoint.
Definition: core.hpp:543
MaxIncIdx(const ViewArray< View > &x0)
Definition: bnd.hpp:109
Bounds consistent distinct propagator.
Definition: distinct.hh:129
Gecode toplevel namespace
IntType pathmax_t(const HallInfo< IntType > hall[], IntType i)
Definition: bnd.hpp:193
Sort-order by increasing minimum (direct)
Definition: bnd.hpp:132
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:201
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
virtual size_t dispose(Space &home)
Destructor.
Definition: bnd.hpp:60
Home class for posting propagators
Definition: core.hpp:922
bool operator()(const int i, const int j)
Definition: bnd.hpp:125
Sort-order by increasing minimum (by index)
Definition: bnd.hpp:118
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58