Generated on Sat May 25 2013 18:00:37 for Gecode by doxygen 1.8.3.1
int.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * David Rijsman <David.Rijsman@quintiq.com>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  *
9  * Copyright:
10  * David Rijsman, 2009
11  * Christian Schulte, 2009
12  *
13  * Last modified:
14  * $Date: 2012-09-07 17:31:22 +0200 (Fri, 07 Sep 2012) $
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 Sequence {
43 
44  template<class View, class Val>
47  int q0, int l0, int u0)
48  : Propagator(home), x(x0), s(s0), q(q0), l(l0), u(u0),
49  vvsamax(home,x,s0,q0), vvsamin(home,x,s0,q0), ac(home) {
50  home.notice(*this,AP_DISPOSE);
51  for (int i=x.size(); i--; ) {
52  if (undecided(x[i],s)) {
53  x[i].subscribe(home,*new (home) SupportAdvisor<View>(home,*this,ac,i));
54  } else {
55  x[i].schedule(home,*this,x[i].assigned() ? ME_INT_VAL : ME_INT_BND);
56  }
57  }
58  }
59 
60  namespace {
62  template<class Val>
63  class UpdateVal {
64  public:
65  static void update(Val& n, Space& home, bool share, Val& old);
66  };
68  template<>
69  class UpdateVal<int> {
70  public:
71  static void update(int& n, Space&, bool, int& old) {
72  n = old;
73  }
74  };
76  template<>
77  class UpdateVal<IntSet> {
78  public:
79  static void update(IntSet& n, Space& home, bool share,
80  IntSet& old) {
81  n.update(home,share,old);
82  }
83  };
84  }
85 
86  template<class View, class Val>
89  : Propagator(home,share,p), q(p.q), l(p.l), u(p.u),
90  vvsamax(), vvsamin() {
91  UpdateVal<Val>::update(s,home,share,p.s);
92  x.update(home,share,p.x);
93  ac.update(home,share,p.ac);
94  vvsamax.update(home,share,p.vvsamax);
95  vvsamin.update(home,share,p.vvsamin);
96  }
97 
98  template<class View,class Val>
101  SupportAdvisor<View>& a = static_cast<SupportAdvisor<View>&>(_a);
102  ExecStatus status = vvsamax.advise(home,x,s,q,a.i,d);
103  if ( ES_NOFIX == vvsamin.advise(home,x,s,q,a.i,d) ) {
104  status = ES_NOFIX;
105  }
106 
107  if (!undecided(x[a.i],s)) {
108  if (!x[a.i].assigned())
109  x[a.i].cancel(home,a);
110 
111  if ( ES_NOFIX == status ) {
112  return home.ES_NOFIX_DISPOSE(ac,a);
113  } else {
114  return home.ES_FIX_DISPOSE(ac,a);
115  }
116  }
117 
118  return status;
119  }
120 
121  template<class View, class Val>
122  forceinline size_t
124  home.ignore(*this,AP_DISPOSE);
125  ac.dispose(home);
126  s.~Val();
127  (void) Propagator::dispose(home);
128  return sizeof(*this);
129  }
130 
131  template<class View, class Val>
133  Sequence<View,Val>::check(Space& home, ViewArray<View>& x, Val s, int q, int l, int u) {
134  Region r(home);
135  // could do this with an array of length q...
136  int* upper = r.alloc<int>(x.size()+1);
137  int* lower = r.alloc<int>(x.size()+1);
138  upper[0] = 0;
139  lower[0] = 0;
140  for ( int j=0; j<x.size(); j++ ) {
141  upper[j+1] = upper[j];
142  lower[j+1] = lower[j];
143  if (includes(x[j],s)) {
144  upper[j+1] += 1;
145  } else if (excludes(x[j],s)) {
146  lower[j+1] += 1;
147  }
148  if ( j+1 >= q && (q - l < lower[j+1] - lower[j+1-q] || upper[j+1] - upper[j+1-q] > u) ) {
149  return ES_FAILED;
150  }
151  }
152  return ES_OK;
153  }
154 
155  template<class View, class Val>
156  ExecStatus
157  Sequence<View,Val>::post(Home home, ViewArray<View>& x, Val s, int q, int l, int u) {
158  GECODE_ES_CHECK(check(home,x,s,q,l,u));
159  Sequence<View,Val>* p = new (home) Sequence<View,Val>(home,x,s,q,l,u);
160 
161  GECODE_ES_CHECK(p->vvsamax.propagate(home,x,s,q,l,u));
162  GECODE_ES_CHECK(p->vvsamin.propagate(home,x,s,q,l,u));
163 
164  return ES_OK;
165  }
166 
167  template<class View, class Val>
168  Actor*
169  Sequence<View,Val>::copy(Space& home, bool share) {
170  return new (home) Sequence<View,Val>(home,share,*this);
171  }
172 
173  template<class View, class Val>
174  PropCost
176  return PropCost::cubic(PropCost::HI,x.size());
177  }
178 
179  template<class View, class Val>
180  ExecStatus
182  GECODE_ES_CHECK(vvsamax.propagate(home,x,s,q,l,u));
183  GECODE_ES_CHECK(vvsamin.propagate(home,x,s,q,l,u));
184 
185  for (int i=x.size(); i--; )
186  if (undecided(x[i],s))
187  return ES_FIX;
188 
189  return home.ES_SUBSUMED(*this);
190  }
191 
192 }}}
193 
194 // STATISTICS: int-prop
195