Generated on for Gecode by doxygen 1.15.0
arithmetic.cpp
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.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2002
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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
35
36namespace Gecode {
37
38 void
39 abs(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
40 using namespace Int;
42 if (vbd(ipl) == IPL_DOM) {
44 } else {
46 }
47 }
48
49
50 void
51 max(Home home, IntVar x0, IntVar x1, IntVar x2,
52 IntPropLevel ipl) {
53 using namespace Int;
55 if (vbd(ipl) == IPL_DOM) {
57 } else {
59 }
60 }
61
62 void
63 max(Home home, const IntVarArgs& x, IntVar y,
64 IntPropLevel ipl) {
65 using namespace Int;
66 if (x.size() == 0)
67 throw TooFewArguments("Int::max");
69 ViewArray<IntView> xv(home,x);
70 if (vbd(ipl) == IPL_DOM) {
72 } else {
74 }
75 }
76
77 void
78 min(Home home, IntVar x0, IntVar x1, IntVar x2,
79 IntPropLevel ipl) {
80 using namespace Int;
82 MinusView m0(x0); MinusView m1(x1); MinusView m2(x2);
83 if (vbd(ipl) == IPL_DOM) {
85 } else {
87 }
88 }
89
90 void
91 min(Home home, const IntVarArgs& x, IntVar y,
92 IntPropLevel ipl) {
93 using namespace Int;
94 if (x.size() == 0)
95 throw TooFewArguments("Int::min");
97 ViewArray<MinusView> m(home,x.size());
98 for (int i=0; i<x.size(); i++)
99 m[i] = MinusView(x[i]);
100 MinusView my(y);
101 if (vbd(ipl) == IPL_DOM) {
103 } else {
105 }
106 }
107
108
109 void
110 argmax(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
111 IntPropLevel) {
112 using namespace Int;
113 if (x.size() == 0)
114 throw TooFewArguments("Int::argmax");
115 if (same(x,y))
116 throw ArgumentSame("Int::argmax");
118 // Constrain y properly
119 IntView yv(y);
120 GECODE_ME_FAIL(yv.gq(home,0));
121 GECODE_ME_FAIL(yv.le(home,x.size()));
122 // Construct index view array
123 IdxViewArray<IntView> ix(home,x.size());
124 for (int i=0; i<x.size(); i++) {
125 ix[i].idx=i; ix[i].view=x[i];
126 }
127 if (tiebreak)
129 ::post(home,ix,yv)));
130 else
132 ::post(home,ix,yv)));
133 }
134
135 void
136 argmax(Home home, const IntVarArgs& x, int o, IntVar y, bool tiebreak,
137 IntPropLevel) {
138 using namespace Int;
139 Limits::nonnegative(o,"Int::argmax");
140 if (x.size() == 0)
141 throw TooFewArguments("Int::argmax");
142 if (same(x,y))
143 throw ArgumentSame("Int::argmax");
145 // Constrain y properly
146 OffsetView yv(y,-o);
147 GECODE_ME_FAIL(yv.gq(home,0));
148 GECODE_ME_FAIL(yv.le(home,x.size()));
149 // Construct index view array
150 IdxViewArray<IntView> ix(home,x.size());
151 for (int i=0; i<x.size(); i++) {
152 ix[i].idx=i; ix[i].view=x[i];
153 }
154 if (tiebreak)
156 ::post(home,ix,yv)));
157 else
159 ::post(home,ix,yv)));
160 }
161
162 void
163 argmin(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
164 IntPropLevel) {
165 using namespace Int;
166 if (x.size() == 0)
167 throw TooFewArguments("Int::argmin");
168 if (same(x,y))
169 throw ArgumentSame("Int::argmin");
171 // Constrain y properly
172 IntView yv(y);
173 GECODE_ME_FAIL(yv.gq(home,0));
174 GECODE_ME_FAIL(yv.le(home,x.size()));
175 // Construct index view array
176 IdxViewArray<MinusView> ix(home,x.size());
177 for (int i=0; i<x.size(); i++) {
178 ix[i].idx=i; ix[i].view=MinusView(x[i]);
179 }
180 if (tiebreak)
182 ::post(home,ix,yv)));
183 else
185 ::post(home,ix,yv)));
186 }
187
188 void
189 argmin(Home home, const IntVarArgs& x, int o, IntVar y, bool tiebreak,
190 IntPropLevel) {
191 using namespace Int;
192 Limits::nonnegative(o,"Int::argmin");
193 if (x.size() == 0)
194 throw TooFewArguments("Int::argmin");
195 if (same(x,y))
196 throw ArgumentSame("Int::argmin");
198 // Constrain y properly
199 OffsetView yv(y,-o);
200 GECODE_ME_FAIL(yv.gq(home,0));
201 GECODE_ME_FAIL(yv.le(home,x.size()));
202 // Construct index view array
203 IdxViewArray<MinusView> ix(home,x.size());
204 for (int i=0; i<x.size(); i++) {
205 ix[i].idx=i; ix[i].view=MinusView(x[i]);
206 }
207 if (tiebreak)
209 ::post(home,ix,yv)));
210 else
212 ::post(home,ix,yv)));
213 }
214
215 void
216 argmax(Home home, const BoolVarArgs& x, IntVar y, bool tiebreak,
217 IntPropLevel) {
218 using namespace Int;
219 if (x.size() == 0)
220 throw TooFewArguments("Int::argmax");
222 // Constrain y properly
223 IntView yv(y);
224 GECODE_ME_FAIL(yv.gq(home,0));
225 GECODE_ME_FAIL(yv.le(home,x.size()));
226 // Construct index view array
227 IdxViewArray<BoolView> ix(home,x.size());
228 for (int i=x.size(); i--; ) {
229 ix[i].idx=i; ix[i].view=x[i];
230 }
231 if (tiebreak)
233 ::post(home,ix,yv)));
234 else
236 ::post(home,ix,yv)));
237 }
238
239 void
240 argmax(Home home, const BoolVarArgs& x, int o, IntVar y, bool tiebreak,
241 IntPropLevel) {
242 using namespace Int;
243 Limits::nonnegative(o,"Int::argmax");
244 if (x.size() == 0)
245 throw TooFewArguments("Int::argmax");
247 // Constrain y properly
248 OffsetView yv(y,-o);
249 GECODE_ME_FAIL(yv.gq(home,0));
250 GECODE_ME_FAIL(yv.le(home,x.size()));
251 // Construct index view array
252 IdxViewArray<BoolView> ix(home,x.size());
253 for (int i=x.size(); i--; ) {
254 ix[i].idx=i; ix[i].view=x[i];
255 }
256 if (tiebreak)
258 ::post(home,ix,yv)));
259 else
261 ::post(home,ix,yv)));
262 }
263
264 void
265 argmin(Home home, const BoolVarArgs& x, IntVar y, bool tiebreak,
266 IntPropLevel) {
267 using namespace Int;
268 if (x.size() == 0)
269 throw TooFewArguments("Int::argmin");
271 // Constrain y properly
272 IntView yv(y);
273 GECODE_ME_FAIL(yv.gq(home,0));
274 GECODE_ME_FAIL(yv.le(home,x.size()));
275 // Construct index view array
276 IdxViewArray<NegBoolView> ix(home,x.size());
277 for (int i=x.size(); i--; ) {
278 ix[i].idx=i; ix[i].view=NegBoolView(x[i]);
279 }
280 if (tiebreak)
282 ::post(home,ix,yv)));
283 else
285 ::post(home,ix,yv)));
286 }
287
288 void
289 argmin(Home home, const BoolVarArgs& x, int o, IntVar y, bool tiebreak,
290 IntPropLevel) {
291 using namespace Int;
292 Limits::nonnegative(o,"Int::argmin");
293 if (x.size() == 0)
294 throw TooFewArguments("Int::argmin");
296 // Constrain y properly
297 OffsetView yv(y,-o);
298 GECODE_ME_FAIL(yv.gq(home,0));
299 GECODE_ME_FAIL(yv.le(home,x.size()));
300 // Construct index view array
301 IdxViewArray<NegBoolView> ix(home,x.size());
302 for (int i=x.size(); i--; ) {
303 ix[i].idx=i; ix[i].view=NegBoolView(x[i]);
304 }
305 if (tiebreak)
307 ::post(home,ix,yv)));
308 else
310 ::post(home,ix,yv)));
311 }
312
313 void
314 mult(Home home, IntVar x0, IntVar x1, IntVar x2,
315 IntPropLevel ipl) {
316 using namespace Int;
318 if (vbd(ipl) == IPL_DOM) {
320 } else {
322 }
323 }
324
325
326 void
327 divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3,
328 IntPropLevel) {
329 using namespace Int;
330 IntVarArgs allvars = {x0, x1, x2, x3};
331 if (same(allvars))
332 throw ArgumentSame("Int::divmod");
334
338 t[0].a = 1; t[0].x = prod;
339 t[1].a = 1; t[1].x = x3;
340 int min, max;
341 Linear::estimate(t,2,0,min,max);
342 IntView x0v(x0);
343 GECODE_ME_FAIL(x0v.gq(home,min));
344 GECODE_ME_FAIL(x0v.lq(home,max));
345 t[2].a=-1; t[2].x=x0;
346 Linear::post(home,t,3,IRT_EQ,0,IPL_BND);
347 if (home.failed()) return;
348 IntView x1v(x1);
350 Arithmetic::DivMod<IntView>::post(home,x0,x1,x3));
351 }
352
353 void
354 div(Home home, IntVar x0, IntVar x1, IntVar x2,
355 IntPropLevel) {
356 using namespace Int;
359 (Arithmetic::DivBnd<IntView>::post(home,x0,x1,x2)));
360 }
361
362 void
363 mod(Home home, IntVar x0, IntVar x1, IntVar x2,
364 IntPropLevel ipl) {
365 using namespace Int;
368 divmod(home, x0, x1, _div, x2, ipl);
369 }
370
371 void
372 sqr(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
373 using namespace Int;
376 if (vbd(ipl) == IPL_DOM) {
378 ::post(home,x0,x1,ops));
379 } else {
381 ::post(home,x0,x1,ops));
382 }
383 }
384
385 void
386 sqrt(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
387 using namespace Int;
390 if (vbd(ipl) == IPL_DOM) {
392 ::post(home,x0,x1,ops));
393 } else {
395 ::post(home,x0,x1,ops));
396 }
397 }
398
399 void
400 pow(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
401 using namespace Int;
402 Limits::nonnegative(n,"Int::pow");
404 if (n == 2) {
405 sqr(home, x0, x1, ipl);
406 return;
407 }
408 Arithmetic::PowOps ops(n);
409 if (vbd(ipl) == IPL_DOM) {
411 ::post(home,x0,x1,ops));
412 } else {
414 ::post(home,x0,x1,ops));
415 }
416 }
417
418 void
419 nroot(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
420 using namespace Int;
421 Limits::positive(n,"Int::nroot");
423 if (n == 2) {
424 sqrt(home, x0, x1, ipl);
425 return;
426 }
427 Arithmetic::PowOps ops(n);
428 if (vbd(ipl) == IPL_DOM) {
430 ::post(home,x0,x1,ops));
431 } else {
433 ::post(home,x0,x1,ops));
434 }
435 }
436
437}
438
439// STATISTICS: int-post
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1597
Passing Boolean variables.
Definition int.hh:738
Home class for posting propagators
Definition core.hpp:863
bool failed(void) const
Check whether corresponding space is failed.
Definition core.hpp:4185
Passing integer variables.
Definition int.hh:680
Integer variables.
Definition int.hh:389
Exception: Arguments contain same variable multiply
Definition exception.hpp:80
static ExecStatus post(Home home, View x0, View x1)
Post bounds consistent propagator .
Definition abs.hpp:93
static ExecStatus post(Home home, View x0, View x1)
Post domain consistent propagator .
Definition abs.hpp:148
Argument maximum propagator.
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator (rounding towards 0).
Definition divmod.hpp:235
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition divmod.hpp:280
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition max.hpp:80
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition max.hpp:242
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition mult.cpp:144
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition mult.cpp:311
static ExecStatus post(Home home, ViewArray< View > &x, View y)
Post propagator .
Definition max.hpp:134
static ExecStatus post(Home home, ViewArray< View > &x, View y)
Post propagator .
Definition max.hpp:318
Bounds consistent n-th root propagator.
Domain consistent n-th root propagator.
Bounds consistent power propagator.
Domain consistent power propagator.
Operations for power and nroot propagators.
Operations for square and square-root propagators.
An array of IdxView pairs.
Definition idx-view.hh:67
Integer view for integer variables.
Definition view.hpp:129
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition int.hpp:121
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
Class for describing linear term .
Definition linear.hh:1336
int a
Coefficient.
Definition linear.hh:1339
Minus integer view.
Definition view.hpp:282
Negated Boolean view.
Definition view.hpp:1572
Offset integer view.
Definition view.hpp:443
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition offset.hpp:145
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition offset.hpp:136
Exception: Too few arguments available in argument array
Definition exception.hpp:66
View arrays.
Definition array.hpp:255
void post(Home home, Term< IntView > *t, int n, IntRelType irt, int c, IntPropLevel ipl=IPL_DEF)
Post propagator for linear constraint over integers.
Definition int-post.cpp:219
#define GECODE_POST
Check for failure in a constraint post function.
Definition macros.hpp:40
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition macros.hpp:103
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition macros.hpp:77
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:1008
@ IRT_EQ
Equality ( ).
Definition int.hh:960
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1013
@ IPL_BND
Bounds propagation.
Definition int.hh:1012
void positive(int n, const char *l)
Check whether n is in range and strictly positive, otherwise throw out of limits with information l.
Definition limits.hpp:57
void nonnegative(int n, const char *l)
Check whether n is in range and nonnegative, otherwise throw out of limits with information l.
Definition limits.hpp:68
const int min
Smallest allowed integer value.
Definition int.hh:122
const int max
Largest allowed integer value.
Definition int.hh:120
void estimate(Term< View > *t, int n, int c, int &l, int &u)
Estimate lower and upper bounds.
Definition post.hpp:41
Finite domain integers.
Definition lastval.hh:52
Gecode toplevel namespace
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
TieBreak< VarBranch > tiebreak(VarBranch a, VarBranch b)
Combine variable selection criteria a and b for tie-breaking.
Definition tiebreak.hpp:80
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void argmax(Home home, const IntVarArgs &x, IntVar y, bool tiebreak=true, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void mult(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool same(VarArgArray< Var > x, VarArgArray< Var > y)
Definition array.hpp:1927
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void argmin(Home home, const IntVarArgs &x, IntVar y, bool tiebreak=true, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .