Generated on Fri Aug 24 2012 04:52:14 for Gecode by doxygen 1.8.1.1
sqrt.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, 2008
8  *
9  * Last modified:
10  * $Date: 2010-03-04 03:32:21 +1100 (Thu, 04 Mar 2010) $ by $Author: schulte $
11  * $Revision: 10364 $
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 <cmath>
39 
40 namespace Gecode { namespace Int { namespace Arithmetic {
41 
42  /*
43  * Bounds consistent square root
44  *
45  */
46 
47  template<class View>
49  prop_sqrt_bnd(Space& home, View x0, View x1) {
50  bool mod;
51  do {
52  mod = false;
53  {
54  ModEvent me =
55  x1.lq(home,floor(::sqrt(static_cast<double>(x0.max()))));
56  if (me_failed(me)) return ES_FAILED;
57  mod |= me_modified(me);
58  }
59  {
60  ModEvent me =
61  x1.gq(home,floor(::sqrt(static_cast<double>(x0.min()))));
62  if (me_failed(me)) return ES_FAILED;
63  mod |= me_modified(me);
64  }
65  {
66  double next = static_cast<double>(x1.max()+1);
67  ModEvent me = x0.le(home,next*next);
68  if (me_failed(me)) return ES_FAILED;
69  mod |= me_modified(me);
70  }
71  {
72  ModEvent me = x0.gq(home,x1.min()*x1.min());
73  if (me_failed(me)) return ES_FAILED;
74  mod |= me_modified(me);
75  }
76  } while (mod);
77  return ES_OK;
78  }
79 
80  template<class View>
82  SqrtBnd<View>::SqrtBnd(Home home, View x0, View x1)
83  : BinaryPropagator<View,PC_INT_BND>(home,x0,x1) {}
84 
85  template<class View>
87  SqrtBnd<View>::post(Home home, View x0, View x1) {
88  GECODE_ME_CHECK(x0.gq(home,0));
89  if (same(x0,x1)) {
90  GECODE_ME_CHECK(x1.lq(home,1));
91  } else {
92  GECODE_ME_CHECK(x1.gq(home,0));
93  GECODE_ES_CHECK(prop_sqrt_bnd(home,x0,x1));
94  (void) new (home) SqrtBnd<View>(home,x0,x1);
95  }
96  return ES_OK;
97  }
98 
99  template<class View>
102  : BinaryPropagator<View,PC_INT_BND>(home,share,p) {}
103 
104  template<class View>
105  Actor*
106  SqrtBnd<View>::copy(Space& home, bool share) {
107  return new (home) SqrtBnd<View>(home,share,*this);
108  }
109 
110  template<class View>
111  ExecStatus
113  GECODE_ES_CHECK(prop_sqrt_bnd(home,x0,x1));
114  return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
115  }
116 
117 
118  /*
119  * Domain consistent square root
120  *
121  */
123  class RangesMapSqr {
124  public:
126  forceinline int min(int n) const {
127  return n*n;
128  }
130  forceinline int max(int n) const {
131  return (n+1)*(n+1)-1;
132  }
133  };
134 
137  public:
139  forceinline int min(int n) const {
140  return static_cast<int>(floor(::sqrt(static_cast<double>(n))));
141  }
143  forceinline int max(int n) const {
144  return static_cast<int>(floor(::sqrt(static_cast<double>(n))));
145  }
146  };
147 
148  template<class View>
150  SqrtDom<View>::SqrtDom(Home home, View x0, View x1)
151  : BinaryPropagator<View,PC_INT_DOM>(home,x0,x1) {}
152 
153  template<class View>
155  SqrtDom<View>::post(Home home, View x0, View x1) {
156  GECODE_ME_CHECK(x0.gq(home,0));
157  if (same(x0,x1)) {
158  GECODE_ME_CHECK(x1.lq(home,1));
159  } else {
160  GECODE_ME_CHECK(x1.gq(home,0));
161  GECODE_ES_CHECK(prop_sqrt_bnd(home,x0,x1));
162  (void) new (home) SqrtDom<View>(home,x0,x1);
163  }
164  return ES_OK;
165  }
166 
167  template<class View>
170  : BinaryPropagator<View,PC_INT_DOM>(home,share,p) {}
171 
172  template<class View>
173  Actor*
174  SqrtDom<View>::copy(Space& home, bool share) {
175  return new (home) SqrtDom<View>(home,share,*this);
176  }
177 
178  template<class View>
179  PropCost
180  SqrtDom<View>::cost(const Space&, const ModEventDelta& med) const {
181  if (View::me(med) == ME_INT_VAL)
183  else if (View::me(med) == ME_INT_DOM)
185  else
187  }
188 
189  template<class View>
190  ExecStatus
192  if (View::me(med) != ME_INT_DOM) {
193  GECODE_ES_CHECK(prop_sqrt_bnd(home,x0,x1));
194  return x1.assigned() ? home.ES_SUBSUMED(*this)
195  : home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM));
196  }
197 
198  {
199  ViewRanges<View> r(x0);
201  GECODE_ME_CHECK(x1.inter_r(home,m,false));
202  }
203 
204  {
205  ViewRanges<View> r(x1);
207  GECODE_ME_CHECK(x0.inter_r(home,m,false));
208  }
209 
210  return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
211  }
212 
213 
214 }}}
215 
216 // STATISTICS: int-prop
217