Generated on Sat Feb 7 2015 02:01:19 for Gecode by doxygen 1.8.9.1
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 
int * s
Array of sizes (will have more elements)
Definition: bin-packing.hh:98
int card(void) const
Return cardinality of set (number of entries)
Definition: propagate.hpp:106
NodeType t
Type of node.
Definition: bool-expr.cpp:234
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
int p
Position of discarded item.
Definition: bin-packing.hh:118
SizeSetMinusOne(void)
Default constructor.
Definition: propagate.hpp:119
bool nosum(const SizeSet &s, int a, int b, int &ap, int &bp)
Detect non-existence of sums in a .. b.
Definition: propagate.hpp:180
IntView bin(void) const
Return bin of item.
Definition: propagate.hpp:52
Item combining bin and size information.
Definition: bin-packing.hh:57
int size(void) const
Return size of item.
Definition: propagate.hpp:60
ViewArray< Item > bs
Items with bin and size.
Definition: bin-packing.hh:150
int t
Total size of all items.
Definition: bin-packing.hh:152
int operator[](int i) const
Return size of item i.
Definition: propagate.hpp:114
int card(void) const
Return cardinality of set (number of entries)
Definition: propagate.hpp:132
Base-class for propagators.
Definition: core.hpp:755
int n
Number of size entries in the set.
Definition: bin-packing.hh:94
Pack(Home home, ViewArray< OffsetView > &l, ViewArray< Item > &bs)
Constructor for posting.
Definition: propagate.hpp:155
ViewArray< OffsetView > l
Views for load of bins.
Definition: bin-packing.hh:148
Handle to region.
Definition: region.hpp:61
Computation spaces.
Definition: core.hpp:1362
Base-class for derived views.
Definition: view.hpp:208
void update(Space &home, bool share, Item &i)
Update item during cloning.
Definition: propagate.hpp:69
SizeSet(void)
Default constructor.
Definition: propagate.hpp:97
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:91
bool operator<(const Item &i, const Item &j)
For sorting according to size.
Definition: propagate.hpp:87
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition: var-type.hpp:100
size_t size
The size of the propagator (used during subsumption)
Definition: core.hpp:766
int operator[](int i) const
Return size of item i.
Definition: propagate.hpp:142
View arrays.
Definition: array.hpp:234
void add(int s)
Add new size s.
Definition: propagate.hpp:102
void update(Space &home, bool share, VarImpView< Var > &y)
Update this view to be a clone of view y.
Definition: view.hpp:494
Item(void)
Default constructor.
Definition: propagate.hpp:45
bool before(const Item &i, const Item &j)
Test whether one item is before another.
Definition: propagate.hpp:80
Bin-packing propagator.
Definition: bin-packing.hh:145
Example: Bin packing
Integer view for integer variables.
Definition: view.hpp:129
virtual size_t dispose(Space &home)
Destructor.
Definition: propagate.hpp:171
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
#define forceinline
Definition: config.hpp:132
bool same(const Item &i, const Item &j)
Whether two items are the same.
Definition: propagate.hpp:76
int t
Total size of the set.
Definition: bin-packing.hh:96
void minus(int s)
Discard size s.
Definition: propagate.hpp:124
IntView x
View from which this view is derived.
Definition: view.hpp:216
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Home class for posting propagators
Definition: core.hpp:717
int total(void) const
Return total size.
Definition: propagate.hpp:110
int total(void) const
Return total size.
Definition: propagate.hpp:137
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.