Generated on Thu Feb 21 2013 23:11:49 for Gecode by doxygen 1.8.3.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: 2011-05-24 00:48:31 +1000 (Tue, 24 May 2011) $ by $Author: tack $
17  * $Revision: 12018 $
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>
225  UnionN<View0,View1>::UnionN(Space& home, bool share, UnionN& p)
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