Generated on Thu Feb 14 2013 20:59:31 for Gecode by doxygen 1.8.3.1
clause.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 namespace Gecode { namespace Int { namespace Bool {
39 
40  /*
41  * Boolean clause propagator (disjunctive, true)
42  *
43  */
44 
45  template<class VX, class VY>
50  (home,x0[x0.size()-1],y0[y0.size()-1]), x(x0), y(y0) {
51  assert((x.size() > 0) && (y.size() > 0));
52  x.size(x.size()-1); y.size(y.size()-1);
53  }
54 
55  template<class VX, class VY>
56  PropCost
59  }
60 
61  template<class VX, class VY>
64  : MixBinaryPropagator<VX,PC_BOOL_VAL,VY,PC_BOOL_VAL>(home,share,p) {
65  x.update(home,share,p.x);
66  y.update(home,share,p.y);
67  }
68 
69  template<class VX, class VY>
70  Actor*
71  ClauseTrue<VX,VY>::copy(Space& home, bool share) {
72  {
73  int n = x.size();
74  if (n > 0) {
75  // Eliminate all zeros and find a one
76  for (int i=n; i--; )
77  if (x[i].one()) {
78  // Only keep the one
79  x[0]=x[i]; n=1; break;
80  } else if (x[i].zero()) {
81  // Eliminate the zero
82  x[i]=x[--n];
83  }
84  x.size(n);
85  }
86  }
87  {
88  int n = y.size();
89  if (n > 0) {
90  // Eliminate all zeros and find a one
91  for (int i=n; i--; )
92  if (y[i].one()) {
93  // Only keep the one
94  y[0]=y[i]; n=1; break;
95  } else if (y[i].zero()) {
96  // Eliminate the zero
97  y[i]=y[--n];
98  }
99  y.size(n);
100  }
101  }
102  if ((x.size() == 0) && (y.size() == 0))
103  return new (home) BinOrTrue<VX,VY>(home,share,*this,x0,x1);
104  else
105  return new (home) ClauseTrue<VX,VY>(home,share,*this);
106  }
107 
108  template<class VX, class VY>
109  inline ExecStatus
111  for (int i=x.size(); i--; )
112  if (x[i].one())
113  return ES_OK;
114  else if (x[i].zero())
115  x.move_lst(i);
116  if (x.size() == 0)
117  return NaryOrTrue<VY>::post(home,y);
118  for (int i=y.size(); i--; )
119  if (y[i].one())
120  return ES_OK;
121  else if (y[i].zero())
122  y.move_lst(i);
123  if (y.size() == 0)
124  return NaryOrTrue<VX>::post(home,x);
125  if ((x.size() == 1) && (y.size() == 1)) {
126  return BinOrTrue<VX,VY>::post(home,x[0],y[0]);
127  } else if (!x.shared(home,y)) {
128  (void) new (home) ClauseTrue(home,x,y);
129  }
130  return ES_OK;
131  }
132 
133  template<class VX, class VY>
134  forceinline size_t
137  return sizeof(*this);
138  }
139 
140  template<class VX, class VY>
143  VX& x0, ViewArray<VX>& x,
144  VY& x1, ViewArray<VY>& y) {
145  if (x0.zero()) {
146  int n = x.size();
147  for (int i=n; i--; )
148  if (x[i].one()) {
149  x.size(n);
150  return home.ES_SUBSUMED(p);
151  } else if (x[i].zero()) {
152  x[i] = x[--n];
153  } else {
154  // Rewrite if there is just one view left
155  if ((i == 0) && (y.size() == 0)) {
156  VX z = x[0]; x.size(0);
157  GECODE_REWRITE(p,(BinOrTrue<VX,VY>::post(home(p),z,x1)));
158  }
159  // Move to x0 and subscribe
160  x0=x[i]; x[i]=x[--n];
161  x.size(n);
162  x0.subscribe(home,p,PC_BOOL_VAL,false);
163  return ES_FIX;
164  }
165  // All x-views have been assigned!
166  ViewArray<VY> z(home,y.size()+1);
167  for (int i=y.size(); i--; )
168  z[i]=y[i];
169  z[y.size()] = x1;
170  GECODE_REWRITE(p,(NaryOrTrue<VY>::post(home(p),z)));
171  }
172  return ES_FIX;
173  }
174 
175  template<class VX, class VY>
176  ExecStatus
178  if (x0.one() || x1.one())
179  return home.ES_SUBSUMED(*this);
180  GECODE_ES_CHECK(resubscribe(home,*this,x0,x,x1,y));
181  GECODE_ES_CHECK(resubscribe(home,*this,x1,y,x0,x));
182  return ES_FIX;
183  }
184 
185 
186  /*
187  * Boolean clause propagator (disjunctive)
188  *
189  */
190 
191  /*
192  * Index advisors
193  *
194  */
195  template<class VX, class VY>
198  Council<Tagged>& c, bool x0)
199  : Advisor(home,p,c), x(x0) {}
200 
201  template<class VX, class VY>
204  : Advisor(home,share,a), x(a.x) {}
205 
206  template<class VX, class VY>
209  VX z0)
210  : Propagator(home), x(x0), y(y0), z(z0), n_zero(0), c(home) {
211  x.subscribe(home,*new (home) Tagged(home,*this,c,true));
212  y.subscribe(home,*new (home) Tagged(home,*this,c,false));
213  z.subscribe(home,*this,PC_BOOL_VAL);
214  }
215 
216  template<class VX, class VY>
219  : Propagator(home,share,p), n_zero(p.n_zero) {
220  x.update(home,share,p.x);
221  y.update(home,share,p.y);
222  z.update(home,share,p.z);
223  c.update(home,share,p.c);
224  }
225 
226  template<class VX>
227  forceinline void
228  eliminate_zero(ViewArray<VX>& x, int& n_zero) {
229  if (n_zero > 0) {
230  int n=x.size();
231  // Eliminate all zeros
232  for (int i=n; i--; )
233  if (x[i].zero()) {
234  x[i]=x[--n]; n_zero--;
235  }
236  x.size(n);
237  }
238  }
239 
240  template<class VX, class VY>
241  Actor*
242  Clause<VX,VY>::copy(Space& home, bool share) {
243  eliminate_zero(x,n_zero);
244  eliminate_zero(y,n_zero);
245  return new (home) Clause<VX,VY>(home,share,*this);
246  }
247 
248  template<class VX, class VY>
249  inline ExecStatus
251  assert(!x.shared(home) && !y.shared(home));
252  if (z.one())
253  return ClauseTrue<VX,VY>::post(home,x,y);
254  if (z.zero()) {
255  for (int i=x.size(); i--; )
256  GECODE_ME_CHECK(x[i].zero(home));
257  for (int i=y.size(); i--; )
258  GECODE_ME_CHECK(y[i].zero(home));
259  return ES_OK;
260  }
261  for (int i=x.size(); i--; )
262  if (x[i].one()) {
263  GECODE_ME_CHECK(z.one_none(home));
264  return ES_OK;
265  } else if (x[i].zero()) {
266  x.move_lst(i);
267  }
268  if (x.size() == 0)
269  return NaryOr<VY,VX>::post(home,y,z);
270  for (int i=y.size(); i--; )
271  if (y[i].one()) {
272  GECODE_ME_CHECK(z.one_none(home));
273  return ES_OK;
274  } else if (y[i].zero()) {
275  y.move_lst(i);
276  }
277  if (y.size() == 0)
278  return NaryOr<VX,VX>::post(home,x,z);
279  if ((x.size() == 1) && (y.size() == 1)) {
280  return Or<VX,VY,VX>::post(home,x[0],y[0],z);
281  } else if (x.shared(home,y)) {
282  GECODE_ME_CHECK(z.one_none(home));
283  } else {
284  (void) new (home) Clause<VX,VY>(home,x,y,z);
285  }
286  return ES_OK;
287  }
288 
289  template<class VX, class VY>
290  PropCost
291  Clause<VX,VY>::cost(const Space&, const ModEventDelta&) const {
293  }
294 
295  template<class VX, class VY>
296  forceinline void
298  for (Advisors<Tagged> as(c); as(); ++as) {
299  if (as.advisor().x)
300  x.cancel(home,as.advisor());
301  else
302  y.cancel(home,as.advisor());
303  as.advisor().dispose(home,c);
304  }
305  c.dispose(home);
306  z.cancel(home,*this,PC_BOOL_VAL);
307  }
308 
309  template<class VX, class VY>
310  forceinline size_t
312  cancel(home);
313  (void) Propagator::dispose(home);
314  return sizeof(*this);
315  }
316 
317 
318  template<class VX, class VY>
319  ExecStatus
321  Tagged& a = static_cast<Tagged&>(_a);
322  // Decides whether the propagator must be run
323  if ((a.x && VX::zero(d)) || (!a.x && VY::zero(d)))
324  if (++n_zero < x.size() + y.size())
325  return ES_FIX;
326  return ES_NOFIX;
327  }
328 
329  template<class VX, class VY>
330  ExecStatus
332  if (z.one())
333  GECODE_REWRITE(*this,(ClauseTrue<VX,VY>::post(home(*this),x,y)));
334  if (z.zero()) {
335  for (int i = x.size(); i--; )
336  GECODE_ME_CHECK(x[i].zero(home));
337  for (int i = y.size(); i--; )
338  GECODE_ME_CHECK(y[i].zero(home));
339  c.dispose(home);
340  } else if (n_zero == x.size() + y.size()) {
341  GECODE_ME_CHECK(z.zero_none(home));
342  c.dispose(home);
343  } else {
344  // There is exactly one view which is one
345  GECODE_ME_CHECK(z.one_none(home));
346  }
347  return home.ES_SUBSUMED(*this);
348  }
349 
350 }}}
351 
352 // STATISTICS: int-prop
353