jagua_rs/probs/bpp/entities/
solution.rs

1use crate::entities::LayoutSnapshot;
2use crate::probs::bpp::entities::{BPInstance, LayKey};
3use slotmap::SecondaryMap;
4use std::time::Instant;
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    pub layout_snapshots: SecondaryMap<LayKey, LayoutSnapshot>,
11    /// Instant the solution was created
12    pub time_stamp: Instant,
13}
14
15impl BPSolution {
16    pub fn density(&self, instance: &BPInstance) -> f32 {
17        let total_bin_area = self
18            .layout_snapshots
19            .values()
20            .map(|ls| ls.container.area())
21            .sum::<f32>();
22
23        let total_item_area = self
24            .layout_snapshots
25            .values()
26            .map(|ls| ls.placed_item_area(instance))
27            .sum::<f32>();
28
29        total_item_area / total_bin_area
30    }
31
32    pub fn cost(&self, instance: &BPInstance) -> u64 {
33        self.layout_snapshots
34            .values()
35            .map(|ls| ls.container.id)
36            .map(|id| instance.bins[id].cost)
37            .sum()
38    }
39}