Generated on Mon Nov 25 2013 15:56:00 for Gecode by doxygen 1.8.5
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: 2013-10-11 09:00:31 +0200 (Fri, 11 Oct 2013) $ by $Author: tack $
15  * $Revision: 14022 $
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 
546  if (x1.assigned()) {
547  for (int i = iv.size(); i--; )
548  if (iv[i].view.in(x1.val()))
549  return shared(x0,x1) ? ES_NOFIX : ES_FIX;
550  return ES_FAILED;
551  } else {
552  Region r(home);
553  ViewRanges<VA>* i_view = r.alloc<ViewRanges<VA> >(iv.size());
554  for (int i = iv.size(); i--; )
555  i_view[i].init(iv[i].view);
556  Iter::Ranges::NaryUnion i_val(r, i_view, iv.size());
557  ModEvent me = x1.inter_r(home,i_val);
558  r.free<ViewRanges<VA> >(i_view,iv.size());
559  GECODE_ME_CHECK(me);
560  return (shared(x0,x1) || me_modified(me)) ? ES_NOFIX : ES_FIX;
561  }
562  }
563 
564 }}}
565 
566 // STATISTICS: int-prop
567 
Domain consistent element propagator for array of views.
Definition: element.hh:317
Relation may hold or not.
Definition: view.hpp:1616
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:3964
Binary domain consistent equality propagator.
Definition: rel.hh:71
View(Space &home, bool share, View &p)
Constructor for cloning p.
Definition: view.hpp:240
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2896
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
VC x1
View for result.
Definition: element.hh:267
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
ExecStatus ES_NOFIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has not computed partial fixpoint
Definition: core.hpp:2909
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Definition: view.hpp:133
IdxView< View > & operator[](int n)
Access element n.
Definition: view.hpp:119
int ModEvent
Type for modification events.
Definition: core.hpp:146
Base-class for propagators.
Definition: core.hpp:755
void init(const IdxView< View > *, const IdxView< View > *)
Definition: view.hpp:296
static ExecStatus post(Home home, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition: view.hpp:394
Handle to region.
Definition: region.hpp:61
Value iterator for integer views.
Definition: view.hpp:94
Propagation has computed fixpoint.
Definition: core.hpp:528
Computation spaces.
Definition: core.hpp:1325
Base-class for both propagators and branchers.
Definition: core.hpp:666
Range iterator for integer views.
Definition: view.hpp:54
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: view.hpp:249
static IdxView * allocate(Space &, int)
Definition: view.hpp:73
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2354
void update(Space &home, bool share, IdxViewArray< View > &x)
Cloning.
Definition: view.hpp:148
Class for bounds-equality test.
Definition: view.hpp:165
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: view.hpp:498
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)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
void cancel(Space &home, Propagator &p, PropCond pc)
Definition: view.hpp:141
static ExecStatus post(Home home, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition: view.hpp:476
IdxViewArray< VA > iv
Current index-view map.
Definition: element.hh:263
Execution has resulted in failure.
Definition: core.hpp:525
bool operator()(void) const
Definition: view.hpp:302
int val(void) const
Return assigned value (only if assigned)
Definition: constint.hpp:66
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Relation does not hold.
Definition: view.hpp:1615
RelTest
Result of testing relation.
Definition: view.hpp:1614
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
int PropCond
Type for propagation conditions.
Definition: core.hpp:156
Range iterator for union of iterators.
unsigned int size(I &i)
Size of all ranges of range iterator i.
Binary bounds consistent equality propagator.
Definition: rel.hh:107
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition: var-type.hpp:100
RelTest operator()(VA, VC)
Definition: view.hpp:212
Expensive.
Definition: core.hpp:564
struct Gecode::@512::NNF::@54::@55 b
For binary nodes (and, or, eqv)
Base-class for element propagator for array of views.
Definition: element.hh:260
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
RelTest rtest_eq_dom(View x, View y)
Test whether views x and y are equal (use full domain information)
Definition: rel-test.hpp:68
Passing integer variables.
Definition: int.hh:634
Passing Boolean variables.
Definition: int.hh:688
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: view.hpp:416
const int v[7]
Definition: distinct.cpp:206
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
Value iterator for indices in index-view map.
Definition: view.hpp:274
ExecStatus scan(Space &home, IdxViewArray< VA > &iv, VB x0, VC x1, Propagator &p, RelTest rt)
Definition: view.hpp:327
Constant integer view.
Definition: view.hpp:804
VB x0
View for index.
Definition: element.hh:265
Integer view for integer variables.
Definition: view.hpp:129
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: view.hpp:422
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: view.hpp:258
ViewBnd(Space &home, bool share, ViewBnd &p)
Constructor for cloning p.
Definition: view.hpp:411
Class for pair of index and view.
Definition: view.hpp:64
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2800
Propagation cost.
Definition: core.hpp:537
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
An array of IndexView pairs.
Definition: element.hh:214
ExecStatus
Definition: core.hpp:523
int size(void) const
Return the current size.
Definition: view.hpp:107
#define forceinline
Definition: config.hpp:129
void free(T *b, long unsigned int n)
Delete n objects allocated from the region starting at b.
Definition: region.hpp:352
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition: modevent.hpp:63
ViewDom(Space &home, bool share, ViewDom &p)
Constructor for cloning p.
Definition: view.hpp:493
Class to get VarArg type for view.
Definition: element.hh:207
Execution is okay.
Definition: core.hpp:527
RelTest rtest_eq_bnd(View x, View y)
Test whether views x and y are equal (use bounds information)
Definition: rel-test.hpp:47
Propagation has not computed fixpoint.
Definition: core.hpp:526
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
IdxViewArray(void)
Default constructor.
Definition: view.hpp:78
RelTest operator()(VA, VC)
Definition: view.hpp:201
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: view.hpp:512
struct Gecode::@512::NNF::@54::@56 a
For atomic nodes.
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: view.hpp:505
Home class for posting propagators
Definition: core.hpp:717
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
Class for domain-equality test.
Definition: view.hpp:184
Relation does hold.
Definition: view.hpp:1617
Bounds consistent element propagator for array of views.
Definition: element.hh:287
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58
Boolean view for Boolean variables.
Definition: view.hpp:1315