Generated on Sat Aug 25 2012 03:32:50 for Gecode by doxygen 1.8.1.2
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-07-01 00:20:54 +1000 (Fri, 01 Jul 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