Generated on Sat Feb 7 2015 02:01:14 for Gecode by doxygen 1.8.9.1
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: 2015-01-16 14:10:48 +0100 (Fri, 16 Jan 2015) $ by $Author: schulte $
11  * $Revision: 14362 $
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, IntConLevel icl) {
44  using namespace Int;
45  if (home.failed()) return;
46  if (icl == ICL_DOM) {
48  } else {
50  }
51  }
52 
53 
54  void
55  max(Home home, IntVar x0, IntVar x1, IntVar x2,
56  IntConLevel icl) {
57  using namespace Int;
58  if (home.failed()) return;
59  if (icl == ICL_DOM) {
61  } else {
63  }
64  }
65 
66  void
67  max(Home home, const IntVarArgs& x, IntVar y,
68  IntConLevel icl) {
69  using namespace Int;
70  if (x.size() == 0)
71  throw TooFewArguments("Int::max");
72  if (home.failed()) return;
73  ViewArray<IntView> xv(home,x);
74  if (icl == ICL_DOM) {
76  } else {
78  }
79  }
80 
81  void
82  min(Home home, IntVar x0, IntVar x1, IntVar x2,
83  IntConLevel icl) {
84  using namespace Int;
85  if (home.failed()) return;
86  MinusView m0(x0); MinusView m1(x1); MinusView m2(x2);
87  if (icl == ICL_DOM) {
89  } else {
91  }
92  }
93 
94  void
95  min(Home home, const IntVarArgs& x, IntVar y,
96  IntConLevel icl) {
97  using namespace Int;
98  if (x.size() == 0)
99  throw TooFewArguments("Int::min");
100  if (home.failed()) return;
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 (icl == ICL_DOM) {
107  } else {
109  }
110  }
111 
112 
113  void
114  argmax(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
115  IntConLevel) {
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  if (home.failed()) return;
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  argmin(Home home, const IntVarArgs& x, IntVar y, bool tiebreak,
141  IntConLevel) {
142  using namespace Int;
143  if (x.size() == 0)
144  throw TooFewArguments("Int::argmin");
145  if (x.same(home,y))
146  throw ArgumentSame("Int::argmin");
147  if (home.failed()) return;
148  // Constrain y properly
149  IntView yv(y);
150  GECODE_ME_FAIL(yv.gq(home,0));
151  GECODE_ME_FAIL(yv.le(home,x.size()));
152  // Construct index view array
153  IdxViewArray<MinusView> ix(home,x.size());
154  for (int i=x.size(); i--; ) {
155  ix[i].idx=i; ix[i].view=MinusView(x[i]);
156  }
157  if (tiebreak)
159  ::post(home,ix,yv)));
160  else
162  ::post(home,ix,yv)));
163  }
164 
165 
166  void
167  mult(Home home, IntVar x0, IntVar x1, IntVar x2,
168  IntConLevel icl) {
169  using namespace Int;
170  if (home.failed()) return;
171  if (icl == ICL_DOM) {
173  } else {
175  }
176  }
177 
178 
179  void
180  divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3,
181  IntConLevel) {
182  using namespace Int;
183  if (home.failed()) return;
184 
186  GECODE_ES_FAIL(Arithmetic::MultBnd::post(home,x1,x2,prod));
188  t[0].a = 1; t[0].x = prod;
189  t[1].a = 1; t[1].x = x3;
190  int min, max;
191  Linear::estimate(t,2,0,min,max);
192  IntView x0v(x0);
193  GECODE_ME_FAIL(x0v.gq(home,min));
194  GECODE_ME_FAIL(x0v.lq(home,max));
195  t[2].a=-1; t[2].x=x0;
196  Linear::post(home,t,3,IRT_EQ,0);
197  if (home.failed()) return;
198  IntView x1v(x1);
200  Arithmetic::DivMod<IntView>::post(home,x0,x1,x3));
201  }
202 
203  void
204  div(Home home, IntVar x0, IntVar x1, IntVar x2,
205  IntConLevel) {
206  using namespace Int;
207  if (home.failed()) return;
209  (Arithmetic::DivBnd<IntView>::post(home,x0,x1,x2)));
210  }
211 
212  void
213  mod(Home home, IntVar x0, IntVar x1, IntVar x2,
214  IntConLevel icl) {
215  using namespace Int;
216  if (home.failed()) return;
218  divmod(home, x0, x1, _div, x2, icl);
219  }
220 
221  void
222  sqr(Home home, IntVar x0, IntVar x1, IntConLevel icl) {
223  using namespace Int;
224  if (home.failed()) return;
225  Arithmetic::SqrOps ops;
226  if (icl == ICL_DOM) {
228  ::post(home,x0,x1,ops));
229  } else {
231  ::post(home,x0,x1,ops));
232  }
233  }
234 
235  void
236  sqrt(Home home, IntVar x0, IntVar x1, IntConLevel icl) {
237  using namespace Int;
238  if (home.failed()) return;
239  Arithmetic::SqrOps ops;
240  if (icl == ICL_DOM) {
242  ::post(home,x0,x1,ops));
243  } else {
245  ::post(home,x0,x1,ops));
246  }
247  }
248 
249  void
250  pow(Home home, IntVar x0, int n, IntVar x1, IntConLevel icl) {
251  using namespace Int;
252  Limits::nonnegative(n,"Int::pow");
253  if (home.failed()) return;
254  if (n == 2) {
255  sqr(home, x0, x1, icl);
256  return;
257  }
258  Arithmetic::PowOps ops(n);
259  if (icl == ICL_DOM) {
261  ::post(home,x0,x1,ops));
262  } else {
264  ::post(home,x0,x1,ops));
265  }
266  }
267 
268  void
269  nroot(Home home, IntVar x0, int n, IntVar x1, IntConLevel icl) {
270  using namespace Int;
271  Limits::positive(n,"Int::nroot");
272  if (home.failed()) return;
273  if (n == 2) {
274  sqrt(home, x0, x1, icl);
275  return;
276  }
277  Arithmetic::PowOps ops(n);
278  if (icl == ICL_DOM) {
280  ::post(home,x0,x1,ops));
281  } else {
283  ::post(home,x0,x1,ops));
284  }
285  }
286 
287 }
288 
289 // STATISTICS: int-post
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3446
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Integer division/modulo propagator.
Definition: arithmetic.hh:836
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
void mult(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:96
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntConLevel icl)
Post propagator for .
Definition: arithmetic.cpp:213
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
Domain consistent ternary maximum propagator.
Definition: arithmetic.hh:188
Domain consistent n-th root propagator.
Definition: arithmetic.hh:582
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
void post(Home home, Term< BoolView > *t, int n, IntRelType irt, IntView x, int c, IntConLevel)
Post propagator for linear constraint over Booleans.
Definition: bool-post.cpp:608
int a
Coefficient.
Definition: linear.hh:1313
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:57
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:49
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:126
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n 0$.
Definition: arithmetic.cpp:117
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
Bounds consistent power propagator.
Definition: arithmetic.hh:397
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:113
const int min
Smallest allowed integer value.
Definition: int.hh:115
void argmin(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntConLevel)
Post propagator for .
Definition: arithmetic.cpp:140
Bounds consistent absolute value propagator.
Definition: arithmetic.hh:63
void argmax(Home home, const IntVarArgs &x, IntVar y, bool tiebreak, IntConLevel)
Post propagator for .
Definition: arithmetic.cpp:114
ModEvent le(Space &home, int n)
Restrict domain values to be less than n.
Definition: int.hpp:124
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:904
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:103
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:110
Operations for square and square-root propagators.
Definition: arithmetic.hh:304
Bounds consistent n-ary maximum propagator.
Definition: arithmetic.hh:163
Argument maximum propagator.
Definition: arithmetic.hh:264
Passing integer variables.
Definition: int.hh:636
Bounds consistent division propagator.
Definition: arithmetic.hh:806
void divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3, IntConLevel)
Post propagator for .
Definition: arithmetic.cpp:180
Domain consistent power propagator.
Definition: arithmetic.hh:456
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
TieBreak< VarBranch > tiebreak(VarBranch a, VarBranch b)
Combine variable selection criteria a and b for tie-breaking.
Integer view for integer variables.
Definition: view.hpp:129
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:135
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
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:329
Bounds consistent ternary maximum propagator.
Definition: arithmetic.hh:135
Minus integer view.
Definition: view.hpp:276
Integer variables.
Definition: int.hh:350
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
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:133
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition: macros.hpp:70
An array of IdxView pairs.
Definition: idx-view.hh:71
Class for describing linear term .
Definition: linear.hh:1310
Gecode toplevel namespace
Home class for posting propagators
Definition: core.hpp:717
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:96
Bounds consistent n-th root propagator.
Definition: arithmetic.hh:522
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
Domain propagation or consistency.
Definition: int.hh:940
bool same(const Space &home) const
Test whether array contains same variable multiply.
Definition: array.hpp:2085