Generated on Fri Aug 24 2012 04:52:10 for Gecode by doxygen 1.8.1.2
incremental.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Mikael Lagerkvist <lagerkvist@gecode.org>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  *
9  * Copyright:
10  * Mikael Lagerkvist, 2007
11  * Christian Schulte, 2008
12  *
13  * Last modified:
14  * $Date: 2010-07-15 01:46:18 +1000 (Thu, 15 Jul 2010) $ by $Author: schulte $
15  * $Revision: 11192 $
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 Extensional {
43 
44  /*
45  * Support advisor
46  *
47  */
48 
49  template<class View>
51  Incremental<View>::SupportAdvisor::
52  SupportAdvisor(Space& home, Propagator& p, Council<SupportAdvisor>& c,
53  int i0)
54  : Advisor(home,p,c), i(i0) {}
55 
56  template<class View>
58  Incremental<View>::SupportAdvisor::
59  SupportAdvisor(Space& home, bool share, SupportAdvisor& a)
60  : Advisor(home,share,a), i(a.i) {}
61 
62  template<class View>
63  forceinline void
64  Incremental<View>::SupportAdvisor::
65  dispose(Space& home, Council<SupportAdvisor>& c) {
66  Advisor::dispose(home,c);
67  }
68 
69 
70  /*
71  * Support entries
72  *
73  */
74  template<class View>
77  : t(t0) {}
78 
79  template<class View>
82  : FreeList(n), t(t0) {}
83 
84  template<class View>
87  return static_cast<SupportEntry*>(FreeList::next());
88  }
89 
90  template<class View>
93  return reinterpret_cast<SupportEntry**>(FreeList::nextRef());
94  }
95 
96  template<class View>
97  forceinline void
99 
100  template<class View>
101  forceinline void
103  GECODE_NEVER;
104  }
105 
106  template<class View>
107  forceinline void*
109  return home.fl_alloc<sizeof(SupportEntry)>();
110  }
111 
112  template<class View>
113  forceinline void
115  home.fl_dispose<sizeof(SupportEntry)>(this,this);
116  }
117 
118  template<class View>
119  forceinline void
121  home.fl_dispose<sizeof(SupportEntry)>(this,l);
122  }
123 
124 
125  /*
126  * Work entries
127  *
128  */
129  template<class View>
132  : FreeList(n), i(i0), n(n0) {}
133 
134  template<class View>
137  return static_cast<WorkEntry*>(FreeList::next());
138  }
139 
140  template<class View>
141  forceinline void
143  return FreeList::next(n);
144  }
145 
146  template<class View>
147  forceinline void
149 
150  template<class View>
151  forceinline void
153  GECODE_NEVER;
154  }
155 
156  template<class View>
157  forceinline void*
159  return home.fl_alloc<sizeof(WorkEntry)>();
160  }
161 
162  template<class View>
163  forceinline void
165  home.fl_dispose<sizeof(WorkEntry)>(this,this);
166  }
167 
168 
169  /*
170  * Work stack
171  *
172  */
173  template<class View>
176  : we(NULL) {}
177  template<class View>
178  forceinline bool
180  return we == NULL;
181  }
182  template<class View>
183  forceinline void
184  Incremental<View>::Work::push(Space& home, int i, int n) {
185  we = new (home) WorkEntry(i,n,we);
186  }
187  template<class View>
188  forceinline void
189  Incremental<View>::Work::pop(Space& home, int& i, int& n) {
190  WorkEntry* d = we;
191  we = we->next();
192  i=d->i; n=d->n;
193  d->dispose(home);
194  }
195 
196 
197  /*
198  * Support management
199  *
200  */
201  template<class View>
204  return support_data[(i*(ts()->domsize)) + (n - ts()->min)];
205  }
206 
207  template<class View>
208  forceinline void
210  assert(support_data == NULL);
211  int literals = static_cast<int>(ts()->domsize*x.size());
212  support_data = home.alloc<SupportEntry*>(literals);
213  for (int i = literals; i--; )
214  support_data[i] = NULL;
215  }
216 
217  template<class View>
218  forceinline void
220  for (int i = x.size(); i--; ) {
221  int pos = (i*static_cast<int>(ts()->domsize)) + (l[i] - ts()->min);
222  support_data[pos] = new (home) SupportEntry(l, support_data[pos]);
223  }
224  }
225 
226  template<class View>
227  forceinline void
229  if (support(i,n) == NULL) {
230  // Find support for value vv.val() in view i
231  Tuple l = Base<View,false>::find_support(dom,i,n - ts()->min);
232  if (l == NULL) {
233  // No possible supports left
234  w_remove.push(home,i,n);
235  } else {
236  // Mark values in support as supported
237  add_support(home,l);
238  }
239  }
240  }
241 
242  template<class View>
243  forceinline void
245  (void) n;
246  for (int j = x.size(); j--; ) {
247  int v = l[j];
248  int ov = v - ts()->min;
249  int pos = (j*(static_cast<int>(ts()->domsize))) + ov;
250 
251  assert(support_data[pos] != NULL);
252 
253  SupportEntry** a = &(support_data[pos]);
254  while ((*a)->t != l) {
255  assert((*a)->next() != NULL);
256  a = (*a)->nextRef();
257  }
258  SupportEntry* old = *a;
259  *a = (*a)->next();
260 
261  old->dispose(home);
262  if ((i != j) && (support_data[pos] == NULL))
263  w_support.push(home, j, v);
264  }
265  }
266 
267 
268 
269  /*
270  * The propagator proper
271  *
272  */
273 
274  template<class View>
277  const TupleSet& t)
278  : Base<View,false>(home,x,t), support_data(NULL),
279  unassigned(x.size()), ac(home) {
280  init_support(home);
281 
282  // Post advisors
283  for (int i = x.size(); i--; )
284  if (x[i].assigned()) {
285  --unassigned;
286  } else {
287  x[i].subscribe(home,*new (home) SupportAdvisor(home,*this,ac,i));
288  }
289 
290  Region r(home);
291 
292  // Add initial supports
293  BitSet* dom = r.alloc<BitSet>(x.size());
294  init_dom(home, dom);
295  for (int i = x.size(); i--; )
296  for (ViewValues<View> vv(x[i]); vv(); ++vv)
297  find_support(home, dom, i, vv.val());
298 
299  // Work to be done or subsumption
300  if (!w_support.empty() || !w_remove.empty() || (unassigned == 0))
301  View::schedule(home,*this,
302  (unassigned != x.size()) ? ME_INT_VAL : ME_INT_DOM);
303  }
304 
305  template<class View>
308  // All variables in the correct domain
309  for (int i = x.size(); i--; ) {
310  GECODE_ME_CHECK(x[i].gq(home, t.min()));
311  GECODE_ME_CHECK(x[i].lq(home, t.max()));
312  }
313  (void) new (home) Incremental<View>(home,x,t);
314  return ES_OK;
315  }
316 
317  template<class View>
320  : Base<View,false>(home,share,p), support_data(NULL),
321  unassigned(p.unassigned) {
322  ac.update(home,share,p.ac);
323 
324  init_support(home);
325  for (int i = static_cast<int>(ts()->domsize*x.size()); i--; ) {
326  SupportEntry** n = &(support_data[i]);
327  SupportEntry* o = p.support_data[i];
328  while (o != NULL) {
329  // Allocate new support entry
330  SupportEntry* s = new (home) SupportEntry(o->t);
331  // Link in support entry
332  (*n) = s; n = s->nextRef();
333  // move to next one
334  o = o->next();
335  }
336  *n = NULL;
337  }
338  }
339 
340  template<class View>
341  PropCost
342  Incremental<View>::cost(const Space&, const ModEventDelta& med) const {
343  if (View::me(med) == ME_INT_VAL)
344  return PropCost::quadratic(PropCost::HI,x.size());
345  else
346  return PropCost::cubic(PropCost::HI,x.size());
347  }
348 
349  template<class View>
350  Actor*
351  Incremental<View>::copy(Space& home, bool share) {
352  return new (home) Incremental<View>(home,share,*this);
353  }
354 
355  template<class View>
356  forceinline size_t
358  if (!home.failed()) {
359  int literals = static_cast<int>(ts()->domsize*x.size());
360  for (int i = literals; i--; )
361  if (support_data[i]) {
362  SupportEntry* lastse = support_data[i];
363  while (lastse->next() != NULL)
364  lastse = lastse->next();
365  support_data[i]->dispose(home, lastse);
366  }
367  home.rfree(support_data, sizeof(SupportEntry*)*literals);
368  }
369  ac.dispose(home);
370  (void) Base<View,false>::dispose(home);
371  return sizeof(*this);
372  }
373 
374  template<class View>
375  ExecStatus
377  assert(!w_support.empty() || !w_remove.empty() || unassigned==0);
378  // Set up datastructures
379  // Bit-sets for amortized O(1) access to domains
380  Region r(home);
381  // Add initial supports
382  BitSet* dom = r.alloc<BitSet>(x.size());
383  init_dom(home, dom);
384 
385  // Work loop
386  while (!w_support.empty() || !w_remove.empty()) {
387  while (!w_remove.empty()) {
388  int i, n;
389  w_remove.pop(home,i,n);
390  // Work is still relevant
391  if (dom[i].get(static_cast<unsigned int>(n-ts()->min))) {
392  GECODE_ME_CHECK(x[i].nq(home,n));
393  dom[i].clear(static_cast<unsigned int>(n-ts()->min));
394  }
395  }
396  while (!w_support.empty()) {
397  int i, n;
398  w_support.pop(home,i,n);
399  // Work is still relevant
400  if (dom[i].get(static_cast<unsigned int>(n-ts()->min)))
401  find_support(home, dom, i, n);
402  }
403  }
404  if (unassigned != 0)
405  return ES_FIX;
406 
407  return home.ES_SUBSUMED(*this);
408  }
409 
410 
411  template<class View>
412  ExecStatus
414  SupportAdvisor& a = static_cast<SupportAdvisor&>(_a);
415  ModEvent me = View::modevent(d);
416  bool scheduled = !w_support.empty() || !w_remove.empty();
417 
418  if (x[a.i].any(d)) {
419  ViewValues<View> vv(x[a.i]);
420  for (int n = ts()->min; n <= ts()->max; n++) {
421  if (vv() && (n == vv.val())) {
422  ++vv;
423  continue;
424  }
425  while (SupportEntry* s = support(a.i,n))
426  remove_support(home, s->t, a.i, n);
427  }
428  } else {
429  for (int n = x[a.i].min(d); n <= x[a.i].max(d); n++)
430  while (SupportEntry* s = support(a.i,n))
431  remove_support(home, s->t, a.i, n);
432  }
433 
434  if (me == ME_INT_VAL) {
435  --unassigned;
436  // nothing to do or already scheduled
437  // propagator is not subsumed since unassigned!=0
438  if (((w_support.empty() && w_remove.empty()) || scheduled) &&
439  (unassigned != 0))
440  return home.ES_FIX_DISPOSE(ac,a);
441  else
442  return home.ES_NOFIX_DISPOSE(ac,a);
443  } else if ((w_support.empty() && w_remove.empty()) || scheduled) {
444  // nothing to do or already scheduled
445  return ES_FIX;
446  }
447  return ES_NOFIX;
448  }
449 
450 
451 }}}
452 
453 // STATISTICS: int-prop
454