Generated on Sat Feb 7 2015 02:01:20 for Gecode by doxygen 1.8.9.1
eqv.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, 2004
8  *
9  * Last modified:
10  * $Date: 2011-10-07 11:28:27 +0200 (Fri, 07 Oct 2011) $ by $Author: schulte $
11  * $Revision: 12427 $
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  template<class BVA, class BVB, class BVC>
42  Eqv<BVA,BVB,BVC>::Eqv(Home home, BVA b0, BVB b1, BVC b2)
43  : BoolTernary<BVA,BVB,BVC>(home,b0,b1,b2) {}
44 
45  template<class BVA, class BVB, class BVC>
48  : BoolTernary<BVA,BVB,BVC>(home,share,p) {}
49 
50  template<class BVA, class BVB, class BVC>
51  inline ExecStatus
52  Eqv<BVA,BVB,BVC>::post(Home home, BVA b0, BVB b1, BVC b2){
53  switch (bool_test(b0,b1)) {
54  case BT_SAME:
55  GECODE_ME_CHECK(b2.one(home)); break;
56  case BT_COMP:
57  GECODE_ME_CHECK(b2.zero(home)); break;
58  case BT_NONE:
59  if (b2.one())
60  return Eq<BVA,BVB>::post(home,b0,b1);
61  if (b0.one()) {
62  if (b1.one()) {
63  GECODE_ME_CHECK(b2.one(home)); return ES_OK;
64  } else if (b1.zero()) {
65  GECODE_ME_CHECK(b2.zero(home)); return ES_OK;
66  }
67  }
68  if (b0.zero()) {
69  if (b1.one()) {
70  GECODE_ME_CHECK(b2.zero(home)); return ES_OK;
71  } else if (b1.zero()) {
72  GECODE_ME_CHECK(b2.one(home)); return ES_OK;
73  }
74  }
75  (void) new (home) Eqv(home,b0,b1,b2);
76  break;
77  default:
79  }
80  return ES_OK;
81  }
82 
83  template<class BVA, class BVB, class BVC>
84  Actor*
85  Eqv<BVA,BVB,BVC>::copy(Space& home, bool share) {
86  return new (home) Eqv<BVA,BVB,BVC>(home,share,*this);
87  }
88 
89  template<class BVA, class BVB, class BVC>
92 #define GECODE_INT_STATUS(S0,S1,S2) \
93  ((BVA::S0<<(2*BVA::BITS))|(BVB::S1<<(1*BVB::BITS))|(BVC::S2<<(0*BVC::BITS)))
94  switch ((x0.status() << (2*BVA::BITS)) | (x1.status() << (1*BVB::BITS)) |
95  (x2.status() << (0*BVC::BITS))) {
96  case GECODE_INT_STATUS(NONE,NONE,NONE):
98  case GECODE_INT_STATUS(NONE,NONE,ZERO):
99  case GECODE_INT_STATUS(NONE,NONE,ONE):
100  case GECODE_INT_STATUS(NONE,ZERO,NONE):
101  return ES_FIX;
102  case GECODE_INT_STATUS(NONE,ZERO,ZERO):
103  GECODE_ME_CHECK(x0.one_none(home)); break;
104  case GECODE_INT_STATUS(NONE,ZERO,ONE):
105  GECODE_ME_CHECK(x0.zero_none(home)); break;
106  case GECODE_INT_STATUS(NONE,ONE,NONE):
107  return ES_FIX;
108  case GECODE_INT_STATUS(NONE,ONE,ZERO):
109  GECODE_ME_CHECK(x0.zero_none(home)); break;
110  case GECODE_INT_STATUS(NONE,ONE,ONE):
111  GECODE_ME_CHECK(x0.one_none(home)); break;
112  case GECODE_INT_STATUS(ZERO,NONE,NONE):
113  return ES_FIX;
114  case GECODE_INT_STATUS(ZERO,NONE,ZERO):
115  GECODE_ME_CHECK(x1.one_none(home)); break;
116  case GECODE_INT_STATUS(ZERO,NONE,ONE):
117  GECODE_ME_CHECK(x1.zero_none(home)); break;
118  case GECODE_INT_STATUS(ZERO,ZERO,NONE):
119  GECODE_ME_CHECK(x2.one_none(home)); break;
120  case GECODE_INT_STATUS(ZERO,ZERO,ZERO):
121  return ES_FAILED;
122  case GECODE_INT_STATUS(ZERO,ZERO,ONE):
123  break;
124  case GECODE_INT_STATUS(ZERO,ONE,NONE):
125  GECODE_ME_CHECK(x2.zero_none(home)); break;
126  case GECODE_INT_STATUS(ZERO,ONE,ZERO):
127  break;
128  case GECODE_INT_STATUS(ZERO,ONE,ONE):
129  return ES_FAILED;
130  case GECODE_INT_STATUS(ONE,NONE,NONE):
131  return ES_FIX;
132  case GECODE_INT_STATUS(ONE,NONE,ZERO):
133  GECODE_ME_CHECK(x1.zero_none(home)); break;
134  case GECODE_INT_STATUS(ONE,NONE,ONE):
135  GECODE_ME_CHECK(x1.one_none(home)); break;
136  case GECODE_INT_STATUS(ONE,ZERO,NONE):
137  GECODE_ME_CHECK(x2.zero_none(home)); break;
138  case GECODE_INT_STATUS(ONE,ZERO,ZERO):
139  break;
140  case GECODE_INT_STATUS(ONE,ZERO,ONE):
141  return ES_FAILED;
142  case GECODE_INT_STATUS(ONE,ONE,NONE):
143  GECODE_ME_CHECK(x2.one_none(home)); break;
144  case GECODE_INT_STATUS(ONE,ONE,ZERO):
145  return ES_FAILED;
146  case GECODE_INT_STATUS(ONE,ONE,ONE):
147  break;
148  default:
149  GECODE_NEVER;
150  }
151  return home.ES_SUBSUMED(*this);
152 #undef GECODE_INT_STATUS
153  }
154 
155 
156  /*
157  * N-ary Boolean equivalence propagator
158  *
159  */
160 
163  : BinaryPropagator<BoolView,PC_BOOL_VAL>(home,x0[0],x0[1]),
164  x(x0), pm2(pm20) {
165  assert(x.size() >= 2);
166  x.drop_fst(2);
167  }
168 
170  NaryEqv::NaryEqv(Space& home, bool share, NaryEqv& p)
171  : BinaryPropagator<BoolView,PC_BOOL_VAL>(home,share,p), pm2(p.pm2) {
172  x.update(home,share,p.x);
173  }
174 
175  forceinline size_t
178  return sizeof(*this);
179  }
180 
181  forceinline void
183  if (x0.assigned()) {
184  pm2 ^= x0.val();
185  int n = x.size();
186  for (int i=n; i--; )
187  if (x[i].assigned()) {
188  pm2 ^= x[i].val();
189  x[i] = x[--n];
190  } else {
191  // Move to x0 and subscribe
192  x0=x[i]; x[i]=x[--n];
193  x0.subscribe(home,*this,PC_BOOL_VAL,false);
194  break;
195  }
196  x.size(n);
197  }
198  }
199 
200 }}}
201 
202 // STATISTICS: int-prop
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: eqv.hpp:176
NaryEqv(Home home, ViewArray< BoolView > &x, int pm2)
Constructor for posting.
Definition: eqv.hpp:162
Eqv(Space &home, bool share, Eqv &p)
Constructor for cloning p.
Definition: eqv.hpp:47
Propagation has computed fixpoint.
Definition: core.hpp:528
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
Same variable.
Definition: view.hpp:1666
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to view.
Definition: view.hpp:453
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Boolean equivalence propagator.
Definition: bool.hh:424
Execution has resulted in failure.
Definition: core.hpp:525
void resubscribe(Space &home, BoolView &x0)
Update subscription.
Definition: eqv.hpp:182
Binary propagator.
Definition: propagator.hpp:87
Boolean equality propagator.
Definition: bool.hh:105
BoolTest bool_test(const BoolView &b0, const BoolView &b1)
Definition: bool-test.hpp:45
void drop_fst(int i)
Drop views from positions 0 to i-1 from array.
Definition: array.hpp:1301
Boolean n-ary equivalence propagator.
Definition: bool.hh:451
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: eqv.hpp:91
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
static ExecStatus post(Home home, BVA b0, BVB b1, BVC b2)
Post propagator (equivalence)
Definition: eqv.hpp:52
Same variable but complement.
Definition: view.hpp:1667
#define GECODE_INT_STATUS(S0, S1, S2)
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
ExecStatus
Definition: core.hpp:523
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:132
int val(void) const
Return assigned value (only if assigned)
Definition: bool.hpp:74
bool assigned(void) const
Test whether view is assigned.
Definition: view.hpp:448
ViewArray< BoolView > x
Views not yet subscribed to.
Definition: bool.hh:456
Execution is okay.
Definition: core.hpp:527
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
No sharing.
Definition: view.hpp:1665
Gecode toplevel namespace
Base-class for ternary Boolean propagators.
Definition: bool.hh:79
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
int pm2
Parity information mod 2.
Definition: bool.hh:458
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: eqv.hpp:85
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value)
Definition: var-type.hpp:126
Boolean view for Boolean variables.
Definition: view.hpp:1315