Generated on Sat Feb 7 2015 02:01:17 for Gecode by doxygen 1.8.9.1
eq.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: 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  template<class BVA, class BVB>
42  Eq<BVA,BVB>::Eq(Home home, BVA b0, BVB b1)
43  : BoolBinary<BVA,BVB>(home,b0,b1) {}
44 
45  template<class BVA, class BVB>
47  Eq<BVA,BVB>::Eq(Space& home, bool share, Eq<BVA,BVB>& p)
48  : BoolBinary<BVA,BVB>(home,share,p) {}
49 
50  template<class BVA, class BVB>
52  Eq<BVA,BVB>::Eq(Space& home, bool share, Propagator& p,
53  BVA b0, BVB b1)
54  : BoolBinary<BVA,BVB>(home,share,p,b0,b1) {}
55 
56  template<class BVA, class BVB>
57  Actor*
58  Eq<BVA,BVB>::copy(Space& home, bool share) {
59  return new (home) Eq<BVA,BVB>(home,share,*this);
60  }
61 
62  template<class BVA, class BVB>
63  inline ExecStatus
64  Eq<BVA,BVB>::post(Home home, BVA b0, BVB b1) {
65  switch (bool_test(b0,b1)) {
66  case BT_SAME: return ES_OK;
67  case BT_COMP: return ES_FAILED;
68  case BT_NONE:
69  if (b0.zero()) {
70  GECODE_ME_CHECK(b1.zero(home));
71  } else if (b0.one()) {
72  GECODE_ME_CHECK(b1.one(home));
73  } else if (b1.zero()) {
74  GECODE_ME_CHECK(b0.zero(home));
75  } else if (b1.one()) {
76  GECODE_ME_CHECK(b0.one(home));
77  } else {
78  (void) new (home) Eq<BVA,BVB>(home,b0,b1);
79  }
80  break;
81  default: GECODE_NEVER;
82  }
83  return ES_OK;
84  }
85 
86  template<class BVA, class BVB>
89 #define GECODE_INT_STATUS(S0,S1) \
90  ((BVA::S0<<(1*BVA::BITS))|(BVB::S1<<(0*BVB::BITS)))
91  switch ((x0.status() << (1*BVA::BITS)) | (x1.status() << (0*BVB::BITS))) {
92  case GECODE_INT_STATUS(NONE,NONE):
94  case GECODE_INT_STATUS(NONE,ZERO):
95  GECODE_ME_CHECK(x0.zero_none(home)); break;
96  case GECODE_INT_STATUS(NONE,ONE):
97  GECODE_ME_CHECK(x0.one_none(home)); break;
98  case GECODE_INT_STATUS(ZERO,NONE):
99  GECODE_ME_CHECK(x1.zero_none(home)); break;
100  case GECODE_INT_STATUS(ZERO,ZERO):
101  break;
102  case GECODE_INT_STATUS(ZERO,ONE):
103  return ES_FAILED;
104  case GECODE_INT_STATUS(ONE,NONE):
105  GECODE_ME_CHECK(x1.one_none(home)); break;
106  case GECODE_INT_STATUS(ONE,ZERO):
107  return ES_FAILED;
108  case GECODE_INT_STATUS(ONE,ONE):
109  break;
110  default:
111  GECODE_NEVER;
112  }
113  return home.ES_SUBSUMED(*this);
114 #undef GECODE_INT_STATUS
115  }
116 
117  template<class BV>
120  : NaryPropagator<BV,PC_BOOL_VAL>(home,x) {}
121 
122  template<class BV>
124  NaryEq<BV>::NaryEq(Space& home, bool share, NaryEq<BV>& p)
125  : NaryPropagator<BV,PC_BOOL_VAL>(home,share,p) {}
126 
127  template<class BV>
128  Actor*
129  NaryEq<BV>::copy(Space& home, bool share) {
130  return new (home) NaryEq<BV>(home,share,*this);
131  }
132 
133  template<class BV>
134  inline ExecStatus
136  x.unique(home);
137  int n = x.size();
138  if (n < 2)
139  return ES_OK;
140  if (n == 2)
141  return Eq<BV,BV>::post(home,x[0],x[1]);
142  for (int i=n; i--; )
143  if (x[i].assigned()) {
144  if (x[i].one()) {
145  for (int j=i; j--; )
146  GECODE_ME_CHECK(x[j].one(home));
147  for (int j=i+1; j<n; j++)
148  GECODE_ME_CHECK(x[j].one_none(home));
149  } else {
150  for (int j=i; j--; )
151  GECODE_ME_CHECK(x[j].zero(home));
152  for (int j=i+1; j<n; j++)
153  GECODE_ME_CHECK(x[j].zero_none(home));
154  }
155  return ES_OK;
156  }
157  (void) new (home) NaryEq<BV>(home,x);
158  return ES_OK;
159  }
160 
161  template<class BV>
162  PropCost
163  NaryEq<BV>::cost(const Space&, const ModEventDelta&) const {
165  }
166 
167  template<class BV>
168  ExecStatus
170  int n=x.size();
171  int i=0;
172  while (true) {
173  if (x[i].assigned()) {
174  if (x[i].one()) {
175  for (int j=0; j<i; j++)
176  GECODE_ME_CHECK(x[j].one_none(home));
177  for (int j=i+1; j<n; j++)
178  GECODE_ME_CHECK(x[j].one(home));
179  } else {
180  for (int j=0; j<i; j++)
181  GECODE_ME_CHECK(x[j].zero_none(home));
182  for (int j=i+1; j<n; j++)
183  GECODE_ME_CHECK(x[j].zero(home));
184  }
185  return home.ES_SUBSUMED(*this);
186  }
187  i++;
188  }
189  GECODE_NEVER;
190  return ES_FIX;
191  }
192 
193 }}}
194 
195 // STATISTICS: int-prop
196 
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: eq.hpp:169
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: eq.hpp:88
static ExecStatus post(Home home, ViewArray< BV > &x)
Post propagator .
Definition: eq.hpp:135
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: eq.hpp:58
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary)
Definition: eq.hpp:163
Base-class for propagators.
Definition: core.hpp:755
Propagation has computed fixpoint.
Definition: core.hpp:528
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4058
void unique(const Space &home)
Remove all duplicate views from array (changes element order)
Definition: array.hpp:1498
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
Same variable.
Definition: view.hpp:1666
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
n-ary Boolean equality propagator
Definition: bool.hh:133
static ExecStatus post(Home home, BVA x0, BVB x1)
Post propagator .
Definition: eq.hpp:64
Execution has resulted in failure.
Definition: core.hpp:525
n-ary propagator
Definition: propagator.hpp:143
Boolean equality propagator.
Definition: bool.hh:105
BoolTest bool_test(const BoolView &b0, const BoolView &b1)
Definition: bool-test.hpp:45
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Same variable but complement.
Definition: view.hpp:1667
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Propagation cost.
Definition: core.hpp:537
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: eq.hpp:129
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
Eq(Home home, BVA b0, BVB b1)
Constructor for posting.
Definition: eq.hpp:42
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
#define GECODE_INT_STATUS(S0, S1)
Gecode toplevel namespace
Base-class for binary Boolean propagators.
Definition: bool.hh:59
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
NaryEq(Home home, ViewArray< BV > &x)
Constructor for posting.
Definition: eq.hpp:119
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value)
Definition: var-type.hpp:126