this repo has no description
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 * 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
40namespace Gecode { namespace Set { namespace RelOp {
41
42 /*
43 * "Ternary union" propagator
44 *
45 */
46
47 template<class View0, class View1, class View2>
48 forceinline
49 Union<View0,View1,View2>::Union(Home home, View0 y0,View1 y1,View2 y2)
50 : MixTernaryPropagator<View0,PC_SET_ANY,View1,PC_SET_ANY,
51 View2,PC_SET_ANY>(home,y0,y1,y2) {}
52
53 template<class View0, class View1, class View2>
54 forceinline
55 Union<View0,View1,View2>::Union(Space& home,
56 Union<View0,View1,View2>& p)
57 : MixTernaryPropagator<View0,PC_SET_ANY,View1,PC_SET_ANY,
58 View2,PC_SET_ANY>(home,p) {}
59
60 template<class View0, class View1, class View2>
61 ExecStatus Union<View0,View1,View2>::post(Home home, View0 x0,
62 View1 x1, View2 x2) {
63 (void) new (home) Union<View0,View1,View2>(home,x0,x1,x2);
64 return ES_OK;
65 }
66
67 template<class View0, class View1, class View2>
68 Actor*
69 Union<View0,View1,View2>::copy(Space& home) {
70 return new (home) Union(home,*this);
71 }
72
73 template<class View0, class View1, class View2>
74 ExecStatus
75 Union<View0,View1,View2>::propagate(Space& home, const ModEventDelta& med) {
76 // This propagator implements the constraint
77 // x2 = x0 u x1
78
79 bool x0ass = x0.assigned();
80 bool x1ass = x1.assigned();
81 bool x2ass = x2.assigned();
82
83 ModEvent me0 = View0::me(med);
84 ModEvent me1 = View1::me(med);
85 ModEvent me2 = View2::me(med);
86
87 bool x0ubmod = false;
88 bool x1ubmod = false;
89 bool modified = false;
90
91 do {
92
93 modified = false;
94 do {
95 // 4) lub(x2) <= lub(x0) u lub(x1)
96 {
97 LubRanges<View0> x0ub(x0);
98 LubRanges<View1> x1ub(x1);
99 Iter::Ranges::Union<LubRanges<View0>, LubRanges<View1> > u2(x0ub,x1ub);
100 GECODE_ME_CHECK_MODIFIED(modified, x2.intersectI(home,u2) );
101 }
102
103 if (modified || Rel::testSetEventUB(me2) )
104 {
105 modified = false;
106 // 5) x0 <= x2
107 LubRanges<View2> x2ub1(x2);
108 GECODE_ME_CHECK_MODIFIED(modified, x0.intersectI(home,x2ub1) );
109 x0ubmod |= modified;
110
111 // 6) x1 <= x2
112 bool modified2=false;
113 LubRanges<View2> x2ub2(x2);
114 GECODE_ME_CHECK_MODIFIED(modified2, x1.intersectI(home,x2ub2) );
115 x1ubmod |= modified2;
116 modified |= modified2;
117 }
118
119 } while (modified);
120
121 modified = false;
122 do {
123 bool modifiedOld = modified;
124 modified = false;
125
126 if (Rel::testSetEventLB(me2) || Rel::testSetEventUB(me0)
127 || x0ubmod || modifiedOld)
128 {
129 // 1) glb(x0) \ lub(x1) <= glb(x2)
130 GlbRanges<View2> x2lb(x2);
131 LubRanges<View0> x0ub(x0);
132 Iter::Ranges::Diff<GlbRanges<View2>, LubRanges<View0> >
133 diff(x2lb, x0ub);
134 GECODE_ME_CHECK_MODIFIED(modified, x1.includeI(home,diff) );
135 }
136
137 if (Rel::testSetEventLB(me2) || Rel::testSetEventUB(me1)
138 || x1ubmod || modifiedOld)
139 {
140 // 2) glb(x0) \ lub(x2) <= glb(x1)
141 GlbRanges<View2> x2lb(x2);
142 LubRanges<View1> x1ub(x1);
143 Iter::Ranges::Diff<GlbRanges<View2>, LubRanges<View1> >
144 diff(x2lb, x1ub);
145 GECODE_ME_CHECK_MODIFIED(modified, x0.includeI(home,diff) );
146 }
147
148 if (Rel::testSetEventLB(me0,me1) || modified)
149 {
150 // modified = false;
151 // 3) glb(x1) u glb(x2) <= glb(x0)
152 GlbRanges<View0> x0lb(x0);
153 GlbRanges<View1> x1lb(x1);
154 Iter::Ranges::Union<GlbRanges<View0>, GlbRanges<View1> >
155 u1(x0lb,x1lb);
156 GECODE_ME_CHECK_MODIFIED(modified, x2.includeI(home,u1) );
157 }
158
159 } while(modified);
160
161 modified = false;
162 {
163 // cardinality
164 ExecStatus ret = unionCard(home,modified, x0, x1, x2);
165 GECODE_ES_CHECK(ret);
166 }
167
168 if (x2.cardMax() == 0) {
169 GECODE_ME_CHECK( x0.cardMax(home, 0) );
170 GECODE_ME_CHECK( x1.cardMax(home, 0) );
171 return home.ES_SUBSUMED(*this);
172 }
173
174 if (x0.cardMax() == 0)
175 GECODE_REWRITE(*this,(Rel::Eq<View1,View2>::post(home(*this),x1,x2)));
176 if (x1.cardMax() == 0)
177 GECODE_REWRITE(*this,(Rel::Eq<View0,View2>::post(home(*this),x0,x2)));
178
179 } while(modified);
180
181 if (shared(x0,x1,x2)) {
182 return (x0ass && x1ass && x2ass) ? home.ES_SUBSUMED(*this) : ES_NOFIX;
183 } else {
184 if (x0ass && x1ass && x2ass)
185 return home.ES_SUBSUMED(*this);
186 if (x0ass != x0.assigned() ||
187 x1ass != x1.assigned() ||
188 x2ass != x2.assigned()) {
189 return ES_NOFIX;
190 } else {
191 return ES_FIX;
192 }
193 }
194 }
195
196
197 /*
198 * "Nary union" propagator
199 *
200 */
201
202 template<class View0, class View1>
203 forceinline
204 UnionN<View0,View1>::UnionN(Home home, ViewArray<View0>& x, View1 y)
205 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,x,y) {
206 shared = Gecode::shared(x) || viewarrayshared(x,y);
207 }
208
209 template<class View0, class View1>
210 forceinline
211 UnionN<View0,View1>::UnionN(Home home, ViewArray<View0>& x,
212 const IntSet& z, View1 y)
213 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,x,y) {
214 shared = Gecode::shared(x) || viewarrayshared(x,y);
215 IntSetRanges rz(z);
216 unionOfDets.includeI(home, rz);
217 }
218
219 template<class View0, class View1>
220 forceinline
221 UnionN<View0,View1>::UnionN(Space& home, UnionN& p)
222 : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,p),
223 shared(p.shared) {
224 unionOfDets.update(home,p.unionOfDets);
225 }
226
227 template<class View0, class View1>
228 Actor*
229 UnionN<View0,View1>::copy(Space& home) {
230 return new (home) UnionN<View0,View1>(home,*this);
231 }
232
233 template<class View0, class View1>
234 ExecStatus
235 UnionN<View0,View1>::post(Home home, ViewArray<View0>& x, View1 y) {
236 switch (x.size()) {
237 case 0:
238 GECODE_ME_CHECK(y.cardMax(home, 0));
239 return ES_OK;
240 case 1:
241 return Rel::Eq<View0,View1>::post(home, x[0], y);
242 case 2:
243 return Union<View0,View0,View1>::post(home, x[0], x[1], y);
244 default:
245 (void) new (home) UnionN<View0,View1>(home,x,y);
246 return ES_OK;
247 }
248 }
249
250 template<class View0, class View1>
251 ExecStatus
252 UnionN<View0,View1>::post(Home home, ViewArray<View0>& x,
253 const IntSet& z, View1 y) {
254 (void) new (home) UnionN<View0,View1>(home,x,z,y);
255 return ES_OK;
256 }
257
258 template<class View0, class View1>
259 PropCost
260 UnionN<View0,View1>::cost(const Space&, const ModEventDelta&) const {
261 return PropCost::quadratic(PropCost::LO, x.size()+1);
262 }
263
264 template<class View0, class View1>
265 ExecStatus
266 UnionN<View0,View1>::propagate(Space& home, const ModEventDelta& med) {
267 ModEvent me0 = View0::me(med);
268 ModEvent me1 = View1::me(med);
269 bool ubevent = Rel::testSetEventUB(me0, me1);
270 bool lbevent = Rel::testSetEventLB(me0, me1);
271 bool anybevent = Rel::testSetEventAnyB(me0, me1);
272 bool cardevent = Rel::testSetEventCard(me0, me1);
273
274 bool modified = false;
275 bool oldModified = false;
276
277 do {
278 do {
279 oldModified = modified;
280 modified = false;
281 if (modified || oldModified || ubevent)
282 GECODE_ES_CHECK(unionNXiUB(home, modified, x, y,unionOfDets));
283 if (modified || oldModified || ubevent)
284 GECODE_ES_CHECK(partitionNYUB(home, modified, x, y,unionOfDets));
285 if (modified || oldModified || anybevent)
286 GECODE_ES_CHECK(partitionNXiLB(home, modified, x, y,unionOfDets));
287 if (modified || oldModified || lbevent)
288 GECODE_ES_CHECK(partitionNYLB(home, modified, x, y,unionOfDets));
289 if (modified || oldModified || cardevent || ubevent) {
290 GECODE_ES_CHECK(unionNCard(home, modified, x, y, unionOfDets));
291 }
292 } while (modified);
293
294 for(int i=0;i<x.size();i++) {
295 //Do not reverse! Eats away the end of the array!
296 while (i<x.size() && x[i].assigned()) {
297 GlbRanges<View0> det(x[i]);
298 unionOfDets.includeI(home,det);
299 x.move_lst(i);
300 modified = true;
301 }
302 }
303
304 } while (modified);
305 // When we run out of variables, make a final check and disolve:
306 if (x.size()==0) {
307 BndSetRanges all1(unionOfDets);
308 GECODE_ME_CHECK( y.intersectI(home,all1) );
309 BndSetRanges all2(unionOfDets);
310 GECODE_ME_CHECK( y.includeI(home,all2) );
311 unionOfDets.dispose(home);
312 return home.ES_SUBSUMED(*this);
313 }
314
315 return shared ? ES_NOFIX : ES_FIX;
316 }
317
318}}}
319
320// STATISTICS: set-prop