Generated on for Gecode by doxygen 1.17.0
bin-packing.hh
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.dev>
5 *
6 * Contributing authors:
7 * Fabio Tardivo <ftardivo@nmsu.edu>
8 * Stefano Gualandi <stefano.gualandi@gmail.com>
9 *
10 * Copyright:
11 * Fabio Tardivo, 2024
12 * Stefano Gualandi, 2013
13 * Christian Schulte, 2010
14 *
15 * This file is part of Gecode, the generic constraint
16 * development environment:
17 * http://www.gecode.dev
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39
40#ifndef GECODE_INT_BIN_PACKING_HH
41#define GECODE_INT_BIN_PACKING_HH
42
43#include <gecode/int.hh>
44
49
50namespace Gecode { namespace Int { namespace BinPacking {
51
55 class Item : public DerivedView<IntView> {
56 protected:
59 int s;
60 public:
62 Item(void);
64 Item(IntView b, int s);
65
67 IntView bin(void) const;
69 void bin(IntView b);
71 int size(void) const;
73 void size(int s);
74
76 void update(Space& home, Item& i);
77 };
78
80 bool operator ==(const Item& i, const Item& j);
82 bool operator !=(const Item& i, const Item& j);
83
85 bool operator <(const Item& i, const Item& j);
86
87
89 class SizeSet {
90 protected:
92 int n;
94 int t;
96 int* s;
97 public:
99 SizeSet(void);
101 SizeSet(Region& region, int n_max);
103 void add(int s);
105 int card(void) const;
107 int total(void) const;
109 int operator [](int i) const;
110 };
111
113 class SizeSetMinusOne : public SizeSet {
114 protected:
116 int p;
117 public:
119 SizeSetMinusOne(void);
121 SizeSetMinusOne(Region& region, int n);
123 void minus(int s);
125 int card(void) const;
127 int total(void) const;
129 int operator [](int i) const;
130 };
131
133 struct LambdaRange {
134 int min;
135 int max;
136 };
137
151 class Pack : public Propagator {
152 protected:
160 int t;
165 Pack(Space& home, Pack& p);
166 public:
169 static ExecStatus post(Home home,
173 template<class SizeSet>
174 bool nosum(const SizeSet& s, int a, int b, int& ap, int& bp);
176 template<class SizeSet>
177 bool nosum(const SizeSet& s, int a, int b);
183 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
186 virtual void reschedule(Space& home);
189 virtual Actor* copy(Space& home);
191 virtual size_t dispose(Space& home);
193 static int const n_reductions = 3;
196 int* weights_base_reduction,
197 int& capacity_base_reduction,
198 int* delta_reductions);
200 static int f_ccm1(int w, int l, int c);
201 static int f_mt(int w, int l, int c);
202 static int f_bj1(int w, int l, int c);
203 static int f_vb2_base(int w, int l, int c);
204 static int f_vb2(int w, int l, int c);
205 static int f_fs1(int w, int l, int c);
206 static int f_rad2_base(int w, int l, int c);
207 static int f_rad2(int w, int l, int c);
208 static int const n_lambda_samples = 256;
209 static LambdaRange l_ccm1(int c);
210 static LambdaRange l_mt(int c);
211 static LambdaRange l_bj1(int c);
212 static LambdaRange l_vb2(int c);
213 static LambdaRange l_fs1(int c);
214 static LambdaRange l_rad2(int c);
216 int n_weights, int max_weight);
218 template<int f(int,int,int)>
219 static int calc_dff_lower_bound_single_lambda(const int* weights,
220 int n_weights,
221 int capacity, int lambda);
222 template<int f(int,int,int), LambdaRange l(int)>
223 static int calc_dff_lower_bound(const int* weights, int n_weights,
224 int capacity, int n_not_zero_weights,
225 int max_weight, bool sanitize = false);
226 };
227
228
231 protected:
235 const IntVarArgs& b;
237 unsigned int bins;
239 int nodes(void) const;
240
243 public:
245 NodeSet(void);
247 NodeSet(Region& r, int n);
249 NodeSet(Region& r, int n, const NodeSet& ns);
251 void allocate(Region& r, int n);
253 void init(Region& r, int n);
255 bool in(int i) const;
257 void incl(int i);
259 void excl(int i);
261 void copy(int n, const NodeSet& ns);
263 void empty(int n);
270 static bool iwn(NodeSet& iwa, const NodeSet& a,
271 NodeSet& iwb, const NodeSet& b,
272 const NodeSet& c, int n);
273 };
274
276 class Node {
277 public:
281 unsigned int d;
283 unsigned int w;
285 Node(void);
286 };
287
289
291 class Nodes {
292 private:
294 const NodeSet& ns;
296 unsigned int c;
297 public:
299 Nodes(const NodeSet& ns);
301
302
303 void operator ++(void);
305
307
308
309 int operator ()(void) const;
311 };
312
314
315
316 class Clique {
317 public:
321 unsigned int c;
323 unsigned int w;
325 Clique(Region& r, int m);
327 void incl(int i, unsigned int w);
329 void excl(int i, unsigned int w);
330 };
331
333 int pivot(const NodeSet& a, const NodeSet& b) const;
338
340
341
346 ExecStatus clique(void);
348 ExecStatus clique(int i);
350 ExecStatus clique(int i, int j);
352 ExecStatus clique(int i, int j, int k);
354 public:
357 int m);
359 void edge(int i, int j, bool add=true);
361 bool adjacent(int i, int j) const;
363 ExecStatus post(void);
365 IntSet maxclique(void) const;
367 ~ConflictGraph(void);
368 };
369
370}}}
371
374
375#endif
376
377// STATISTICS: int-prop
Home class for posting propagators
Definition core.hpp:863
Integer sets.
Definition int.hh:178
Passing integer variables.
Definition int.hh:680
Clique(Region &r, int m)
Constructor for m nodes.
void excl(int i, unsigned int w)
Exclude node i with weight w.
void incl(int i, unsigned int w)
Include node i with weight w.
unsigned int c
Cardinality of clique.
void empty(int n)
Clear the whole node set for n nodes.
static bool iwn(NodeSet &iwa, const NodeSet &a, NodeSet &iwb, const NodeSet &b, const NodeSet &c, int n)
void init(Region &r, int n)
Initialize node set for n nodes.
bool in(int i) const
Test whether node i is included.
void allocate(Region &r, int n)
Allocate node set for n nodes.
void copy(int n, const NodeSet &ns)
Copy elements from node set ns with n nodes.
unsigned int w
Weight (initialized with degree before graph is reduced).
void operator++(void)
Move iterator to next node (if possible).
Nodes(const NodeSet &ns)
Initialize for nodes in ns.
int operator()(void) const
Return current node.
ExecStatus clique(void)
Report the current clique.
int nodes(void) const
Return number of nodes.
ExecStatus post(void)
Post additional constraints.
ConflictGraph(Home &home, Region &r, const IntVarArgs &b, int m)
Initialize graph.
int pivot(const NodeSet &a, const NodeSet &b) const
Find a pivot node with maximal degree from a or b.
bool adjacent(int i, int j) const
Test whether nodes i and j are adjacent.
Node * node
The nodes in the graph.
ExecStatus bk(NodeSet &p, NodeSet &x)
Run Bosch-Kerbron algorithm for finding max cliques.
unsigned int bins
Number of bins.
void edge(int i, int j, bool add=true)
Add or remove an edge between nodes i and j (i must be less than j).
const IntVarArgs & b
Bin variables.
Clique max
Largest clique so far.
IntSet maxclique(void) const
Return maximal clique found.
Item combining bin and size information.
IntView bin(void) const
Return bin of item.
Definition propagate.hpp:52
void update(Space &home, Item &i)
Update item during cloning.
Definition propagate.hpp:69
Item(void)
Default constructor.
Definition propagate.hpp:45
int size(void) const
Return size of item.
Definition propagate.hpp:60
static LambdaRange l_rad2(int c)
static int f_bj1(int w, int l, int c)
ViewArray< OffsetView > l
Views for load of bins.
static int calc_dff_lower_bound(const int *weights, int n_weights, int capacity, int n_not_zero_weights, int max_weight, bool sanitize=false)
IntPropLevel ipl
Propagation level.
static LambdaRange sanitize_lambda_range(LambdaRange lambda, int n_weights, int max_weight)
static int f_mt(int w, int l, int c)
ViewArray< Item > bs
Items with bin and size.
virtual void reschedule(Space &home)
Schedule function.
int t
Total size of all items.
static void calc_reductions(const ViewArray< Item > &bs, const ViewArray< OffsetView > &l, int *weights_base_reduction, int &capacity_base_reduction, int *delta_reductions)
static int f_rad2(int w, int l, int c)
static int calc_dff_lower_bound_single_lambda(const int *weights, int n_weights, int capacity, int lambda)
Lower bound.
static int const n_lambda_samples
static int const n_reductions
Reductions.
static LambdaRange l_ccm1(int c)
static int f_ccm1(int w, int l, int c)
Dual Feasible Functions.
static LambdaRange l_bj1(int c)
static ExecStatus post(Home home, ViewArray< OffsetView > &l, ViewArray< Item > &bs, IntPropLevel ipl=IPL_DEF)
Post propagator for loads l and items bs.
bool nosum(const SizeSet &s, int a, int b, int &ap, int &bp)
Detect non-existence of sums in a .. b.
static int f_vb2(int w, int l, int c)
static LambdaRange l_mt(int c)
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
virtual Actor * copy(Space &home)
Copy propagator during cloning.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
static int f_vb2_base(int w, int l, int c)
static LambdaRange l_vb2(int c)
Pack(Home home, ViewArray< OffsetView > &l, ViewArray< Item > &bs, IntPropLevel ipl)
Constructor for posting.
static int f_rad2_base(int w, int l, int c)
static LambdaRange l_fs1(int c)
static int f_fs1(int w, int l, int c)
virtual size_t dispose(Space &home)
Destructor.
int operator[](int i) const
Return size of item i.
void minus(int s)
Discard size s.
SizeSetMinusOne(void)
Default constructor.
int p
Position of discarded item.
int total(void) const
Return total size.
int card(void) const
Return cardinality of set (number of entries).
int t
Total size of the set.
SizeSet(void)
Default constructor.
Definition propagate.hpp:96
void add(int s)
Add new size s.
int total(void) const
Return total size.
int operator[](int i) const
Return size of item i.
int n
Number of size entries in the set.
int * s
Array of sizes (will have more elements).
int card(void) const
Return cardinality of set (number of entries).
Integer view for integer variables.
Definition view.hpp:129
Propagation cost.
Definition core.hpp:493
friend class Space
Definition core.hpp:1075
ModEventDelta med
A set of modification events (used during propagation).
Definition core.hpp:1084
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3607
Handle to region.
Definition region.hpp:55
Computation spaces.
Definition core.hpp:1775
Basic bitset support (without stored size information).
View arrays.
Definition array.hpp:255
#define GECODE_INT_EXPORT
Definition int.hh:85
int ModEventDelta
Modification event deltas.
Definition core.hpp:94
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:1008
@ IPL_DEF
Simple propagation levels.
Definition int.hh:1010
void weights(Home home, IntSharedArray elements, IntSharedArray weights, SetVar x, IntVar y)
Post propagator for .
Bin-packing propagators
bool operator<(const Item &i, const Item &j)
Order, also for sorting according to size.
Definition propagate.hpp:86
bool operator!=(const Item &i, const Item &j)
Whether two items are not the same.
Definition propagate.hpp:80
bool operator==(const Item &i, const Item &j)
Whether two items are the same.
Definition propagate.hpp:76
Finite domain integers.
Definition lastval.hh:52
Gecode toplevel namespace
ExecStatus
Definition core.hpp:479
const int capacity[n_warehouses]
Capacity of a single warehouse.