Generated on Thu Mar 13 2014 04:39:35 for Gecode by doxygen 1.8.1.2
unionConst.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,2007
9  * Christian Schulte, 2004
10  *
11  * Last modified:
12  * $Date: 2012-02-22 16:04:20 +1100 (Wed, 22 Feb 2012) $ by $Author: tack $
13  * $Revision: 12537 $
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 SView, class RView>
45  ElementUnionConst(Home home, SView y0,
46  const IntSetArgs& iv0,
47  RView y1)
48  : Propagator(home), x0(y0), n_iv(iv0.size()), x1(y1) {
49  home.notice(*this,AP_DISPOSE);
50  x0.subscribe(home,*this, PC_SET_ANY);
51  x1.subscribe(home,*this, PC_SET_ANY);
52  iv=static_cast<Space&>(home).alloc<IntSet>(n_iv);
53  for (unsigned int i=iv0.size(); i--;)
54  iv[i]=iv0[i];
55  }
56 
57  template<class SView, class RView>
60  ElementUnionConst(Space& home, bool share,
62  : Propagator(home,share,p), n_iv(p.n_iv) {
63  x0.update(home,share,p.x0);
64  x1.update(home,share,p.x1);
65  iv=home.alloc<IntSet>(n_iv);
66  for (unsigned int i=n_iv; i--;)
67  iv[i].update(home,share,p.iv[i]);
68  }
69 
70  template<class SView, class RView>
71  PropCost
73  return PropCost::linear(PropCost::HI, n_iv+2);
74  }
75 
76  template<class SView, class RView>
77  forceinline size_t
79  home.ignore(*this,AP_DISPOSE);
80  if (!home.failed()) {
81  x0.cancel(home,*this, PC_SET_ANY);
82  x1.cancel(home,*this, PC_SET_ANY);
83  }
84  for (unsigned int i=n_iv; i--;)
85  iv[i].~IntSet();
86  (void) Propagator::dispose(home);
87  return sizeof(*this);
88  }
89 
90  template<class SView, class RView>
93  post(Home home, SView x0, const IntSetArgs& xs,
94  RView x1) {
95  int n = xs.size();
96 
97  // s2 \subseteq {1,...,n}
98  Iter::Ranges::Singleton s(0, n-1);
99  GECODE_ME_CHECK(x1.intersectI(home,s));
100  (void) new (home)
101  ElementUnionConst<SView,RView>(home,x0,xs,x1);
102  return ES_OK;
103  }
104 
105  template<class SView, class RView>
106  Actor*
108  return new (home) ElementUnionConst<SView,RView>(home,share,*this);
109  }
110 
111  template<class SView, class RView>
112  ExecStatus
114  Region r(home);
115 
116  bool* stillSelected = r.alloc<bool>(n_iv);
117 
118  bool loopVar;
119  do {
120  loopVar = false;
121  for (int i=n_iv; i--;)
122  stillSelected[i] = false;
123 
124  // Cache the upper bound iterator, as we have to
125  // modify the upper bound while iterating
126  LubRanges<RView> x1ub(x1);
127  Iter::Ranges::Cache x1ubc(r,x1ub);
129  vx1ub(x1ubc);
130 
131  GlbRanges<RView> x1lb(x1);
132  Iter::Ranges::Cache x1lbc(r,x1lb);
134  vx1(x1lbc);
135 
136  // In the first iteration, compute in before[i] the union
137  // of all the upper bounds of the x_i. At the same time,
138  // exclude inconsistent x_i from x1.
139 
140  GLBndSet sofarBefore(home);
141  LUBndSet selectedInter(home, IntSet (Limits::min,
142  Limits::max));
143  GLBndSet* before =
144  static_cast<GLBndSet*>(r.ralloc(sizeof(GLBndSet)*n_iv));
145 
146  unsigned int maxCard = 0;
147  unsigned int minCard = Limits::card;
148 
149  while (vx1ub()) {
150  int i = vx1ub.val();
151 
152  IntSetRanges candCardR(iv[i]);
153  unsigned int candidateCard = Iter::Ranges::size(candCardR);
154 
155  IntSetRanges candlb(iv[i]);
156  LubRanges<SView> x0ub(x0);
158  LubRanges<SView> > diff(candlb, x0ub);
159 
160  bool selectSingleInconsistent = false;
161  if (x1.cardMax() <= 1) {
162  GlbRanges<SView> x0lb(x0);
163  IntSetRanges candub(iv[i]);
165  IntSetRanges > diff2(x0lb, candub);
166  selectSingleInconsistent = diff2() || candidateCard < x0.cardMin();
167  }
168 
169  // exclude inconsistent x_i
170  // an x_i is inconsistent if
171  // * at most one x_i can be selected and there are
172  // elements in x_0 that can't be in x_i
173  // (selectSingleInconsistent)
174  // * its min cardinality is greater than maxCard of x0
175  // * inter is not empty (there are elements in x_i
176  // that can't be in x_0)
177  if (selectSingleInconsistent ||
178  candidateCard > x0.cardMax() ||
179  diff()) {
180  ModEvent me = (x1.exclude(home,i));
181  loopVar |= me_modified(me);
182  GECODE_ME_CHECK(me);
183  } else {
184  stillSelected[i] = true;
185  // if x_i is consistent, check whether we know
186  // that its index is in x1
187  if (vx1() && vx1.val()==i) {
188  // x0 >= candidate, candidate <= x0
189  // GlbRanges<SView> candlb(candidate);
190  IntSetRanges candlb(iv[i]);
191  ModEvent me = x0.includeI(home,candlb);
192  loopVar |= me_modified(me);
193  GECODE_ME_CHECK(me);
194  ++vx1;
195  }
196  new (&before[i]) GLBndSet(home);
197  before[i].update(home,sofarBefore);
198  IntSetRanges cub(iv[i]);
199  sofarBefore.includeI(home,cub);
200  IntSetRanges clb(iv[i]);
201  selectedInter.intersectI(home,clb);
202  maxCard = std::max(maxCard, candidateCard);
203  minCard = std::min(minCard, candidateCard);
204  }
205 
206  ++vx1ub;
207  }
208 
209  if (x1.cardMax()==0) {
210  // Selector is empty, hence the result must be empty
211  {
212  GECODE_ME_CHECK(x0.cardMax(home,0));
213  }
214  for (int i=n_iv; i--;)
215  if (stillSelected[i])
216  before[i].dispose(home);
217  selectedInter.dispose(home);
218  sofarBefore.dispose(home);
219  return home.ES_SUBSUMED(*this);
220  }
221 
222  if (x1.cardMin() > 0) {
223  // Selector is not empty, hence the intersection of the
224  // possibly selected lower bounds is contained in x0
225  BndSetRanges si(selectedInter);
226  ModEvent me = x0.includeI(home, si);
227  loopVar |= me_modified(me);
228  GECODE_ME_CHECK(me);
229  me = x0.cardMin(home, minCard);
230  loopVar |= me_modified(me);
231  GECODE_ME_CHECK(me);
232  }
233  selectedInter.dispose(home);
234 
235  if (x1.cardMax() <= 1) {
236  ModEvent me = x0.cardMax(home, maxCard);
237  loopVar |= me_modified(me);
238  GECODE_ME_CHECK(me);
239  }
240 
241  {
242  // x0 <= sofarBefore
243  BndSetRanges sfB(sofarBefore);
244  ModEvent me = x0.intersectI(home,sfB);
245  loopVar |= me_modified(me);
246  GECODE_ME_CHECK(me);
247  }
248 
249  sofarBefore.dispose(home);
250 
251  GLBndSet sofarAfter(home);
252 
253  // In the second iteration, this time backwards, compute
254  // sofarAfter as the union of all lub(x_j) with j>i
255  for (int i=n_iv; i--;) {
256  if (!stillSelected[i])
257  continue;
258  BndSetRanges b(before[i]);
259  BndSetRanges s(sofarAfter);
260  GlbRanges<SView> x0lb(x0);
264  if (diff()) {
265  ModEvent me = (x1.include(home,i));
266  loopVar |= me_modified(me);
267  GECODE_ME_CHECK(me);
268 
269  // candidate != extra
270  IntSetRanges ivi(iv[i]);
271  if (!Iter::Ranges::subset(diff, ivi))
273  }
274 
275  IntSetRanges iviub(iv[i]);
276  sofarAfter.includeI(home,iviub);
277  before[i].dispose(home);
278  }
279  sofarAfter.dispose(home);
280 
281  } while (loopVar);
282 
283  if (x1.assigned()) {
284  assert(x0.assigned());
285  return home.ES_SUBSUMED(*this);
286  }
287 
288  return ES_FIX;
289  }
290 
291 }}}
292 
293 // STATISTICS: set-prop