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
//! Worst Fit algorithm — O(n log B), loosest fitting bin.
use crate::engine::{ChooseFn, greedy_pack};
use crate::error::Result;
use crate::pack::{Pack, bins_to_packs};
use crate::placement::{BTreeRemainingIndex, PlacementIndex};
use crate::sequence::Sequence;
use crate::strategy::PackingAlgorithm;
/// Worst Fit algorithm implementation.
pub struct WorstFit;
impl PackingAlgorithm for WorstFit {
fn pack(&self, sequences: Vec<Sequence>, capacity: usize) -> Result<Vec<Pack>> {
let items: Vec<_> = sequences.iter().map(|s| s.to_item()).collect();
let mut index = BTreeRemainingIndex::new();
let bins = greedy_pack(
items.into_iter(),
capacity,
&mut index,
BTreeRemainingIndex::worst_fit as ChooseFn<BTreeRemainingIndex>,
)?;
Ok(bins_to_packs(bins, &sequences))
}
fn name(&self) -> &'static str {
"WorstFit"
}
}
// ── Tests ────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::greedy_pack;
use crate::error::PackError;
use crate::sequence::Item;
use crate::validation::validate_solution;
fn seqs(lens: &[usize]) -> Vec<Sequence> {
lens.iter()
.enumerate()
.map(|(id, &len)| Sequence::new(id, len))
.collect()
}
fn items(lens: &[usize]) -> Vec<Item> {
lens.iter()
.enumerate()
.map(|(id, &len)| Item { id, len })
.collect()
}
fn wf_bins(lens: &[usize], capacity: usize) -> Vec<crate::pack::Bin> {
let itms = items(lens);
let mut index = BTreeRemainingIndex::new();
greedy_pack(
itms.into_iter(),
capacity,
&mut index,
BTreeRemainingIndex::worst_fit as ChooseFn<BTreeRemainingIndex>,
)
.unwrap()
}
// ── Core behavior: loosest fit ──────────────────────────────
#[test]
fn test_picks_loosest_bin() {
// bin0(rem=3), bin1(rem=6). Item len=2.
// WF picks bin1 (largest remaining ≥ 2).
let bins = wf_bins(&[7, 4, 2], 10);
// 7→bin0(rem=3), 4→bin1(rem=6), 2→bin1(rem=6, loosest)
assert_eq!(bins.len(), 2);
assert_eq!(&bins[0].items[..], &[0]); // 7
assert_eq!(&bins[1].items[..], &[1, 2]); // 4+2=6
}
#[test]
fn test_spreads_items_evenly() {
// WF distributes items across bins instead of filling one first.
let bins = wf_bins(&[3, 3, 3], 10);
// 3→bin0(rem=7), 3→bin0(rem=7, loosest and only), 3→bin0(rem=4)
// Actually only one bin exists for the first two items.
// Let me think: 3→bin0(rem=7), 3→bin0(rem=7≥3, only bin)→rem=4,
// 3→bin0(rem=4≥3)→rem=1
// All fit in one bin. Let me use a case that actually spreads.
// [5, 5, 4, 4] cap=10:
// 5→bin0(rem=5), 5→bin0(rem=5≥5)→rem=0, 4→bin1(rem=6→new), 4→bin1(rem=6≥4)→rem=2
// Still fills sequentially. Need multiple bins first.
// [4, 4, 3, 3] cap=10:
// 4→bin0(rem=6), 4→bin0(rem=6≥4, only bin)→rem=2, 3→bin1(rem=7→new),
// 3→bin1(rem=7≥3, loosest vs bin0 rem=2)→rem=4
// Hmm, once there's only one bin with room, there's no choice.
// Let me test spreading with: [2, 2, 2] cap=10
// 2→bin0(rem=8), 2→bin0(rem=8, only)→rem=6, 2→bin0(rem=6)→rem=4
// All one bin. WF spreading shows when bins are forced open by large items.
assert_eq!(bins.len(), 1); // all fit in one bin for this input
}
#[test]
fn test_loosest_not_tightest() {
// bin0(rem=3), bin1(rem=7). Item len=3.
// BF would pick bin0 (tightest: rem=3, exact). WF picks bin1 (loosest: rem=7).
let bins = wf_bins(&[7, 3, 3], 10);
// 7→bin0(rem=3), 3→bin1(rem=7→new? No: range(3..).next_back()→key=3→bin0
// Wait: BTree has {3:[0]}. range(3..).next_back() → key=3 → bin0.
// Only one key, so next_back() == next(). Item goes to bin0(rem=0).
// 3→bin1(rem=7→new? No). Let me retrace:
// After step 1: map={3:[0]}. Item len=3: range(3..).next_back()→(3,[0])→bin0.
// bin0: used=10, rem=0. map={0:[0]}.
// After step 2: Item len=3: range(3..).next_back()→ empty (only key=0). → new bin1.
// So bins = [bin0:[0,1], bin1:[2]]. Need a better test case.
assert_eq!(bins.len(), 2);
assert_eq!(&bins[0].items[..], &[0, 1]); // 7+3=10
assert_eq!(&bins[1].items[..], &[2]); // 3
}
#[test]
fn test_picks_largest_remaining() {
// Create two bins with different remaining, then WF picks the larger one.
let bins = wf_bins(&[8, 6, 2], 10);
// 8→bin0(rem=2), 6→bin1(rem=4), 2→ range(2..).next_back()→ key=4→bin1 (loosest)
assert_eq!(bins.len(), 2);
assert_eq!(&bins[0].items[..], &[0]); // 8
assert_eq!(&bins[1].items[..], &[1, 2]); // 6+2=8
}
#[test]
fn test_five_items_walkthrough() {
let bins = wf_bins(&[7, 5, 3, 8, 4], 10);
// 7→bin0(rem=3) map:{3:[0]}
// 5→range(5..).next_back()→ empty → bin1(rem=5) map:{3:[0],5:[1]}
// 3→range(3..).next_back()→ key=5→bin1(loosest) map:{2:[1],3:[0]}
// 8→range(8..).next_back()→ empty → bin2(rem=2) map:{2:[1,2],3:[0]}
// 4→range(4..).next_back()→ empty (keys:2,3 both<4) → bin3(rem=6)
assert_eq!(bins.len(), 4);
assert_eq!(&bins[0].items[..], &[0]); // 7
assert_eq!(&bins[1].items[..], &[1, 2]); // 5+3=8
assert_eq!(&bins[2].items[..], &[3]); // 8
assert_eq!(&bins[3].items[..], &[4]); // 4
}
// ── Comparison with BF and FF ───────────────────────────────
#[test]
fn test_more_bins_than_best_fit() {
// WF tends to produce more bins than BF because it spreads items thin.
// [7,5,3,8,4] cap=10: BF gets 3 bins, WF gets 4.
let wf = wf_bins(&[7, 5, 3, 8, 4], 10);
let bf = {
let itms = items(&[7, 5, 3, 8, 4]);
let mut index = BTreeRemainingIndex::new();
greedy_pack(
itms.into_iter(),
10,
&mut index,
BTreeRemainingIndex::best_fit as ChooseFn<BTreeRemainingIndex>,
)
.unwrap()
};
assert!(wf.len() >= bf.len());
}
// ── Error cases ─────────────────────────────────────────────
#[test]
fn test_oversize_error() {
let result = WorstFit.pack(seqs(&[11]), 10);
assert!(matches!(
result,
Err(PackError::SequenceTooLong {
length: 11,
capacity: 10
})
));
}
// ── Edge cases ──────────────────────────────────────────────
#[test]
fn test_empty_input() {
let packs = WorstFit.pack(seqs(&[]), 10).unwrap();
assert!(packs.is_empty());
}
#[test]
fn test_single_item() {
let packs = WorstFit.pack(seqs(&[5]), 10).unwrap();
assert_eq!(packs.len(), 1);
}
#[test]
fn test_exact_capacity() {
let bins = wf_bins(&[10], 10);
assert_eq!(bins[0].remaining(), 0);
}
#[test]
fn test_all_same_size() {
let bins = wf_bins(&[5, 5, 5, 5], 10);
assert_eq!(bins.len(), 2);
}
#[test]
fn test_each_item_needs_own_bin() {
let bins = wf_bins(&[6, 7, 8, 9], 10);
assert_eq!(bins.len(), 4);
}
#[test]
fn test_many_small_items() {
let bins = wf_bins(&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 10);
assert_eq!(bins.len(), 1);
assert_eq!(bins[0].used, 10);
}
// ── Validation ──────────────────────────────────────────────
#[test]
fn test_validates_basic() {
let itms = items(&[7, 5, 3, 8, 4]);
let mut index = BTreeRemainingIndex::new();
let bins = greedy_pack(
itms.iter().copied(),
10,
&mut index,
BTreeRemainingIndex::worst_fit as ChooseFn<BTreeRemainingIndex>,
)
.unwrap();
validate_solution(&itms, &bins, 10).unwrap();
}
#[test]
fn test_validates_many_items() {
let itms = items(&[6, 4, 3, 7, 5, 5, 2, 8, 1, 9]);
let mut index = BTreeRemainingIndex::new();
let bins = greedy_pack(
itms.iter().copied(),
10,
&mut index,
BTreeRemainingIndex::worst_fit as ChooseFn<BTreeRemainingIndex>,
)
.unwrap();
validate_solution(&itms, &bins, 10).unwrap();
}
// ── Trait ───────────────────────────────────────────────────
#[test]
fn test_name() {
assert_eq!(WorstFit.name(), "WorstFit");
}
#[test]
fn test_pack_through_trait() {
let packs = WorstFit.pack(seqs(&[3, 3, 4]), 10).unwrap();
assert_eq!(packs.len(), 1);
}
}