jagua_rs/probs/bpp/entities/
instance.rs

1use crate::entities::Instance;
2use crate::entities::{Container, Item};
3use crate::probs::bpp::entities::bin::Bin;
4use crate::probs::bpp::util::assertions::instance_item_bin_ids_correct;
5
6#[derive(Debug, Clone)]
7/// Instance of the Bin Packing Problem: a set of items to be packed into a set of bins.
8pub struct BPInstance {
9    /// The items to be packed and their demands
10    pub items: Vec<(Item, usize)>,
11    /// Set of bins available to pack the items
12    pub bins: Vec<Bin>,
13}
14
15impl BPInstance {
16    pub fn new(items: Vec<(Item, usize)>, bins: Vec<Bin>) -> Self {
17        assert!(instance_item_bin_ids_correct(&items, &bins));
18
19        Self { items, bins }
20    }
21
22    pub fn item_area(&self) -> f32 {
23        self.items
24            .iter()
25            .map(|(item, qty)| item.shape_orig.area() * *qty as f32)
26            .sum()
27    }
28
29    pub fn item_qty(&self, id: usize) -> usize {
30        self.items[id].1
31    }
32
33    pub fn bins(&self) -> impl Iterator<Item = &Bin> {
34        self.bins.iter()
35    }
36
37    pub fn bin_qty(&self, id: usize) -> usize {
38        self.bins[id].stock
39    }
40
41    pub fn total_item_qty(&self) -> usize {
42        self.items.iter().map(|(_, qty)| *qty).sum()
43    }
44}
45
46impl Instance for BPInstance {
47    fn items(&self) -> impl Iterator<Item = &Item> {
48        self.items.iter().map(|(item, _qty)| item)
49    }
50
51    fn containers(&self) -> impl Iterator<Item = &Container> {
52        self.bins.iter().map(|bin| &bin.container)
53    }
54
55    fn item(&self, id: usize) -> &Item {
56        &self.items.get(id).unwrap().0
57    }
58
59    fn container(&self, id: usize) -> &Container {
60        &self.bins[id].container
61    }
62}