Generated on Sat Feb 7 2015 02:01:23 for Gecode by doxygen 1.8.9.1
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: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
15  * $Revision: 13068 $
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
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 =
331  new (home) SupportEntry(ts()->data+(o->t-p.ts()->data));
332  // Link in support entry
333  (*n) = s; n = s->nextRef();
334  // move to next one
335  o = o->next();
336  }
337  *n = NULL;
338  }
339  }
340 
341  template<class View>
342  PropCost
343  Incremental<View>::cost(const Space&, const ModEventDelta& med) const {
344  if (View::me(med) == ME_INT_VAL)
345  return PropCost::quadratic(PropCost::HI,x.size());
346  else
347  return PropCost::cubic(PropCost::HI,x.size());
348  }
349 
350  template<class View>
351  Actor*
352  Incremental<View>::copy(Space& home, bool share) {
353  return new (home) Incremental<View>(home,share,*this);
354  }
355 
356  template<class View>
357  forceinline size_t
359  if (!home.failed()) {
360  int literals = static_cast<int>(ts()->domsize*x.size());
361  for (int i = literals; i--; )
362  if (support_data[i]) {
363  SupportEntry* lastse = support_data[i];
364  while (lastse->next() != NULL)
365  lastse = lastse->next();
366  support_data[i]->dispose(home, lastse);
367  }
368  home.rfree(support_data, sizeof(SupportEntry*)*literals);
369  }
370  ac.dispose(home);
371  (void) Base<View,false>::dispose(home);
372  return sizeof(*this);
373  }
374 
375  template<class View>
376  ExecStatus
378  assert(!w_support.empty() || !w_remove.empty() || unassigned==0);
379  // Set up datastructures
380  // Bit-sets for amortized O(1) access to domains
381  Region r(home);
382  // Add initial supports
383  BitSet* dom = r.alloc<BitSet>(x.size());
384  init_dom(home, dom);
385 
386  // Work loop
387  while (!w_support.empty() || !w_remove.empty()) {
388  while (!w_remove.empty()) {
389  int i, n;
390  w_remove.pop(home,i,n);
391  // Work is still relevant
392  if (dom[i].get(static_cast<unsigned int>(n-ts()->min))) {
393  GECODE_ME_CHECK(x[i].nq(home,n));
394  dom[i].clear(static_cast<unsigned int>(n-ts()->min));
395  }
396  }
397  while (!w_support.empty()) {
398  int i, n;
399  w_support.pop(home,i,n);
400  // Work is still relevant
401  if (dom[i].get(static_cast<unsigned int>(n-ts()->min)))
402  find_support(home, dom, i, n);
403  }
404  }
405  if (unassigned != 0)
406  return ES_FIX;
407 
408  return home.ES_SUBSUMED(*this);
409  }
410 
411 
412  template<class View>
413  ExecStatus
415  SupportAdvisor& a = static_cast<SupportAdvisor&>(_a);
416  ModEvent me = View::modevent(d);
417  bool scheduled = !w_support.empty() || !w_remove.empty();
418 
419  if (x[a.i].any(d)) {
420  ViewValues<View> vv(x[a.i]);
421  for (int n = ts()->min; n <= ts()->max; n++) {
422  if (vv() && (n == vv.val())) {
423  ++vv;
424  continue;
425  }
426  while (SupportEntry* s = support(a.i,n))
427  remove_support(home, s->t, a.i, n);
428  }
429  } else {
430  for (int n = x[a.i].min(d); n <= x[a.i].max(d); n++)
431  while (SupportEntry* s = support(a.i,n))
432  remove_support(home, s->t, a.i, n);
433  }
434 
435  if (me == ME_INT_VAL) {
436  --unassigned;
437  // nothing to do or already scheduled
438  // propagator is not subsumed since unassigned!=0
439  if (((w_support.empty() && w_remove.empty()) || scheduled) &&
440  (unassigned != 0))
441  return home.ES_FIX_DISPOSE(ac,a);
442  else
443  return home.ES_NOFIX_DISPOSE(ac,a);
444  } else if ((w_support.empty() && w_remove.empty()) || scheduled) {
445  // nothing to do or already scheduled
446  return ES_FIX;
447  }
448  return ES_NOFIX;
449  }
450 
451 
452 }}}
453 
454 // STATISTICS: int-prop
455 
NodeType t
Type of node.
Definition: bool-expr.cpp:234
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Work w_remove
Work for removing values.
Definition: extensional.hh:442
void init_dom(Space &home, Domain dom)
Initialize domain information.
Description of work to be done.
Definition: extensional.hh:390
static PropCost quadratic(PropCost::Mod m, unsigned int n)
Quadratic complexity for modifier m and size measure n.
Definition: core.hpp:4032
int min(void) const
Minimum domain element.
Definition: tuple-set.hpp:155
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
void update(Space &home, bool share, Council< A > &c)
Update during cloning (copies all advisors)
Definition: core.hpp:3324
Tuple find_support(Domain dom, int i, int n)
Find support for view at position i and value n.
Definition: base.hpp:138
void clear(unsigned int i)
Clear bit i.
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
void dispose(Space &home)
Free memory for this element.
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
WorkEntry(int i, int n, WorkEntry *ne)
Initialize with position i, value n, and next entry ne.
size_t dispose(Space &home)
Delete propagator and return its size.
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Basic bitset support.
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:44
int ModEvent
Type for modification events.
Definition: core.hpp:146
Expensive.
Definition: core.hpp:564
FreeList * next(void) const
Return next freelist object.
Base-class for advisors.
Definition: core.hpp:926
ExecStatus ES_NOFIX_DISPOSE(Council< A > &c, A &a)
Advisor a must be disposed and its propagator must be run
Definition: core.hpp:3285
Handle to region.
Definition: region.hpp:61
Value iterator for integer views.
Definition: view.hpp:94
void dispose(Space &home, SupportEntry *l)
Free memory for all elements between this and l (inclusive)
Propagation has computed fixpoint.
Definition: core.hpp:528
void add_support(Space &home, Tuple l)
Add support.
Computation spaces.
Definition: core.hpp:1362
void init_support(Space &home)
Initialize support.
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3442
Base-class for both propagators and branchers.
Definition: core.hpp:666
Gecode::IntSet d(v, 7)
SupportEntry ** support_data
Support information.
Definition: extensional.hh:445
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2397
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
TupleSet::Tuple Tuple
Definition: extensional.hh:227
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
void remove_support(Space &home, Tuple l, int i, int n)
Remove support for view at position i and value n.
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1400
SupportEntry(Tuple t)
Initialize with Tuple t.
Definition: incremental.hpp:76
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:56
FreeList ** nextRef(void)
Return pointer to next link in freelist object.
unsigned int domsize
Domain size.
Definition: int.hh:2052
void find_support(Space &home, Domain dom, int i, int n)
Find a next support for view at position i and value n.
size_t size
The size of the propagator (used during subsumption)
Definition: core.hpp:766
int min
Minimum and maximum in domain-values.
Definition: int.hh:2050
int i
Position of view in view array.
Definition: extensional.hh:393
bool empty(void) const
Check whether work stack is empty.
ExecStatus ES_FIX_DISPOSE(Council< A > &c, A &a)
Advisor a must be disposed
Definition: core.hpp:3278
View arrays.
Definition: array.hpp:234
int * data
Tuples data.
Definition: int.hh:2046
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
int unassigned
Number of unassigned views.
Definition: extensional.hh:447
int max(void) const
Maximum domain element.
Definition: tuple-set.hpp:162
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
Class represeting a set of tuples.
Definition: int.hh:2022
SupportEntry ** nextRef(void)
Return reference to field for next support entry.
Definition: incremental.hpp:92
void push(Space &home, int i, int n)
Push new work entry for position i and value n.
Work w_support
Work for finding support.
Definition: extensional.hh:440
Domain consistent extensional propagator.
Definition: extensional.hh:343
static PropCost cubic(PropCost::Mod m, unsigned int n)
Cubic complexity for modifier m and size measure n.
Definition: core.hpp:4023
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
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
static ExecStatus post(Home home, ViewArray< View > &x, const TupleSet &t)
Post propagator for views x.
Propagation cost.
Definition: core.hpp:537
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
ExecStatus
Definition: core.hpp:523
Incremental(Space &home, bool share, Incremental< View > &p)
Constructor for cloning p.
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:132
SupportEntry * support(int i, int n)
Creat support entry for view at position i and value n.
Base-class for freelist-managed objects.
void fl_dispose(FreeList *f, FreeList *l)
Return freelist-managed memory to freelist.
Definition: core.hpp:2387
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
int val(void) const
Return current value.
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Base for domain consistent extensional propagation
Definition: extensional.hh:242
Gecode toplevel namespace
void pop(Space &home, int &i, int &n)
Pop current top entry and set position i and value n.
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
SupportEntry * next(void) const
Return next support entry.
Definition: incremental.hpp:86
WorkEntry * next(void) const
Return next work entry.
void rfree(void *p, size_t s)
Free memory previously allocated with alloc (might be reused later)
Definition: core.hpp:2363