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  * Contributing authors:
8  * Gabor Szokoli <szokoli@gecode.org>
9  *
10  * Copyright:
11  * Guido Tack, 2004
12  * Christian Schulte, 2004
13  * Gabor Szokoli, 2004
14  *
15  * Last modified:
16  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
17  * $Revision: 13068 $
18  *
19  * This file is part of Gecode, the generic constraint
20  * development environment:
21  * http://www.gecode.org
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining
24  * a copy of this software and associated documentation files (the
25  * "Software"), to deal in the Software without restriction, including
26  * without limitation the rights to use, copy, modify, merge, publish,
27  * distribute, sublicense, and/or sell copies of the Software, and to
28  * permit persons to whom the Software is furnished to do so, subject to
29  * the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be
32  * included in all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
38  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
39  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41  *
42  */
43 
44 namespace Gecode { namespace Set { namespace RelOp {
45 
46  /*
47  * "Ternary union" propagator
48  *
49  */
50 
51  template<class View0, class View1, class View2>
53  Union<View0,View1,View2>::Union(Home home, View0 y0,View1 y1,View2 y2)
55  View2,PC_SET_ANY>(home,y0,y1,y2) {}
56 
57  template<class View0, class View1, class View2>
62  View2,PC_SET_ANY>(home,share,p) {}
63 
64  template<class View0, class View1, class View2>
66  View1 x1, View2 x2) {
67  (void) new (home) Union<View0,View1,View2>(home,x0,x1,x2);
68  return ES_OK;
69  }
70 
71  template<class View0, class View1, class View2>
72  Actor*
74  return new (home) Union(home,share,*this);
75  }
76 
77  template<class View0, class View1, class View2>
80  // This propagator implements the constraint
81  // x2 = x0 u x1
82 
83  bool x0ass = x0.assigned();
84  bool x1ass = x1.assigned();
85  bool x2ass = x2.assigned();
86 
87  ModEvent me0 = View0::me(med);
88  ModEvent me1 = View1::me(med);
89  ModEvent me2 = View2::me(med);
90 
91  bool x0ubmod = false;
92  bool x1ubmod = false;
93  bool modified = false;
94 
95  do {
96 
97  modified = false;
98  do {
99  // 4) lub(x2) <= lub(x0) u lub(x1)
100  {
101  LubRanges<View0> x0ub(x0);
102  LubRanges<View1> x1ub(x1);
104  GECODE_ME_CHECK_MODIFIED(modified, x2.intersectI(home,u2) );
105  }
106 
107  if (modified || Rel::testSetEventUB(me2) )
108  {
109  modified = false;
110  // 5) x0 <= x2
111  LubRanges<View2> x2ub1(x2);
112  GECODE_ME_CHECK_MODIFIED(modified, x0.intersectI(home,x2ub1) );
113  x0ubmod |= modified;
114 
115  // 6) x1 <= x2
116  bool modified2=false;
117  LubRanges<View2> x2ub2(x2);
118  GECODE_ME_CHECK_MODIFIED(modified2, x1.intersectI(home,x2ub2) );
119  x1ubmod |= modified2;
120  modified |= modified2;
121  }
122 
123  } while (modified);
124 
125  modified = false;
126  do {
127  bool modifiedOld = modified;
128  modified = false;
129 
131  || x0ubmod || modifiedOld)
132  {
133  // 1) glb(x0) \ lub(x1) <= glb(x2)
134  GlbRanges<View2> x2lb(x2);
135  LubRanges<View0> x0ub(x0);
137  diff(x2lb, x0ub);
138  GECODE_ME_CHECK_MODIFIED(modified, x1.includeI(home,diff) );
139  }
140 
142  || x1ubmod || modifiedOld)
143  {
144  // 2) glb(x0) \ lub(x2) <= glb(x1)
145  GlbRanges<View2> x2lb(x2);
146  LubRanges<View1> x1ub(x1);
148  diff(x2lb, x1ub);
149  GECODE_ME_CHECK_MODIFIED(modified, x0.includeI(home,diff) );
150  }
151 
152  if (Rel::testSetEventLB(me0,me1) || modified)
153  {
154  // modified = false;
155  // 3) glb(x1) u glb(x2) <= glb(x0)
156  GlbRanges<View0> x0lb(x0);
157  GlbRanges<View1> x1lb(x1);
159  u1(x0lb,x1lb);
160  GECODE_ME_CHECK_MODIFIED(modified, x2.includeI(home,u1) );
161  }
162 
163  } while(modified);
164 
165  modified = false;
166  {
167  // cardinality
168  ExecStatus ret = unionCard(home,modified, x0, x1, x2);
169  GECODE_ES_CHECK(ret);
170  }
171 
172  if (x2.cardMax() == 0) {
173  GECODE_ME_CHECK( x0.cardMax(home, 0) );
174  GECODE_ME_CHECK( x1.cardMax(home, 0) );
175  return home.ES_SUBSUMED(*this);
176  }
177 
178  if (x0.cardMax() == 0)
179  GECODE_REWRITE(*this,(Rel::Eq<View1,View2>::post(home(*this),x1,x2)));
180  if (x1.cardMax() == 0)
181  GECODE_REWRITE(*this,(Rel::Eq<View0,View2>::post(home(*this),x0,x2)));
182 
183  } while(modified);
184 
185  if (shared(x0,x1,x2)) {
186  return (x0ass && x1ass && x2ass) ? home.ES_SUBSUMED(*this) : ES_NOFIX;
187  } else {
188  if (x0ass && x1ass && x2ass)
189  return home.ES_SUBSUMED(*this);
190  if (x0ass != x0.assigned() ||
191  x1ass != x1.assigned() ||
192  x2ass != x2.assigned()) {
193  return ES_NOFIX;
194  } else {
195  return ES_FIX;
196  }
197  }
198  }
199 
200 
201  /*
202  * "Nary union" propagator
203  *
204  */
205 
206  template<class View0, class View1>
209  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,x,y) {
210  shared = x.shared(home) || viewarrayshared(home,x,y);
211  }
212 
213  template<class View0, class View1>
216  const IntSet& z, View1 y)
217  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,x,y) {
218  shared = x.shared(home) || viewarrayshared(home,x,y);
219  IntSetRanges rz(z);
220  unionOfDets.includeI(home, rz);
221  }
222 
223  template<class View0, class View1>
226  : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,share,p),
227  shared(p.shared) {
229  }
230 
231  template<class View0, class View1>
232  Actor*
233  UnionN<View0,View1>::copy(Space& home, bool share) {
234  return new (home) UnionN<View0,View1>(home,share,*this);
235  }
236 
237  template<class View0, class View1>
238  ExecStatus
240  switch (x.size()) {
241  case 0:
242  GECODE_ME_CHECK(y.cardMax(home, 0));
243  return ES_OK;
244  case 1:
245  return Rel::Eq<View0,View1>::post(home, x[0], y);
246  case 2:
247  return Union<View0,View0,View1>::post(home, x[0], x[1], y);
248  default:
249  (void) new (home) UnionN<View0,View1>(home,x,y);
250  return ES_OK;
251  }
252  }
253 
254  template<class View0, class View1>
255  ExecStatus
257  const IntSet& z, View1 y) {
258  (void) new (home) UnionN<View0,View1>(home,x,z,y);
259  return ES_OK;
260  }
261 
262  template<class View0, class View1>
263  PropCost
265  return PropCost::quadratic(PropCost::LO, x.size()+1);
266  }
267 
268  template<class View0, class View1>
269  ExecStatus
271  ModEvent me0 = View0::me(med);
272  ModEvent me1 = View1::me(med);
273  bool ubevent = Rel::testSetEventUB(me0, me1);
274  bool lbevent = Rel::testSetEventLB(me0, me1);
275  bool anybevent = Rel::testSetEventAnyB(me0, me1);
276  bool cardevent = Rel::testSetEventCard(me0, me1);
277 
278  bool modified = false;
279  bool oldModified = false;
280 
281  do {
282  oldModified = modified;
283  modified = false;
284  if (modified || oldModified || ubevent)
285  GECODE_ES_CHECK(unionNXiUB(home, modified, x, y,unionOfDets));
286  if (modified || oldModified || ubevent)
287  GECODE_ES_CHECK(partitionNYUB(home, modified, x, y,unionOfDets));
288  if (modified || oldModified || anybevent)
289  GECODE_ES_CHECK(partitionNXiLB(home, modified, x, y,unionOfDets));
290  if (modified || oldModified || lbevent)
291  GECODE_ES_CHECK(partitionNYLB(home, modified, x, y,unionOfDets));
292  if (modified || oldModified || cardevent || ubevent)
293  GECODE_ES_CHECK(unionNCard(home, modified, x, y, unionOfDets));
294  } while (modified);
295 
296  for(int i=0;i<x.size();i++){
297  //Do not reverse! Eats away the end of the array!
298  while (i<x.size() && x[i].assigned()) {
299  GlbRanges<View0> det(x[i]);
300  unionOfDets.includeI(home,det);
301  x.move_lst(i);
302  }
303  }
304  // When we run out of variables, make a final check and disolve:
305  if (x.size()==0) {
306  BndSetRanges all1(unionOfDets);
307  GECODE_ME_CHECK( y.intersectI(home,all1) );
308  BndSetRanges all2(unionOfDets);
309  GECODE_ME_CHECK( y.includeI(home,all2) );
310  unionOfDets.dispose(home);
311  return home.ES_SUBSUMED(*this);
312  }
313 
314  return shared ? ES_NOFIX : ES_FIX;
315  }
316 
317 }}}
318 
319 // STATISTICS: set-prop
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
bool testSetEventAnyB(ModEvent me0, ModEvent me1, ModEvent me2)
Definition: common.hpp:83
ExecStatus partitionNYLB(Space &home, bool &modified, ViewArray< View0 > &x, View1 &y, GLBndSet &unionOfDets)
Definition: common.hpp:565
static PropCost quadratic(PropCost::Mod m, unsigned int n)
Quadratic complexity for modifier m and size measure n.
Definition: core.hpp:4032
Propagator for nary union
Definition: rel-op.hh:221
Range iterator for integer sets.
Definition: int.hh:271
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: union.hpp:79
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
ExecStatus unionNXiUB(Space &home, bool &modified, ViewArray< View0 > &x, View1 &y, GLBndSet &)
Definition: common.hpp:299
Propagator for ternary union
Definition: rel-op.hh:156
Mixed (n+1)-ary propagator.
Definition: propagator.hpp:268
int ModEvent
Type for modification events.
Definition: core.hpp:146
ExecStatus partitionNXiLB(Space &home, bool &modified, ViewArray< View0 > &x, View1 &y, GLBndSet &unionOfDets)
Definition: common.hpp:518
Range iterator for the greatest lower bound.
Definition: var-imp.hpp:363
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: union.hpp:270
ExecStatus unionNCard(Space &home, bool &modified, ViewArray< View0 > &x, View1 &y, GLBndSet &unionOfDets)
Definition: common.hpp:198
Propagation has computed fixpoint.
Definition: core.hpp:528
Union(Space &home, bool share, Union &p)
Constructor for cloning p.
Definition: union.hpp:59
Computation spaces.
Definition: core.hpp:1362
bool shared
Whether the any views share a variable implementation.
Definition: rel-op.hh:227
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: union.hpp:233
#define GECODE_ME_CHECK_MODIFIED(modified, me)
Check whether me is failed or modified, and forward failure.
Definition: macros.hpp:57
Range iterator for the least upper bound.
Definition: var-imp.hpp:321
Base-class for both propagators and branchers.
Definition: core.hpp:666
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
static ExecStatus post(Home home, View0 x, View1 y, View2 z)
Post propagator .
Definition: union.hpp:65
Mixed ternary propagator.
Definition: propagator.hpp:235
bool shared(const Space &home) const
Test whether array contains shared views.
Definition: array.hpp:1511
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low linear)
Definition: union.hpp:264
Range iterator for integer sets.
Definition: var-imp.hpp:189
ExecStatus partitionNYUB(Space &home, bool &modified, ViewArray< View0 > &x, View1 &y, GLBndSet &unionOfDets)
Definition: common.hpp:592
Integer sets.
Definition: int.hh:171
void update(Space &home, BndSet &x)
Update this set to be a clone of set x.
Definition: integerset.hpp:144
GLBndSet unionOfDets
Union of the determined (which are dropped)
Definition: rel-op.hh:229
#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
const Gecode::PropCond PC_SET_ANY
Propagate when any bound or the cardinality of a view changes.
Definition: var-type.hpp:248
bool shared(View0 v0, View1 v1, View2 v2)
Definition: common.hpp:89
Range iterator for computing union (binary)
bool testSetEventLB(ModEvent me0, ModEvent me1, ModEvent me2)
Definition: common.hpp:75
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: union.hpp:73
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
Propagator for set equality
Definition: rel.hh:146
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
bool testSetEventCard(ModEvent me0, ModEvent me1, ModEvent me2)
Definition: common.hpp:87
Gecode toplevel namespace
bool testSetEventUB(ModEvent me0, ModEvent me1, ModEvent me2)
Definition: common.hpp:79
Range iterator for computing set difference.
Definition: ranges-diff.hpp:47
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
ExecStatus unionCard(Space &home, bool &retmodified, View0 &x0, View1 &x1, View2 &x2)
Definition: common.hpp:150
Home class for posting propagators
Definition: core.hpp:717
UnionN(Space &home, bool share, UnionN &p)
Constructor for cloning p.
Definition: union.hpp:225
static ExecStatus post(Home home, View0, View1)
Post propagator .
Definition: eq.hpp:58
static ExecStatus post(Home home, ViewArray< View0 > &y, View1 x)
Post propagator .
Definition: union.hpp:239
bool viewarrayshared(const Space &home, const ViewArray< View0 > &va, const View1 &y)
Definition: common.hpp:51