Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
inter.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
9  * Christian Schulte, 2004
10  *
11  * Last modified:
12  * $Date$ by $Author$
13  * $Revision$
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  ElementIntersection(Home home, IdxViewArray& iv0, View0 y0, View1 y1,
46  const IntSet& theUniverse)
47  : Propagator(home), universe(theUniverse), iv(iv0), x0(y0), x1(y1) {
48  home.notice(*this,AP_DISPOSE);
49  x0.subscribe(home,*this, PC_SET_ANY);
50  x1.subscribe(home,*this, PC_SET_ANY);
51  iv.subscribe(home,*this, PC_SET_ANY);
52  }
53 
54  template<class View, class View0, class View1>
58  : Propagator(home,p), universe(p.universe) {
59  x0.update(home,p.x0);
60  x1.update(home,p.x1);
61  iv.update(home,p.iv);
62  }
63 
64  template<class View, class View0, class View1>
65  PropCost
67  const ModEventDelta&) const {
68  return PropCost::linear(PropCost::HI, iv.size()+2);
69  }
70 
71  template<class View, class View0, class View1>
72  void
74  x0.reschedule(home,*this, PC_SET_ANY);
75  x1.reschedule(home,*this, PC_SET_ANY);
76  iv.reschedule(home,*this, PC_SET_ANY);
77  }
78 
79  template<class View, class View0, class View1>
80  forceinline size_t
82  home.ignore(*this,AP_DISPOSE);
83  if (!home.failed()) {
84  x0.cancel(home,*this, PC_SET_ANY);
85  x1.cancel(home,*this, PC_SET_ANY);
86  iv.cancel(home,*this,PC_SET_ANY);
87  }
88  universe.~IntSet();
89  (void) Propagator::dispose(home);
90  return sizeof(*this);
91  }
92 
93  template<class View, class View0, class View1>
96  post(Home home, IdxViewArray& xs, View0 x0, View1 x1,
97  const IntSet& universe) {
98  int n = xs.size();
99 
100  // x0 \subseteq {1,...,n}
101  Iter::Ranges::Singleton s(0, n-1);
102  GECODE_ME_CHECK(x0.intersectI(home,s));
103  (void) new (home)
105  return ES_OK;
106  }
107 
108  template<class View, class View0, class View1>
109  Actor*
111  return new (home) ElementIntersection<View,View0,View1>(home,*this);
112  }
113 
114  template<class View, class View0, class View1>
115  ExecStatus
117  const ModEventDelta&) {
118  Region r;
119  int n = iv.size();
120 
121  bool loopVar;
122  do {
123  loopVar = false;
124 
125  // Cache the upper bound iterator, as we have to
126  // modify the upper bound while iterating
127  LubRanges<View0> x0ub(x0);
128  Iter::Ranges::Cache x0ubc(r,x0ub);
130 
131  GlbRanges<View0> x0lb(x0);
132  Iter::Ranges::Cache x0lbc(r,x0lb);
134 
135  // In the first iteration, compute in before[i] the intersection
136  // of all the lower bounds of the x_i. At the same time,
137  // exclude inconsistent x_i from x0 and remove them from
138  // the list, cancel their dependencies.
139 
140  LUBndSet sofarBefore(home,universe);
141  LUBndSet* before = r.alloc<LUBndSet>(n);
142 
143  int j = 0;
144  int i = 0;
145  while ( vx0ub() ) {
146 
147  // Remove vars at indices not in the upper bound
148  if (iv[i].idx < vx0ub.val()) {
149  iv[i].view.cancel(home,*this, PC_SET_ANY);
150  ++i;
151  continue;
152  }
153  assert(iv[i].idx == vx0ub.val());
154  iv[j] = iv[i];
155 
156  View candidate = iv[j].view;
157  int candidateInd = iv[j].idx;
158 
159  // inter = glb(x1) & complement(lub(candidate))
160  GlbRanges<View1> x1lb(x1);
161  LubRanges<View> candub(candidate);
163  inter(x1lb, candub);
164 
165  // exclude inconsistent x_i
166  // an x_i is inconsistent if
167  // * its max cardinality is less than minCard of x1
168  // * inter is not empty (there are elements in x_0
169  // that can't be in x_i)
170  if (candidate.cardMax() < x1.cardMin() ||
171  inter()) {
172  ModEvent me = (x0.exclude(home,candidateInd));
173  loopVar |= me_modified(me);
174  GECODE_ME_CHECK(me);
175 
176  iv[j].view.cancel(home,*this, PC_SET_ANY);
177  ++i;
178  ++vx0ub;
179  continue;
180  } else {
181  // if x_i is consistent, check whether we know
182  // that its index is in x0
183  if (vx0() && vx0.val()==candidateInd) {
184  // x1 <= candidate, candidate >= x1
185  GlbRanges<View1> x1lb(x1);
186  ModEvent me = candidate.includeI(home,x1lb);
187  loopVar |= me_modified(me);
188  GECODE_ME_CHECK(me);
189 
190  LubRanges<View> candub(candidate);
191  me = x1.intersectI(home,candub);
192  loopVar |= me_modified(me);
193  GECODE_ME_CHECK(me);
194  ++vx0;
195  }
196  new (&before[j]) LUBndSet(home);
197  before[j].update(home,sofarBefore);
198  GlbRanges<View> clb(candidate);
199  sofarBefore.intersectI(home,clb);
200  }
201 
202  ++vx0ub;
203  ++i; ++j;
204  }
205 
206  // cancel the variables with index greater than
207  // max of lub(x0)
208  for (int k=i; k<n; k++) {
209  iv[k].view.cancel(home,*this, PC_SET_ANY);
210  }
211  n = j;
212  iv.size(n);
213 
214  if (x0.cardMax()==0) {
215  // Elementor is empty, hence the result must be universe
216  {
217  IntSetRanges uniI(universe);
218  GECODE_ME_CHECK(x1.includeI(home,uniI));
219  }
220  {
221  IntSetRanges uniI(universe);
222  GECODE_ME_CHECK(x1.intersectI(home,uniI));
223  }
224  for (int i=n; i--;)
225  before[i].dispose(home);
226  return home.ES_SUBSUMED(*this);
227  }
228 
229  {
230  // x1 >= sofarBefore
231  BndSetRanges sfB(sofarBefore);
232  ModEvent me = x1.includeI(home,sfB);
233  loopVar |= me_modified(me);
234  GECODE_ME_CHECK(me);
235  }
236 
237  sofarBefore.dispose(home);
238 
239  LUBndSet sofarAfter(home, universe);
240 
241  // In the second iteration, this time backwards, compute
242  // sofarAfter as the intersection of all glb(x_j) with j>i
243  for (int i=n; i--;) {
244  if (sofarAfter.size() == 0) break;
245 
246  // extra = inter(before[i], sofarAfter) - lub(x1)
247  BndSetRanges b(before[i]);
248  BndSetRanges s(sofarAfter);
249  LubRanges<View1> x1ub(x1);
252  BndSetRanges>, LubRanges<View1> > diff(inter, x1ub);
253  if (diff()) {
254  ModEvent me = (x0.include(home,iv[i].idx));
255  loopVar |= me_modified(me);
256  GECODE_ME_CHECK(me);
257 
258  // candidate != extra
259  me = iv[i].view.excludeI(home,diff);
260  loopVar |= me_modified(me);
261  GECODE_ME_CHECK(me);
262  }
263 
264  GlbRanges<View> ivilb(iv[i].view);
265  sofarAfter.intersectI(home,ivilb);
266  before[i].dispose(home);
267  }
268  sofarAfter.dispose(home);
269 
270  } while (loopVar);
271 
272  // Test whether we determined x0 without determining x1
273  if (x0.assigned() && !x1.assigned()) {
274  int ubsize = static_cast<int>(x0.lubSize());
275  if (ubsize > 2) {
276  assert(ubsize==n);
277  ViewArray<View> is(home,ubsize);
278  for (int i=n; i--;)
279  is[i]=iv[i].view;
281  ::post(home(*this),is,x1)));
282  } else if (ubsize == 2) {
283  assert(n==2);
284  View a = iv[0].view;
285  View b = iv[1].view;
287  ::post(home(*this),a,b,x1)));
288  } else if (ubsize == 1) {
289  assert(n==1);
290  GECODE_REWRITE(*this,
291  (Rel::Eq<View1,View>::post(home(*this),x1,iv[0].view)));
292  } else {
293  GECODE_ME_CHECK(x1.cardMax(home, 0));
294  return home.ES_SUBSUMED(*this);
295  }
296  }
297 
298  bool allAssigned = true;
299  for (int i=iv.size(); i--;) {
300  if (!iv[i].view.assigned()) {
301  allAssigned = false;
302  break;
303  }
304  }
305  if (x1.assigned() && x0.assigned() && allAssigned) {
306  return home.ES_SUBSUMED(*this);
307  }
308 
309  return ES_FIX;
310  }
311 
312 }}}
313 
314 // STATISTICS: set-prop
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:120
static ExecStatus post(Home home, IdxViewArray &x, View0 y, View1 z, const IntSet &u)
Definition: inter.hpp:96
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:4643
Range iterator for integer sets.
Definition: int.hh:272
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: inter.hpp:66
Actor must always be disposed.
Definition: core.hpp:554
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:384
Shrinking sets of integers.
Definition: var-imp.hpp:247
int ModEvent
Type for modification events.
Definition: core.hpp:64
Base-class for propagators.
Definition: core.hpp:1016
void update(Space &home, IdxViewArray< View > &x)
Cloning.
Definition: idx-view.hpp:151
Range iterator for the greatest lower bound.
Definition: var-imp.hpp:363
Handle to region.
Definition: region.hpp:57
#define forceinline
Definition: config.hpp:182
Propagation has computed fixpoint.
Definition: core.hpp:469
Computation spaces.
Definition: core.hpp:1668
int val(void) const
Return current value.
Range iterator for the least upper bound.
Definition: var-imp.hpp:321
Base-class for both propagators and branchers.
Definition: core.hpp:620
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: inter.hpp:110
virtual void reschedule(Space &home)
Schedule function.
Definition: inter.hpp:73
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
unsigned int size(void) const
Return size.
Definition: integerset.hpp:97
Range iterator for computing intersection (binary)
void cancel(Space &home, Propagator &p, PropCond pc)
Definition: idx-view.hpp:137
ElementIntersection(Space &home, ElementIntersection &p)
Constructor for cloning p.
Definition: inter.hpp:57
bool intersectI(Space &home, I &i)
Exclude all elements not in the set represented by i from this set.
Definition: integerset.hpp:374
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3914
Value iterator from range iterator.
Range iterator for integer sets.
Definition: var-imp.hpp:189
Integer sets.
Definition: int.hh:174
Expensive.
Definition: core.hpp:506
View arrays.
Definition: array.hpp:228
int size(void) const
Return the current size.
Definition: idx-view.hpp:103
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:56
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3139
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
Propagator for element with intersection
Definition: element.hh:82
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
SetExpr inter(const SetVarArgs &x)
Intersection of set variables.
Definition: set-expr.cpp:699
void reschedule(Space &home, Propagator &p, PropCond pc)
Schedule propagator p.
Definition: idx-view.hpp:144
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:3944
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3172
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: inter.hpp:116
Propagation cost.
Definition: core.hpp:478
Range iterator cache
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Definition: idx-view.hpp:129
ExecStatus
Definition: core.hpp:464
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: inter.hpp:81
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition: modevent.hpp:63
Propagator for set equality
Definition: rel.hh:154
Execution is okay.
Definition: core.hpp:468
An array of IdxView pairs.
Definition: idx-view.hh:71
Gecode toplevel namespace
Range iterator for computing set difference.
Definition: ranges-diff.hpp:47
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
bool before(const ConstSetView &x, const ConstSetView &y)
Definition: const.hpp:698
Home class for posting propagators
Definition: core.hpp:846
Propagator for nary intersection
Definition: rel-op.hh:186
Propagator for ternary intersection
Definition: rel-op.hh:126