-
Notifications
You must be signed in to change notification settings - Fork 3
/
first-fit.cpp
288 lines (227 loc) · 7.65 KB
/
first-fit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*!
@file first-fit.cpp
@brief Contains "First-Fit" and "First-Fit-Decreasing" heuristics.
@author Bastian Rieck
*/
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include "bin-packing.h"
/*!
Applies the "First-Fit" heuristic to the current problem. Worst-case
running time is O(n^2).
@param objects Array of object sizes
@param positions Array that will contain the associations for the
objects. The entry in position i signifies the bin
that contains object i.
@param time Variable that will be filled with the elapsed time
after the bins haven been sorted.
@return Number of bins opened by the heuristic.
*/
unsigned int first_fit(const unsigned int* objects, unsigned int* positions, double& time)
{
unsigned int num_open_bins = 1;
unsigned int num_full_bins = 0;
unsigned int* bins = new unsigned int[n];
memset(bins, 0, n*sizeof(unsigned int));
memset(positions, 0, n*sizeof(unsigned int));
// This variable is set per object. It is the minimum capacity that is
// required in order to fit the object into a certain bin.
unsigned int required_capacity;
clock_t start = clock();
for(unsigned int i = 0; i < n; i++)
{
required_capacity = K-objects[i];
for(unsigned int j = 0; j < num_open_bins; j++)
{
if(bins[j] <= required_capacity)
{
bins[j] += objects[i];
positions[i] = j+1;
break;
}
}
// Object could not be placed--create a new bin and put it in
// there
if(positions[i] == 0)
{
bins[num_open_bins] = objects[i];
positions[i] = (++num_open_bins);
}
}
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
delete[] bins;
return(num_open_bins+num_full_bins);
}
/*
Applies the "First-Fit-Decreasing" heuristic to the current problem.
Worst-case running time is, not taking into account sorting with O(n
log n), O(n^2) because the "First-Fit" heuristic is called.
*/
unsigned int first_fit_decreasing(const unsigned int* objects, unsigned int* positions, double& time)
{
unsigned int* sorted_objects = new unsigned int[n];
memcpy(sorted_objects, objects, n*sizeof(unsigned int));
unsigned int num_bins;
clock_t start = clock();
qsort(sorted_objects, n, sizeof(unsigned int), compare_uints);
num_bins = first_fit(sorted_objects, positions, time);
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
delete[] sorted_objects;
return(num_bins);
}
/*!
Implementation of First-Fit heuristic using STL vectors. This allows us
to remove bins that are almost full without disturbing the order of
other elements. The positions array is not used in this implementation.
*/
unsigned int first_fit_vec(const unsigned int* objects, double& time)
{
unsigned int num_open_bins = 1;
unsigned int num_full_bins = 0;
std::vector<unsigned int> bins;
bins.push_back(0);
// If the bin is filled to more than (K-min_size), no object will fit
// anymore. Hence, the bin is removed and treated as if it was full.
unsigned int limit_capacity = K-min_size;
// This variable is set per object. It is the minimum capacity that is
// required in order to fit the object into a certain bin.
unsigned int required_capacity;
// Flag that signals whether the current object could be placed in an
// existing bin or not.
bool placed;
std::vector<unsigned int>::iterator last_bin;
std::vector<unsigned int>::iterator bin;
clock_t start = clock();
for(unsigned int i = 0; i < n; i++)
{
required_capacity = K-objects[i];
placed = false;
// The previous object was smaller and could _not_ be placed in
// a bin with smaller index. It follows that we can skip the
// other bins.
if(i >= 1 && objects[i] >= objects[i-1])
bin = last_bin;
else
bin = bins.begin();
for(; bin != bins.end(); bin++)
{
if(*bin <= required_capacity)
{
*bin += objects[i];
last_bin = bin;
if(*bin > limit_capacity)
{
num_open_bins--;
num_full_bins++;
bins.erase(bin);
}
placed = true;
break;
}
}
if(!placed)
{
bins.push_back(objects[i]);
num_open_bins++;
last_bin = bins.end()-1;
}
}
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
return(num_open_bins+num_full_bins);
}
/*!
Applies the "First-Fit-Decreasing" heuristic to the current problem
using the STL vector class. Since first_fit_vec is used, no positions
array is required.
*/
unsigned int first_fit_decreasing_vec( const unsigned int* objects,
double& time,
void(*sort)(void*, size_t, size_t, int (*)(const void*, const void*)))
{
unsigned int* sorted_objects = new unsigned int[n];
memcpy(sorted_objects, objects, n*sizeof(unsigned int));
unsigned int num_bins;
clock_t start = clock();
sort(sorted_objects, n, sizeof(unsigned int), compare_uints);
num_bins = first_fit_vec(sorted_objects, time);
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
delete[] sorted_objects;
return(num_bins);
}
/*!
Implementation of First-Fit heuristic using STL maps, thus speeding up
the process of looking for a suitable bin. The positions array is not
used in this implementation.
*/
unsigned int first_fit_map(const unsigned int* objects, double& time)
{
unsigned int num_open_bins = 1;
unsigned int num_full_bins = 0;
unsigned int* bins = new unsigned int[n];
memset(bins, 0, n*sizeof(unsigned int));
// The map associates an object weight to an index in the bin array.
// The index signifies the minimum index at which an object of the
// current weight could be placed the last time. All bins with smaller
// index need not be checked.
std::map<unsigned int, unsigned int> bin_map;
// This variable is set per object. It is the minimum capacity that is
// required in order to fit the object into a certain bin.
unsigned int required_capacity;
// Flag that signals whether the current object could be placed in an
// existing bin or not.
bool placed;
clock_t start = clock();
for(unsigned int i = 0; i < n; i++)
{
placed = false;
required_capacity = K-objects[i];
for(unsigned int j = bin_map[objects[i]]; j < num_open_bins; j++)
{
if(bins[j] <= required_capacity)
{
bins[j] += objects[i];
bin_map[objects[i]] = j;
placed = true;
break;
}
}
// Object could not be placed--create a new bin and put it in
// there
if(!placed)
{
bins[num_open_bins] = objects[i];
bin_map[objects[i]] = num_open_bins++;
}
}
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
delete[] bins;
return(num_open_bins+num_full_bins);
}
/*!
Applies the "First-Fit-Decreasing" heuristic to the current problem
using the STL map class.
*/
unsigned int first_fit_decreasing_map( const unsigned int* objects,
double& time,
void(*sort)(void*, size_t, size_t, int (*)(const void*, const void*)))
{
unsigned int* sorted_objects = new unsigned int[n];
memcpy(sorted_objects, objects, n*sizeof(unsigned int));
unsigned int num_bins;
clock_t start = clock();
sort(sorted_objects, n, sizeof(unsigned int), compare_uints);
num_bins = first_fit_map(sorted_objects, time);
clock_t end = clock();
time = (end-start)/static_cast<double>(CLOCKS_PER_SEC);
delete[] sorted_objects;
return(num_bins);
}