jagua_rs/probs/bpp/entities/
problem.rs1use crate::Instant;
2use crate::entities::Instance;
3use crate::entities::Layout;
4use crate::entities::{PItemKey, PlacedItem};
5use crate::geometry::DTransformation;
6use crate::probs::bpp::entities::BPInstance;
7use crate::probs::bpp::entities::BPSolution;
8use crate::probs::bpp::util::assertions::problem_matches_solution;
9use itertools::Itertools;
10use slotmap::{SlotMap, new_key_type};
11
12new_key_type! {
13 pub struct LayKey;
15}
16
17#[derive(Clone)]
19pub struct BPProblem {
20 pub instance: BPInstance,
21 pub layouts: SlotMap<LayKey, Layout>,
22 pub item_demand_qtys: Vec<usize>,
23 pub bin_stock_qtys: Vec<usize>,
24}
25
26impl BPProblem {
27 pub fn new(instance: BPInstance) -> Self {
28 let item_demand_qtys = instance.items.iter().map(|(_, qty)| *qty).collect_vec();
29 let bin_stock_qtys = instance.bins.iter().map(|bin| bin.stock).collect_vec();
30
31 Self {
32 instance,
33 layouts: SlotMap::with_key(),
34 item_demand_qtys,
35 bin_stock_qtys,
36 }
37 }
38
39 pub fn remove_layout(&mut self, key: LayKey) {
41 self.deregister_layout(key);
42 }
43
44 pub fn place_item(&mut self, placement: BPPlacement) -> (LayKey, PItemKey) {
46 let lkey = match placement.layout_id {
47 BPLayoutType::Open(lkey) => lkey,
48 BPLayoutType::Closed { bin_id } => {
49 let bin = &self.instance.bins[bin_id];
51 let layout = Layout::new(bin.container.clone());
52 self.register_layout(layout)
53 }
54 };
55
56 let layout = &mut self.layouts[lkey];
57 let item = self.instance.item(placement.item_id);
58 let pik = layout.place_item(item, placement.d_transf);
59
60 self.register_included_item(placement.item_id);
61
62 (lkey, pik)
63 }
64
65 pub fn remove_item(&mut self, lkey: LayKey, pik: PItemKey) -> BPPlacement {
67 let pi = self.layouts[lkey].remove_item(pik);
68 self.deregister_included_item(pi.item_id);
69 if self.layouts[lkey].is_empty() {
70 let bin_id = self.layouts[lkey].container.id;
72 self.deregister_layout(lkey);
73 BPPlacement::from_placed_item(BPLayoutType::Closed { bin_id }, &pi)
74 } else {
75 BPPlacement::from_placed_item(BPLayoutType::Open(lkey), &pi)
76 }
77 }
78
79 pub fn save(&self) -> BPSolution {
81 let layout_snapshots = self
82 .layouts
83 .iter()
84 .map(|(lkey, l)| (lkey, l.save()))
85 .collect();
86
87 let solution = BPSolution {
88 layout_snapshots,
89 time_stamp: Instant::now(),
90 };
91
92 debug_assert!(problem_matches_solution(self, &solution));
93
94 solution
95 }
96
97 pub fn restore(&mut self, solution: &BPSolution) -> bool {
100 let mut layout_keys_changed = false;
101 let mut layouts_to_remove = vec![];
102
103 for (lkey, layout) in self.layouts.iter_mut() {
106 match solution.layout_snapshots.get(lkey) {
107 Some(ls) => match layout.container.id == ls.container.id {
108 true => layout.restore(ls),
109 false => layouts_to_remove.push(lkey),
110 },
111 None => {
112 layouts_to_remove.push(lkey);
113 }
114 }
115 }
116
117 for lkey in layouts_to_remove {
119 layout_keys_changed = true;
120 self.layouts.remove(lkey);
121 }
122
123 for (lkey, ls) in solution.layout_snapshots.iter() {
125 if !self.layouts.contains_key(lkey) {
126 self.layouts.insert(Layout::from_snapshot(ls));
127 layout_keys_changed = true;
128 }
129 }
130
131 {
133 self.item_demand_qtys
134 .iter_mut()
135 .enumerate()
136 .for_each(|(id, demand)| {
137 *demand = self.instance.item_qty(id);
138 });
139
140 self.bin_stock_qtys
141 .iter_mut()
142 .enumerate()
143 .for_each(|(id, stock)| {
144 *stock = self.instance.bin_qty(id);
145 });
146
147 self.layouts.values().for_each(|layout| {
148 self.bin_stock_qtys[layout.container.id] -= 1;
149 layout
150 .placed_items
151 .values()
152 .for_each(|pi| self.item_demand_qtys[pi.item_id] -= 1);
153 });
154 }
155
156 debug_assert!(problem_matches_solution(self, solution));
157 layout_keys_changed
158 }
159
160 pub fn density(&self) -> f32 {
161 let total_bin_area = self
162 .layouts
163 .values()
164 .map(|l| l.container.area())
165 .sum::<f32>();
166
167 let total_item_area = self
168 .layouts
169 .values()
170 .map(|l| l.placed_item_area(&self.instance))
171 .sum::<f32>();
172
173 total_item_area / total_bin_area
174 }
175
176 pub fn item_placed_qtys(&self) -> impl Iterator<Item = usize> {
177 self.item_demand_qtys
178 .iter()
179 .enumerate()
180 .map(|(i, demand)| self.instance.item_qty(i) - demand)
181 }
182
183 pub fn bin_used_qtys(&self) -> impl Iterator<Item = usize> {
184 self.bin_stock_qtys
185 .iter()
186 .enumerate()
187 .map(|(i, stock)| self.instance.bin_qty(i) - stock)
188 }
189
190 pub fn bin_cost(&self) -> u64 {
192 self.bin_used_qtys()
193 .enumerate()
194 .map(|(id, qty)| self.instance.bins[id].cost * qty as u64)
195 .sum()
196 }
197
198 fn register_layout(&mut self, layout: Layout) -> LayKey {
199 self.open_bin(layout.container.id);
200 layout
201 .placed_items
202 .values()
203 .for_each(|pi| self.register_included_item(pi.item_id));
204 self.layouts.insert(layout)
205 }
206
207 fn deregister_layout(&mut self, key: LayKey) {
208 let layout = self.layouts.remove(key).expect("layout key not present");
209 self.close_bin(layout.container.id);
210 layout
211 .placed_items
212 .values()
213 .for_each(|pi| self.deregister_included_item(pi.item_id));
214 }
215
216 fn register_included_item(&mut self, item_id: usize) {
217 self.item_demand_qtys[item_id] -= 1;
218 }
219
220 fn deregister_included_item(&mut self, item_id: usize) {
221 self.item_demand_qtys[item_id] += 1;
222 }
223
224 fn open_bin(&mut self, bin_id: usize) {
225 self.bin_stock_qtys[bin_id] -= 1
226 }
227
228 fn close_bin(&mut self, bin_id: usize) {
229 self.bin_stock_qtys[bin_id] += 1
230 }
231}
232
233#[derive(Clone, Debug, Copy)]
234pub struct BPPlacement {
236 pub layout_id: BPLayoutType,
238 pub item_id: usize,
240 pub d_transf: DTransformation,
242}
243
244impl BPPlacement {
245 pub fn from_placed_item(layout_id: BPLayoutType, placed_item: &PlacedItem) -> Self {
246 BPPlacement {
247 layout_id,
248 item_id: placed_item.item_id,
249 d_transf: placed_item.d_transf,
250 }
251 }
252}
253
254#[derive(Debug, Clone, Copy, PartialEq, Eq)]
256pub enum BPLayoutType {
257 Open(LayKey),
259 Closed { bin_id: usize },
261}