Generated on Sat Feb 7 2015 02:01:17 for Gecode by doxygen 1.8.9.1
nq.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 #include <algorithm>
39 
40 namespace Gecode { namespace Int { namespace Rel {
41 
42  /*
43  * Disequality
44  *
45  */
46  template<class View>
48  Nq<View>::Nq(Home home, View x0, View x1)
49  : BinaryPropagator<View,PC_INT_VAL>(home,x0,x1) {}
50 
51  template<class View>
53  Nq<View>::post(Home home, View x0, View x1){
54  if (x0.assigned()) {
55  GECODE_ME_CHECK(x1.nq(home,x0.val()));
56  } else if (x1.assigned()) {
57  GECODE_ME_CHECK(x0.nq(home,x1.val()));
58  } else if (same(x0,x1)) {
59  return ES_FAILED;
60  } else {
61  (void) new (home) Nq<View>(home,x0,x1);
62  }
63  return ES_OK;
64  }
65 
66  template<class View>
68  Nq<View>::Nq(Space& home, bool share, Nq<View>& p)
69  : BinaryPropagator<View,PC_INT_VAL>(home,share,p) {}
70 
71  template<class View>
72  Actor*
73  Nq<View>::copy(Space& home, bool share) {
74  return new (home) Nq<View>(home,share,*this);
75  }
76 
77  template<class View>
78  PropCost
79  Nq<View>::cost(const Space&, const ModEventDelta&) const {
81  }
82 
83  template<class View>
86  if (x0.assigned()) {
87  GECODE_ME_CHECK(x1.nq(home,x0.val()));
88  } else {
89  GECODE_ME_CHECK(x0.nq(home,x1.val()));
90  }
91  return home.ES_SUBSUMED(*this);
92  }
93 
94 
95  /*
96  * Nary disequality propagator
97  */
98  template<class View>
101  : NaryPropagator<View,PC_INT_VAL>(home,x) {}
102 
103  template<class View>
104  PropCost
105  NaryNq<View>::cost(const Space&, const ModEventDelta&) const {
106  return PropCost::linear(PropCost::LO,x.size());
107  }
108 
109  template<class View>
112  : NaryPropagator<View,PC_INT_VAL>(home,share,p) {}
113 
114  template<class View>
115  Actor*
116  NaryNq<View>::copy(Space& home, bool share) {
117  return new (home) NaryNq<View>(home,share,*this);
118  }
119 
120  template<class View>
121  inline ExecStatus
123  x.unique(home);
124  // Try to find an assigned view
125  int n = x.size();
126  if (n <= 1)
127  return ES_FAILED;
128  for (int i=n; i--; )
129  if (x[i].assigned()) {
130  std::swap(x[0],x[i]);
131  break;
132  }
133  if (x[0].assigned()) {
134  int v = x[0].val();
135  // Eliminate all equal views and possibly find disequal view
136  for (int i=n-1; i>0; i--)
137  if (!x[i].in(v)) {
138  return ES_OK;
139  } else if (x[i].assigned()) {
140  assert(x[i].val() == v);
141  x[i]=x[--n];
142  }
143  x.size(n);
144  }
145  if (n == 1)
146  return ES_FAILED;
147  if (n == 2)
148  return Nq<View>::post(home,x[0],x[1]);
149  (void) new (home) NaryNq(home,x);
150  return ES_OK;
151  }
152 
153  template<class View>
154  forceinline size_t
157  return sizeof(*this);
158  }
159 
160  template<class View>
161  ExecStatus
163  // Make sure that x[0] is assigned
164  if (!x[0].assigned()) {
165  // Note that there is at least one assigned view
166  for (int i=1; true; i++)
167  if (x[i].assigned()) {
168  std::swap(x[0],x[i]);
169  break;
170  }
171  }
172  int v = x[0].val();
173  int n = x.size();
174  for (int i=n-1; i>0; i--)
175  if (!x[i].in(v)) {
176  x.size(n);
177  return home.ES_SUBSUMED(*this);
178  } else if (x[i].assigned()) {
179  assert(x[i].val() == v);
180  x[i] = x[--n];
181  }
182  x.size(n);
183  if (n == 1)
184  return ES_FAILED;
185  if (n == 2) {
186  GECODE_ME_CHECK(x[1].nq(home,v));
187  return home.ES_SUBSUMED(*this);
188  }
189  return ES_FIX;
190  }
191 
192 
193 
194 }}}
195 
196 // STATISTICS: int-prop
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator .
Definition: nq.hpp:122
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
Nq(Space &home, bool share, Nq< View > &p)
Constructor for cloning p.
Definition: nq.hpp:68
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
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: nq.hpp:73
Base-class for both propagators and branchers.
Definition: core.hpp:666
Nary disequality propagator.
Definition: rel.hh:290
bool same(const CachedView< View > &x, const CachedView< View > &y)
Definition: cached.hpp:389
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: nq.hpp:116
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
Execution has resulted in failure.
Definition: core.hpp:525
NaryNq(Home home, ViewArray< View > &x)
Constructor for posting.
Definition: nq.hpp:100
Binary propagator.
Definition: propagator.hpp:87
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition: nq.hpp:105
n-ary propagator
Definition: propagator.hpp:143
View arrays.
Definition: array.hpp:234
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: nq.hpp:162
const int v[7]
Definition: distinct.cpp:207
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Propagation cost.
Definition: core.hpp:537
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
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: nq.hpp:155
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary)
Definition: nq.hpp:79
Binary disequality propagator.
Definition: rel.hh:432
Execution is okay.
Definition: core.hpp:527
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: nq.hpp:85
Gecode toplevel namespace
static ExecStatus post(Home home, View x0, View x1)
Post propagator .
Definition: nq.hpp:53
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
const Gecode::PropCond PC_INT_VAL
Propagate when a view becomes assigned (single value)
Definition: var-type.hpp:82