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