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: a set of items to be packed into a single strip with a fixed height and variable width.
8pub struct SPInstance {
9    /// The items to be packed and their demands
10    pub items: Vec<(Item, usize)>,
11    /// The height of the strip (fixed)
12    pub base_strip: Strip,
13}
14
15impl SPInstance {
16    pub fn new(items: Vec<(Item, usize)>, base_strip: Strip) -> Self {
17        assert!(
18            assertions::instance_item_ids_correct(&items),
19            "All items should have consecutive IDs starting from 0"
20        );
21
22        Self { items, base_strip }
23    }
24
25    pub fn item_area(&self) -> f32 {
26        self.items
27            .iter()
28            .map(|(item, qty)| item.shape_orig.area() * *qty as f32)
29            .sum()
30    }
31
32    pub fn item_qty(&self, id: usize) -> usize {
33        self.items[id].1
34    }
35
36    pub fn total_item_qty(&self) -> usize {
37        self.items.iter().map(|(_, qty)| *qty).sum()
38    }
39}
40
41impl Instance for SPInstance {
42    fn items(&self) -> impl Iterator<Item = &Item> {
43        self.items.iter().map(|(item, _qty)| item)
44    }
45
46    fn containers(&self) -> impl Iterator<Item = &Container> {
47        iter::empty()
48    }
49
50    fn item(&self, id: usize) -> &Item {
51        &self.items.get(id).unwrap().0
52    }
53
54    fn container(&self, _id: usize) -> &Container {
55        panic!("no predefined containers for strip packing instances")
56    }
57}