Generated on Sat Feb 7 2015 02:01:30 for Gecode by doxygen 1.8.9.1
union.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  * Christian Schulte <schulte@gecode.org>
6  *
7  * Copyright:
8  * Guido Tack, 2004,2006
9  * Christian Schulte, 2004
10  *
11  * Last modified:
12  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
13  * $Revision: 13068 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 namespace Gecode { namespace Set { namespace Element {
41 
42  template<class View, class View0, class View1>
45  ElementUnion(Home home, IdxViewArray& iv0, View0 y0, View1 y1)
46  : Propagator(home), iv(iv0), x0(y0), x1(y1) {
47  home.notice(*this,AP_DISPOSE);
48  x0.subscribe(home,*this, PC_SET_ANY);
49  x1.subscribe(home,*this, PC_SET_ANY);
50  iv.subscribe(home,*this, PC_SET_ANY);
51  }
52 
53  template<class View, class View0, class View1>
57  : Propagator(home,share,p) {
58  x0.update(home,share,p.x0);
59  x1.update(home,share,p.x1);
60  iv.update(home,share,p.iv);
61  }
62 
63  template<class View, class View0, class View1>
64  PropCost
66  const ModEventDelta&) const {
67  return PropCost::linear(PropCost::HI, iv.size()+2);
68  }
69 
70  template<class View, class View0, class View1>
71  forceinline size_t
73  home.ignore(*this,AP_DISPOSE);
74  if (!home.failed()) {
75  x0.cancel(home,*this,PC_SET_ANY);
76  x1.cancel(home,*this,PC_SET_ANY);
77  iv.cancel(home,*this,PC_SET_ANY);
78  }
79  (void) Propagator::dispose(home);
80  return sizeof(*this);
81  }
82 
83  template<class View, class View0, class View1>
86  post(Home home, IdxViewArray& xs, View0 x0, View1 x1) {
87  int n = xs.size();
88 
89  // x0 \subseteq {1,...,n}
90  Iter::Ranges::Singleton s(0, n-1);
91  GECODE_ME_CHECK(x0.intersectI(home,s));
92  (void) new (home)
93  ElementUnion<View,View0,View1>(home,xs,x0,x1);
94  return ES_OK;
95  }
96 
97  template<class View, class View0, class View1>
98  Actor*
100  return new (home) ElementUnion<View,View0,View1>(home,share,*this);
101  }
102 
103  template<class View, class View0, class View1>
104  ExecStatus
106  Region r(home);
107  int n = iv.size();
108 
109  bool loopVar;
110  do {
111  loopVar = false;
112 
113  // Cache the upper bound iterator, as we have to
114  // modify the upper bound while iterating
115  LubRanges<View0> x0ub(x0);
116  Iter::Ranges::Cache x0ubc(r,x0ub);
118 
119  GlbRanges<View0> x0lb(x0);
120  Iter::Ranges::Cache x0lbc(r,x0lb);
122 
123  // In the first iteration, compute in before[i] the union
124  // of all the upper bounds of the x_i. At the same time,
125  // exclude inconsistent x_i from x0 and remove them from
126  // the list, cancel their dependencies.
127 
128  GLBndSet sofarBefore(home);
129  LUBndSet selectedInter(home, IntSet (Limits::min,
130  Limits::max));
131  GLBndSet* before =
132  static_cast<GLBndSet*>(r.ralloc(sizeof(GLBndSet)*n));
133 
134  int j = 0;
135  int i = 0;
136 
137  unsigned int maxCard = 0;
138  unsigned int minCard = Limits::card;
139 
140  while ( vx0ub() ) {
141 
142  // Remove vars at indices not in the upper bound
143  if (iv[i].idx < vx0ub.val()) {
144  iv[i].view.cancel(home,*this, PC_SET_ANY);
145  ++i;
146  continue;
147  }
148  assert(iv[i].idx == vx0ub.val());
149  iv[j] = iv[i];
150 
151  View candidate = iv[j].view;
152  int candidateInd = iv[j].idx;
153 
154  // inter = glb(candidate) & complement(lub(x1))
155  GlbRanges<View> candlb(candidate);
156  LubRanges<View1> x1ub(x1);
158  diff(candlb, x1ub);
159 
160  bool selectSingleInconsistent = false;
161  if (x0.cardMax() <= 1) {
162  GlbRanges<View1> x1lb(x1);
163  LubRanges<View> candub(candidate);
165  LubRanges<View> > diff2(x1lb, candub);
166  selectSingleInconsistent =
167  diff2() || candidate.cardMax() < x1.cardMin();
168  }
169 
170  // exclude inconsistent x_i
171  // an x_i is inconsistent if
172  // * at most one x_i can be selected and there are
173  // elements in x_0 that can't be in x_i
174  // (selectSingleInconsistent)
175  // * its min cardinality is greater than maxCard of x1
176  // * inter is not empty (there are elements in x_i
177  // that can't be in x_0)
178  if (selectSingleInconsistent ||
179  candidate.cardMin() > x1.cardMax() ||
180  diff()) {
181  ModEvent me = (x0.exclude(home,candidateInd));
182  loopVar |= me_modified(me);
183  GECODE_ME_CHECK(me);
184 
185  iv[j].view.cancel(home,*this, PC_SET_ANY);
186  ++i;
187  ++vx0ub;
188  continue;
189  } else {
190  // if x_i is consistent, check whether we know
191  // that its index is in x0
192  if (vx0() && vx0.val()==candidateInd) {
193  // x1 >= candidate, candidate <= x1
194  GlbRanges<View> candlb(candidate);
195  ModEvent me = x1.includeI(home,candlb);
196  loopVar |= me_modified(me);
197  GECODE_ME_CHECK(me);
198 
199  LubRanges<View1> x1ub(x1);
200  me = candidate.intersectI(home,x1ub);
201  loopVar |= me_modified(me);
202  GECODE_ME_CHECK(me);
203  ++vx0;
204  }
205  new (&before[j]) GLBndSet(home);
206  before[j].update(home,sofarBefore);
207  LubRanges<View> cub(candidate);
208  sofarBefore.includeI(home,cub);
209  GlbRanges<View> clb(candidate);
210  selectedInter.intersectI(home,clb);
211  maxCard = std::max(maxCard, candidate.cardMax());
212  minCard = std::min(minCard, candidate.cardMin());
213  }
214 
215  ++vx0ub;
216  ++i; ++j;
217  }
218 
219  // cancel the variables with index greater than
220  // max of lub(x0)
221  for (int k=i; k<n; k++) {
222  iv[k].view.cancel(home,*this, PC_SET_ANY);
223  }
224  n = j;
225  iv.size(n);
226 
227  if (x0.cardMax()==0) {
228  // Selector is empty, hence the result must be empty
229  {
230  GECODE_ME_CHECK(x1.cardMax(home,0));
231  }
232  for (int i=n; i--;)
233  before[i].dispose(home);
234  return home.ES_SUBSUMED(*this);
235  }
236 
237  if (x0.cardMin() > 0) {
238  // Selector is not empty, hence the intersection of the
239  // possibly selected lower bounds is contained in x1
240  BndSetRanges si(selectedInter);
241  ModEvent me = x1.includeI(home, si);
242  loopVar |= me_modified(me);
243  GECODE_ME_CHECK(me);
244  me = x1.cardMin(home, minCard);
245  loopVar |= me_modified(me);
246  GECODE_ME_CHECK(me);
247  }
248  selectedInter.dispose(home);
249 
250  if (x0.cardMax() <= 1) {
251  ModEvent me = x1.cardMax(home, maxCard);
252  loopVar |= me_modified(me);
253  GECODE_ME_CHECK(me);
254  }
255 
256  {
257  // x1 <= sofarBefore
258  BndSetRanges sfB(sofarBefore);
259  ModEvent me = x1.intersectI(home,sfB);
260  loopVar |= me_modified(me);
261  GECODE_ME_CHECK(me);
262  }
263 
264  sofarBefore.dispose(home);
265 
266  GLBndSet sofarAfter(home);
267 
268  // In the second iteration, this time backwards, compute
269  // sofarAfter as the union of all lub(x_j) with j>i
270  for (int i=n; i--;) {
271  // TODO: check for size of universe here?
272  // if (sofarAfter.size() == 0) break;
273 
274  // extra = inter(before[i], sofarAfter) - lub(x1)
275  BndSetRanges b(before[i]);
276  BndSetRanges s(sofarAfter);
277  GlbRanges<View1> x1lb(x1);
281  if (diff()) {
282  ModEvent me = (x0.include(home,iv[i].idx));
283  loopVar |= me_modified(me);
284  GECODE_ME_CHECK(me);
285 
286  // candidate != extra
287  me = iv[i].view.includeI(home,diff);
288  loopVar |= me_modified(me);
289  GECODE_ME_CHECK(me);
290  }
291 
292  LubRanges<View> iviub(iv[i].view);
293  sofarAfter.includeI(home,iviub);
294  before[i].dispose(home);
295  }
296  sofarAfter.dispose(home);
297 
298  } while (loopVar);
299 
300  // Test whether we determined x0 without determining x1
301  if (x0.assigned() && !x1.assigned()) {
302  int ubsize = static_cast<int>(x0.lubSize());
303  if (ubsize > 2) {
304  assert(ubsize==n);
305  ViewArray<View> is(home,ubsize);
306  for (int i=n; i--;)
307  is[i]=iv[i].view;
309  ::post(home(*this),is,x1)));
310  } else if (ubsize == 2) {
311  assert(n==2);
312  View a = iv[0].view;
313  View b = iv[1].view;
315  ::post(home(*this),a,b,x1)));
316  } else if (ubsize == 1) {
317  assert(n==1);
318  GECODE_REWRITE(*this,
319  (Rel::Eq<View1,View>::post(home(*this),x1,iv[0].view)));
320  } else {
321  GECODE_ME_CHECK(x1.cardMax(home, 0));
322  return home.ES_SUBSUMED(*this);
323  }
324  }
325 
326  bool allAssigned = true;
327  for (int i=iv.size(); i--;) {
328  if (!iv[i].view.assigned()) {
329  allAssigned = false;
330  break;
331  }
332  }
333  if (x1.assigned() && x0.assigned() && allAssigned) {
334  return home.ES_SUBSUMED(*this);
335  }
336 
337  return ES_FIX;
338  }
339 
340 }}}
341 
342 // STATISTICS: set-prop
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
const SetInstr * si[]
Definition: mm-set.cpp:4340
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: union.hpp:72
Propagator for nary union
Definition: rel-op.hh:221
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
Range iterator for singleton range.
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
const int min
Smallest allowed integer in integer set.
Definition: set.hh:101
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: union.hpp:65
Actor must always be disposed.
Definition: core.hpp:610
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
Propagator for ternary union
Definition: rel-op.hh:156
Shrinking sets of integers.
Definition: var-imp.hpp:247
int ModEvent
Type for modification events.
Definition: core.hpp:146
Base-class for propagators.
Definition: core.hpp:755
Expensive.
Definition: core.hpp:564
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: union.hpp:105
Range iterator for the greatest lower bound.
Definition: var-imp.hpp:363
Handle to region.
Definition: region.hpp:61
Propagation has computed fixpoint.
Definition: core.hpp:528
const unsigned int card
Maximum cardinality of an integer set.
Definition: set.hh:103
const int max
Largest allowed integer in integer set.
Definition: set.hh:99
Computation spaces.
Definition: core.hpp:1362
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3442
Range iterator for the least upper bound.
Definition: var-imp.hpp:321
Base-class for both propagators and branchers.
Definition: core.hpp:666
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
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: union.hpp:99
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
bool intersectI(Space &home, I &i)
Exclude all elements not in the set represented by i from this set.
Definition: integerset.hpp:374
Value iterator from range iterator.
Range iterator for integer sets.
Definition: var-imp.hpp:189
static ExecStatus post(Home home, IdxViewArray &x, View0 y, View1 z)
Definition: union.hpp:86
Integer sets.
Definition: int.hh:171
View arrays.
Definition: array.hpp:234
void update(Space &home, BndSet &x)
Update this set to be a clone of set x.
Definition: integerset.hpp:144
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
bool includeI(Space &home, I &i)
Include the set represented by i in this set.
Definition: integerset.hpp:300
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:2849
void dispose(Space &home)
Free memory used by this set.
Definition: integerset.hpp:64
const Gecode::PropCond PC_SET_ANY
Propagate when any bound or the cardinality of a view changes.
Definition: var-type.hpp:248
Range iterator for computing union (binary)
SetExpr inter(const SetVarArgs &x)
Intersection of set variables.
Definition: set-expr.cpp:696
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.cpp:169
Propagator for element with union
Definition: element.hh:117
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
Propagation cost.
Definition: core.hpp:537
Range iterator cache
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
Growing sets of integers.
Definition: var-imp.hpp:209
ElementUnion(Space &home, bool share, ElementUnion &p)
Constructor for cloning p.
Definition: union.hpp:56
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition: modevent.hpp:63
Propagator for set equality
Definition: rel.hh:146
Execution is okay.
Definition: core.hpp:527
int val(void) const
Return current value.
An array of IdxView pairs.
Definition: idx-view.hh:71
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Range iterator for computing set difference.
Definition: ranges-diff.hpp:47
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
bool before(const ConstSetView &x, const ConstSetView &y)
Definition: const.hpp:698
Home class for posting propagators
Definition: core.hpp:717
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:302
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