Generated on Sat May 25 2013 18:00:34 for Gecode by doxygen 1.8.3.1
view.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  * Contributing authors:
7  * Guido Tack <tack@gecode.org>
8  *
9  * Copyright:
10  * Christian Schulte, 2004
11  * Guido Tack, 2004
12  *
13  * Last modified:
14  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
15  * $Revision: 13068 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 #include <algorithm>
43 
44 namespace Gecode { namespace Int { namespace Element {
45 
47  template<>
49  public:
51  };
53  template<>
55  public:
57  };
58 
63  template<class View>
64  class IdxView {
65  public:
66  int idx; View view;
67 
68  static IdxView* allocate(Space&, int);
69  };
70 
71  template<class View>
74  return home.alloc<IdxView<View> >(n);
75  }
76 
77  template<class View>
78  IdxViewArray<View>::IdxViewArray(void) : xs(NULL), n(0) {}
79 
80  template<class View>
82  n = a.n; xs = a.xs;
83  }
84 
85  template<class View>
87  const typename ViewToVarArg<View>::argtype& xa) : xs(NULL) {
88  n = xa.size();
89  if (n>0) {
90  xs = IdxView<View>::allocate(home, n);
91  for (int i = n; i--; ) {
92  xs[i].idx = i; xs[i].view = xa[i];
93  }
94  }
95  }
96 
97  template<class View>
98  IdxViewArray<View>::IdxViewArray(Space& home, int n0) : xs(NULL) {
99  n = n0;
100  if (n>0) {
101  xs = IdxView<View>::allocate(home, n);
102  }
103  }
104 
105  template<class View>
106  forceinline int
108  return n;
109  }
110 
111  template<class View>
112  forceinline void
114  n = n0;
115  }
116 
117  template<class View>
120  assert((i >= 0) && (i < size()));
121  return xs[i];
122  }
123 
124  template<class View>
127  assert((i >= 0) && (i < size()));
128  return xs[i];
129  }
130 
131  template<class View>
132  void
134  bool process) {
135  for (int i = n; i--; )
136  xs[i].view.subscribe(home,p,pc,process);
137  }
138 
139  template<class View>
140  void
142  for (int i = n; i--; )
143  xs[i].view.cancel(home,p,pc);
144  }
145 
146  template<class View>
147  void
149  n = a.size();
150  if (n>0) {
151  xs = IdxView<View>::allocate(home,n);
152  for (int i=n; i--; ) {
153  xs[i].idx = a[i].idx;
154  xs[i].view.update(home,share,a[i].view);
155  }
156  }
157  }
158 
159 
164  template<class VA, class VC>
165  class RelTestBnd {
166  public:
167  RelTest operator ()(VA,VC);
168  };
173  template<class VA>
175  public:
177  };
178 
183  template<class VA, class VC>
184  class RelTestDom {
185  public:
186  RelTest operator ()(VA,VC);
187  };
192  template<class VA>
194  public:
196  };
197 
198 
199  template<class VA, class VC>
202  return rtest_eq_bnd(x,y);
203  }
204  template<class VA>
207  return rtest_eq_bnd(x,y.val());
208  }
209 
210  template<class VA, class VC>
213  return rtest_eq_dom(x,y);
214  }
215  template<class VA>
218  return rtest_eq_dom(x,y.val());
219  }
220 
221 
222 
223 
224  /*
225  * Base class
226  *
227  */
228 
229  template<class VA, class VB, class VC, PropCond pc_ac>
231  VB y0, VC y1)
232  : Propagator(home), iv(iv0), x0(y0), x1(y1) {
233  x0.subscribe(home,*this,PC_INT_DOM);
234  x1.subscribe(home,*this,pc_ac);
235  iv.subscribe(home,*this,pc_ac);
236  }
237 
238  template<class VA, class VB, class VC, PropCond pc_ac>
240  View<VA,VB,VC,pc_ac>::View(Space& home, bool share, View& p)
241  : Propagator(home,share,p) {
242  x0.update(home,share,p.x0);
243  x1.update(home,share,p.x1);
244  iv.update(home,share,p.iv);
245  }
246 
247  template<class VA, class VB, class VC, PropCond pc_ac>
248  PropCost
250  // This is required for subscribing to variables in the
251  // above constructor, but this is then the only time this
252  // virtual function is ever used!
253  return PropCost::linear(PropCost::LO,iv.size()+2);
254  }
255 
256  template<class VA, class VB, class VC, PropCond pc_ac>
257  forceinline size_t
259  x0.cancel(home,*this,PC_INT_DOM);
260  x1.cancel(home,*this,pc_ac);
261  iv.cancel(home,*this,pc_ac);
262  (void) Propagator::dispose(home);
263  return sizeof(*this);
264  }
265 
266 
267 
268 
273  template<class View>
274  class IterIdxView {
275  private:
276  const IdxView<View> *cur, *end;
277  public:
278  IterIdxView(void);
279  IterIdxView(const IdxView<View>*, const IdxView<View>*);
280  void init(const IdxView<View>*, const IdxView<View>*);
281  bool operator ()(void) const;
282  void operator ++(void);
283  int val(void) const;
284  };
285 
286  template<class View>
289  template<class View>
292  const IdxView<View>* e)
293  : cur(b), end(e) {}
294  template<class View>
295  forceinline void
297  const IdxView<View>* e) {
298  cur=b; end=e;
299  }
300  template<class View>
301  forceinline bool
303  return cur < end;
304  }
305  template<class View>
306  forceinline void
308  cur++;
309  }
310  template<class View>
311  forceinline int
313  return cur->idx;
314  }
315 
316 
317 
318 
319  /*
320  * Generic scanning: does all but computing new domain for result
321  * (which is specific to bounds/domain version)
322  *
323  */
324 
325  template<class VA, class VB, class VC, PropCond pc_ac, class RelTest>
326  ExecStatus
328  VB x0, VC x1, Propagator& p, RelTest rt) {
329  assert(iv.size() > 1);
330  /*
331  * Prunes pairs of index, variable
332  * - checks for idx value removed
333  * - checks for disequal variables
334  *
335  */
336  ViewValues<VB> vx0(x0);
337  int i = 0;
338  int j = 0;
339  while (vx0() && (i < iv.size())) {
340  if (iv[i].idx < vx0.val()) {
341  iv[i].view.cancel(home,p,pc_ac);
342  ++i;
343  } else if (iv[i].idx > vx0.val()) {
344  ++vx0;
345  } else {
346  assert(iv[i].idx == vx0.val());
347  switch (rt(iv[i].view,x1)) {
348  case RT_FALSE:
349  iv[i].view.cancel(home,p,pc_ac);
350  break;
351  case RT_TRUE:
352  case RT_MAYBE:
353  iv[j++] = iv[i];
354  break;
355  default: GECODE_NEVER;
356  }
357  ++vx0; ++i;
358  }
359  }
360  while (i < iv.size())
361  iv[i++].view.cancel(home,p,pc_ac);
362  bool adjust = (j<iv.size());
363  iv.size(j);
364 
365  if (iv.size() == 0)
366  return ES_FAILED;
367 
368  if (iv.size() == 1) {
369  GECODE_ME_CHECK(x0.eq(home,iv[0].idx));
370  } else if (adjust) {
371  IterIdxView<VA> v(&iv[0],&iv[0]+iv.size());
372  GECODE_ME_CHECK(x0.narrow_v(home,v,false));
373  assert(x0.size() == static_cast<unsigned int>(iv.size()));
374  }
375  return ES_OK;
376  }
377 
378 
379 
380 
381  /*
382  * Bounds consistent propagator
383  *
384  */
385 
386  template<class VA, class VB, class VC>
389  IdxViewArray<VA>& iv, VB x0, VC x1)
390  : View<VA,VB,VC,PC_INT_BND>(home,iv,x0,x1) {}
391 
392  template<class VA, class VB, class VC>
393  ExecStatus
395  IdxViewArray<VA>& iv, VB x0, VC x1) {
396  GECODE_ME_CHECK(x0.gq(home,0));
397  GECODE_ME_CHECK(x0.le(home,iv.size()));
398  if (x0.assigned()) {
399  (void) new (home) Rel::EqBnd<VA,VC>(home,iv[x0.val()].view,x1);
400  return ES_OK;
401  } else {
402  assert(iv.size()>1);
403  (void) new (home) ViewBnd<VA,VB,VC>(home,iv,x0,x1);
404  }
405  return ES_OK;
406  }
407 
408 
409  template<class VA, class VB, class VC>
412  : View<VA,VB,VC,PC_INT_BND>(home,share,p) {}
413 
414  template<class VA, class VB, class VC>
415  Actor*
416  ViewBnd<VA,VB,VC>::copy(Space& home, bool share) {
417  return new (home) ViewBnd<VA,VB,VC>(home,share,*this);
418  }
419 
420  template<class VA, class VB, class VC>
421  ExecStatus
423  assert(iv.size() > 1);
426  (home,iv,x0,x1,*this,rt)));
427  if (iv.size() == 1) {
428  ExecStatus es = home.ES_SUBSUMED(*this);
429  (void) new (home) Rel::EqBnd<VA,VC>(home,iv[0].view,x1);
430  return es;
431  }
432  assert(iv.size() > 1);
433  // Compute new result
434  int min = iv[iv.size()-1].view.min();
435  int max = iv[iv.size()-1].view.max();
436  for (int i=iv.size()-1; i--; ) {
437  min = std::min(iv[i].view.min(),min);
438  max = std::max(iv[i].view.max(),max);
439  }
440  ExecStatus es = shared(x0,x1) ? ES_NOFIX : ES_FIX;
441  {
442  ModEvent me = x1.lq(home,max);
443  if (me_failed(me))
444  return ES_FAILED;
445  if (me_modified(me) && (x1.max() != max))
446  es = ES_NOFIX;
447  }
448  {
449  ModEvent me = x1.gq(home,min);
450  if (me_failed(me))
451  return ES_FAILED;
452  if (me_modified(me) && (x1.min() != min))
453  es = ES_NOFIX;
454  }
455  return (x1.assigned() && (min == max)) ?
456  home.ES_SUBSUMED(*this) : es;
457  }
458 
459 
460 
461 
462 
463  /*
464  * Domain consistent propagator
465  *
466  */
467 
468  template<class VA, class VB, class VC>
471  IdxViewArray<VA>& iv, VB x0, VC x1)
472  : View<VA,VB,VC,PC_INT_DOM>(home,iv,x0,x1) {}
473 
474  template<class VA, class VB, class VC>
475  ExecStatus
477  IdxViewArray<VA>& iv, VB x0, VC x1){
478  GECODE_ME_CHECK(x0.gq(home,0));
479  GECODE_ME_CHECK(x0.le(home,iv.size()));
480  if (x0.assigned()) {
481  (void) new (home) Rel::EqDom<VA,VC>(home,iv[x0.val()].view,x1);
482  return ES_OK;
483  } else {
484  assert(iv.size()>1);
485  (void) new (home) ViewDom<VA,VB,VC>(home,iv,x0,x1);
486  }
487  return ES_OK;
488  }
489 
490 
491  template<class VA, class VB, class VC>
494  : View<VA,VB,VC,PC_INT_DOM>(home,share,p) {}
495 
496  template<class VA, class VB, class VC>
497  Actor*
498  ViewDom<VA,VB,VC>::copy(Space& home, bool share) {
499  return new (home) ViewDom<VA,VB,VC>(home,share,*this);
500  }
501 
502 
503  template<class VA, class VB, class VC>
504  PropCost
505  ViewDom<VA,VB,VC>::cost(const Space&, const ModEventDelta& med) const {
506  return PropCost::linear((VA::me(med) != ME_INT_DOM) ?
507  PropCost::LO : PropCost::HI, iv.size()+2);
508  }
509 
510  template<class VA, class VB, class VC>
511  ExecStatus
513  assert(iv.size() > 1);
514  if (VA::me(med) != ME_INT_DOM) {
517  (home,iv,x0,x1,*this,rt)));
518  if (iv.size() == 1) {
519  ExecStatus es = home.ES_SUBSUMED(*this);
520  (void) new (home) Rel::EqDom<VA,VC>(home,iv[0].view,x1);
521  return es;
522  }
523  // Compute new result
524  int min = iv[iv.size()-1].view.min();
525  int max = iv[iv.size()-1].view.max();
526  for (int i=iv.size()-1; i--; ) {
527  min = std::min(iv[i].view.min(),min);
528  max = std::max(iv[i].view.max(),max);
529  }
530  GECODE_ME_CHECK(x1.lq(home,max));
531  GECODE_ME_CHECK(x1.gq(home,min));
532  return (x1.assigned() && (min == max)) ?
533  home.ES_SUBSUMED(*this) :
534  home.ES_NOFIX_PARTIAL(*this,VA::med(ME_INT_DOM));
535  }
538  (home,iv,x0,x1,*this,rt)));
539  if (iv.size() == 1) {
540  ExecStatus es = home.ES_SUBSUMED(*this);
541  (void) new (home) Rel::EqDom<VA,VC>(home,iv[0].view,x1);
542  return es;
543  }
544  assert(iv.size() > 1);
545  Region r(home);
546  ViewRanges<VA>* i_view = r.alloc<ViewRanges<VA> >(iv.size());
547  for (int i = iv.size(); i--; )
548  i_view[i].init(iv[i].view);
549  Iter::Ranges::NaryUnion i_val(r, i_view, iv.size());
550  ModEvent me = x1.inter_r(home,i_val);
551  r.free<ViewRanges<VA> >(i_view,iv.size());
552  GECODE_ME_CHECK(me);
553  return (shared(x0,x1) || me_modified(me)) ? ES_NOFIX : ES_FIX;
554  }
555 
556 }}}
557 
558 // STATISTICS: int-prop
559