Generated on Sat Feb 7 2015 02:01:30 for Gecode by doxygen 1.8.9.1
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-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 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
const Gecode::ModEvent ME_SET_FAILED
Domain operation has resulted in failure.
Definition: var-type.hpp:138
const SetInstr * si[]
Definition: mm-set.cpp:4340
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
Range iterator for integer sets.
Definition: int.hh:271
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
Actor must always be disposed.
Definition: core.hpp:610
static ExecStatus post(Home home, SView z, const IntSetArgs &x, RView y)
Definition: unionConst.hpp:93
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: unionConst.hpp:113
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: unionConst.hpp:72
Shrinking sets of integers.
Definition: var-imp.hpp:247
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: unionConst.hpp:78
int ModEvent
Type for modification events.
Definition: core.hpp:146
Base-class for propagators.
Definition: core.hpp:755
Expensive.
Definition: core.hpp:564
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
Propagator for element with union of constant sets
Definition: element.hh:152
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
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2397
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: unionConst.hpp:107
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
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
ElementUnionConst(Space &home, bool share, ElementUnionConst &p)
Constructor for cloning p.
Definition: unionConst.hpp:60
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.
unsigned int size(I &i)
Size of all ranges of range iterator i.
Range iterator for integer sets.
Definition: var-imp.hpp:189
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
#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
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
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
Growing sets of integers.
Definition: var-imp.hpp:209
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition: modevent.hpp:63
Execution is okay.
Definition: core.hpp:527
int val(void) const
Return current value.
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
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
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:302