Generated on Sat Jun 2 2018 07:17:44 for Gecode by doxygen 1.8.13
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  * 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 Precede {
39 
41  template<class View>
42  forceinline bool
43  assigned(View x, int v) {
44  return x.assigned() && (x.val() == v);
45  }
46 
47  template<class View>
50  Council<Index>& c, int i0)
51  : Advisor(home,p,c), i(i0) {}
52 
53  template<class View>
56  : Advisor(home,a), i(a.i) {}
57 
58 
59  template<class View>
62  int n = x.size();
63  while ((alpha < n) && !x[alpha].in(s))
64  GECODE_ME_CHECK(x[alpha++].nq(home, t));
65  if (alpha < n)
66  GECODE_ME_CHECK(x[alpha].nq(home, t));
67  return ES_OK;
68  }
69 
70  template<class View>
73  int n = x.size();
74  do {
75  beta++;
76  } while ((beta < n) && !x[beta].in(s));
77  if (beta > gamma)
78  GECODE_ME_CHECK(x[alpha].eq(home, s));
79  return ES_OK;
80  }
81 
82  template<class View>
85  int s0, int t0, int b, int g)
86  : NaryPropagator<View, PC_INT_NONE>(home,x0),
87  c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) {
88  for (int i=x.size(); i--; )
89  if (!x[i].assigned())
90  x[i].subscribe(home,*new (home) Index(home,*this,c,i));
91  View::schedule(home, *this, ME_INT_BND);
92  }
93 
94  template<class View>
95  inline ExecStatus
97  {
98  int alpha = 0;
99  while ((alpha < x.size()) && !x[alpha].in(s))
100  GECODE_ME_CHECK(x[alpha++].nq(home,t));
101  x.drop_fst(alpha);
102  if (x.size() == 0)
103  return ES_OK;
104  }
105  // alpha has been normalized to 0
106  int beta = 0, gamma = 0;
107  GECODE_ME_CHECK(x[0].nq(home,t));
108  do {
109  gamma++;
110  } while ((gamma < x.size()) && !assigned(x[gamma],t));
111  do {
112  beta++;
113  } while ((beta < x.size()) && !x[beta].in(s));
114  if (beta > gamma) {
115  GECODE_ME_CHECK(x[0].eq(home, s));
116  return ES_OK;
117  }
118  if (gamma < x.size())
119  x.drop_lst(gamma);
120  (void) new (home) Single<View>(home, x, s, t, beta, gamma);
121  return ES_OK;
122  }
123 
124 
125 
126  template<class View>
129  : NaryPropagator<View,PC_INT_NONE>(home, p),
130  s(p.s), t(p.t),
131  alpha(p.alpha), beta(p.beta), gamma(p.gamma) {
132  c.update(home, p.c);
133  }
134  template<class View>
135  Propagator*
137  // Try to eliminate assigned views at the beginning
138  if (alpha > 0) {
139  int i = 0;
140  while ((i < alpha) && x[i].assigned())
141  i++;
142  x.drop_fst(i);
143  for (Advisors<Index> as(c); as(); ++as)
144  as.advisor().i -= i;
145  alpha -= i; beta -= i; gamma -= i;
146  }
147  // Try to eliminate assigned views at the end
148  if (gamma < x.size()) {
149  int i = x.size()-1;
150  while ((i > gamma) && x[i].assigned())
151  i--;
152  x.drop_lst(i);
153  }
154  return new (home) Single<View>(home, *this);
155  }
156 
157 
158  template<class View>
159  inline size_t
161  // Cancel remaining advisors
162  for (Advisors<Index> as(c); as(); ++as)
163  x[as.advisor().i].cancel(home,as.advisor());
164  c.dispose(home);
166  return sizeof(*this);
167  }
168 
169  template<class View>
170  PropCost
171  Single<View>::cost(const Space&, const ModEventDelta&) const {
172  return PropCost::linear(PropCost::LO, x.size());
173  }
174 
175  template<class View>
176  void
178  View::schedule(home, *this, ME_INT_BND);
179  }
180 
181  template<class View>
182  ExecStatus
183  Single<View>::advise(Space& home, Advisor& a0, const Delta& d) {
184  Index& a(static_cast<Index&>(a0));
185  int i = a.i;
186  // Check for gamma
187  if ((beta <= gamma) && (i < gamma) && assigned(x[i],t))
188  gamma = i;
189  if (x[i].assigned()) {
190  a.dispose(home,c);
191  if (c.empty())
192  return ES_NOFIX;
193  } else if ((i < alpha) || (i > gamma)) {
194  x[i].cancel(home,a);
195  a.dispose(home,c);
196  return (c.empty()) ? ES_NOFIX : ES_FIX;
197  }
198  if (beta > gamma)
199  return ES_NOFIX;
200  if ((alpha == i) || (beta == i)) {
201  if (x[i].any(d) && !x[i].in(s))
202  return ES_NOFIX;
203  if ((x[i].min(d) <= s) && (s <= x[i].max(d)))
204  return ES_NOFIX;
205  }
206  return ES_FIX;
207  }
208 
209  template<class View>
210  ExecStatus
212  int n = x.size();
213  if (beta > gamma) {
214  GECODE_ME_CHECK(x[alpha].eq(home, s));
215  return home.ES_SUBSUMED(*this);
216  }
217  if ((alpha < n) && !x[alpha].in(s)) {
218  alpha++;
219  while (alpha < beta)
220  GECODE_ME_CHECK(x[alpha++].nq(home, t));
222  beta = alpha;
223  if (alpha < n)
225  } else if ((beta < n) && !x[beta].in(s)) {
227  }
228 
229  return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX;
230  }
231 
232 }}}
233 
234 // STATISTICS: int-prop
Council of advisors
Definition: core.hpp:154
int s
The value s must precede t.
Definition: precede.hh:77
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: single.hpp:160
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4715
Single(Home home, ViewArray< View > &x, int s, int t, int beta, int gamma)
Constructor for posting.
Definition: single.hpp:84
Council< Index > c
The advisor council.
Definition: precede.hh:75
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: single.hpp:211
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3482
Advisors for views (by position in array)
Definition: precede.hh:65
bool any(const View &x)
Test whether x is neither positive nor negative.
Definition: mult.hpp:53
int i
The position of the view in the view array.
Definition: precede.hh:68
ViewArray< View > x
Array of views.
Definition: pattern.hpp:145
Base-class for propagators.
Definition: core.hpp:1023
Base-class for advisors.
Definition: core.hpp:1251
Class to iterate over advisors of a council.
Definition: core.hpp:155
#define forceinline
Definition: config.hpp:185
Propagation has computed fixpoint.
Definition: core.hpp:476
Computation spaces.
Definition: core.hpp:1701
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:91
virtual void reschedule(Space &home)
Schedule function.
Definition: single.hpp:177
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
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:1292
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
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:71
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition: single.hpp:171
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: pattern.hpp:142
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition: single.hpp:183
int alpha
Pointers updated during propagation.
Definition: precede.hh:79
View arrays.
Definition: array.hpp:224
void drop_fst(int i)
Drop views from positions 0 to i-1 from array.
Definition: array.hpp:1285
static ExecStatus post(Home home, ViewArray< View > &x, int s, int t)
Post propagator that s precedes t in x.
Definition: single.hpp:96
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:52
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
Definition: single.hpp:49
const int v[7]
Definition: distinct.cpp:259
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:67
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
Generic domain change information to be supplied to advisors.
Definition: core.hpp:203
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3783
Propagation cost.
Definition: core.hpp:485
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition: single.hpp:136
ExecStatus
Definition: core.hpp:471
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
ExecStatus updateAlpha(Space &home)
Update the alpha pointer.
Definition: single.hpp:61
Post propagator for SetVar x
Definition: set.hh:765
Execution is okay.
Definition: core.hpp:475
Propagation has not computed fixpoint.
Definition: core.hpp:474
Single value precedence propagator.
Definition: precede.hh:61
ExecStatus updateBeta(Space &home)
Update the beta pointer.
Definition: single.hpp:72
Gecode toplevel namespace
int ModEventDelta
Modification event deltas.
Definition: core.hpp:89
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1199
Home class for posting propagators
Definition: core.hpp:853