diff --git a/jagua-rs/src/entities/problems/bin_packing.rs b/jagua-rs/src/entities/problems/bin_packing.rs index fa2b590..806707c 100644 --- a/jagua-rs/src/entities/problems/bin_packing.rs +++ b/jagua-rs/src/entities/problems/bin_packing.rs @@ -16,7 +16,7 @@ use crate::util::assertions; #[derive(Clone)] pub struct BPProblem { pub instance: BPInstance, - layouts: Vec, + pub layouts: Vec, template_layouts: Vec, missing_item_qtys: Vec, bin_qtys: Vec, @@ -154,7 +154,7 @@ impl ProblemGeneric for BPProblem { fn create_solution(&mut self, old_solution: &Option) -> Solution { let id = self.next_solution_id(); - let included_item_qtys = self.included_item_qtys(); + let included_item_qtys = self.placed_item_qtys().collect_vec(); let bin_qtys = self.bin_qtys().to_vec(); let layout_snapshots = match old_solution { Some(old_solution) => { @@ -269,15 +269,13 @@ impl ProblemGeneric for BPProblem { &self.missing_item_qtys } - fn included_item_qtys(&self) -> Vec { - (0..self.missing_item_qtys().len()) - .map(|i| (self.instance.item_qty(i) as isize - self.missing_item_qtys()[i]) as usize) - .collect_vec() - } - fn bin_qtys(&self) -> &[usize] { &self.bin_qtys } + + fn instance(&self) -> &dyn InstanceGeneric { + &self.instance + } } diff --git a/jagua-rs/src/entities/problems/problem.rs b/jagua-rs/src/entities/problems/problem.rs index 4c96910..74b3f87 100644 --- a/jagua-rs/src/entities/problems/problem.rs +++ b/jagua-rs/src/entities/problems/problem.rs @@ -1,3 +1,4 @@ +use crate::entities::instances::instance_generic::InstanceGeneric; use crate::entities::layout::Layout; use crate::entities::placed_item::PlacedItemUID; use crate::entities::placing_option::PlacingOption; @@ -79,17 +80,17 @@ impl ProblemGeneric for Problem { } } - fn included_item_qtys(&self) -> Vec { + fn bin_qtys(&self) -> &[usize] { match self { - Problem::BP(bp) => bp.included_item_qtys(), - Problem::SP(sp) => sp.included_item_qtys(), + Problem::BP(bp) => bp.bin_qtys(), + Problem::SP(sp) => sp.bin_qtys(), } } - fn bin_qtys(&self) -> &[usize] { + fn instance(&self) -> &dyn InstanceGeneric { match self { - Problem::BP(bp) => bp.bin_qtys(), - Problem::SP(sp) => sp.bin_qtys(), + Problem::BP(bp) => bp.instance(), + Problem::SP(sp) => sp.instance(), } } } diff --git a/jagua-rs/src/entities/problems/problem_generic.rs b/jagua-rs/src/entities/problems/problem_generic.rs index 6dbac5f..ca6dc45 100644 --- a/jagua-rs/src/entities/problems/problem_generic.rs +++ b/jagua-rs/src/entities/problems/problem_generic.rs @@ -1,4 +1,5 @@ use std::borrow::Borrow; +use crate::entities::instances::instance_generic::InstanceGeneric; use crate::entities::layout::Layout; use crate::entities::placed_item::PlacedItemUID; @@ -31,10 +32,15 @@ pub trait ProblemGeneric: ProblemGenericPrivate { /// When an item is placed in a template layout, it is cloned into a real layout. fn template_layouts(&self) -> &[Layout]; - /// The quantity of each item that is requested but currently missing in the problem instance. - /// Indexed by item id. + /// The quantity of each item that is requested but currently missing in the problem instance, indexed by item id. fn missing_item_qtys(&self) -> &[isize]; + /// The quantity of each item that is currently placed in the problem instance, indexed by item id. + fn placed_item_qtys(&self) -> impl Iterator{ + self.missing_item_qtys().iter().enumerate() + .map(|(i, missing_qty)| (self.instance().item_qty(i) as isize - missing_qty) as usize) + } + fn usage(&mut self) -> f64 { let (total_bin_area, total_used_area) = self.layouts_mut().iter_mut().fold((0.0, 0.0), |acc, l| { let bin_area = l.bin().area; @@ -44,16 +50,16 @@ pub trait ProblemGeneric: ProblemGenericPrivate { total_used_area / total_bin_area } - fn used_bin_value(&self) -> u64 { + fn used_bin_cost(&self) -> u64 { self.layouts().iter().map(|l| l.bin().value).sum() } - fn included_item_qtys(&self) -> Vec; - + /// Returns the `LayoutIndex` of all layouts. fn layout_indices(&self) -> impl Iterator { (0..self.layouts().len()).into_iter().map(|i| LayoutIndex::Real(i)) } + /// Returns the `LayoutIndex` of all template layouts that have remaining stock. fn template_layout_indices_with_stock(&self) -> impl Iterator { self.template_layouts().iter().enumerate().filter_map(|(i, l)| { match self.bin_qtys()[l.bin().id] { @@ -70,22 +76,13 @@ pub trait ProblemGeneric: ProblemGenericPrivate { } } - fn min_usage_layout_index(&mut self) -> Option { - (0..self.layouts().len()) - .into_iter() - .min_by(|&i, &j| - self.layouts_mut()[i].usage() - .partial_cmp( - &self.layouts_mut()[j].usage() - ).unwrap() - ) - } - fn bin_qtys(&self) -> &[usize]; fn flush_changes(&mut self) { self.layouts_mut().iter_mut().for_each(|l| l.flush_changes()); } + + fn instance(&self) -> &dyn InstanceGeneric; } pub(super) mod private { diff --git a/jagua-rs/src/entities/problems/strip_packing.rs b/jagua-rs/src/entities/problems/strip_packing.rs index 350581b..6474ae4 100644 --- a/jagua-rs/src/entities/problems/strip_packing.rs +++ b/jagua-rs/src/entities/problems/strip_packing.rs @@ -22,7 +22,7 @@ use crate::util::config::CDEConfig; #[derive(Clone)] pub struct SPProblem { pub instance: SPInstance, - layout: Layout, + pub layout: Layout, strip_height: f64, strip_width: f64, missing_item_qtys: Vec, @@ -116,7 +116,7 @@ impl ProblemGeneric for SPProblem { fn create_solution(&mut self, _old_solution: &Option) -> Solution { let id = self.next_solution_id(); - let included_item_qtys = self.included_item_qtys(); + let included_item_qtys = self.placed_item_qtys().collect_vec(); let bin_qtys = self.bin_qtys().to_vec(); let layout_snapshots = vec![self.layout.create_layout_snapshot()]; let target_item_qtys = self.instance.items.iter().map(|(_, qty)| *qty).collect_vec(); @@ -154,12 +154,6 @@ impl ProblemGeneric for SPProblem { &self.missing_item_qtys } - fn included_item_qtys(&self) -> Vec { - (0..self.missing_item_qtys().len()) - .map(|i| (self.instance.item_qty(i) as isize - self.missing_item_qtys()[i]) as usize) - .collect_vec() - } - fn template_layout_indices_with_stock(&self) -> impl Iterator { iter::empty::() } @@ -167,6 +161,10 @@ impl ProblemGeneric for SPProblem { fn bin_qtys(&self) -> &[usize] { &[0] } + + fn instance(&self) -> &dyn InstanceGeneric { + &self.instance + } } diff --git a/lbf/src/lbf_optimizer.rs b/lbf/src/lbf_optimizer.rs index 8e93391..dbc0c0f 100644 --- a/lbf/src/lbf_optimizer.rs +++ b/lbf/src/lbf_optimizer.rs @@ -87,7 +87,7 @@ impl LBFOptimizer { Some(i_opt) => { info!("Placing item {} at {}", i_opt.item_id, i_opt.d_transf); self.problem.place_item(&i_opt); - if self.problem.included_item_qtys().iter().sum::() >= ITEM_LIMIT { + if self.problem.placed_item_qtys().sum::() >= ITEM_LIMIT { break 'outer; } }