jagua_rs/probs/spp/entities/
instance.rs

1use crate::entities::{Container, Instance, Item};
2use crate::probs::spp::entities::Strip;
3use crate::probs::spp::util::assertions;
4use std::iter;
5
6#[derive(Debug, Clone)]
7/// Instance of the Strip Packing Problem.
8pub struct SPInstance {
9    /// The items to be packed and their demands
10    pub items: Vec<(Item, usize)>,
11    /// The strip in which to pack the items
12    pub base_strip: Strip,
13}
14
15impl SPInstance {
16    #[must_use]
17    pub fn new(items: Vec<(Item, usize)>, base_strip: Strip) -> Self {
18        assert!(
19            assertions::instance_item_ids_correct(&items),
20            "All items should have consecutive IDs starting from 0"
21        );
22
23        Self { items, base_strip }
24    }
25
26    #[allow(clippy::cast_precision_loss)]
27    #[must_use]
28    pub fn item_area(&self) -> f32 {
29        self.items
30            .iter()
31            .map(|(item, qty)| item.shape_orig.area() * *qty as f32)
32            .sum()
33    }
34
35    #[must_use]
36    pub fn item_qty(&self, id: usize) -> usize {
37        self.items[id].1
38    }
39
40    #[must_use]
41    pub fn total_item_qty(&self) -> usize {
42        self.items.iter().map(|(_, qty)| *qty).sum()
43    }
44}
45
46impl Instance for SPInstance {
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        iter::empty()
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        panic!("no predefined containers for strip packing instances")
61    }
62}