jagua_rs/entities/
solution.rsuse std::time::Instant;
use itertools::Itertools;
use crate::entities::instances::instance::Instance;
use crate::entities::instances::instance_generic::InstanceGeneric;
use crate::entities::layout::LayoutSnapshot;
use crate::fsize;
use crate::geometry::geo_traits::Shape;
#[derive(Debug, Clone)]
pub struct Solution {
pub id: usize,
pub layout_snapshots: Vec<LayoutSnapshot>,
pub usage: fsize,
pub placed_item_qtys: Vec<usize>,
pub target_item_qtys: Vec<usize>,
pub bin_qtys: Vec<usize>,
pub time_stamp: Instant,
}
impl Solution {
pub fn new(
id: usize,
layout_snapshots: Vec<LayoutSnapshot>,
usage: fsize,
placed_item_qtys: Vec<usize>,
target_item_qtys: Vec<usize>,
bin_qtys: Vec<usize>,
) -> Self {
Solution {
id,
layout_snapshots,
usage,
placed_item_qtys,
target_item_qtys,
bin_qtys,
time_stamp: Instant::now(),
}
}
pub fn is_complete(&self, instance: &dyn InstanceGeneric) -> bool {
self.placed_item_qtys
.iter()
.enumerate()
.all(|(i, &qty)| qty >= instance.item_qty(i))
}
pub fn completeness(&self, instance: &Instance) -> fsize {
let total_item_area = instance.item_area();
let included_item_area = self
.placed_item_qtys
.iter()
.enumerate()
.map(|(i, qty)| instance.item(i).shape.area() * *qty as fsize)
.sum::<fsize>();
included_item_area / total_item_area
}
pub fn missing_item_qtys(&self, instance: &Instance) -> Vec<isize> {
debug_assert!(instance.items().len() == self.placed_item_qtys.len());
self.placed_item_qtys
.iter()
.enumerate()
.map(|(i, &qty)| instance.item_qty(i) as isize - qty as isize)
.collect_vec()
}
pub fn n_items_placed(&self) -> usize {
self.placed_item_qtys.iter().sum()
}
}