Generated on Sat Jan 20 2018 22:21:10 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  * Last modified:
10  * $Date: 2017-03-23 15:08:48 +0100 (Thu, 23 Mar 2017) $ by $Author: schulte $
11  * $Revision: 15616 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/int/arithmetic.hh>
39 
40 namespace Gecode {
41 
42  void
43  abs(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
44  using namespace Int;
46  if (vbd(ipl) == IPL_DOM) {
48  } else {
50  }
51  }
52 
53 
54  void
55  max(Home home, IntVar x0, IntVar x1, IntVar x2,
56  IntPropLevel ipl) {
57  using namespace Int;
59  if (vbd(ipl) == IPL_DOM) {
61  } else {
63  }
64  }
65 
66  void
67  max(Home home, const IntVarArgs& x, IntVar y,
68  IntPropLevel ipl) {
69  using namespace Int;
70  if (x.size() == 0)
71  throw TooFewArguments("Int::max");
73  ViewArray<IntView> xv(home,x);
74  if (vbd(ipl) == IPL_DOM) {
76  } else {
78  }
79  }
80 
81  void
82  min(Home home, IntVar x0, IntVar x1, IntVar x2,
83  IntPropLevel ipl) {
84  using namespace Int;
86  MinusView m0(x0); MinusView m1(x1); MinusView m2(x2);
87  if (vbd(ipl) == IPL_DOM) {
89  } else {
91  }
92  }
93 
94  void
95  min(Home home, const IntVarArgs& x, IntVar y,
96  IntPropLevel ipl) {
97  using namespace Int;
98  if (x.size() == 0)
99  throw TooFewArguments("Int::min");
100  GECODE_POST;
101  ViewArray<MinusView> m(home,x.size());
102  for (int i=x.size(); i--; )
103  m[i] = MinusView(x[i]);
104  MinusView my(y);
105  if (vbd(ipl) == IPL_DOM) {
107  } else {
109  }
110  }
111 
112 
113  void
114  argmax(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
115  IntPropLevel) {
116  using namespace Int;
117  if (x.size() == 0)
118  throw TooFewArguments("Int::argmax");
119  if (x.same(home,y))
120  throw ArgumentSame("Int::argmax");
121  GECODE_POST;
122  // Constrain y properly
123  IntView yv(y);
124  GECODE_ME_FAIL(yv.gq(home,0));
125  GECODE_ME_FAIL(yv.le(home,x.size()));
126  // Construct index view array
127  IdxViewArray<IntView> ix(home,x.size());
128  for (int i=x.size(); i--; ) {
129  ix[i].idx=i; ix[i].view=x[i];
130  }
131  if (tiebreak)
133  ::post(home,ix,yv)));
134  else
136  ::post(home,ix,yv)));
137  }
138 
139  void
140  argmax(Home home, const IntVarArgs& x, int o, IntVar y, bool tiebreak,
141  IntPropLevel) {
142  using namespace Int;
143  Limits::nonnegative(o,"Int::argmax");
144  if (x.size() == 0)
145  throw TooFewArguments("Int::argmax");
146  if (x.same(home,y))
147  throw ArgumentSame("Int::argmax");
148  GECODE_POST;
149  // Constrain y properly
150  OffsetView yv(y,-o);
151  GECODE_ME_FAIL(yv.gq(home,0));
152  GECODE_ME_FAIL(yv.le(home,x.size()));
153  // Construct index view array
154  IdxViewArray<IntView> ix(home,x.size());
155  for (int i=x.size(); i--; ) {
156  ix[i].idx=i; ix[i].view=x[i];
157  }
158  if (tiebreak)
160  ::post(home,ix,yv)));
161  else
163  ::post(home,ix,yv)));
164  }
165 
166  void
167  argmin(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
168  IntPropLevel) {
169  using namespace Int;
170  if (x.size() == 0)
171  throw TooFewArguments("Int::argmin");
172  if (x.same(home,y))
173  throw ArgumentSame("Int::argmin");
174  GECODE_POST;
175  // Constrain y properly
176  IntView yv(y);
177  GECODE_ME_FAIL(yv.gq(home,0));
178  GECODE_ME_FAIL(yv.le(home,x.size()));
179  // Construct index view array
180  IdxViewArray<MinusView> ix(home,x.size());
181  for (int i=x.size(); i--; ) {
182  ix[i].idx=i; ix[i].view=MinusView(x[i]);
183  }
184  if (tiebreak)
186  ::post(home,ix,yv)));
187  else
189  ::post(home,ix,yv)));
190  }
191 
192  void
193  argmin(Home home, const IntVarArgs& x, int o, IntVar y, bool tiebreak,
194  IntPropLevel) {
195  using namespace Int;
196  Limits::nonnegative(o,"Int::argmin");
197  if (x.size() == 0)
198  throw TooFewArguments("Int::argmin");
199  if (x.same(home,y))
200  throw ArgumentSame("Int::argmin");
201  GECODE_POST;
202  // Constrain y properly
203  OffsetView yv(y,-o);
204  GECODE_ME_FAIL(yv.gq(home,0));
205  GECODE_ME_FAIL(yv.le(home,x.size()));
206  // Construct index view array
207  IdxViewArray<MinusView> ix(home,x.size());
208  for (int i=x.size(); i--; ) {
209  ix[i].idx=i; ix[i].view=MinusView(x[i]);
210  }
211  if (tiebreak)
213  ::post(home,ix,yv)));
214  else
216  ::post(home,ix,yv)));
217  }
218 
219 
220  void
221  mult(Home home, IntVar x0, IntVar x1, IntVar x2,
222  IntPropLevel ipl) {
223  using namespace Int;
224  GECODE_POST;
225  if (vbd(ipl) == IPL_DOM) {
227  } else {
229  }
230  }
231 
232 
233  void
234  divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3,
235  IntPropLevel) {
236  using namespace Int;
237  GECODE_POST;
238 
240  GECODE_ES_FAIL(Arithmetic::MultBnd::post(home,x1,x2,prod));
242  t[0].a = 1; t[0].x = prod;
243  t[1].a = 1; t[1].x = x3;
244  int min, max;
245  Linear::estimate(t,2,0,min,max);
246  IntView x0v(x0);
247  GECODE_ME_FAIL(x0v.gq(home,min));
248  GECODE_ME_FAIL(x0v.lq(home,max));
249  t[2].a=-1; t[2].x=x0;
250  Linear::post(home,t,3,IRT_EQ,0,IPL_BND);
251  if (home.failed()) return;
252  IntView x1v(x1);
254  Arithmetic::DivMod<IntView>::post(home,x0,x1,x3));
255  }
256 
257  void
258  div(Home home, IntVar x0, IntVar x1, IntVar x2,
259  IntPropLevel) {
260  using namespace Int;
261  GECODE_POST;
263  (Arithmetic::DivBnd<IntView>::post(home,x0,x1,x2)));
264  }
265 
266  void
267  mod(Home home, IntVar x0, IntVar x1, IntVar x2,
268  IntPropLevel ipl) {
269  using namespace Int;
270  GECODE_POST;
272  divmod(home, x0, x1, _div, x2, ipl);
273  }
274 
275  void
276  sqr(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
277  using namespace Int;
278  GECODE_POST;
279  Arithmetic::SqrOps ops;
280  if (vbd(ipl) == IPL_DOM) {
282  ::post(home,x0,x1,ops));
283  } else {
285  ::post(home,x0,x1,ops));
286  }
287  }
288 
289  void
290  sqrt(Home home, IntVar x0, IntVar x1, IntPropLevel ipl) {
291  using namespace Int;
292  GECODE_POST;
293  Arithmetic::SqrOps ops;
294  if (vbd(ipl) == IPL_DOM) {
296  ::post(home,x0,x1,ops));
297  } else {
299  ::post(home,x0,x1,ops));
300  }
301  }
302 
303  void
304  pow(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
305  using namespace Int;
306  Limits::nonnegative(n,"Int::pow");
307  GECODE_POST;
308  if (n == 2) {
309  sqr(home, x0, x1, ipl);
310  return;
311  }
312  Arithmetic::PowOps ops(n);
313  if (vbd(ipl) == IPL_DOM) {
315  ::post(home,x0,x1,ops));
316  } else {
318  ::post(home,x0,x1,ops));
319  }
320  }
321 
322  void
323  nroot(Home home, IntVar x0, int n, IntVar x1, IntPropLevel ipl) {
324  using namespace Int;
325  Limits::positive(n,"Int::nroot");
326  GECODE_POST;
327  if (n == 2) {
328  sqrt(home, x0, x1, ipl);
329  return;
330  }
331  Arithmetic::PowOps ops(n);
332  if (vbd(ipl) == IPL_DOM) {
334  ::post(home,x0,x1,ops));
335  } else {
337  ::post(home,x0,x1,ops));
338  }
339  }
340 
341 }
342 
343 // STATISTICS: int-post
Bounds propagation.
Definition: int.hh:959
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl)
Post propagator for .
Definition: arithmetic.cpp:267
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition: ipl.hpp:41
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Integer division/modulo propagator.
Definition: arithmetic.hh:838
void mult(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:92
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1669
Domain consistent ternary maximum propagator.
Definition: arithmetic.hh:188
Domain consistent n-th root propagator.
Definition: arithmetic.hh:584
int a
Coefficient.
Definition: linear.hh:1343
Domain consistent n-ary maximum propagator.
Definition: arithmetic.hh:223
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:53
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:45
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:122
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:113
Exception: Too few arguments available in argument array
Definition: exception.hpp:70
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:72
void argmin(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:167
Bounds consistent power propagator.
Definition: arithmetic.hh:399
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition: mult.cpp:148
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:63
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition: int.hpp:124
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition: offset.hpp:130
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Equality ( )
Definition: int.hh:907
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: offset.hpp:139
void divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:234
Domain consistent absolute value propagator.
Definition: arithmetic.hh:96
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition: int.hpp:115
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:99
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:106
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:4099
Operations for square and square-root propagators.
Definition: arithmetic.hh:306
void argmax(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntPropLevel)
Post propagator for .
Definition: arithmetic.cpp:114
Bounds consistent n-ary maximum propagator.
Definition: arithmetic.hh:163
Offset integer view.
Definition: view.hpp:422
Argument maximum propagator.
Definition: arithmetic.hh:264
Passing integer variables.
Definition: int.hh:639
Bounds consistent division propagator.
Definition: arithmetic.hh:808
Domain consistent power propagator.
Definition: arithmetic.hh:458
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
TieBreak< VarBranch > tiebreak(VarBranch a, VarBranch b)
Combine variable selection criteria a and b for tie-breaking.
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:955
Integer view for integer variables.
Definition: view.hpp:129
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:784
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:131
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition: mult.cpp:315
Operations for power and nroot propagators.
Definition: arithmetic.hh:331
Bounds consistent ternary maximum propagator.
Definition: arithmetic.hh:135
Minus integer view.
Definition: view.hpp:276
Integer variables.
Definition: int.hh:353
Exception: Arguments contain same variable multiply
Definition: exception.hpp:84
void estimate(Term< View > *t, int n, int c, int &l, int &u)
Estimate lower and upper bounds.
Definition: post.hpp:45
Domain propagation Preferences: prefer speed or memory.
Definition: int.hh:960
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:133
Post propagator for SetVar x
Definition: set.hh:784
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition: macros.hpp:81
An array of IdxView pairs.
Definition: idx-view.hh:71
Class for describing linear term .
Definition: linear.hh:1340
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:593
#define GECODE_POST
Check for failure in a constraint post function.
Definition: macros.hpp:44
Home class for posting propagators
Definition: core.hpp:922
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:107
Bounds consistent n-th root propagator.
Definition: arithmetic.hh:524
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:61
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
bool same(const Space &home) const
Test whether array contains same variable multiply.
Definition: array.hpp:2081