Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
max.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2004
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/int/rel.hh>
35
36namespace Gecode { namespace Int { namespace Arithmetic {
37
38 /*
39 * Ternary bounds consistent maximum
40 *
41 */
42
43 template<class View>
45 prop_max_bnd(Space& home, View x0, View x1, View x2) {
46 bool mod = false;
47 do {
48 mod = false;
49 {
50 ModEvent me = x2.lq(home,std::max(x0.max(),x1.max()));
51 if (me_failed(me)) return ES_FAILED;
52 mod |= me_modified(me);
53 }
54 {
55 ModEvent me = x2.gq(home,std::max(x0.min(),x1.min()));
56 if (me_failed(me)) return ES_FAILED;
57 mod |= me_modified(me);
58 }
59 {
60 ModEvent me = x0.lq(home,x2.max());
61 if (me_failed(me)) return ES_FAILED;
62 mod |= me_modified(me);
63 }
64 {
65 ModEvent me = x1.lq(home,x2.max());
66 if (me_failed(me)) return ES_FAILED;
67 mod |= me_modified(me);
68 }
69 } while (mod);
70 return ES_OK;
71 }
72
73 template<class View>
77
78 template<class View>
81 GECODE_ME_CHECK(x2.gq(home,std::max(x0.min(),x1.min())));
82 GECODE_ME_CHECK(x2.lq(home,std::max(x0.max(),x1.max())));
83 if (x0 == x1)
85 if (x0 == x2)
86 return Rel::Lq<View,View>::post(home,x1,x2);
87 if (x1 == x2)
88 return Rel::Lq<View,View>::post(home,x0,x2);
89 (void) new (home) MaxBnd<View>(home,x0,x1,x2);
90 return ES_OK;
91 }
92
93 template<class View>
97
98 template<class View>
103
104 template<class View>
105 Actor*
107 return new (home) MaxBnd<View>(home,*this);
108 }
109
110 template<class View>
114 if ((x0.max() <= x1.min()) || (x0.max() < x2.min()))
115 GECODE_REWRITE(*this,(Rel::EqBnd<View,View>::post(home(*this),x1,x2)));
116 if ((x1.max() <= x0.min()) || (x1.max() < x2.min()))
117 GECODE_REWRITE(*this,(Rel::EqBnd<View,View>::post(home(*this),x0,x2)));
118 return x0.assigned() && x1.assigned() && x2.assigned() ?
119 home.ES_SUBSUMED(*this) : ES_FIX;
120 }
121
122 /*
123 * Nary bounds consistent maximum
124 *
125 */
126
127 template<class View>
131
132 template<class View>
135 assert(x.size() > 0);
136 x.unique();
137 if (x.size() == 1)
138 return Rel::EqBnd<View,View>::post(home,x[0],y);
139 if (x.size() == 2)
140 return MaxBnd<View>::post(home,x[0],x[1],y);
141 int l = x[0].min();
142 int u = x[0].max();
143 for (int i=1; i<x.size(); i++) {
144 l = std::max(l,x[i].min());
145 u = std::max(u,x[i].max());
146 }
147 GECODE_ME_CHECK(y.gq(home,l));
148 GECODE_ME_CHECK(y.lq(home,u));
149 if (x.same(y)) {
150 // Check whether y occurs in x
151 for (int i=0; i<x.size(); i++)
153 } else {
154 (void) new (home) NaryMaxBnd<View>(home,x,y);
155 }
156 return ES_OK;
157 }
158
159 template<class View>
162 : NaryOnePropagator<View,PC_INT_BND>(home,p) {}
163
164 template<class View>
165 Actor*
167 if (x.size() == 1)
168 return new (home) Rel::EqBnd<View,View>(home,*this,x[0],y);
169 if (x.size() == 2)
170 return new (home) MaxBnd<View>(home,*this,x[0],x[1],y);
171 return new (home) NaryMaxBnd<View>(home,*this);
172 }
173
180
181 template<class View>
185 rerun:
186 assert(x.size() > 0);
187 int maxmax = x[0].max();
188 int maxmin = x[0].min();
189 for (int i=1; i<x.size(); i++) {
190 maxmax = std::max(x[i].max(),maxmax);
191 maxmin = std::max(x[i].min(),maxmin);
192 }
193 GECODE_ME_CHECK(y.lq(home,maxmax));
194 GECODE_ME_CHECK(y.gq(home,maxmin));
195 maxmin = y.min();
196 maxmax = y.max();
197 int status = MPS_ASSIGNED;
198 for (int i=x.size(); i--; ) {
199 ModEvent me = x[i].lq(home,maxmax);
200 if (me == ME_INT_FAILED)
201 return ES_FAILED;
202 if (me_modified(me) && (x[i].max() != maxmax))
203 status |= MPS_NEW_BOUND;
204 if (x[i].max() < maxmin) {
205 x.move_lst(i,home,p,pc);
206 status |= MPS_REMOVED;
207 } else if (!x[i].assigned())
208 status &= ~MPS_ASSIGNED;
209 }
210 if (x.size() == 0)
211 return ES_FAILED;
212 if ((status & MPS_REMOVED) != 0)
213 goto rerun;
214 if (((status & MPS_ASSIGNED) != 0) && y.assigned())
215 return home.ES_SUBSUMED(p);
216 return ((status & MPS_NEW_BOUND) != 0) ? ES_NOFIX : ES_FIX;
217 }
218
219 template<class View>
222 ExecStatus es = prop_nary_max_bnd(home,*this,x,y,PC_INT_BND);
223 GECODE_ES_CHECK(es);
224 if (x.size() == 1)
225 GECODE_REWRITE(*this,(Rel::EqBnd<View,View>::post(home(*this),x[0],y)));
226 return es;
227 }
228
229
230 /*
231 * Ternary domain consistent maximum
232 *
233 */
234
235 template<class View>
239
240 template<class View>
243 GECODE_ME_CHECK(x2.gq(home,std::max(x0.min(),x1.min())));
244 GECODE_ME_CHECK(x2.lq(home,std::max(x0.max(),x1.max())));
245 if (x0 == x1)
246 return Rel::EqDom<View,View>::post(home,x0,x2);
247 if (x0 == x2)
248 return Rel::Lq<View,View>::post(home,x1,x2);
249 if (x1 == x2)
250 return Rel::Lq<View,View>::post(home,x0,x2);
251 (void) new (home) MaxDom<View>(home,x0,x1,x2);
252 return ES_OK;
253 }
254
255 template<class View>
258 : TernaryPropagator<View,PC_INT_DOM>(home,p) {}
259
260 template<class View>
265
266 template<class View>
267 Actor*
269 return new (home) MaxDom<View>(home,*this);
270 }
271
272 template<class View>
275 return PropCost::ternary((View::me(med) == ME_INT_DOM) ?
277 }
278
279 template<class View>
282 if (View::me(med) != ME_INT_DOM) {
284 if ((x0.max() <= x1.min()) || (x0.max() < x2.min()))
285 GECODE_REWRITE(*this,(Rel::EqDom<View,View>::post(home(*this),x1,x2)));
286 if ((x1.max() <= x0.min()) || (x1.max() < x2.min()))
287 GECODE_REWRITE(*this,(Rel::EqDom<View,View>::post(home(*this),x0,x2)));
288 return x0.assigned() && x1.assigned() && x2.assigned() ?
289 home.ES_SUBSUMED(*this) :
290 home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM));
291 }
292 ViewRanges<View> r0(x0), r1(x1);
294 GECODE_ME_CHECK(x2.inter_r(home,u,false));
295 if (rtest_nq_dom(x0,x2) == RT_TRUE) {
297 GECODE_REWRITE(*this,(Rel::EqDom<View,View>::post(home(*this),x1,x2)));
298 }
299 if (rtest_nq_dom(x1,x2) == RT_TRUE) {
301 GECODE_REWRITE(*this,(Rel::EqDom<View,View>::post(home(*this),x0,x2)));
302 }
303 return ES_FIX;
304 }
305
306 /*
307 * Nary domain consistent maximum
308 *
309 */
310
311 template<class View>
315
316 template<class View>
319 assert(x.size() > 0);
320 x.unique();
321 if (x.size() == 1)
322 return Rel::EqDom<View,View>::post(home,x[0],y);
323 if (x.size() == 2)
324 return MaxDom<View>::post(home,x[0],x[1],y);
325 int l = x[0].min();
326 int u = x[0].max();
327 for (int i=0; i<x.size(); i++) {
328 l = std::max(l,x[i].min());
329 u = std::max(u,x[i].max());
330 }
331 GECODE_ME_CHECK(y.gq(home,l));
332 GECODE_ME_CHECK(y.lq(home,u));
333 if (x.same(y)) {
334 // Check whether y occurs in x
335 for (int i=0; i<x.size(); i++)
337 } else {
338 (void) new (home) NaryMaxDom<View>(home,x,y);
339 }
340 return ES_OK;
341 }
342
343 template<class View>
346 : NaryOnePropagator<View,PC_INT_DOM>(home,p) {}
347
348 template<class View>
349 Actor*
351 if (x.size() == 1)
352 return new (home) Rel::EqDom<View,View>(home,*this,x[0],y);
353 if (x.size() == 2)
354 return new (home) MaxDom<View>(home,*this,x[0],x[1],y);
355 return new (home) NaryMaxDom<View>(home,*this);
356 }
357
358 template<class View>
361 if (View::me(med) == ME_INT_DOM)
362 return PropCost::linear(PropCost::HI, y.size());
363 else
364 return PropCost::linear(PropCost::LO, x.size());
365 }
366
367 template<class View>
370 if (View::me(med) != ME_INT_DOM) {
371 ExecStatus es = prop_nary_max_bnd(home,*this,x,y,PC_INT_DOM);
372 GECODE_ES_CHECK(es);
373 return (es == ES_FIX) ?
374 home.ES_FIX_PARTIAL(*this,View::med(ME_INT_DOM)) :
375 home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM));
376 }
377 Region r;
378 ViewRanges<View>* i_x = r.alloc<ViewRanges<View> >(x.size());
379 for (int i=0; i<x.size(); i++) {
380 ViewRanges<View> i_xi(x[i]); i_x[i]=i_xi;
381 }
382 Iter::Ranges::NaryUnion u(r, i_x, x.size());
383 GECODE_ME_CHECK(y.inter_r(home,u,false));
384 for (int i = x.size(); i--; )
385 if (rtest_nq_dom(x[i],y) == RT_TRUE) {
387 x.move_lst(i,home,*this,PC_INT_DOM);
388 }
389 assert(x.size() > 0);
390 if (x.size() == 1)
391 GECODE_REWRITE(*this,(Rel::EqDom<View,View>::post(home(*this),x[0],y)));
392 return ES_FIX;
393 }
394
395}}}
396
397// STATISTICS: int-prop
398
Base-class for both propagators and branchers.
Definition core.hpp:628
Home class for posting propagators
Definition core.hpp:856
Bounds consistent ternary maximum propagator.
MaxBnd(Space &home, MaxBnd &p)
Constructor for cloning p.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition max.hpp:112
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition max.hpp:80
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition max.hpp:106
Domain consistent ternary maximum propagator.
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition max.hpp:242
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition max.hpp:281
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition max.hpp:268
MaxDom(Space &home, MaxDom &p)
Constructor for cloning p.
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition max.hpp:274
Bounds consistent n-ary maximum propagator.
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition max.hpp:166
NaryMaxBnd(Space &home, NaryMaxBnd &p)
Constructor for cloning p.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition max.hpp:221
static ExecStatus post(Home home, ViewArray< View > &x, View y)
Post propagator .
Definition max.hpp:134
Domain consistent n-ary maximum propagator.
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition max.hpp:350
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition max.hpp:360
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition max.hpp:369
static ExecStatus post(Home home, ViewArray< View > &x, View y)
Post propagator .
Definition max.hpp:318
NaryMaxDom(Space &home, NaryMaxDom &p)
Constructor for cloning p.
Binary bounds consistent equality propagator.
Definition rel.hh:134
static ExecStatus post(Home home, View0 x0, View1 x1)
Post bounds consistent propagator .
Definition eq.hpp:108
Binary domain consistent equality propagator.
Definition rel.hh:68
static ExecStatus post(Home home, View0 x0, View1 x1)
Post domain consistent propagator .
Definition eq.hpp:176
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition lq-le.hpp:50
Range iterator for integer views.
Definition view.hpp:54
Range iterator for union of iterators.
Range iterator for computing union (binary)
(n+1)-ary propagator
Definition pattern.hpp:172
NaryOnePropagator(Space &home, NaryOnePropagator &p)
Propagation cost.
Definition core.hpp:486
static PropCost ternary(PropCost::Mod m)
Three variables for modifier pcm.
Definition core.hpp:4812
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition core.hpp:4803
@ HI
Expensive.
Definition core.hpp:514
Base-class for propagators.
Definition core.hpp:1066
friend class Space
Definition core.hpp:1068
ModEventDelta med
A set of modification events (used during propagation)
Definition core.hpp:1077
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3505
Handle to region.
Definition region.hpp:55
Computation spaces.
Definition core.hpp:1744
Ternary propagator.
Definition pattern.hpp:113
TernaryPropagator(Space &home, TernaryPropagator &p)
View arrays.
Definition array.hpp:253
ExecStatus ES_NOFIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has not computed partial fixpoint
Definition core.hpp:3583
ExecStatus ES_FIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has computed partial fixpoint
Definition core.hpp:3576
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3570
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition macros.hpp:116
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition modevent.hpp:59
Numerical (arithmetic) propagators.
ExecStatus prop_nary_max_bnd(Space &home, Propagator &p, ViewArray< View > &x, View y, PropCond pc)
Definition max.hpp:183
ExecStatus prop_max_bnd(Space &home, View x0, View x1, View x2)
Definition max.hpp:45
MaxPropStatus
Status of propagation for nary max.
Definition max.hpp:175
@ MPS_REMOVED
A view is removed.
Definition max.hpp:177
@ MPS_NEW_BOUND
Telling has found a new upper bound.
Definition max.hpp:178
@ MPS_ASSIGNED
All views are assigned.
Definition max.hpp:176
Finite domain integers.
RelTest rtest_nq_dom(VX x, VY y)
Test whether views x and y are different (use full domain information)
Definition rel-test.hpp:126
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition var-type.hpp:91
const Gecode::ModEvent ME_INT_FAILED
Domain operation has resulted in failure.
Definition var-type.hpp:52
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition var-type.hpp:100
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition var-type.hpp:72
@ RT_TRUE
Relation does hold.
Definition view.hpp:1737
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
int PropCond
Type for propagation conditions.
Definition core.hpp:72
Post propagator for SetVar x
Definition set.hh:773
int ModEvent
Type for modification events.
Definition core.hpp:62
#define forceinline
Definition config.hpp:194