Generated on Sat Jul 28 2018 17:18:56 for Gecode by doxygen 1.8.14
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: 2016-06-29 17:28:17 +0200 (Wed, 29 Jun 2016) $ by $Author: schulte $
13  * $Revision: 15137 $
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  void
79  x0.reschedule(home,*this, PC_SET_ANY);
80  x1.reschedule(home,*this, PC_SET_ANY);
81  }
82 
83  template<class SView, class RView>
84  forceinline size_t
86  home.ignore(*this,AP_DISPOSE);
87  if (!home.failed()) {
88  x0.cancel(home,*this, PC_SET_ANY);
89  x1.cancel(home,*this, PC_SET_ANY);
90  }
91  for (unsigned int i=n_iv; i--;)
92  iv[i].~IntSet();
93  (void) Propagator::dispose(home);
94  return sizeof(*this);
95  }
96 
97  template<class SView, class RView>
100  post(Home home, SView x0, const IntSetArgs& xs,
101  RView x1) {
102  int n = xs.size();
103 
104  // s2 \subseteq {1,...,n}
105  Iter::Ranges::Singleton s(0, n-1);
106  GECODE_ME_CHECK(x1.intersectI(home,s));
107  (void) new (home)
108  ElementUnionConst<SView,RView>(home,x0,xs,x1);
109  return ES_OK;
110  }
111 
112  template<class SView, class RView>
113  Actor*
115  return new (home) ElementUnionConst<SView,RView>(home,share,*this);
116  }
117 
118  template<class SView, class RView>
119  ExecStatus
121  Region r(home);
122 
123  bool* stillSelected = r.alloc<bool>(n_iv);
124 
125  bool loopVar;
126  do {
127  loopVar = false;
128  for (int i=n_iv; i--;)
129  stillSelected[i] = false;
130 
131  // Cache the upper bound iterator, as we have to
132  // modify the upper bound while iterating
133  LubRanges<RView> x1ub(x1);
134  Iter::Ranges::Cache x1ubc(r,x1ub);
136  vx1ub(x1ubc);
137 
138  GlbRanges<RView> x1lb(x1);
139  Iter::Ranges::Cache x1lbc(r,x1lb);
141  vx1(x1lbc);
142 
143  // In the first iteration, compute in before[i] the union
144  // of all the upper bounds of the x_i. At the same time,
145  // exclude inconsistent x_i from x1.
146 
147  GLBndSet sofarBefore(home);
148  LUBndSet selectedInter(home, IntSet (Limits::min,
149  Limits::max));
150  GLBndSet* before =
151  static_cast<GLBndSet*>(r.ralloc(sizeof(GLBndSet)*n_iv));
152 
153  unsigned int maxCard = 0;
154  unsigned int minCard = Limits::card;
155 
156  while (vx1ub()) {
157  int i = vx1ub.val();
158 
159  IntSetRanges candCardR(iv[i]);
160  unsigned int candidateCard = Iter::Ranges::size(candCardR);
161 
162  IntSetRanges candlb(iv[i]);
163  LubRanges<SView> x0ub(x0);
165  LubRanges<SView> > diff(candlb, x0ub);
166 
167  bool selectSingleInconsistent = false;
168  if (x1.cardMax() <= 1) {
169  GlbRanges<SView> x0lb(x0);
170  IntSetRanges candub(iv[i]);
172  IntSetRanges > diff2(x0lb, candub);
173  selectSingleInconsistent = diff2() || candidateCard < x0.cardMin();
174  }
175 
176  // exclude inconsistent x_i
177  // an x_i is inconsistent if
178  // * at most one x_i can be selected and there are
179  // elements in x_0 that can't be in x_i
180  // (selectSingleInconsistent)
181  // * its min cardinality is greater than maxCard of x0
182  // * inter is not empty (there are elements in x_i
183  // that can't be in x_0)
184  if (selectSingleInconsistent ||
185  candidateCard > x0.cardMax() ||
186  diff()) {
187  ModEvent me = (x1.exclude(home,i));
188  loopVar |= me_modified(me);
189  GECODE_ME_CHECK(me);
190  } else {
191  stillSelected[i] = true;
192  // if x_i is consistent, check whether we know
193  // that its index is in x1
194  if (vx1() && vx1.val()==i) {
195  // x0 >= candidate, candidate <= x0
196  // GlbRanges<SView> candlb(candidate);
197  IntSetRanges candlb(iv[i]);
198  ModEvent me = x0.includeI(home,candlb);
199  loopVar |= me_modified(me);
200  GECODE_ME_CHECK(me);
201  ++vx1;
202  }
203  new (&before[i]) GLBndSet(home);
204  before[i].update(home,sofarBefore);
205  IntSetRanges cub(iv[i]);
206  sofarBefore.includeI(home,cub);
207  IntSetRanges clb(iv[i]);
208  selectedInter.intersectI(home,clb);
209  maxCard = std::max(maxCard, candidateCard);
210  minCard = std::min(minCard, candidateCard);
211  }
212 
213  ++vx1ub;
214  }
215 
216  if (x1.cardMax()==0) {
217  // Selector is empty, hence the result must be empty
218  {
219  GECODE_ME_CHECK(x0.cardMax(home,0));
220  }
221  for (int i=n_iv; i--;)
222  if (stillSelected[i])
223  before[i].dispose(home);
224  selectedInter.dispose(home);
225  sofarBefore.dispose(home);
226  return home.ES_SUBSUMED(*this);
227  }
228 
229  if (x1.cardMin() > 0) {
230  // Selector is not empty, hence the intersection of the
231  // possibly selected lower bounds is contained in x0
232  BndSetRanges si(selectedInter);
233  ModEvent me = x0.includeI(home, si);
234  loopVar |= me_modified(me);
235  GECODE_ME_CHECK(me);
236  me = x0.cardMin(home, minCard);
237  loopVar |= me_modified(me);
238  GECODE_ME_CHECK(me);
239  }
240  selectedInter.dispose(home);
241 
242  if (x1.cardMax() <= 1) {
243  ModEvent me = x0.cardMax(home, maxCard);
244  loopVar |= me_modified(me);
245  GECODE_ME_CHECK(me);
246  }
247 
248  {
249  // x0 <= sofarBefore
250  BndSetRanges sfB(sofarBefore);
251  ModEvent me = x0.intersectI(home,sfB);
252  loopVar |= me_modified(me);
253  GECODE_ME_CHECK(me);
254  }
255 
256  sofarBefore.dispose(home);
257 
258  GLBndSet sofarAfter(home);
259 
260  // In the second iteration, this time backwards, compute
261  // sofarAfter as the union of all lub(x_j) with j>i
262  for (int i=n_iv; i--;) {
263  if (!stillSelected[i])
264  continue;
266  BndSetRanges s(sofarAfter);
267  GlbRanges<SView> x0lb(x0);
271  if (diff()) {
272  ModEvent me = (x1.include(home,i));
273  loopVar |= me_modified(me);
274  GECODE_ME_CHECK(me);
275 
276  // candidate != extra
277  IntSetRanges ivi(iv[i]);
278  if (!Iter::Ranges::subset(diff, ivi))
280  }
281 
282  IntSetRanges iviub(iv[i]);
283  sofarAfter.includeI(home,iviub);
284  before[i].dispose(home);
285  }
286  sofarAfter.dispose(home);
287 
288  } while (loopVar);
289 
290  if (x1.assigned()) {
291  assert(x0.assigned());
292  return home.ES_SUBSUMED(*this);
293  }
294 
295  return ES_FIX;
296  }
297 
298 }}}
299 
300 // 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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1669
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:4841
const int min
Smallest allowed integer in integer set.
Definition: set.hh:103
Range iterator for integer sets.
Definition: int.hh:274
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
Actor must always be disposed.
Definition: core.hpp:630
static ExecStatus post(Home home, SView z, const IntSetArgs &x, RView y)
Definition: unionConst.hpp:100
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: unionConst.hpp:120
Shrinking sets of integers.
Definition: var-imp.hpp:247
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: unionConst.hpp:85
int ModEvent
Type for modification events.
Definition: core.hpp:142
Base-class for propagators.
Definition: core.hpp:1092
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:545
const unsigned int card
Maximum cardinality of an integer set.
Definition: set.hh:105
const int max
Largest allowed integer in integer set.
Definition: set.hh:101
Computation spaces.
Definition: core.hpp:1748
Propagator for element with union of constant sets
Definition: element.hh:161
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:696
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2868
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: unionConst.hpp:72
virtual Actor * copy(Space &home, bool)
Copy propagator during cloning.
Definition: unionConst.hpp:114
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
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
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:4095
Value iterator from range iterator.
unsigned int size(I &i)
Size of all ranges of range iterator i.
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Range iterator for integer sets.
Definition: var-imp.hpp:189
virtual void reschedule(Space &home)
Schedule function.
Definition: unionConst.hpp:78
Integer sets.
Definition: int.hh:174
Expensive.
Definition: core.hpp:582
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:56
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:3321
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)
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
SetExpr inter(const SetVarArgs &x)
Intersection of set variables.
Definition: set-expr.cpp:699
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
Propagation cost.
Definition: core.hpp:554
Range iterator cache
ExecStatus
Definition: core.hpp:540
#define forceinline
Definition: config.hpp:173
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:544
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
Gecode toplevel namespace
Range iterator for computing set difference.
Definition: ranges-diff.hpp:47
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
bool before(const ConstSetView &x, const ConstSetView &y)
Definition: const.hpp:698
Home class for posting propagators
Definition: core.hpp:922