Generated on Sat Feb 7 2015 02:01:23 for Gecode by doxygen 1.8.9.1
layered-graph.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Last modified:
10  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
11  * $Revision: 13068 $
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 #include <climits>
39 #include <algorithm>
40 
41 namespace Gecode { namespace Int { namespace Extensional {
42 
49  template<class Var>
50  class VarTraits {};
51 
57  template<>
58  class VarTraits<IntVar> {
59  public:
61  typedef Int::IntView View;
62  };
63 
69  template<>
70  class VarTraits<BoolVar> {
71  public:
74  };
75 
76 
77  /*
78  * States
79  */
80  template<class View, class Val, class Degree, class StateIdx>
81  forceinline void
83  i_deg=o_deg=0;
84  }
85 
86 
87  template<class View, class Val, class Degree, class StateIdx>
90  return layers[i].states[is];
91  }
92  template<class View, class Val, class Degree, class StateIdx>
95  (int i, const typename LayeredGraph<View,Val,Degree,StateIdx>::Edge& e) {
96  return i_state(i,e.i_state);
97  }
98  template<class View, class Val, class Degree, class StateIdx>
99  forceinline bool
102  return --i_state(i,e).o_deg == 0;
103  }
104  template<class View, class Val, class Degree, class StateIdx>
107  return layers[i+1].states[os];
108  }
109  template<class View, class Val, class Degree, class StateIdx>
112  (int i, const typename LayeredGraph<View,Val,Degree,StateIdx>::Edge& e) {
113  return o_state(i,e.o_state);
114  }
115  template<class View, class Val, class Degree, class StateIdx>
116  forceinline bool
118  (int i, const typename LayeredGraph<View,Val,Degree,StateIdx>::Edge& e) {
119  return --o_state(i,e).i_deg == 0;
120  }
121 
122 
123  /*
124  * Value iterator
125  */
126  template<class View, class Val, class Degree, class StateIdx>
129  template<class View, class Val, class Degree, class StateIdx>
133  : s1(l.support), s2(l.support+l.size) {}
134  template<class View, class Val, class Degree, class StateIdx>
135  forceinline void
137  s1=l.support; s2=l.support+l.size;
138  }
139  template<class View, class Val, class Degree, class StateIdx>
140  forceinline bool
142  ::operator ()(void) const {
143  return s1<s2;
144  }
145  template<class View, class Val, class Degree, class StateIdx>
146  forceinline void
148  s1++;
149  }
150  template<class View, class Val, class Degree, class StateIdx>
151  forceinline int
153  return s1->val;
154  }
155 
156 
157  /*
158  * Index advisors
159  *
160  */
161  template<class View, class Val, class Degree, class StateIdx>
164  Council<Index>& c,
165  int i0)
166  : Advisor(home,p,c), i(i0) {}
167 
168  template<class View, class Val, class Degree, class StateIdx>
171  Index& a)
172  : Advisor(home,share,a), i(a.i) {}
173 
174 
175  /*
176  * Index ranges
177  *
178  */
179  template<class View, class Val, class Degree, class StateIdx>
182  : _fst(INT_MAX), _lst(INT_MIN) {}
183  template<class View, class Val, class Degree, class StateIdx>
184  forceinline void
186  _fst=INT_MAX; _lst=INT_MIN;
187  }
188  template<class View, class Val, class Degree, class StateIdx>
189  forceinline void
191  _fst=std::min(_fst,i); _lst=std::max(_lst,i);
192  }
193  template<class View, class Val, class Degree, class StateIdx>
194  forceinline void
197  _fst=std::min(_fst,ir._fst); _lst=std::max(_lst,ir._lst);
198  }
199  template<class View, class Val, class Degree, class StateIdx>
200  forceinline bool
202  return _fst>_lst;
203  }
204  template<class View, class Val, class Degree, class StateIdx>
205  forceinline void
207  if (empty())
208  return;
209  if (n > _lst) {
210  reset();
211  } else {
212  _fst = std::max(0,_fst-n);
213  _lst -= n;
214  }
215  }
216  template<class View, class Val, class Degree, class StateIdx>
217  forceinline int
219  return _fst;
220  }
221  template<class View, class Val, class Degree, class StateIdx>
222  forceinline int
224  return _lst;
225  }
226 
227 
228 
229  /*
230  * The layered graph
231  *
232  */
233 
234  template<class View, class Val, class Degree, class StateIdx>
235  template<class Var>
238  const VarArgArray<Var>& x,
239  const DFA& dfa)
240  : Propagator(home), c(home), n(x.size()),
241  max_states(static_cast<StateIdx>(dfa.n_states())) {
242  assert(n > 0);
243  }
244 
245  template<class View, class Val, class Degree, class StateIdx>
246  forceinline void
248 #ifdef GECODE_AUDIT
249  // Check states and edge information to be consistent
250  unsigned int n_e = 0; // Number of edges
251  unsigned int n_s = 0; // Number of states
252  StateIdx m_s = 0; // Maximal number of states per layer
253  for (int i=n; i--; ) {
254  n_s += layers[i].n_states;
255  m_s = std::max(m_s,layers[i].n_states);
256  for (ValSize j=layers[i].size; j--; )
257  n_e += layers[i].support[j].n_edges;
258  }
259  n_s += layers[n].n_states;
260  m_s = std::max(m_s,layers[n].n_states);
261  assert(n_e == n_edges);
262  assert(n_s <= n_states);
263  assert(m_s <= max_states);
264 #endif
265  }
266 
267  template<class View, class Val, class Degree, class StateIdx>
268  template<class Var>
271  const VarArgArray<Var>& x,
272  const DFA& dfa) {
273 
274  Region r(home);
275 
276  // Allocate memory for layers
277  layers = home.alloc<Layer>(n+1);
278 
279  // Allocate temporary memory for all possible states
280  State* states = r.alloc<State>(max_states*(n+1));
281  for (int i=static_cast<int>(max_states)*(n+1); i--; )
282  states[i].init();
283  for (int i=n+1; i--; )
284  layers[i].states = states + i*max_states;
285 
286  // Allocate temporary memory for edges
287  Edge* edges = r.alloc<Edge>(dfa.max_degree());
288 
289  // Mark initial state as being reachable
290  i_state(0,0).i_deg = 1;
291 
292  // Forward pass: add transitions
293  for (int i=0; i<n; i++) {
294  layers[i].x = x[i];
295  layers[i].support = home.alloc<Support>(layers[i].x.size());
296  ValSize j=0;
297  // Enter links leaving reachable states (indegree != 0)
298  for (ViewValues<View> nx(layers[i].x); nx(); ++nx) {
299  Degree n_edges=0;
300  for (DFA::Transitions t(dfa,nx.val()); t(); ++t)
301  if (i_state(i,static_cast<StateIdx>(t.i_state())).i_deg != 0) {
302  i_state(i,static_cast<StateIdx>(t.i_state())).o_deg++;
303  o_state(i,static_cast<StateIdx>(t.o_state())).i_deg++;
304  edges[n_edges].i_state = static_cast<StateIdx>(t.i_state());
305  edges[n_edges].o_state = static_cast<StateIdx>(t.o_state());
306  n_edges++;
307  }
308  assert(n_edges <= dfa.max_degree());
309  // Found support for value
310  if (n_edges > 0) {
311  Support& s = layers[i].support[j];
312  s.val = static_cast<Val>(nx.val());
313  s.n_edges = n_edges;
314  s.edges = Heap::copy(home.alloc<Edge>(n_edges),edges,n_edges);
315  j++;
316  }
317  }
318  if ((layers[i].size = j) == 0)
319  return ES_FAILED;
320  }
321 
322  // Mark final states as reachable
323  for (int s=dfa.final_fst(); s<dfa.final_lst(); s++)
324  if (o_state(n-1,static_cast<StateIdx>(s)).i_deg != 0)
325  o_state(n-1,static_cast<StateIdx>(s)).o_deg = 1;
326 
327  // Backward pass: prune all transitions that do not lead to final state
328  for (int i=n; i--; ) {
329  ValSize k=0;
330  for (ValSize j=0; j<layers[i].size; j++) {
331  Support& s = layers[i].support[j];
332  for (Degree d=s.n_edges; d--; )
333  if (o_state(i,s.edges[d]).o_deg == 0) {
334  // Adapt states
335  i_dec(i,s.edges[d]); o_dec(i,s.edges[d]);
336  // Prune edge
337  s.edges[d] = s.edges[--s.n_edges];
338  }
339  // Value has support, copy the support information
340  if (s.n_edges > 0)
341  layers[i].support[k++]=s;
342  }
343  if ((layers[i].size = k) == 0)
344  return ES_FAILED;
345  LayerValues lv(layers[i]);
346  GECODE_ME_CHECK(layers[i].x.narrow_v(home,lv,false));
347  if (!layers[i].x.assigned())
348  layers[i].x.subscribe(home, *new (home) Index(home,*this,c,i));
349  }
350 
351  // Copy and compress states, setup other information
352  {
353  // State map for in-states
354  StateIdx* i_map = r.alloc<StateIdx>(max_states);
355  // State map for out-states
356  StateIdx* o_map = r.alloc<StateIdx>(max_states);
357  // Number of in-states
358  StateIdx i_n = 0;
359 
360  // Initialize map for in-states (special for last layer)
361  // Degree for single final state
362  unsigned int d = 0;
363  for (StateIdx j=max_states; j--; )
364  d += static_cast<unsigned int>(layers[n].states[j].i_deg);
365  // Check whether all final states can be joined to a single state
366  if (d >
367  static_cast<unsigned int>
369  // Initialize map for in-states
370  for (StateIdx j=max_states; j--; )
371  if ((layers[n].states[j].o_deg != 0) ||
372  (layers[n].states[j].i_deg != 0))
373  i_map[j]=i_n++;
374  } else {
375  i_n = 1;
376  for (StateIdx j=max_states; j--; ) {
377  layers[n].states[j].init();
378  i_map[j] = 0;
379  }
380  layers[n].states[0].i_deg = static_cast<Degree>(d);
381  layers[n].states[0].o_deg = 1;
382  }
383  layers[n].n_states = i_n;
384 
385  // Total number of states
386  n_states = i_n;
387  // Total number of edges
388  n_edges = 0;
389  // New maximal number of states
390  StateIdx max_s = i_n;
391 
392  for (int i=n; i--; ) {
393  // In-states become out-states
394  std::swap(o_map,i_map); i_n=0;
395  // Initialize map for in-states
396  for (StateIdx j=max_states; j--; )
397  if ((layers[i].states[j].o_deg != 0) ||
398  (layers[i].states[j].i_deg != 0))
399  i_map[j]=i_n++;
400  layers[i].n_states = i_n;
401  n_states += i_n;
402  max_s = std::max(max_s,i_n);
403 
404  // Update states in edges
405  for (ValSize j=layers[i].size; j--; ) {
406  Support& s = layers[i].support[j];
407  n_edges += s.n_edges;
408  for (Degree d=s.n_edges; d--; ) {
409  s.edges[d].i_state = i_map[s.edges[d].i_state];
410  s.edges[d].o_state = o_map[s.edges[d].o_state];
411  }
412  }
413  }
414 
415  // Allocate and copy states
416  State* a_states = home.alloc<State>(n_states);
417  for (int i=n+1; i--; ) {
418  StateIdx k=0;
419  for (StateIdx j=max_states; j--; )
420  if ((layers[i].states[j].o_deg != 0) ||
421  (layers[i].states[j].i_deg != 0))
422  a_states[k++] = layers[i].states[j];
423  assert(k == layers[i].n_states);
424  layers[i].states = a_states;
425  a_states += layers[i].n_states;
426  }
427 
428  // Update maximal number of states
429  max_states = max_s;
430  }
431 
432  // Schedule if subsumption is needed
433  if (c.empty())
434  View::schedule(home,*this,ME_INT_VAL);
435 
436  audit();
437  return ES_OK;
438  }
439 
440  template<class View, class Val, class Degree, class StateIdx>
441  ExecStatus
443  Advisor& _a, const Delta& d) {
444  // Check whether state information has already been created
445  if (layers[0].states == NULL) {
446  State* states = home.alloc<State>(n_states);
447  for (unsigned int i=n_states; i--; )
448  states[i].init();
449  layers[n].states = states;
450  states += layers[n].n_states;
451  for (int i=n; i--; ) {
452  layers[i].states = states;
453  states += layers[i].n_states;
454  for (ValSize j=layers[i].size; j--; ) {
455  Support& s = layers[i].support[j];
456  for (Degree d=s.n_edges; d--; ) {
457  i_state(i,s.edges[d]).o_deg++;
458  o_state(i,s.edges[d]).i_deg++;
459  }
460  }
461  }
462  }
463 
464  Index& a = static_cast<Index&>(_a);
465  const int i = a.i;
466 
467  if (layers[i].size <= layers[i].x.size()) {
468  // Propagator has already done everything
469  if (View::modevent(d) == ME_INT_VAL) {
470  a.dispose(home,c);
471  return c.empty() ? ES_NOFIX : ES_FIX;
472  } else {
473  return ES_FIX;
474  }
475  }
476 
477  bool i_mod = false;
478  bool o_mod = false;
479 
480  if (View::modevent(d) == ME_INT_VAL) {
481  Val n = static_cast<Val>(layers[i].x.val());
482  ValSize j=0;
483  for (; layers[i].support[j].val < n; j++) {
484  Support& s = layers[i].support[j];
485  n_edges -= s.n_edges;
486  // Supported value not any longer in view
487  for (Degree d=s.n_edges; d--; ) {
488  // Adapt states
489  o_mod |= i_dec(i,s.edges[d]);
490  i_mod |= o_dec(i,s.edges[d]);
491  }
492  }
493  assert(layers[i].support[j].val == n);
494  layers[i].support[0] = layers[i].support[j++];
495  ValSize s=layers[i].size;
496  layers[i].size = 1;
497  for (; j<s; j++) {
498  Support& s = layers[i].support[j];
499  n_edges -= s.n_edges;
500  for (Degree d=s.n_edges; d--; ) {
501  // Adapt states
502  o_mod |= i_dec(i,s.edges[d]);
503  i_mod |= o_dec(i,s.edges[d]);
504  }
505  }
506  } else if (layers[i].x.any(d)) {
507  ValSize j=0;
508  ValSize k=0;
509  ValSize s=layers[i].size;
510  for (ViewRanges<View> rx(layers[i].x); rx() && (j<s);) {
511  Support& s = layers[i].support[j];
512  if (s.val < static_cast<Val>(rx.min())) {
513  // Supported value not any longer in view
514  n_edges -= s.n_edges;
515  for (Degree d=s.n_edges; d--; ) {
516  // Adapt states
517  o_mod |= i_dec(i,s.edges[d]);
518  i_mod |= o_dec(i,s.edges[d]);
519  }
520  ++j;
521  } else if (s.val > static_cast<Val>(rx.max())) {
522  ++rx;
523  } else {
524  layers[i].support[k++]=s;
525  ++j;
526  }
527  }
528  assert(k > 0);
529  layers[i].size = k;
530  // Remove remaining values
531  for (; j<s; j++) {
532  Support& s=layers[i].support[j];
533  n_edges -= s.n_edges;
534  for (Degree d=s.n_edges; d--; ) {
535  // Adapt states
536  o_mod |= i_dec(i,s.edges[d]);
537  i_mod |= o_dec(i,s.edges[d]);
538  }
539  }
540  } else {
541  Val min = static_cast<Val>(layers[i].x.min(d));
542  ValSize j=0;
543  // Skip values smaller than min (to keep)
544  for (; layers[i].support[j].val < min; j++) {}
545  Val max = static_cast<Val>(layers[i].x.max(d));
546  ValSize k=j;
547  ValSize s=layers[i].size;
548  // Remove pruned values
549  for (; (j<s) && (layers[i].support[j].val <= max); j++) {
550  Support& s=layers[i].support[j];
551  n_edges -= s.n_edges;
552  for (Degree d=s.n_edges; d--; ) {
553  // Adapt states
554  o_mod |= i_dec(i,s.edges[d]);
555  i_mod |= o_dec(i,s.edges[d]);
556  }
557  }
558  // Keep remaining values
559  while (j<s)
560  layers[i].support[k++]=layers[i].support[j++];
561  layers[i].size =k;
562  assert(k > 0);
563  }
564 
565  audit();
566 
567  bool fix = true;
568  if (o_mod && (i > 0)) {
569  o_ch.add(i-1); fix = false;
570  }
571  if (i_mod && (i+1 < n)) {
572  i_ch.add(i+1); fix = false;
573  }
574  if (fix) {
575  if (View::modevent(d) == ME_INT_VAL) {
576  a.dispose(home,c);
577  return c.empty() ? ES_NOFIX : ES_FIX;
578  }
579  return ES_FIX;
580  } else {
581  return (View::modevent(d) == ME_INT_VAL)
582  ? home.ES_NOFIX_DISPOSE(c,a) : ES_NOFIX;
583  }
584  }
585 
586  template<class View, class Val, class Degree, class StateIdx>
587  forceinline size_t
589  c.dispose(home);
590  (void) Propagator::dispose(home);
591  return sizeof(*this);
592  }
593 
594  template<class View, class Val, class Degree, class StateIdx>
595  ExecStatus
597  const ModEventDelta&) {
598  // Forward pass
599  for (int i=i_ch.fst(); i<=i_ch.lst(); i++) {
600  bool i_mod = false;
601  bool o_mod = false;
602  ValSize j=0;
603  ValSize k=0;
604  ValSize s=layers[i].size;
605  do {
606  Support& s=layers[i].support[j];
607  n_edges -= s.n_edges;
608  for (Degree d=s.n_edges; d--; )
609  if (i_state(i,s.edges[d]).i_deg == 0) {
610  // Adapt states
611  o_mod |= i_dec(i,s.edges[d]);
612  i_mod |= o_dec(i,s.edges[d]);
613  // Remove edge
614  s.edges[d] = s.edges[--s.n_edges];
615  }
616  n_edges += s.n_edges;
617  // Check whether value is still supported
618  if (s.n_edges == 0) {
619  layers[i].size--;
620  GECODE_ME_CHECK(layers[i].x.nq(home,s.val));
621  } else {
622  layers[i].support[k++]=s;
623  }
624  } while (++j<s);
625  assert(k > 0);
626  // Update modification information
627  if (o_mod && (i > 0))
628  o_ch.add(i-1);
629  if (i_mod && (i+1 < n))
630  i_ch.add(i+1);
631  }
632 
633  // Backward pass
634  for (int i=o_ch.lst(); i>=o_ch.fst(); i--) {
635  bool o_mod = false;
636  ValSize j=0;
637  ValSize k=0;
638  ValSize s=layers[i].size;
639  do {
640  Support& s=layers[i].support[j];
641  n_edges -= s.n_edges;
642  for (Degree d=s.n_edges; d--; )
643  if (o_state(i,s.edges[d]).o_deg == 0) {
644  // Adapt states
645  o_mod |= i_dec(i,s.edges[d]);
646  (void) o_dec(i,s.edges[d]);
647  // Remove edge
648  s.edges[d] = s.edges[--s.n_edges];
649  }
650  n_edges += s.n_edges;
651  // Check whether value is still supported
652  if (s.n_edges == 0) {
653  layers[i].size--;
654  GECODE_ME_CHECK(layers[i].x.nq(home,s.val));
655  } else {
656  layers[i].support[k++]=s;
657  }
658  } while (++j<s);
659  assert(k > 0);
660  // Update modification information
661  if (o_mod && (i > 0))
662  o_ch.add(i-1);
663  }
664 
665  a_ch.add(i_ch); i_ch.reset();
666  a_ch.add(o_ch); o_ch.reset();
667 
668  audit();
669 
670  // Check subsumption
671  if (c.empty())
672  return home.ES_SUBSUMED(*this);
673  else
674  return ES_FIX;
675  }
676 
677 
678  template<class View, class Val, class Degree, class StateIdx>
679  template<class Var>
680  ExecStatus
682  const VarArgArray<Var>& x,
683  const DFA& dfa) {
684  if (x.size() == 0) {
685  // Check whether the start state 0 is also a final state
686  if ((dfa.final_fst() <= 0) && (dfa.final_lst() >= 0))
687  return ES_OK;
688  return ES_FAILED;
689  }
690  assert(x.size() > 0);
691  for (int i=x.size(); i--; ) {
692  DFA::Symbols s(dfa);
693  typename VarTraits<Var>::View xi(x[i]);
694  GECODE_ME_CHECK(xi.inter_v(home,s,false));
695  }
697  new (home) LayeredGraph<View,Val,Degree,StateIdx>(home,x,dfa);
698  return p->initialize(home,x,dfa);
699  }
700 
701  template<class View, class Val, class Degree, class StateIdx>
704  ::LayeredGraph(Space& home, bool share,
706  : Propagator(home,share,p),
707  n(p.n), layers(home.alloc<Layer>(n+1)),
708  max_states(p.max_states), n_states(p.n_states), n_edges(p.n_edges) {
709  c.update(home,share,p.c);
710  // Do not allocate states, postpone to advise!
711  layers[n].n_states = p.layers[n].n_states;
712  layers[n].states = NULL;
713  // Allocate memory for edges
714  Edge* edges = home.alloc<Edge>(n_edges);
715  // Copy layers
716  for (int i=n; i--; ) {
717  layers[i].x.update(home,share,p.layers[i].x);
718  assert(layers[i].x.size() == p.layers[i].size);
719  layers[i].size = p.layers[i].size;
720  layers[i].support = home.alloc<Support>(layers[i].size);
721  for (ValSize j=layers[i].size; j--; ) {
722  layers[i].support[j].val = p.layers[i].support[j].val;
723  layers[i].support[j].n_edges = p.layers[i].support[j].n_edges;
724  assert(layers[i].support[j].n_edges > 0);
725  layers[i].support[j].edges =
726  Heap::copy(edges,p.layers[i].support[j].edges,
727  layers[i].support[j].n_edges);
728  edges += layers[i].support[j].n_edges;
729  }
730  layers[i].n_states = p.layers[i].n_states;
731  layers[i].states = NULL;
732  }
733  audit();
734  }
735 
736  template<class View, class Val, class Degree, class StateIdx>
737  PropCost
739  const ModEventDelta&) const {
741  }
742 
743  template<class View, class Val, class Degree, class StateIdx>
744  Actor*
746  // Eliminate an assigned prefix
747  {
748  int k=0;
749  while (layers[k].size == 1) {
750  assert(layers[k].support[0].n_edges == 1);
751  n_states -= layers[k].n_states;
752  k++;
753  }
754  if (k > 0) {
755  /*
756  * The state information is always available: either the propagator
757  * has been created (hence, also the state information has been
758  * created), or the first variable become assigned and hence
759  * an advisor must have been run (which then has created the state
760  * information).
761  */
762  // Eliminate assigned layers
763  n -= k; layers += k;
764  // Eliminate edges
765  n_edges -= static_cast<unsigned int>(k);
766  // Update advisor indices
767  for (Advisors<Index> as(c); as(); ++as)
768  as.advisor().i -= k;
769  // Update all change information
770  a_ch.lshift(k);
771  }
772  }
773  audit();
774 
775  // Compress states
776  if (!a_ch.empty()) {
777  int f = a_ch.fst();
778  int l = a_ch.lst();
779  assert((f >= 0) && (l <= n));
780  Region r(home);
781  // State map for in-states
782  StateIdx* i_map = r.alloc<StateIdx>(max_states);
783  // State map for out-states
784  StateIdx* o_map = r.alloc<StateIdx>(max_states);
785  // Number of in-states
786  StateIdx i_n = 0;
787 
788  n_states -= layers[l].n_states;
789  // Initialize map for in-states and compress
790  for (StateIdx j=0; j<layers[l].n_states; j++)
791  if ((layers[l].states[j].i_deg != 0) ||
792  (layers[l].states[j].o_deg != 0)) {
793  layers[l].states[i_n]=layers[l].states[j];
794  i_map[j]=i_n++;
795  }
796  layers[l].n_states = i_n;
797  n_states += layers[l].n_states;
798  assert(i_n > 0);
799 
800  // Update in-states in edges for last layer, if any
801  if (l < n)
802  for (ValSize j=layers[l].size; j--; ) {
803  Support& s = layers[l].support[j];
804  for (Degree d=s.n_edges; d--; )
805  s.edges[d].i_state = i_map[s.edges[d].i_state];
806  }
807 
808  // Update all changed layers
809  for (int i=l-1; i>=f; i--) {
810  // In-states become out-states
811  std::swap(o_map,i_map); i_n=0;
812  // Initialize map for in-states and compress
813  n_states -= layers[i].n_states;
814  for (StateIdx j=0; j<layers[i].n_states; j++)
815  if ((layers[i].states[j].o_deg != 0) ||
816  (layers[i].states[j].i_deg != 0)) {
817  layers[i].states[i_n]=layers[i].states[j];
818  i_map[j]=i_n++;
819  }
820  layers[i].n_states = i_n;
821  n_states += layers[i].n_states;
822  assert(i_n > 0);
823 
824  // Update states in edges
825  for (ValSize j=layers[i].size; j--; ) {
826  Support& s = layers[i].support[j];
827  for (Degree d=s.n_edges; d--; ) {
828  s.edges[d].i_state = i_map[s.edges[d].i_state];
829  s.edges[d].o_state = o_map[s.edges[d].o_state];
830  }
831  }
832  }
833 
834  // Update out-states in edges for previous layer, if any
835  if (f > 0)
836  for (ValSize j=layers[f-1].size; j--; ) {
837  Support& s = layers[f-1].support[j];
838  for (Degree d=s.n_edges; d--; )
839  s.edges[d].o_state = i_map[s.edges[d].o_state];
840  }
841 
842  a_ch.reset();
843  }
844  audit();
845 
846  return new (home) LayeredGraph<View,Val,Degree,StateIdx>(home,share,*this);
847  }
848 
850  template<class Var>
852  post_lgp(Home home, const VarArgArray<Var>& x, const DFA& dfa) {
853  Gecode::Support::IntType t_state_idx =
854  Gecode::Support::u_type(static_cast<unsigned int>(dfa.n_states()));
855  Gecode::Support::IntType t_degree =
857  Gecode::Support::IntType t_val =
859  Support::s_type(dfa.symbol_max()));
860  switch (t_val) {
863  switch (t_state_idx) {
865  switch (t_degree) {
868  <typename VarTraits<Var>::View,short int,unsigned char,unsigned char>
869  ::post(home,x,dfa);
872  <typename VarTraits<Var>::View,short int,unsigned short int,unsigned char>
873  ::post(home,x,dfa);
876  <typename VarTraits<Var>::View,short int,unsigned int,unsigned char>
877  ::post(home,x,dfa);
878  default: GECODE_NEVER;
879  }
880  break;
882  switch (t_degree) {
885  <typename VarTraits<Var>::View,short int,unsigned char,unsigned short int>
886  ::post(home,x,dfa);
889  <typename VarTraits<Var>::View,short int,unsigned short int,unsigned short int>
890  ::post(home,x,dfa);
893  <typename VarTraits<Var>::View,short int,unsigned int,unsigned short int>
894  ::post(home,x,dfa);
895  default: GECODE_NEVER;
896  }
897  break;
899  switch (t_degree) {
902  <typename VarTraits<Var>::View,short int,unsigned char,unsigned int>
903  ::post(home,x,dfa);
906  <typename VarTraits<Var>::View,short int,unsigned short int,unsigned int>
907  ::post(home,x,dfa);
910  <typename VarTraits<Var>::View,short int,unsigned int,unsigned int>
911  ::post(home,x,dfa);
912  default: GECODE_NEVER;
913  }
914  break;
915  default: GECODE_NEVER;
916  }
917 
919  switch (t_state_idx) {
921  switch (t_degree) {
924  <typename VarTraits<Var>::View,int,unsigned char,unsigned char>
925  ::post(home,x,dfa);
928  <typename VarTraits<Var>::View,int,unsigned short int,unsigned char>
929  ::post(home,x,dfa);
932  <typename VarTraits<Var>::View,int,unsigned int,unsigned char>
933  ::post(home,x,dfa);
934  default: GECODE_NEVER;
935  }
936  break;
938  switch (t_degree) {
941  <typename VarTraits<Var>::View,int,unsigned char,unsigned short int>
942  ::post(home,x,dfa);
945  <typename VarTraits<Var>::View,int,unsigned short int,unsigned short int>
946  ::post(home,x,dfa);
949  <typename VarTraits<Var>::View,int,unsigned int,unsigned short int>
950  ::post(home,x,dfa);
951  default: GECODE_NEVER;
952  }
953  break;
955  switch (t_degree) {
958  <typename VarTraits<Var>::View,int,unsigned char,unsigned int>
959  ::post(home,x,dfa);
962  <typename VarTraits<Var>::View,int,unsigned short int,unsigned int>
963  ::post(home,x,dfa);
966  <typename VarTraits<Var>::View,int,unsigned int,unsigned int>
967  ::post(home,x,dfa);
968  default: GECODE_NEVER;
969  }
970  break;
971  default: GECODE_NEVER;
972  }
973 
974  default: GECODE_NEVER;
975  }
976  return ES_OK;
977  }
978 
979 }}}
980 
981 // STATISTICS: int-prop
982 
int lst(void) const
Return last position.
Council of advisors
Definition: core.hpp:226
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
Definition: heap.hpp:552
NodeType t
Type of node.
Definition: bool-expr.cpp:234
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
void audit(void)
Perform consistency check on data structures.
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
Edge defined by in-state and out-state
Definition: extensional.hh:80
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition: core.hpp:4041
Council< Index > c
The advisor council.
Definition: extensional.hh:156
Iterator for DFA symbols.
Definition: int.hh:1921
int final_fst(void) const
Return the number of the first final state.
Definition: dfa.hpp:129
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
int val(void) const
Return supported value.
int n
Number of layers (and views)
Definition: extensional.hh:158
Iterator for telling variable domains by scanning support.
Definition: extensional.hh:104
int final_lst(void) const
Return the number of the last final state.
Definition: dfa.hpp:135
int size(void) const
Return size of array (number of elements)
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
Traits class for variables.
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
unsigned int n_states
Total number of states.
Definition: extensional.hh:164
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
IntType u_type(unsigned int n)
Return type required to represent n.
Definition: int-type.hpp:151
void init(const Layer &l)
Initialize for support of layer l.
StateIdx i_state
Number of in-state.
Definition: extensional.hh:82
Base-class for propagators.
Definition: core.hpp:755
Expensive.
Definition: core.hpp:564
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
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as high linear)
Class to iterate over advisors of a council.
Definition: core.hpp:227
Handle to region.
Definition: region.hpp:61
Value iterator for integer views.
Definition: view.hpp:94
void lshift(int n)
Shift index range by n elements to the left.
Propagation has computed fixpoint.
Definition: core.hpp:528
Int::IntView View
The variable type of an IntView.
Computation spaces.
Definition: core.hpp:1362
Support information for a value
Definition: extensional.hh:86
bool i_dec(int i, const Edge &e)
Decrement out degree for in state of edge e for layer i.
Base-class for both propagators and branchers.
Definition: core.hpp:666
Range iterator for integer views.
Definition: view.hpp:54
IntType s_type(signed int n)
Return type required to represent n.
Gecode::IntSet d(v, 7)
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2397
State & o_state(int i, StateIdx os)
Return out state for layer i and state index os.
ValSize size
Number of supported values.
Definition: extensional.hh:99
char integer type
Definition: int-type.hpp:44
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
StateIdx o_state
Number of out-state.
Definition: extensional.hh:83
Deterministic finite automaton (DFA)
Definition: int.hh:1881
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
void operator++(void)
Move to next supported value.
Execution has resulted in failure.
Definition: core.hpp:525
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Range approximation of which positions have changed.
Definition: extensional.hh:133
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:56
Layer for a view in the layered graph
Definition: extensional.hh:95
Degree n_edges
Number of supporting edges.
Definition: extensional.hh:89
unsigned int size(I &i)
Size of all ranges of range iterator i.
unsigned int max_degree(void) const
Return maximal degree (in-degree and out-degree) of any state.
Definition: dfa.hpp:123
LayeredGraph(Space &home, bool share, LayeredGraph< View, Val, Degree, StateIdx > &p)
Constructor for cloning p.
size_t size
The size of the propagator (used during subsumption)
Definition: core.hpp:766
bool operator()(void) const
Test whether more values supported.
Layer * layers
The layers of the graph.
Definition: extensional.hh:160
int fst(void) const
Return first position.
StateIdx max_states
Maximal number of states per layer.
Definition: extensional.hh:162
ExecStatus post_lgp(Home home, const VarArgArray< Var > &x, const DFA &dfa)
Select small types for the layered graph propagator.
void reset(void)
Reset range to be empty.
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Traits to for information about integer types.
Definition: int-type.hpp:56
Degree o_deg
The out-degree (number of outgoing edges) Initialize with zeroes.
Definition: extensional.hh:75
State * states
States used by outgoing edges.
Definition: extensional.hh:100
Boolean integer variables.
Definition: int.hh:491
Domain consistent layered graph (regular) propagator.
Definition: extensional.hh:69
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
Integer view for integer variables.
Definition: view.hpp:129
static ExecStatus post(Home home, const VarArgArray< Var > &x, const DFA &dfa)
Post propagator on views x and DFA dfa.
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
Advisors for views (by position in array)
Definition: extensional.hh:123
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
void dispose(Space &home, Council< A > &c)
Dispose the advisor.
Definition: core.hpp:3267
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
Integer variables.
Definition: int.hh:350
#define forceinline
Definition: config.hpp:132
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Iterator for DFA transitions (sorted by symbols)
Definition: int.hh:1898
int n_states(void) const
Return the number of states.
Definition: dfa.hpp:105
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
Edge * edges
Supporting edges in layered graph.
Definition: extensional.hh:90
Int::BoolView View
The variable type of an IntView.
State & i_state(int i, StateIdx is)
Return in state for layer i and state index is.
Gecode toplevel namespace
Argument array for variables.
Definition: array.hpp:53
ExecStatus initialize(Space &home, const VarArgArray< Var > &x, const DFA &dfa)
Initialize layered graph.
States are described by number of incoming and outgoing edges.
Definition: extensional.hh:72
Degree i_deg
The in-degree (number of incoming edges)
Definition: extensional.hh:74
bool o_dec(int i, const Edge &e)
Decrement in degree for out state of edge e for layer i.
virtual size_t dispose(Space &home)
Delete propagator and return its size.
IntType
Description of integer types.
Definition: int-type.hpp:43
int symbol_min(void) const
Return smallest symbol in DFA.
Definition: dfa.hpp:141
int symbol_max(void) const
Return largest symbol in DFA.
Definition: dfa.hpp:148
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
short integer type
Definition: int-type.hpp:45
Home class for posting propagators
Definition: core.hpp:717
bool empty(void) const
Test whether range is empty.
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Gecode::Support::IntTypeTraits< Val >::utype ValSize
Type for support size.
Definition: extensional.hh:93
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
bool assigned(void) const
Test if all variables are assigned.
Definition: array.hpp:2076
int i
The position of the view in the view array.
Definition: extensional.hh:126
Boolean view for Boolean variables.
Definition: view.hpp:1315