Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
bin-packing.cpp
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: 2014-08-28 14:29:11 +0200 (Thu, 28 Aug 2014) $ by $Author: schulte $
11  * $Revision: 14203 $
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 "test/int.hh"
39 
40 #include <gecode/minimodel.hh>
41 #include <climits>
42 
43 namespace Test { namespace Int {
44 
46  namespace BinPacking {
47 
53  class LoadBinAssignment : public Assignment {
55  protected:
57  int n_bins;
59  int n_items;
65  int load;
68  public:
70  LoadBinAssignment(int m, const Gecode::IntSet& d_m,
71  int n, const Gecode::IntSet& d_n,
72  int l)
73  : Assignment(m+n,d_m),
74  n_bins(m), n_items(n), d_load(d_m), d_bin(d_n), load(l),
75  dsv(new Gecode::IntSetValues[static_cast<unsigned int>(m+n)]) {
76  for (int i=n_bins; i--; )
77  dsv[i].init(d_load);
78  for (int i=n_items; i--; )
79  dsv[n_bins+i].init(d_bin);
80  }
82  virtual bool operator()(void) const {
83  return dsv[0]();
84  }
86  virtual void operator++(void) {
87  // Try to generate next bin assignment
88  {
89  int i = n_items-1;
90  while (i >= 0) {
91  ++dsv[n_bins+i];
92  if (dsv[n_bins+i]())
93  return;
94  dsv[n_bins+(i--)].init(d_bin);
95  }
96  }
97  // Try to generate next load assignment
98  {
99  retry:
100  int i = n_bins-1;
101  while (true) {
102  ++dsv[i];
103  if (dsv[i]() || (i == 0)) {
104  if (dsv[i]() && (load >= 0)) {
105  int l = 0;
106  for (int k=0;k<n_bins; k++)
107  l += dsv[k].val();
108  if (load != l)
109  goto retry;
110  }
111  return;
112  }
113  dsv[i--].init(d_load);
114  }
115  }
116  }
118  virtual int operator[](int i) const {
119  assert((i>=0) && (i<n_bins+n_items));
120  return dsv[i].val();
121  }
123  virtual ~LoadBinAssignment(void) {
124  delete [] dsv;
125  }
126  };
127 
129  class BPT : public Test {
130  protected:
132  int m;
136  bool valid;
138  int t;
140  mutable int il[8];
142  static int total(const Gecode::IntArgs& s) {
143  int t = 0;
144  for (int i=s.size(); i--; )
145  t += s[i];
146  return t;
147  }
148  public:
150  BPT(int m0, const Gecode::IntArgs& s0, bool v=true)
151  : Test("BinPacking::"+str(m0)+"::"+str(s0)+"::"+(v ? "+" : "-"),
152  m0+s0.size(), 0, 100),
153  m(m0), s(s0), valid(v), t(total(s)) {
154  testsearch = false;
155  }
157  virtual Assignment* assignment(void) const {
158  // Compute plausible bin sizes
159  int a = t / m;
160  return new LoadBinAssignment(m,Gecode::IntSet(a-1,a+2),
161  s.size(),Gecode::IntSet(0,m-1),
162  valid ? t : -1);
163  }
165  virtual bool solution(const Assignment& x) const {
166  // Loads are from 0 to m-1, after that are items
167  // Check whether loads sum up to total size
168  int l=0;
169  for (int j=m; j--; )
170  l += x[j];
171  if (l != t)
172  return false;
173  // Compute whether items add up
174  for (int j=m; j--; )
175  il[j] = 0;
176  for (int i=s.size(); i--; )
177  il[x[m+i]] += s[i];
178  for (int j=m; j--; )
179  if (il[j] != x[j])
180  return false;
181  return true;
182  }
184  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
185  using namespace Gecode;
186  IntVarArgs l(m);
187  IntVarArgs b(s.size());
188  for (int j=m; j--; )
189  l[j]=x[j];
190  for (int i=s.size(); i--; )
191  b[i]=x[m+i];
192  binpacking(home, l, b, s);
193  }
194  };
195 
197  class MBPT : public Test {
198  protected:
200  int d;
202  int m;
208  mutable int il[4][8];
209  public:
211  MBPT(int d0, int m0,
212  const Gecode::IntArgs& s0, const Gecode::IntArgs& c0)
213  : Test("MultiBinPacking::"+str(d0)+"::"+str(m0)+"::"+
214  str(s0)+"::"+str(c0), s0.size() / d0, 0, m0-1),
215  d(d0), m(m0), s(s0), c(c0) {
216  testsearch = false;
217  testfix = false;
218  }
220  virtual bool solution(const Assignment& x) const {
221  // x are the bin variables
222  for (int k=d; k--; )
223  for (int j=m; j--; )
224  il[k][j] = 0;
225  for (int k=d; k--; )
226  for (int i=x.size(); i--; )
227  il[k][x[i]] += s[i*d+k];
228  for (int k=d; k--; )
229  for (int j=m; j--; )
230  if (il[k][j] > c[k])
231  return false;
232  return true;
233  }
235  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
236  using namespace Gecode;
237  IntVarArgs l(d*m);
238  for (int j=m*d; j--; )
239  l[j]=IntVar(home, 0, Gecode::Int::Limits::max);
240  binpacking(home, d, l, x, s, c);
241  }
242  };
243 
245  class CliqueMBPT : public Base {
246  protected:
248  int n_items;
252  class TestSpace : public Gecode::Space {
253  public:
254  // Constructor
255  TestSpace(void) {}
256  // Copy function
257  virtual Gecode::Space* copy(bool share) {
258  return NULL;
259  }
260  };
261  public:
264  : Base("Int::MultiBinPacking::Clique::"+Test::str(c)), clique(c) {}
266  virtual bool run(void) {
267  using namespace Gecode;
268  TestSpace* home = new TestSpace;
269  /*
270  * Set up a multi-dimensional bin packing problems of dimension 2
271  * where the item sizes in one dimension are all one but for some
272  * random items and two in the other dimension if the item is
273  * included in the clique and where the capacity in both dimensions
274  * is 3.
275  */
276  // Number of items
277  int n_items = clique[clique.size()-1] + 1;
278  // Capacity
279  IntArgs c(2, 3,3);
280  // Item sizes
281  IntArgs s(2*n_items);
282  for (int i=2*n_items; i--; )
283  s[i]=1;
284  // Create some random conflicts
285  for (int i=clique.size()-1; i--; )
286  s[rand(n_items)*2+0]=2;
287  // Create conflicts corresponding to the clique
288  for (int i=clique.size(); i--; )
289  s[clique[i]*2+1]=2;
290  // Load and bin variables
291  IntVarArgs b(*home, n_items, 0, n_items-1);
292  IntVarArgs l(*home, 2*n_items, 0, 3);
293  IntSet mc = binpacking(*home, 2, l, b, s, c);
294  if (home->status() == SS_FAILED) {
295  delete home;
296  return false;
297  }
298  if (clique.size() != mc.size()) {
299  delete home;
300  return false;
301  }
302  for (int i=clique.size(); i--; )
303  if (!mc.in(clique[i])) {
304  delete home;
305  return false;
306  }
307  delete home;
308  return true;
309  }
310  };
311 
313  class Create {
314  public:
316  Create(void) {
317  using namespace Gecode;
318 
319  {
320  IntArgs s1(3, 2,1,1);
321  IntArgs s2(4, 1,2,3,4);
322  IntArgs s3(4, 4,3,2,1);
323  IntArgs s4(4, 1,2,4,8);
324  IntArgs s5(4, 1,1,1,1);
325  IntArgs s6(4, 1,1,2,2);
326  IntArgs s7(4, 1,3,3,4);
327  IntArgs s8(6, 1,3,3,0,4,0);
328  IntArgs s9(6, 1,2,4,8,16,32);
329 
330  for (int m=1; m<4; m++) {
331  (void) new BPT(m, s1);
332  (void) new BPT(m, s2);
333  (void) new BPT(m, s3);
334  (void) new BPT(m, s4);
335  (void) new BPT(m, s5);
336  (void) new BPT(m, s6);
337  (void) new BPT(m, s7);
338  (void) new BPT(m, s8);
339  (void) new BPT(m, s9);
340  (void) new BPT(m, s1, false);
341  }
342  }
343 
344  {
345  IntArgs s1(2*4, 1,2, 2,1, 1,2, 2,1);
346  IntArgs c1(2, 3,3);
347  (void) new MBPT(2, 4, s1, c1);
348  (void) new MBPT(2, 6, s1, c1);
349  IntArgs s2(2*3, 1,1, 1,1, 1,1);
350  IntArgs c21(2, 1,1);
351  IntArgs c22(2, 2,2);
352  (void) new MBPT(2, 6, s2, c21);
353  (void) new MBPT(2, 6, s2, c22);
354  IntArgs s3(3*4, 1,2,3, 3,2,1, 2,1,3, 1,3,2);
355  IntArgs c31(3, 3,3,3);
356  IntArgs c32(3, 4,4,4);
357  IntArgs c33(3, 6,6,6);
358  (void) new MBPT(3, 4, s3, c31);
359  (void) new MBPT(3, 4, s3, c32);
360  (void) new MBPT(3, 4, s3, c33);
361  (void) new MBPT(3, 5, s3, c31);
362  (void) new MBPT(3, 5, s3, c32);
363  (void) new MBPT(3, 5, s3, c33);
364  }
365 
366  {
367  IntArgs c1(4, 0,2,4,6);
368  IntArgs c2(8, 1,2,3,4,5,6,7,8);
369  IntArgs c3(8, 1,3,7,10,15,22,27,97);
370  IntArgs c41(8, 1,2,3,4,5,6,7,14);
371  IntArgs c42(8, 1,2,3,4,5,6,7,15);
372  IntArgs c43(8, 1,2,3,4,5,6,7,16);
373  IntArgs c44(8, 1,2,3,4,5,6,7,30);
374  IntArgs c45(8, 1,2,3,4,5,6,7,31);
375  IntArgs c46(8, 1,2,3,4,5,6,7,32);
376  IntArgs c47(8, 1,2,3,4,5,6,7,62);
377  IntArgs c48(8, 1,2,3,4,5,6,7,63);
378  IntArgs c49(8, 1,2,3,4,5,6,7,64);
379 
380  (void) new CliqueMBPT(c1);
381  (void) new CliqueMBPT(c2);
382  (void) new CliqueMBPT(c3);
383  (void) new CliqueMBPT(c41);
384  (void) new CliqueMBPT(c42);
385  (void) new CliqueMBPT(c43);
386  (void) new CliqueMBPT(c44);
387  (void) new CliqueMBPT(c45);
388  (void) new CliqueMBPT(c46);
389  (void) new CliqueMBPT(c47);
390  (void) new CliqueMBPT(c48);
391  (void) new CliqueMBPT(c49);
392  }
393  }
394  };
395 
397 
399 
400  }
401 
402 }}
403 
404 
405 // STATISTICS: test-int
406 
int m
Number of bins.
virtual int operator[](int i) const
Return value for variable i.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
IntSet binpacking(Home home, int d, const IntVarArgs &l, const IntVarArgs &b, const IntArgs &s, const IntArgs &c, IntConLevel)
Definition: bin-packing.cpp:70
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
virtual bool solution(const Assignment &x) const
Test whether x is solution
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:140
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:138
int t
Total item sizes.
Create(void)
Perform creation and registration.
int m
Number of bins.
void init(const IntSet &s)
Initialize with values for s.
Definition: int-set-1.hpp:229
Gecode::IntArgs s
Item sizes.
Gecode::IntArgs c
Bin capacities.
bool valid
Whether to generate only valid loads.
Integer variable array.
Definition: int.hh:741
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: bin-packing.cpp:82
virtual ~LoadBinAssignment(void)
Destructor.
MBPT(int d0, int m0, const Gecode::IntArgs &s0, const Gecode::IntArgs &c0)
Create and register test for d0 dimensions, m0 bins, item sizes s0, and capacities c0...
Gecode::IntSet d_bin
Domain for bin variables.
Definition: bin-packing.cpp:63
const int max
Largest allowed integer value.
Definition: int.hh:113
virtual bool solution(const Assignment &x) const
Test whether x is solution
static int total(const Gecode::IntArgs &s)
Compute total size.
Computation spaces.
Definition: core.hpp:1362
virtual void operator++(void)
Move to next assignment.
Definition: bin-packing.cpp:86
int n
Number of variables.
Definition: int.hh:65
static std::string str(Gecode::ExtensionalPropKind epk)
Map extensional propagation kind to string.
Definition: int.hpp:212
Test with different bin loads and items
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::IntArgs clique
Expected clique.
Help class to create and register tests.
Gecode::IntSetValues * dsv
Iterator for each variable.
Definition: bin-packing.cpp:67
unsigned int size(I &i)
Size of all ranges of range iterator i.
Base class for all tests to be run
Definition: test.hh:107
Integer sets.
Definition: int.hh:171
CliqueMBPT(const Gecode::IntArgs &c)
Test for number of items n expected clique c.
unsigned int size(void) const
Return size (cardinality) of set.
Definition: int-set-1.hpp:161
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
Generate load and bin assignments.
Definition: bin-packing.cpp:54
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:232
const int v[7]
Definition: distinct.cpp:207
General test support.
Definition: afc.cpp:43
Example: Bin packing
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Test with different bin loads and items
Gecode::IntSet d_load
Domain for load variables.
Definition: bin-packing.cpp:61
bool testsearch
Whether to perform search test.
Definition: int.hh:230
Base class for assignments
Definition: int.hh:63
Integer variables.
Definition: int.hh:350
virtual Assignment * assignment(void) const
Create assignment.
Value iterator for integer sets.
Definition: int.hh:312
Test for testing the max-clique finding for multi bin-packing.
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
int il[8]
Array of sufficient size for computing item loads.
int val(void) const
Return current value.
virtual Gecode::Space * copy(bool share)
Copying member function.
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
BPT(int m0, const Gecode::IntArgs &s0, bool v=true)
Create and register test for m bins and item sizes s.
virtual bool run(void)
Run the actual test.
Gecode::IntArgs s
Item sizes.
Space is failed
Definition: core.hpp:1301
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
int size(void) const
Return number of variables.
Definition: int.hpp:50
LoadBinAssignment(int m, const Gecode::IntSet &d_m, int n, const Gecode::IntSet &d_n, int l)
Initialize assignments for load and bin variables.
Definition: bin-packing.cpp:70
int load
Load to generate (unless -1)
Definition: bin-packing.cpp:65