main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
int
bin-packing
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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11
* $Revision: 14967 $
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
*/
44
forceinline
45
Item::Item
(
void
)
46
: s(0) {}
47
forceinline
48
Item::Item
(
IntView
b
,
int
s0)
49
:
DerivedView
<
IntView
>(
b
), s(s0) {}
50
51
forceinline
IntView
52
Item::bin
(
void
)
const
{
53
return
x
;
54
}
55
forceinline
56
void
Item::bin
(
IntView
b
) {
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
*/
96
forceinline
97
SizeSet::SizeSet
(
void
) {}
98
forceinline
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
118
forceinline
119
SizeSetMinusOne::SizeSetMinusOne
(
void
) {}
120
forceinline
121
SizeSetMinusOne::SizeSetMinusOne
(
Region
& region,
int
n_max)
122
:
SizeSet
(region,n_max),
p
(-1) {}
123
forceinline
void
124
SizeSetMinusOne::minus
(
int
s0) {
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
137
SizeSetMinusOne::total
(
void
)
const
{
138
assert(
p
>= 0);
139
return
t
-
s
[
p
];
140
}
141
forceinline
int
142
SizeSetMinusOne::operator []
(
int
i
)
const
{
143
assert(
p
>= 0);
144
return
s
[(
i
<
p
) ?
i
:
i
+1];
145
}
146
147
148
149
/*
150
* Packing propagator
151
*
152
*/
153
154
forceinline
155
Pack::Pack
(
Home
home,
ViewArray<OffsetView>
& l0,
ViewArray<Item>
& bs0)
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
163
forceinline
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
171
Pack::dispose
(
Space
& home) {
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
forceinline
#define forceinline
Definition:
config.hpp:173
BinPacking
Example: Bin packing
Definition:
bin-packing.cpp:407
Gecode::Int::BinPacking::Item::s
int s
Size of item.
Definition:
bin-packing.hh:61
Gecode::Int::BinPacking::Pack::nosum
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
Gecode::Int::BinPacking::Pack::bs
ViewArray< Item > bs
Items with bin and size.
Definition:
bin-packing.hh:150
Gecode::Int::BinPacking::Pack::t
int t
Total size of all items.
Definition:
bin-packing.hh:152
Test::Int::Basic::i
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::Int::BinPacking::same
bool same(const Item &i, const Item &j)
Whether two items are the same.
Definition:
propagate.hpp:76
t
NodeType t
Type of node.
Definition:
bool-expr.cpp:234
Gecode::Space
Computation spaces.
Definition:
core.hpp:1748
Gecode::Int::BinPacking::SizeSet::n
int n
Number of size entries in the set.
Definition:
bin-packing.hh:94
Gecode::Int::BinPacking::Pack::Pack
Pack(Home home, ViewArray< OffsetView > &l, ViewArray< Item > &bs)
Constructor for posting.
Definition:
propagate.hpp:155
Gecode::Int::BinPacking::Item::update
void update(Space &home, bool share, Item &i)
Update item during cloning.
Definition:
propagate.hpp:69
Gecode::Int::BinPacking::Pack::l
ViewArray< OffsetView > l
Views for load of bins.
Definition:
bin-packing.hh:148
Gecode::Int::BinPacking::operator<
bool operator<(const Item &i, const Item &j)
For sorting according to size.
Definition:
propagate.hpp:87
Gecode::Int::BinPacking::Item::size
int size(void) const
Return size of item.
Definition:
propagate.hpp:60
Gecode::Int::BinPacking::SizeSet
Size sets.
Definition:
bin-packing.hh:91
Gecode::Int::BinPacking::SizeSetMinusOne::p
int p
Position of discarded item.
Definition:
bin-packing.hh:118
Gecode::DerivedView
Base-class for derived views.
Definition:
view.hpp:222
Gecode
Gecode toplevel namespace
Gecode::Propagator
Base-class for propagators.
Definition:
core.hpp:1092
Gecode::Int::BinPacking::SizeSet::SizeSet
SizeSet(void)
Default constructor.
Definition:
propagate.hpp:97
Gecode::Int::BinPacking::SizeSetMinusOne::card
int card(void) const
Return cardinality of set (number of entries)
Definition:
propagate.hpp:132
Gecode::Int::BinPacking::SizeSetMinusOne::minus
void minus(int s)
Discard size s.
Definition:
propagate.hpp:124
Gecode::Int::BinPacking::SizeSetMinusOne::SizeSetMinusOne
SizeSetMinusOne(void)
Default constructor.
Definition:
propagate.hpp:119
Gecode::Home
Home class for posting propagators
Definition:
core.hpp:922
a
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
b
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Gecode::Region
Handle to region.
Definition:
region.hpp:61
Gecode::Int::BinPacking::SizeSet::add
void add(int s)
Add new size s.
Definition:
propagate.hpp:102
Gecode::VarImpView::update
void update(Space &home, bool share, VarImpView< Var > &y)
Update this view to be a clone of view y.
Gecode::Int::BinPacking::SizeSet::card
int card(void) const
Return cardinality of set (number of entries)
Definition:
propagate.hpp:106
Gecode::Int::BinPacking::SizeSetMinusOne::operator[]
int operator[](int i) const
Return size of item i.
Definition:
propagate.hpp:142
Gecode::Int::BinPacking::Pack::dispose
virtual size_t dispose(Space &home)
Destructor.
Definition:
propagate.hpp:171
Gecode::Int::PC_INT_BND
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition:
var-type.hpp:91
Gecode::Int::BinPacking::before
bool before(const Item &i, const Item &j)
Test whether one item is before another.
Definition:
propagate.hpp:80
Gecode::Int::BinPacking::Item::Item
Item(void)
Default constructor.
Definition:
propagate.hpp:45
Gecode::DerivedView< IntView >::x
IntView x
View from which this view is derived.
Definition:
view.hpp:230
l
NNF * l
Left subtree.
Definition:
bool-expr.cpp:244
Gecode::Int::BinPacking::SizeSet::operator[]
int operator[](int i) const
Return size of item i.
Definition:
propagate.hpp:114
Gecode::Int::Count::shared
bool shared(const IntSet &, VX)
Definition:
view-base.hpp:122
Gecode::Int::BinPacking::Item::bin
IntView bin(void) const
Return bin of item.
Definition:
propagate.hpp:52
Gecode::Int::IntView
Integer view for integer variables.
Definition:
view.hpp:129
Gecode::Int::BinPacking::SizeSet::t
int t
Total size of the set.
Definition:
bin-packing.hh:96
Gecode::Int::BinPacking::SizeSetMinusOne::total
int total(void) const
Return total size.
Definition:
propagate.hpp:137
Gecode::Int::BinPacking::SizeSet::total
int total(void) const
Return total size.
Definition:
propagate.hpp:110
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition:
core.hpp:3354
Gecode::ViewArray
View arrays.
Definition:
array.hpp:234
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::Int::PC_INT_DOM
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition:
var-type.hpp:100
p
int p
Number of positive literals for node type.
Definition:
bool-expr.cpp:236
Gecode::Int::BinPacking::Pack
Bin-packing propagator.
Definition:
bin-packing.hh:145
Gecode::Int::BinPacking::SizeSet::s
int * s
Array of sizes (will have more elements)
Definition:
bin-packing.hh:98
Gecode::Int::BinPacking::Item
Item combining bin and size information.
Definition:
bin-packing.hh:57