Generated on Wed Jan 24 2018 21:22:26 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  * 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: 2016-08-17 16:31:52 +0200 (Wed, 17 Aug 2016) $ by $Author: schulte $
17  * $Revision: 15152 $
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
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  void
196  View::schedule(home, *this, ME_SET_BB);
197  }
198 
199  template<class View>
200  ExecStatus
201  Single<View>::advise(Space& home, Advisor& a0, const Delta&) {
202  Index& a(static_cast<Index&>(a0));
203  int i = a.i;
204  // Check for gamma
205  if ((beta <= gamma) && (i < gamma) &&
206  x[i].notContains(s) && x[i].contains(t))
207  gamma = i;
208  if (x[i].assigned()) {
209  a.dispose(home,c);
210  if (c.empty())
211  return ES_NOFIX;
212  } else if ((i < alpha) || (i > gamma)) {
213  x[i].cancel(home,a);
214  a.dispose(home,c);
215  return (c.empty()) ? ES_NOFIX : ES_FIX;
216  }
217  if (beta > gamma)
218  return ES_NOFIX;
219  if ((alpha == i) || (beta == i))
220  return ES_NOFIX;
221  return ES_FIX;
222  }
223 
224  template<class View>
225  ExecStatus
227  int n = x.size();
228  if (beta > gamma) {
229  GECODE_ME_CHECK(x[alpha].exclude(home, t));
230  GECODE_ME_CHECK(x[alpha].include(home, s));
231  return home.ES_SUBSUMED(*this);
232  }
233  if (alpha < n && (x[alpha].notContains(s) || x[alpha].contains(t))) {
234  if (x[alpha].notContains(s)) {
235  GECODE_ME_CHECK(x[alpha].exclude(home, t));
236  } else {
237  GECODE_ME_CHECK(x[alpha].include(home, s));
238  }
239  alpha++;
240  while (alpha < beta) {
241  if (x[alpha].notContains(s)) {
242  GECODE_ME_CHECK(x[alpha].exclude(home, t));
243  } else {
244  GECODE_ME_CHECK(x[alpha].include(home, s));
245  }
246  alpha++;
247  }
249  beta = alpha;
250  if (alpha < n)
252  } else if ((beta < n) && (x[beta].notContains(s) || x[beta].contains(t))) {
254  }
255 
256  return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX;
257  }
258 
259 }}}
260 
261 // STATISTICS: set-prop
Council of advisors
Definition: core.hpp:232
Council< Index > c
The advisor council.
Definition: precede.hh:81
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4841
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
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:152
Base-class for propagators.
Definition: core.hpp:1092
Base-class for advisors.
Definition: core.hpp:1294
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:233
Propagation has computed fixpoint.
Definition: core.hpp:545
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:201
Computation spaces.
Definition: core.hpp:1748
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:95
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
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:75
int alpha
Pointers updated during propagation.
Definition: precede.hh:85
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Advisors for views (by position in array)
Definition: precede.hh:71
n-ary propagator
Definition: propagator.hpp:149
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:56
Single value precedence propagator.
Definition: precede.hh:67
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: single.hpp:178
virtual void reschedule(Space &home)
Schedule function.
Definition: single.hpp:195
Generic domain change information to be supplied to advisors.
Definition: core.hpp:281
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3915
Propagation cost.
Definition: core.hpp:554
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:226
ExecStatus
Definition: core.hpp:540
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
Execution is okay.
Definition: core.hpp:544
Propagation has not computed fixpoint.
Definition: core.hpp:543
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
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition: single.hpp:189
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Home class for posting propagators
Definition: core.hpp:922