Generated on Sat Nov 9 2013 19:18:27 for Gecode by doxygen 1.8.4
propagate.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, 2010
8  *
9  * Last modified:
10  * $Date: 2010-10-05 17:03:20 +0200 (Tue, 05 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11448 $
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 namespace Gecode { namespace Int { namespace BinPacking {
39 
40  /*
41  * Item
42  *
43  */
45  Item::Item(void)
46  : s(0) {}
48  Item::Item(IntView b, int s0)
49  : DerivedView<IntView>(b), s(s0) {}
50 
52  Item::bin(void) const {
53  return x;
54  }
57  x = b;
58  }
59  forceinline int
60  Item::size(void) const {
61  return s;
62  }
63  forceinline void
64  Item::size(int s0) {
65  s = s0;
66  }
67 
68  forceinline void
69  Item::update(Space& home, bool share, Item& i) {
70  x.update(home,share,i.x);
71  s = i.s;
72  }
73 
74 
75  forceinline bool
76  same(const Item& i, const Item& j) {
77  return same(i.bin(),j.bin()) && (i.size() == j.size());
78  }
79  forceinline bool
80  before(const Item& i, const Item& j) {
81  return before(i.bin(),j.bin())
82  || (same(i.bin(),j.bin()) && (i.size() == j.size()));
83  }
84 
86  forceinline bool
87  operator <(const Item& i, const Item& j) {
88  return i.size() > j.size();
89  }
90 
91 
92  /*
93  * Size set
94  *
95  */
99  SizeSet::SizeSet(Region& region, int n_max)
100  : n(0), t(0), s(region.alloc<int>(n_max)) {}
101  forceinline void
102  SizeSet::add(int s0) {
103  t += s0; s[n++] = s0;
104  }
105  forceinline int
106  SizeSet::card(void) const {
107  return n;
108  }
109  forceinline int
110  SizeSet::total(void) const {
111  return t;
112  }
113  forceinline int
114  SizeSet::operator [](int i) const {
115  return s[i];
116  }
117 
122  : SizeSet(region,n_max), p(-1) {}
123  forceinline void
125  // This rests on the fact that items are removed in order
126  do
127  p++;
128  while (s[p] > s0);
129  assert(p < n);
130  }
131  forceinline int
132  SizeSetMinusOne::card(void) const {
133  assert(p >= 0);
134  return n - 1;
135  }
136  forceinline int
138  assert(p >= 0);
139  return t - s[p];
140  }
141  forceinline int
143  assert(p >= 0);
144  return s[(i < p) ? i : i+1];
145  }
146 
147 
148 
149  /*
150  * Packing propagator
151  *
152  */
153 
156  : Propagator(home), l(l0), bs(bs0), t(0) {
157  l.subscribe(home,*this,PC_INT_BND);
158  bs.subscribe(home,*this,PC_INT_DOM);
159  for (int i=bs.size(); i--; )
160  t += bs[i].size();
161  }
162 
164  Pack::Pack(Space& home, bool shared, Pack& p)
165  : Propagator(home,shared,p), t(p.t) {
166  l.update(home,shared,p.l);
167  bs.update(home,shared,p.bs);
168  }
169 
170  forceinline size_t
172  l.cancel(home,*this,PC_INT_BND);
173  bs.cancel(home,*this,PC_INT_DOM);
174  (void) Propagator::dispose(home);
175  return sizeof(*this);
176  }
177 
178  template<class SizeSet>
179  forceinline bool
180  Pack::nosum(const SizeSet& s, int a, int b, int& ap, int& bp) {
181  if ((a <= 0) || (b >= s.total()))
182  return false;
183  int n=s.card()-1;
184  int sc=0;
185  int kp=0;
186  while (sc + s[n-kp] < a) {
187  sc += s[n-kp];
188  kp++;
189  }
190  int k=0;
191  int sa=0, sb = s[n-kp];
192  while ((sa < a) && (sb <= b)) {
193  sa += s[k++];
194  if (sa < a) {
195  kp--;
196  sb += s[n-kp];
197  sc -= s[n-kp];
198  while (sa + sc >= a) {
199  kp--;
200  sc -= s[n-kp];
201  sb += s[n-kp] - s[n-kp-k-1];
202  }
203  }
204  }
205  ap = sa + sc; bp = sb;
206  return sa < a;
207  }
208 
209  template<class SizeSet>
210  forceinline bool
211  Pack::nosum(const SizeSet& s, int a, int b) {
212  int ap, bp;
213  return nosum(s, a, b, ap, bp);
214  }
215 
216 }}}
217 
218 // STATISTICS: int-prop
219