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    /// A map of the layout snapshots, identified by the same keys as in the problem
11    pub layout_snapshots: SecondaryMap<LayKey, LayoutSnapshot>,
12    /// Instant the solution was created
13    pub time_stamp: Instant,
14}
15
16impl BPSolution {
17    #[must_use]
18    pub fn density(&self, instance: &BPInstance) -> f32 {
19        let total_bin_area = self
20            .layout_snapshots
21            .values()
22            .map(|ls| ls.container.area())
23            .sum::<f32>();
24
25        let total_item_area = self
26            .layout_snapshots
27            .values()
28            .map(|ls| ls.placed_item_area(instance))
29            .sum::<f32>();
30
31        total_item_area / total_bin_area
32    }
33
34    #[must_use]
35    pub fn cost(&self, instance: &BPInstance) -> u64 {
36        self.layout_snapshots
37            .values()
38            .map(|ls| ls.container.id)
39            .map(|id| instance.bins[id].cost)
40            .sum()
41    }
42}