Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
nroot.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, 2012
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
36#include <climits>
37#include <algorithm>
38
39namespace Gecode { namespace Int { namespace Arithmetic {
40
41 /*
42 * Positive bounds consistent nth root
43 *
44 */
45
46 template<class Ops, bool minus>
48 prop_nroot_plus_bnd(Space& home, IntView x0, IntView x1, const Ops& ops) {
49 if (minus) {
50 bool mod;
51 do {
52 mod = false;
53 {
54 ModEvent me = x1.gq(home,-ops.cnroot(-x0.min()));
55 if (me_failed(me)) return ES_FAILED;
56 mod |= me_modified(me);
57 }
58 {
59 ModEvent me = x1.lq(home,-ops.cnroot(-x0.max()));
60 if (me_failed(me)) return ES_FAILED;
61 mod |= me_modified(me);
62 }
63 {
64 ModEvent me = x0.gq(home,-ops.tpow(-x1.min()));
65 if (me_failed(me)) return ES_FAILED;
66 mod |= me_modified(me);
67 }
68 {
69 ModEvent me = x0.lq(home,-(ops.tpow(-x1.max()-1)+1));
70 if (me_failed(me)) return ES_FAILED;
71 mod |= me_modified(me);
72 }
73 } while (mod);
74 } else {
75 bool mod;
76 do {
77 mod = false;
78 {
79 ModEvent me = x1.lq(home,ops.fnroot(x0.max()));
80 if (me_failed(me)) return ES_FAILED;
81 mod |= me_modified(me);
82 }
83 {
84 ModEvent me = x1.gq(home,ops.fnroot(x0.min()));
85 if (me_failed(me)) return ES_FAILED;
86 mod |= me_modified(me);
87 }
88 {
89 ModEvent me = x0.le(home,ops.tpow(x1.max()+1));
90 if (me_failed(me)) return ES_FAILED;
91 mod |= me_modified(me);
92 }
93 {
94 ModEvent me = x0.gq(home,ops.tpow(x1.min()));
95 if (me_failed(me)) return ES_FAILED;
96 mod |= me_modified(me);
97 }
98 } while (mod);
99 }
100 return ES_OK;
101 }
102
103 template<class Ops, bool minus>
109
110 template<class Ops, bool minus>
113 if (minus) {
114 GECODE_ME_CHECK(x0.lq(home,0));
115 GECODE_ME_CHECK(x1.lq(home,0));
116 } else {
117 GECODE_ME_CHECK(x0.gq(home,0));
118 GECODE_ME_CHECK(x1.gq(home,0));
119 }
120 (void) new (home) NrootPlusBnd<Ops,minus>(home,x0,x1,ops);
121 return ES_OK;
122 }
123
124 template<class Ops, bool minus>
130
131 template<class Ops, bool minus>
132 Actor*
134 return new (home) NrootPlusBnd<Ops,minus>(home,*this);
135 }
136
137 template<class Ops, bool minus>
141 return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
142 }
143
144
145 /*
146 * Bounds consistent nth root
147 *
148 */
149
150 template<class Ops>
152 prop_nroot_bnd(Space& home, IntView x0, IntView x1, const Ops& ops) {
153 assert((x0.min() < 0) && (x0.max() > 0));
154 assert((x1.min() < 0) && (x1.max() > 0));
155
156 GECODE_ME_CHECK(x1.lq(home,ops.fnroot(x0.max())));
157 GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-x0.min())));
158 GECODE_ME_CHECK(x0.le(home,ops.tpow(x1.max()+1)));
159 GECODE_ME_CHECK(x0.gq(home,ops.tpow(x1.min()-1)+1));
160
161 return ES_OK;
162 }
163
164 template<class Ops>
169
170 template<class Ops>
173 if (static_cast<unsigned int>(ops.exp()) >= sizeof(int) * CHAR_BIT) {
174 // The integer limits allow only -2, -1, 0, 1 for x1
175 GECODE_ME_CHECK(x1.lq(home,1));
176 GECODE_ME_CHECK(x1.gq(home,-2));
177 // Just rewrite to values that can be handeled without overflow
178 ops.exp(ops.even() ? 30 : 31);
179 }
180
181 if (ops.exp() == 0) {
182 GECODE_ME_CHECK(x1.eq(home,1));
183 return ES_OK;
184 } else if (ops.exp() == 1) {
186 }
187
188 if (x0 == x1) {
189 assert(ops.exp() > 1);
190 GECODE_ME_CHECK(x0.lq(home,1));
191 GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -2));
192 return ES_OK;
193 }
194
195 // Limits values such that no overflow can occur
196 GECODE_ME_CHECK(x1.lq(home,ops.fnroot(Limits::max)));
197 GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-Limits::min)));
198
199 if (ops.even()) {
200 GECODE_ME_CHECK(x0.gq(home,0));
201 GECODE_ME_CHECK(x1.gq(home,0));
202 }
203
204 if ((x0.min() >= 0) || (x1.min() >= 0))
206
207 if ((x0.max() <= 0) || (x1.max() <= 0))
209
210 assert((x0.min() < 0) && (x0.max() > 0));
211 assert((x1.min() < 0) && (x1.max() > 0));
213 (void) new (home) NrootBnd(home,x0,x1,ops);
214 return ES_OK;
215 }
216
217 template<class Ops>
222
223 template<class Ops>
224 Actor*
226 return new (home) NrootBnd<Ops>(home,*this);
227 }
228
229 template<class Ops>
232 assert(!ops.even());
233 if ((x0.min() >= 0) || (x1.min() >= 0))
235
236 if ((x0.max() <= 0) || (x1.max() <= 0))
238
240
241 return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this) : ES_NOFIX;
242 }
243
244
245 /*
246 * Domain consistent nth root
247 *
248 */
250 template<class Ops>
252 protected:
254 Ops ops;
255 public:
257 forceinline RangesMapPow(const Ops& o) : ops(o) {}
259 forceinline int min(int x) const {
260 return ops.tpow(x);
261 }
262
263 forceinline int max(int x) const {
264 return ops.tpow(x+1)-1;
265 }
266 };
267
269 template<class Ops>
271 protected:
273 Ops ops;
274 public:
276 forceinline RangesMapNroot(const Ops& o) : ops(o) {}
278 forceinline int min(int x) const {
279 return (x < 0) ? -ops.cnroot(-x) : ops.fnroot(x);
280 }
281
282 forceinline int max(int x) const {
283 return (x < 0) ? -ops.cnroot(-x) : ops.fnroot(x);
284 }
285 };
286
287 template<class Ops, bool minus>
293
294 template<class Ops, bool minus>
297 if (minus) {
298 GECODE_ME_CHECK(x0.lq(home,0));
299 GECODE_ME_CHECK(x1.lq(home,0));
300 } else {
301 GECODE_ME_CHECK(x0.gq(home,0));
302 GECODE_ME_CHECK(x1.gq(home,0));
303 }
305 (void) new (home) NrootPlusDom<Ops,minus>(home,x0,x1,ops);
306 return ES_OK;
307 }
308
309 template<class Ops, bool minus>
315
316 template<class Ops, bool minus>
317 Actor*
319 return new (home) NrootPlusDom<Ops,minus>(home,*this);
320 }
321
322 template<class Ops, bool minus>
325 if (IntView::me(med) == ME_INT_VAL)
327 else if (IntView::me(med) == ME_INT_DOM)
329 else
331 }
332
333 template<class Ops, bool minus>
336 if (IntView::me(med) != ME_INT_DOM) {
338 return x1.assigned() ? home.ES_SUBSUMED(*this)
340 }
341
342 {
346 m(r,rmn);
347 GECODE_ME_CHECK(x1.inter_r(home,m,false));
348 }
349
350 {
354 m(r,rmp);
355 GECODE_ME_CHECK(x0.inter_r(home,m,false));
356 }
357
358 return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
359 }
360
361
362
363 template<class Ops>
368
369 template<class Ops>
372 if (static_cast<unsigned int>(ops.exp()) >= sizeof(int) * CHAR_BIT) {
373 // The integer limits allow only -2, -1, 0, 1 for x1
374 GECODE_ME_CHECK(x1.lq(home,1));
375 GECODE_ME_CHECK(x1.gq(home,-2));
376 // Just rewrite to values that can be handeled without overflow
377 ops.exp(ops.even() ? 30 : 31);
378 }
379
380 if (ops.exp() == 0) {
381 GECODE_ME_CHECK(x1.eq(home,1));
382 return ES_OK;
383 } else if (ops.exp() == 1) {
385 }
386
387 if (x0 == x1) {
388 assert(ops.exp() > 1);
389 GECODE_ME_CHECK(x0.lq(home,1));
390 GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -2));
391 return ES_OK;
392 }
393
394 // Limits values such that no overflow can occur
395 GECODE_ME_CHECK(x1.lq(home,ops.fnroot(Limits::max)));
396 GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-Limits::min)));
397
398 if (ops.even()) {
399 GECODE_ME_CHECK(x0.gq(home,0));
400 GECODE_ME_CHECK(x1.gq(home,0));
401 }
402
403 if ((x0.min() >= 0) || (x1.min() >= 0))
405
406 if ((x0.max() <= 0) || (x1.max() <= 0))
408
409 assert((x0.min() < 0) && (x0.max() > 0));
410 assert((x1.min() < 0) && (x1.max() > 0));
412 (void) new (home) NrootDom(home,x0,x1,ops);
413 return ES_OK;
414 }
415
416 template<class Ops>
421
422 template<class Ops>
423 Actor*
425 return new (home) NrootDom<Ops>(home,*this);
426 }
427
428 template<class Ops>
431 if (IntView::me(med) == ME_INT_VAL)
433 else if (IntView::me(med) == ME_INT_DOM)
435 else
437 }
438
439 template<class Ops>
442 assert(!ops.even());
443 if ((x0.min() >= 0) || (x1.min() >= 0))
445
446 if ((x0.max() <= 0) || (x1.max() <= 0))
448
449 if (IntView::me(med) != ME_INT_DOM) {
451 return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this)
453 }
454
455 {
459 m(r,rmn);
460 GECODE_ME_CHECK(x1.inter_r(home,m,false));
461 }
462
463 {
467 m(r,rmp);
468 GECODE_ME_CHECK(x0.inter_r(home,m,false));
469 }
470
471 return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
472 }
473
474
475}}}
476
477// STATISTICS: int-prop
478
Base-class for both propagators and branchers.
Definition core.hpp:628
BinaryPropagator(Space &home, BinaryPropagator &p)
Home class for posting propagators
Definition core.hpp:856
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nroot.hpp:231
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nroot.hpp:225
NrootBnd(Space &home, NrootBnd< Ops > &p)
Constructor for cloning p.
Definition nroot.hpp:219
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition nroot.hpp:172
NrootDom(Space &home, NrootDom< Ops > &p)
Constructor for cloning p.
Definition nroot.hpp:418
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition nroot.hpp:430
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nroot.hpp:441
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition nroot.hpp:371
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nroot.hpp:424
NrootPlusBnd(Space &home, NrootPlusBnd< Ops, minus > &p)
Constructor for cloning p.
Definition nroot.hpp:126
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition nroot.hpp:112
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nroot.hpp:133
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nroot.hpp:139
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nroot.hpp:318
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nroot.hpp:335
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition nroot.hpp:296
NrootPlusDom(Space &home, NrootPlusDom< Ops, minus > &p)
Constructor for cloning p.
Definition nroot.hpp:311
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition nroot.hpp:324
Mapping integer to n-th root.
Definition nroot.hpp:270
int max(int x) const
Perform mapping of maximum.
Definition nroot.hpp:282
RangesMapNroot(const Ops &o)
Initialize with operations o.
Definition nroot.hpp:276
int min(int x) const
Perform mapping of minimum.
Definition nroot.hpp:278
Mapping ranges to powers.
Definition nroot.hpp:251
int min(int x) const
Perform mapping of minimum.
Definition nroot.hpp:259
RangesMapPow(const Ops &o)
Initialize with operations o.
Definition nroot.hpp:257
int max(int x) const
Perform mapping of maximum.
Definition nroot.hpp:263
Integer view for integer variables.
Definition view.hpp:129
int min(void) const
Return minimum of domain.
Definition int.hpp:58
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition int.hpp:121
int med(void) const
Return median of domain (greatest element not greater than the median)
Definition int.hpp:66
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition int.hpp:130
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition int.hpp:139
int max(void) const
Return maximum of domain.
Definition int.hpp:62
static ExecStatus post(Home home, View0 x0, View1 x1)
Post bounds consistent propagator .
Definition eq.hpp:108
static ExecStatus post(Home home, View0 x0, View1 x1)
Post domain consistent propagator .
Definition eq.hpp:176
Range iterator for integer views.
Definition view.hpp:54
Range iterator for mapping ranges.
Propagation cost.
Definition core.hpp:486
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition core.hpp:4820
static PropCost binary(PropCost::Mod m)
Two variables for modifier pcm.
Definition core.hpp:4816
@ HI
Expensive.
Definition core.hpp:514
friend class Space
Definition core.hpp:1068
ModEventDelta med
A set of modification events (used during propagation)
Definition core.hpp:1077
Computation spaces.
Definition core.hpp:1744
static ModEvent me(const ModEventDelta &med)
Definition view.hpp:552
ExecStatus ES_NOFIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has not computed partial fixpoint
Definition core.hpp:3583
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_nroot_bnd(Space &home, IntView x0, IntView x1, const Ops &ops)
Definition nroot.hpp:152
ExecStatus prop_nroot_plus_bnd(Space &home, IntView x0, IntView x1, const Ops &ops)
Definition nroot.hpp:48
const int min
Smallest allowed integer value.
Definition int.hh:118
const int max
Largest allowed integer value.
Definition int.hh:116
Finite domain integers.
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition var-type.hpp:91
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition var-type.hpp:100
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition var-type.hpp:56
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition var-type.hpp:72
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 .
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
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