Skip to main content

jagua_rs/probs/bpp/entities/
solution.rs

1use crate::Instant;
2use crate::entities::LayoutSnapshot;
3use crate::probs::bpp::entities::{BPInstance, LayKey};
4use slotmap::SecondaryMap;
5
6/// Snapshot of [`BPProblem`](crate::probs::bpp::entities::BPProblem) at a specific moment.
7/// Can be used to restore to a previous state.
8#[derive(Debug, Clone)]
9pub struct BPSolution {
10    /// Layout snapshots keyed by their keys at capture time.
11    pub(crate) layout_snapshots: SecondaryMap<LayKey, LayoutSnapshot>,
12    /// Source bin index for each layout.
13    pub(crate) layout_bins: SecondaryMap<LayKey, usize>,
14    /// Instant the solution was created
15    pub time_stamp: Instant,
16}
17
18impl BPSolution {
19    #[must_use]
20    pub fn layout_snapshots(&self) -> &SecondaryMap<LayKey, LayoutSnapshot> {
21        &self.layout_snapshots
22    }
23
24    #[must_use]
25    pub fn layout_bins(&self) -> &SecondaryMap<LayKey, usize> {
26        &self.layout_bins
27    }
28
29    #[must_use]
30    pub fn density(&self) -> f32 {
31        let total_bin_area = self
32            .layout_snapshots
33            .values()
34            .map(|ls| ls.container.area())
35            .sum::<f32>();
36
37        let total_item_area = self
38            .layout_snapshots
39            .values()
40            .map(LayoutSnapshot::placed_item_area)
41            .sum::<f32>();
42
43        total_item_area / total_bin_area
44    }
45
46    #[must_use]
47    pub fn cost(&self, instance: &BPInstance) -> u64 {
48        self.layout_bins
49            .values()
50            .map(|idx| instance.bins[*idx].cost)
51            .sum()
52    }
53}