Generated on Sat Feb 7 2015 02:01:17 for Gecode by doxygen 1.8.9.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: 2015-01-16 14:10:48 +0100 (Fri, 16 Jan 2015) $ by $Author: schulte $
15  * $Revision: 14362 $
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 
50  template<class VA, class VC>
51  class RelTestBnd {
52  public:
53  RelTest operator ()(VA,VC);
54  };
59  template<class VA>
61  public:
63  };
64 
69  template<class VA, class VC>
70  class RelTestDom {
71  public:
72  RelTest operator ()(VA,VC);
73  };
78  template<class VA>
80  public:
82  };
83 
84 
85  template<class VA, class VC>
88  return rtest_eq_bnd(x,y);
89  }
90  template<class VA>
93  return rtest_eq_bnd(x,y.val());
94  }
95 
96  template<class VA, class VC>
99  return rtest_eq_dom(x,y);
100  }
101  template<class VA>
104  return rtest_eq_dom(x,y.val());
105  }
106 
107 
108 
109 
110  /*
111  * Base class
112  *
113  */
114 
115  template<class VA, class VB, class VC, PropCond pc_ac>
117  VB y0, VC y1)
118  : Propagator(home), iv(iv0), x0(y0), x1(y1) {
119  x0.subscribe(home,*this,PC_INT_DOM);
120  x1.subscribe(home,*this,pc_ac);
121  iv.subscribe(home,*this,pc_ac);
122  }
123 
124  template<class VA, class VB, class VC, PropCond pc_ac>
126  View<VA,VB,VC,pc_ac>::View(Space& home, bool share, View& p)
127  : Propagator(home,share,p) {
128  x0.update(home,share,p.x0);
129  x1.update(home,share,p.x1);
130  iv.update(home,share,p.iv);
131  }
132 
133  template<class VA, class VB, class VC, PropCond pc_ac>
134  PropCost
136  // This is required for subscribing to variables in the
137  // above constructor, but this is then the only time this
138  // virtual function is ever used!
139  return PropCost::linear(PropCost::LO,iv.size()+2);
140  }
141 
142  template<class VA, class VB, class VC, PropCond pc_ac>
143  forceinline size_t
145  x0.cancel(home,*this,PC_INT_DOM);
146  x1.cancel(home,*this,pc_ac);
147  iv.cancel(home,*this,pc_ac);
148  (void) Propagator::dispose(home);
149  return sizeof(*this);
150  }
151 
152 
153 
154 
159  template<class View>
160  class IterIdxView {
161  private:
162  const IdxView<View> *cur, *end;
163  public:
164  IterIdxView(void);
165  IterIdxView(const IdxView<View>*, const IdxView<View>*);
166  void init(const IdxView<View>*, const IdxView<View>*);
167  bool operator ()(void) const;
168  void operator ++(void);
169  int val(void) const;
170  };
171 
172  template<class View>
175  template<class View>
178  const IdxView<View>* e)
179  : cur(b), end(e) {}
180  template<class View>
181  forceinline void
183  const IdxView<View>* e) {
184  cur=b; end=e;
185  }
186  template<class View>
187  forceinline bool
189  return cur < end;
190  }
191  template<class View>
192  forceinline void
194  cur++;
195  }
196  template<class View>
197  forceinline int
199  return cur->idx;
200  }
201 
202 
203 
204 
205  /*
206  * Generic scanning: does all but computing new domain for result
207  * (which is specific to bounds/domain version)
208  *
209  */
210 
211  template<class VA, class VB, class VC, PropCond pc_ac, class RelTest>
212  ExecStatus
214  VB x0, VC x1, Propagator& p, RelTest rt) {
215  assert(iv.size() > 1);
216  /*
217  * Prunes pairs of index, variable
218  * - checks for idx value removed
219  * - checks for disequal variables
220  *
221  */
222  ViewValues<VB> vx0(x0);
223  int i = 0;
224  int j = 0;
225  while (vx0() && (i < iv.size())) {
226  if (iv[i].idx < vx0.val()) {
227  iv[i].view.cancel(home,p,pc_ac);
228  ++i;
229  } else if (iv[i].idx > vx0.val()) {
230  ++vx0;
231  } else {
232  assert(iv[i].idx == vx0.val());
233  switch (rt(iv[i].view,x1)) {
234  case RT_FALSE:
235  iv[i].view.cancel(home,p,pc_ac);
236  break;
237  case RT_TRUE:
238  case RT_MAYBE:
239  iv[j++] = iv[i];
240  break;
241  default: GECODE_NEVER;
242  }
243  ++vx0; ++i;
244  }
245  }
246  while (i < iv.size())
247  iv[i++].view.cancel(home,p,pc_ac);
248  bool adjust = (j<iv.size());
249  iv.size(j);
250 
251  if (iv.size() == 0)
252  return ES_FAILED;
253 
254  if (iv.size() == 1) {
255  GECODE_ME_CHECK(x0.eq(home,iv[0].idx));
256  } else if (adjust) {
257  IterIdxView<VA> v(&iv[0],&iv[0]+iv.size());
258  GECODE_ME_CHECK(x0.narrow_v(home,v,false));
259  assert(x0.size() == static_cast<unsigned int>(iv.size()));
260  }
261  return ES_OK;
262  }
263 
264 
265 
266 
267  /*
268  * Bounds consistent propagator
269  *
270  */
271 
272  template<class VA, class VB, class VC>
275  IdxViewArray<VA>& iv, VB x0, VC x1)
276  : View<VA,VB,VC,PC_INT_BND>(home,iv,x0,x1) {}
277 
278  template<class VA, class VB, class VC>
279  ExecStatus
281  IdxViewArray<VA>& iv, VB x0, VC x1) {
282  GECODE_ME_CHECK(x0.gq(home,0));
283  GECODE_ME_CHECK(x0.le(home,iv.size()));
284  if (x0.assigned()) {
285  (void) new (home) Rel::EqBnd<VA,VC>(home,iv[x0.val()].view,x1);
286  return ES_OK;
287  } else {
288  assert(iv.size()>1);
289  (void) new (home) ViewBnd<VA,VB,VC>(home,iv,x0,x1);
290  }
291  return ES_OK;
292  }
293 
294 
295  template<class VA, class VB, class VC>
298  : View<VA,VB,VC,PC_INT_BND>(home,share,p) {}
299 
300  template<class VA, class VB, class VC>
301  Actor*
302  ViewBnd<VA,VB,VC>::copy(Space& home, bool share) {
303  return new (home) ViewBnd<VA,VB,VC>(home,share,*this);
304  }
305 
306  template<class VA, class VB, class VC>
307  ExecStatus
309  assert(iv.size() > 1);
312  (home,iv,x0,x1,*this,rt)));
313  if (iv.size() == 1) {
314  ExecStatus es = home.ES_SUBSUMED(*this);
315  (void) new (home) Rel::EqBnd<VA,VC>(home,iv[0].view,x1);
316  return es;
317  }
318  assert(iv.size() > 1);
319  // Compute new result
320  int min = iv[iv.size()-1].view.min();
321  int max = iv[iv.size()-1].view.max();
322  for (int i=iv.size()-1; i--; ) {
323  min = std::min(iv[i].view.min(),min);
324  max = std::max(iv[i].view.max(),max);
325  }
326  ExecStatus es = shared(x0,x1) ? ES_NOFIX : ES_FIX;
327  {
328  ModEvent me = x1.lq(home,max);
329  if (me_failed(me))
330  return ES_FAILED;
331  if (me_modified(me) && (x1.max() != max))
332  es = ES_NOFIX;
333  }
334  {
335  ModEvent me = x1.gq(home,min);
336  if (me_failed(me))
337  return ES_FAILED;
338  if (me_modified(me) && (x1.min() != min))
339  es = ES_NOFIX;
340  }
341  return (x1.assigned() && (min == max)) ?
342  home.ES_SUBSUMED(*this) : es;
343  }
344 
345 
346 
347 
348 
349  /*
350  * Domain consistent propagator
351  *
352  */
353 
354  template<class VA, class VB, class VC>
357  IdxViewArray<VA>& iv, VB x0, VC x1)
358  : View<VA,VB,VC,PC_INT_DOM>(home,iv,x0,x1) {}
359 
360  template<class VA, class VB, class VC>
361  ExecStatus
363  IdxViewArray<VA>& iv, VB x0, VC x1){
364  GECODE_ME_CHECK(x0.gq(home,0));
365  GECODE_ME_CHECK(x0.le(home,iv.size()));
366  if (x0.assigned()) {
367  (void) new (home) Rel::EqDom<VA,VC>(home,iv[x0.val()].view,x1);
368  return ES_OK;
369  } else {
370  assert(iv.size()>1);
371  (void) new (home) ViewDom<VA,VB,VC>(home,iv,x0,x1);
372  }
373  return ES_OK;
374  }
375 
376 
377  template<class VA, class VB, class VC>
380  : View<VA,VB,VC,PC_INT_DOM>(home,share,p) {}
381 
382  template<class VA, class VB, class VC>
383  Actor*
384  ViewDom<VA,VB,VC>::copy(Space& home, bool share) {
385  return new (home) ViewDom<VA,VB,VC>(home,share,*this);
386  }
387 
388 
389  template<class VA, class VB, class VC>
390  PropCost
391  ViewDom<VA,VB,VC>::cost(const Space&, const ModEventDelta& med) const {
392  return PropCost::linear((VA::me(med) != ME_INT_DOM) ?
393  PropCost::LO : PropCost::HI, iv.size()+2);
394  }
395 
396  template<class VA, class VB, class VC>
397  ExecStatus
399  assert(iv.size() > 1);
400  if (VA::me(med) != ME_INT_DOM) {
403  (home,iv,x0,x1,*this,rt)));
404  if (iv.size() == 1) {
405  ExecStatus es = home.ES_SUBSUMED(*this);
406  (void) new (home) Rel::EqDom<VA,VC>(home,iv[0].view,x1);
407  return es;
408  }
409  // Compute new result
410  int min = iv[iv.size()-1].view.min();
411  int max = iv[iv.size()-1].view.max();
412  for (int i=iv.size()-1; i--; ) {
413  min = std::min(iv[i].view.min(),min);
414  max = std::max(iv[i].view.max(),max);
415  }
416  GECODE_ME_CHECK(x1.lq(home,max));
417  GECODE_ME_CHECK(x1.gq(home,min));
418  return (x1.assigned() && (min == max)) ?
419  home.ES_SUBSUMED(*this) :
420  home.ES_NOFIX_PARTIAL(*this,VA::med(ME_INT_DOM));
421  }
424  (home,iv,x0,x1,*this,rt)));
425  if (iv.size() == 1) {
426  ExecStatus es = home.ES_SUBSUMED(*this);
427  (void) new (home) Rel::EqDom<VA,VC>(home,iv[0].view,x1);
428  return es;
429  }
430  assert(iv.size() > 1);
431 
432  if (x1.assigned()) {
433  for (int i = iv.size(); i--; )
434  if (iv[i].view.in(x1.val()))
435  return shared(x0,x1) ? ES_NOFIX : ES_FIX;
436  return ES_FAILED;
437  } else {
438  Region r(home);
439  ViewRanges<VA>* i_view = r.alloc<ViewRanges<VA> >(iv.size());
440  for (int i = iv.size(); i--; )
441  i_view[i].init(iv[i].view);
442  Iter::Ranges::NaryUnion i_val(r, i_view, iv.size());
443  ModEvent me = x1.inter_r(home,i_val);
444  r.free<ViewRanges<VA> >(i_view,iv.size());
445  GECODE_ME_CHECK(me);
446  return (shared(x0,x1) || me_modified(me)) ? ES_NOFIX : ES_FIX;
447  }
448  }
449 
450 }}}
451 
452 // STATISTICS: int-prop
453 
Domain consistent element propagator for array of views.
Definition: element.hh:262
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:4041
Binary domain consistent equality propagator.
Definition: rel.hh:71
View(Space &home, bool share, View &p)
Constructor for cloning p.
Definition: view.hpp:126
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
VC x1
View for result.
Definition: element.hh:212
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:2986
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
int ModEvent
Type for modification events.
Definition: core.hpp:146
Base-class for propagators.
Definition: core.hpp:755
Expensive.
Definition: core.hpp:564
void init(const IdxView< View > *, const IdxView< View > *)
Definition: view.hpp:182
static ExecStatus post(Home home, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition: view.hpp:280
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:1362
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:135
Class for bounds-equality test.
Definition: view.hpp:51
#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:384
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, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition: view.hpp:362
IdxViewArray< VA > iv
Current index-view map.
Definition: element.hh:208
Execution has resulted in failure.
Definition: core.hpp:525
bool operator()(void) const
Definition: view.hpp:188
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
void cancel(Space &home, Propagator &p, PropCond pc)
Definition: idx-view.hpp:137
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
Range iterator for union of iterators.
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:98
Base-class for element propagator for array of views.
Definition: element.hh:205
#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
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: view.hpp:302
const int v[7]
Definition: distinct.cpp:207
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:160
ExecStatus scan(Space &home, IdxViewArray< VA > &iv, VB x0, VC x1, Propagator &p, RelTest rt)
Definition: view.hpp:213
Constant integer view.
Definition: view.hpp:804
VB x0
View for index.
Definition: element.hh:210
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: view.hpp:308
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:144
ViewBnd(Space &home, bool share, ViewBnd &p)
Constructor for cloning p.
Definition: view.hpp:297
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
Propagation cost.
Definition: core.hpp:537
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Definition: idx-view.hpp:129
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
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:379
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
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
RelTest operator()(VA, VC)
Definition: view.hpp:87
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: view.hpp:398
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: view.hpp:391
Home class for posting propagators
Definition: core.hpp:717
Class for pair of index and view.
Definition: idx-view.hh:52
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
Class for domain-equality test.
Definition: view.hpp:70
Relation does hold.
Definition: view.hpp:1617
Bounds consistent element propagator for array of views.
Definition: element.hh:232
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58
int size(void) const
Return the current size.
Definition: idx-view.hpp:103
void update(Space &home, bool share, IdxViewArray< View > &x)
Cloning.
Definition: idx-view.hpp:144