Generated on Sat Feb 7 2015 02:01:20 for Gecode by doxygen 1.8.9.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: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
11  * $Revision: 13068 $
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 
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: clause.hpp:242
Council of advisors
Definition: core.hpp:226
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
Boolean clause propagator (disjunctive, true)
Definition: bool.hh:542
ViewArray< VY > y
Positive views (origin from negative variables)
Definition: bool.hh:495
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low binary)
Definition: clause.hpp:57
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:84
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary)
Definition: clause.hpp:291
ExecStatus resubscribe(Space &home, Propagator &p, VX &x0, ViewArray< VX > &x, VY &x1, ViewArray< VY > &y)
Definition: clause.hpp:142
Clause(Home home, ViewArray< VX > &x, ViewArray< VY > &y, VX z)
Constructor for posting.
Definition: clause.hpp:208
Base-class for propagators.
Definition: core.hpp:755
Base-class for advisors.
Definition: core.hpp:926
Boolean clause propagator (disjunctive)
Definition: bool.hh:490
static ExecStatus post(Home home, BVA b0, BVB b1, BVC b2)
Post propagator .
Definition: or.hpp:498
Class to iterate over advisors of a council.
Definition: core.hpp:227
ViewArray< VX > x
Positive views.
Definition: bool.hh:493
Propagation has computed fixpoint.
Definition: core.hpp:528
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4058
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
Gecode::IntSet d(v, 7)
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: clause.hpp:320
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1400
bool shared(const Space &home) const
Test whether array contains shared views.
Definition: array.hpp:1511
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: clause.hpp:311
static ExecStatus post(Home home, ViewArray< VX > &x, ViewArray< VY > &y, VX z)
Post propagator .
Definition: clause.hpp:250
ViewArray< VX > x
Views not yet subscribed to.
Definition: bool.hh:548
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: clause.hpp:331
unsigned int size(I &i)
Size of all ranges of range iterator i.
void cancel(Space &home)
Cancel subscriptions.
Definition: clause.hpp:297
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: clause.hpp:135
ViewArray< VY > y
Views not yet subscribed to (origin from negative variables)
Definition: bool.hh:550
Council< Tagged > c
The advisor council.
Definition: bool.hh:511
int n_zero
The number of views assigned to zero in x and y.
Definition: bool.hh:499
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
void move_lst(int i)
Move view from position size()-1 to position i (truncate array by one)
Definition: array.hpp:1295
Boolean n-ary disjunction propagator (true)
Definition: bool.hh:391
static ExecStatus post(Home home, BVA b0, BVB b1)
Post propagator .
Definition: or.hpp:129
static ExecStatus post(Home home, ViewArray< VX > &x, ViewArray< VY > &y)
Post propagator .
Definition: clause.hpp:110
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Generic domain change information to be supplied to advisors.
Definition: core.hpp:275
Mixed binary propagator.
Definition: propagator.hpp:203
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
Propagation cost.
Definition: core.hpp:537
Boolean n-ary disjunction propagator.
Definition: bool.hh:355
Binary Boolean disjunction propagator (true)
Definition: bool.hh:237
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
void eliminate_zero(ViewArray< VX > &x, int &n_zero)
Definition: clause.hpp:228
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: clause.hpp:71
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Tagged(Space &home, Propagator &p, Council< Tagged > &c, bool x)
Create tagged advisor.
Definition: clause.hpp:197
Gecode toplevel namespace
Advisors for views (tagged whether for x or y)
Definition: bool.hh:501
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: clause.hpp:177
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
static PropCost binary(PropCost::Mod m)
Two variables for modifier pcm.
Definition: core.hpp:4054
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
const bool x
Whether advises a view for x or y.
Definition: bool.hh:504
ClauseTrue(Home home, ViewArray< VX > &x, ViewArray< VY > &y)
Constructor for posting.
Definition: clause.hpp:47
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value)
Definition: var-type.hpp:126