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    /// 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    pub fn density(&self, instance: &BPInstance) -> f32 {
18        let total_bin_area = self
19            .layout_snapshots
20            .values()
21            .map(|ls| ls.container.area())
22            .sum::<f32>();
23
24        let total_item_area = self
25            .layout_snapshots
26            .values()
27            .map(|ls| ls.placed_item_area(instance))
28            .sum::<f32>();
29
30        total_item_area / total_bin_area
31    }
32
33    pub fn cost(&self, instance: &BPInstance) -> u64 {
34        self.layout_snapshots
35            .values()
36            .map(|ls| ls.container.id)
37            .map(|id| instance.bins[id].cost)
38            .sum()
39    }
40}