Generated on Tue Jan 19 2021 06:15:49 for Gecode by doxygen 1.8.13
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.org>
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.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/arithmetic.hh>
35 
36 namespace 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");
117  GECODE_POST;
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");
144  GECODE_POST;
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");
170  GECODE_POST;
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");
197  GECODE_POST;
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 
216  void
217  mult(Home home, IntVar x0, IntVar x1, IntVar x2,
218  IntPropLevel ipl) {
219  using namespace Int;
220  GECODE_POST;
221  if (vbd(ipl) == IPL_DOM) {
223  } else {
225  }
226  }
227 
228 
229  void
230  divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3,
231  IntPropLevel) {
232  using namespace Int;
233  GECODE_POST;
234 
236  GECODE_ES_FAIL(Arithmetic::MultBnd::post(home,x1,x2,prod));
238  t[0].a = 1; t[0].x = prod;
239  t[1].a = 1; t[1].x = x3;
240  int min, max;
241  Linear::estimate(t,2,0,min,max);
242  IntView x0v(x0);
243  GECODE_ME_FAIL(x0v.gq(home,min));
244  GECODE_ME_FAIL(x0v.lq(home,max));
245  t[2].a=-1; t[2].x=x0;
246  Linear::post(home,t,3,IRT_EQ,0,IPL_BND);
247  if (home.failed()) return;
248  IntView x1v(x1);
250  Arithmetic::DivMod<IntView>::post(home,x0,x1,x3));
251  }
252 
253  void
254  div(Home home, IntVar x0, IntVar x1, IntVar x2,
255  IntPropLevel) {
256  using namespace Int;
257  GECODE_POST;
259  (Arithmetic::DivBnd<IntView>::post(home,x0,x1,x2)));
260  }
261 
262  void
263  mod(Home home, IntVar x0, IntVar x1, IntVar x2,
264  IntPropLevel ipl) {
265  using namespace Int;
266  GECODE_POST;
268  divmod(home, x0, x1, _div, x2, ipl);
269  }
270 
271  void
272  sqr(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
273  using namespace Int;
274  GECODE_POST;
275  Arithmetic::SqrOps ops;
276  if (vbd(ipl) == IPL_DOM) {
278  ::post(home,x0,x1,ops));
279  } else {
281  ::post(home,x0,x1,ops));
282  }
283  }
284 
285  void
286  sqrt(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
287  using namespace Int;
288  GECODE_POST;
289  Arithmetic::SqrOps ops;
290  if (vbd(ipl) == IPL_DOM) {
292  ::post(home,x0,x1,ops));
293  } else {
295  ::post(home,x0,x1,ops));
296  }
297  }
298 
299  void
300  pow(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
301  using namespace Int;
302  Limits::nonnegative(n,"Int::pow");
303  GECODE_POST;
304  if (n == 2) {
305  sqr(home, x0, x1, ipl);
306  return;
307  }
308  Arithmetic::PowOps ops(n);
309  if (vbd(ipl) == IPL_DOM) {
311  ::post(home,x0,x1,ops));
312  } else {
314  ::post(home,x0,x1,ops));
315  }
316  }
317 
318  void
319  nroot(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
320  using namespace Int;
321  Limits::positive(n,"Int::nroot");
322  GECODE_POST;
323  if (n == 2) {
324  sqrt(home, x0, x1, ipl);
325  return;
326  }
327  Arithmetic::PowOps ops(n);
328  if (vbd(ipl) == IPL_DOM) {
330  ::post(home,x0,x1,ops));
331  } else {
333  ::post(home,x0,x1,ops));
334  }
335  }
336 
337 }
338 
339 // STATISTICS: int-post
Bounds propagation.
Definition: int.hh:978
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl)
Post propagator for .
Definition: arithmetic.cpp:263
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition: ipl.hpp:37
NodeType t
Type of node.
Definition: bool-expr.cpp:230
Integer division/modulo propagator.
Definition: arithmetic.hh:834
void mult(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:88
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1569
Domain consistent ternary maximum propagator.
Definition: arithmetic.hh:184
Domain consistent n-th root propagator.
Definition: arithmetic.hh:580
int a
Coefficient.
Definition: linear.hh:1339
Domain consistent n-ary maximum propagator.
Definition: arithmetic.hh:219
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:49
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:41
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:118
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:109
Exception: Too few arguments available in argument array
Definition: exception.hpp:66
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
void argmin(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:163
Bounds consistent power propagator.
Definition: arithmetic.hh:395
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition: mult.cpp:144
const int max
Largest allowed integer value.
Definition: int.hh:116
const int min
Smallest allowed integer value.
Definition: int.hh:118
Bounds consistent absolute value propagator.
Definition: arithmetic.hh:59
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition: int.hpp:130
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition: offset.hpp:136
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Equality ( )
Definition: int.hh:926
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: offset.hpp:145
Gecode::IntArgs i({1, 2, 3, 4})
void divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:230
Domain consistent absolute value propagator.
Definition: arithmetic.hh:92
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition: int.hpp:121
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:95
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:102
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3975
Operations for square and square-root propagators.
Definition: arithmetic.hh:302
void argmax(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:110
Bounds consistent n-ary maximum propagator.
Definition: arithmetic.hh:159
Offset integer view.
Definition: view.hpp:443
Argument maximum propagator.
Definition: arithmetic.hh:260
Passing integer variables.
Definition: int.hh:656
Bounds consistent division propagator.
Definition: arithmetic.hh:804
bool same(VarArgArray< Var > x, VarArgArray< Var > y)
Definition: array.hpp:1899
Domain consistent power propagator.
Definition: arithmetic.hh:454
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:67
TieBreak< VarBranch > tiebreak(VarBranch a, VarBranch b)
Combine variable selection criteria a and b for tie-breaking.
Definition: tiebreak.hpp:80
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Integer view for integer variables.
Definition: view.hpp:129
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:127
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition: mult.cpp:311
Operations for power and nroot propagators.
Definition: arithmetic.hh:327
Bounds consistent ternary maximum propagator.
Definition: arithmetic.hh:131
Minus integer view.
Definition: view.hpp:282
Integer variables.
Definition: int.hh:371
Exception: Arguments contain same variable multiply
Definition: exception.hpp:80
void estimate(Term< View > *t, int n, int c, int &l, int &u)
Estimate lower and upper bounds.
Definition: post.hpp:41
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:139
Post propagator for SetVar x
Definition: set.hh:767
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition: macros.hpp:77
An array of IdxView pairs.
Definition: idx-view.hh:67
Class for describing linear term .
Definition: linear.hh:1336
Gecode toplevel namespace
void post(Home home, Term< BoolView > *t, int n, IntRelType irt, IntView x, int c, IntPropLevel)
Post propagator for linear constraint over Booleans.
Definition: bool-post.cpp:589
#define GECODE_POST
Check for failure in a constraint post function.
Definition: macros.hpp:40
Home class for posting propagators
Definition: core.hpp:853
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:103
Bounds consistent n-th root propagator.
Definition: arithmetic.hh:520
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
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition: filter.cpp:138