Generated on Sat Feb 7 2015 02:01:25 for Gecode by doxygen 1.8.9.1
single.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christopher Mears <Chris.Mears@monash.edu>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  * Guido Tack <tack@gecode.org>
9  *
10  * Copyright:
11  * Christopher Mears, 2011
12  * Christian Schulte, 2011
13  * Guido Tack, 2011
14  *
15  * Last modified:
16  * $Date: 2011-07-12 12:49:06 +0200 (Tue, 12 Jul 2011) $ by $Author: tack $
17  * $Revision: 12172 $
18  *
19  * This file is part of Gecode, the generic constraint
20  * development environment:
21  * http://www.gecode.org
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining
24  * a copy of this software and associated documentation files (the
25  * "Software"), to deal in the Software without restriction, including
26  * without limitation the rights to use, copy, modify, merge, publish,
27  * distribute, sublicense, and/or sell copies of the Software, and to
28  * permit persons to whom the Software is furnished to do so, subject to
29  * the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be
32  * included in all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
38  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
39  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41  *
42  */
43 
44 namespace Gecode { namespace Set { namespace Precede {
45 
46  template<class View>
49  Council<Index>& c, int i0)
50  : Advisor(home,p,c), i(i0) {}
51 
52  template<class View>
54  Single<View>::Index::Index(Space& home, bool share, Index& a)
55  : Advisor(home,share,a), i(a.i) {}
56 
57 
58  template<class View>
61  int n = x.size();
62  while (alpha < n) {
63  if (x[alpha].notContains(s)) {
64  GECODE_ME_CHECK(x[alpha].exclude(home, t));
65  } else if (x[alpha].contains(t)) {
66  GECODE_ME_CHECK(x[alpha].include(home, s));
67  } else {
68  break;
69  }
70  alpha++;
71  }
72  return ES_OK;
73  }
74 
75  template<class View>
78  int n = x.size();
79  do {
80  beta++;
81  } while ((beta < n) &&
82  (x[beta].notContains(s) || x[beta].contains(t)));
83  if (beta > gamma) {
84  GECODE_ME_CHECK(x[alpha].exclude(home, t));
85  GECODE_ME_CHECK(x[alpha].include(home, s));
86  }
87  return ES_OK;
88  }
89 
90  template<class View>
93  int s0, int t0, int b, int g)
94  : NaryPropagator<View, PC_SET_NONE>(home,x0),
95  c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) {
96  for (int i=x.size(); i--; )
97  if (!x[i].assigned())
98  x[i].subscribe(home,*new (home) Index(home,*this,c,i));
99  View::schedule(home, *this, ME_SET_BB);
100  }
101 
102  template<class View>
103  inline ExecStatus
104  Single<View>::post(Home home, ViewArray<View>& x, int s, int t) {
105  {
106  int alpha = 0;
107  while (alpha < x.size()) {
108  if (x[alpha].notContains(s)) {
109  GECODE_ME_CHECK(x[alpha].exclude(home, t));
110  } else if (x[alpha].contains(t)) {
111  GECODE_ME_CHECK(x[alpha].include(home, s));
112  } else {
113  break;
114  }
115  alpha++;
116  }
117  x.drop_fst(alpha);
118  if (x.size() == 0)
119  return ES_OK;
120  }
121  // alpha has been normalized to 0
122  int beta = 0, gamma = 0;
123  do {
124  gamma++;
125  } while ((gamma < x.size()) &&
126  (!x[gamma].notContains(s) || !x[gamma].contains(t)));
127  do {
128  beta++;
129  } while ((beta < x.size()) &&
130  (x[beta].notContains(s) || x[beta].contains(t)));
131  if (beta > gamma) {
132  GECODE_ME_CHECK(x[0].exclude(home, t));
133  GECODE_ME_CHECK(x[0].include(home, s));
134  return ES_OK;
135  }
136  if (gamma < x.size())
137  x.drop_lst(gamma);
138  (void) new (home) Single<View>(home, x, s, t, beta, gamma);
139  return ES_OK;
140  }
141 
142 
143 
144  template<class View>
146  Single<View>::Single(Space& home, bool share, Single& p)
147  : NaryPropagator<View,PC_SET_NONE>(home, share, p),
148  s(p.s), t(p.t),
149  alpha(p.alpha), beta(p.beta), gamma(p.gamma) {
150  c.update(home, share, p.c);
151  }
152  template<class View>
153  Propagator*
154  Single<View>::copy(Space& home, bool share) {
155  // Try to eliminate assigned views at the beginning
156  if (alpha > 0) {
157  int i = 0;
158  while ((i < alpha) && x[i].assigned())
159  i++;
160  x.drop_fst(i);
161  for (Advisors<Index> as(c); as(); ++as)
162  as.advisor().i -= i;
163  alpha -= i; beta -= i; gamma -= i;
164  }
165  // Try to eliminate assigned views at the end
166  if (gamma < x.size()) {
167  int i = x.size()-1;
168  while ((i > gamma) && x[i].assigned())
169  i--;
170  x.drop_lst(i);
171  }
172  return new (home) Single<View>(home, share, *this);
173  }
174 
175 
176  template<class View>
177  inline size_t
179  // Cancel remaining advisors
180  for (Advisors<Index> as(c); as(); ++as)
181  x[as.advisor().i].cancel(home,as.advisor());
182  c.dispose(home);
184  return sizeof(*this);
185  }
186 
187  template<class View>
188  PropCost
189  Single<View>::cost(const Space&, const ModEventDelta&) const {
190  return PropCost::linear(PropCost::LO, x.size());
191  }
192 
193  template<class View>
194  ExecStatus
195  Single<View>::advise(Space& home, Advisor& a0, const Delta&) {
196  Index& a(static_cast<Index&>(a0));
197  int i = a.i;
198  // Check for gamma
199  if ((beta <= gamma) && (i < gamma) &&
200  x[i].notContains(s) && x[i].contains(t))
201  gamma = i;
202  if (x[i].assigned()) {
203  a.dispose(home,c);
204  if (c.empty())
205  return ES_NOFIX;
206  } else if ((i < alpha) || (i > gamma)) {
207  x[i].cancel(home,a);
208  a.dispose(home,c);
209  return (c.empty()) ? ES_NOFIX : ES_FIX;
210  }
211  if (beta > gamma)
212  return ES_NOFIX;
213  if ((alpha == i) || (beta == i))
214  return ES_NOFIX;
215  return ES_FIX;
216  }
217 
218  template<class View>
219  ExecStatus
221  int n = x.size();
222  if (beta > gamma) {
223  GECODE_ME_CHECK(x[alpha].exclude(home, t));
224  GECODE_ME_CHECK(x[alpha].include(home, s));
225  return home.ES_SUBSUMED(*this);
226  }
227  if (alpha < n && (x[alpha].notContains(s) || x[alpha].contains(t))) {
228  if (x[alpha].notContains(s)) {
229  GECODE_ME_CHECK(x[alpha].exclude(home, t));
230  } else {
231  GECODE_ME_CHECK(x[alpha].include(home, s));
232  }
233  alpha++;
234  while (alpha < beta) {
235  if (x[alpha].notContains(s)) {
236  GECODE_ME_CHECK(x[alpha].exclude(home, t));
237  } else {
238  GECODE_ME_CHECK(x[alpha].include(home, s));
239  }
240  alpha++;
241  }
242  GECODE_ES_CHECK(updateAlpha(home));
243  beta = alpha;
244  if (alpha < n)
245  GECODE_ES_CHECK(updateBeta(home));
246  } else if ((beta < n) && (x[beta].notContains(s) || x[beta].contains(t))) {
247  GECODE_ES_CHECK(updateBeta(home));
248  }
249 
250  return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX;
251  }
252 
253 }}}
254 
255 // STATISTICS: set-prop
Council of advisors
Definition: core.hpp:226
Council< Index > c
The advisor council.
Definition: precede.hh:81
NodeType t
Type of node.
Definition: bool-expr.cpp:234
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
Single(Home home, ViewArray< View > &x, int s, int t, int beta, int gamma)
Constructor for posting.
Definition: single.hpp:92
virtual Propagator * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: single.hpp:154
const Gecode::ModEvent ME_SET_BB
Domain operation has changed both greatest lower and least upper bound.
Definition: var-type.hpp:172
ViewArray< View > x
Array of views.
Definition: propagator.hpp:146
Base-class for propagators.
Definition: core.hpp:755
Base-class for advisors.
Definition: core.hpp:926
const Gecode::PropCond PC_SET_NONE
Propagation condition to be ignored (convenience)
Definition: var-type.hpp:199
int i
The position of the view in the view array.
Definition: precede.hh:74
ExecStatus updateAlpha(Space &home)
Update the alpha pointer.
Definition: single.hpp:60
Class to iterate over advisors of a council.
Definition: core.hpp:227
Propagation has computed fixpoint.
Definition: core.hpp:528
static ExecStatus post(Home home, ViewArray< View > &x, int s, int t)
Post propagator that s precedes t in x.
Definition: single.hpp:104
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: single.hpp:195
Computation spaces.
Definition: core.hpp:1362
ModEvent exclude(Space &home, View &x, int s)
Prune view x to exclude all values from s.
Definition: set-op.hpp:141
#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)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
void drop_lst(int i)
Drop views from positions i+1 to size()-1 from array.
Definition: array.hpp:1308
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
ExecStatus updateBeta(Space &home)
Update the beta pointer.
Definition: single.hpp:77
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:74
int alpha
Pointers updated during propagation.
Definition: precede.hh:85
Advisors for views (by position in array)
Definition: precede.hh:71
n-ary propagator
Definition: propagator.hpp:143
View arrays.
Definition: array.hpp:234
void drop_fst(int i)
Drop views from positions 0 to i-1 from array.
Definition: array.hpp:1301
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Single value precedence propagator.
Definition: precede.hh:67
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: single.hpp:178
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
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3267
Propagation cost.
Definition: core.hpp:537
ModEvent include(Space &home, View &x, int s)
Prune view x to only include values from s.
Definition: set-op.hpp:127
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: single.hpp:220
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
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition: single.hpp:189
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
Definition: single.hpp:48
Gecode toplevel namespace
int s
The value s must precede t.
Definition: precede.hh:83
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.