Skip to content

Commit

Permalink
fixed strip packing layout id mismatch when restoring to larger strip
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Apr 3, 2024
1 parent 8e09fac commit e93e115
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
10 changes: 5 additions & 5 deletions jagua-rs/src/entities/problems/bin_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ impl BPProblem {
}
}

fn next_layout_id(&mut self) -> usize {
self.layout_id_counter += 1;
self.layout_id_counter
}

fn reset_unmodified_layouts(&mut self, ref_solution_id: usize) {
self.unmodified_layout_ids = self.layouts.iter().map(|l| l.id()).collect();
self.unmodified_layouts_ref_solution = Some(ref_solution_id);
Expand Down Expand Up @@ -333,6 +328,11 @@ impl ProblemGenericPrivate for BPProblem {
self.solution_id_counter
}

fn next_layout_id(&mut self) -> usize {
self.layout_id_counter += 1;
self.layout_id_counter
}

fn missing_item_qtys_mut(&mut self) -> &mut [isize] {
&mut self.missing_item_qtys
}
Expand Down
7 changes: 7 additions & 0 deletions jagua-rs/src/entities/problems/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ impl ProblemGenericPrivate for Problem {
}
}

fn next_layout_id(&mut self) -> usize {
match self {
Problem::BP(bp) => bp.next_layout_id(),
Problem::SP(sp) => sp.next_layout_id(),
}
}

fn missing_item_qtys_mut(&mut self) -> &mut [isize] {
match self {
Problem::BP(bp) => bp.missing_item_qtys_mut(),
Expand Down
2 changes: 2 additions & 0 deletions jagua-rs/src/entities/problems/problem_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub(super) mod private {
pub trait ProblemGenericPrivate: Clone {
fn next_solution_id(&mut self) -> usize;

fn next_layout_id(&mut self) -> usize;

fn missing_item_qtys_mut(&mut self) -> &mut [isize];

fn register_included_item(&mut self, item_id: usize) {
Expand Down
27 changes: 18 additions & 9 deletions jagua-rs/src/entities/problems/strip_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::entities::instances::strip_packing::SPInstance;
use crate::entities::layout::Layout;
use crate::entities::placed_item::PlacedItemUID;
use crate::entities::placing_option::PlacingOption;
use crate::entities::problems::problem_generic::private::ProblemGenericPrivate;
use crate::entities::problems::problem_generic::LayoutIndex;
use crate::entities::problems::problem_generic::private::ProblemGenericPrivate;
use crate::entities::problems::problem_generic::ProblemGeneric;
use crate::entities::solution::Solution;
use crate::fsize;
Expand All @@ -25,9 +25,8 @@ use crate::util::fpa::FPA;
pub struct SPProblem {
pub instance: SPInstance,
pub layout: Layout,
pub strip_height: fsize,
pub strip_width: fsize,
missing_item_qtys: Vec<isize>,
layout_id_counter: usize,
solution_id_counter: usize,
}

Expand All @@ -46,9 +45,8 @@ impl SPProblem {
Self {
instance,
layout,
strip_height,
strip_width,
missing_item_qtys,
layout_id_counter: 0,
solution_id_counter: 0,
}
}
Expand Down Expand Up @@ -105,11 +103,9 @@ impl SPProblem {
.enumerate()
.for_each(|(i, qty)| *qty = self.instance.item_qty(i) as isize);

self.strip_width = rect.width();

//Modifying the width causes the bin to change, so the layout must be replaced
self.layout = Layout::new(
self.layout.id() + 1,
self.next_layout_id(),
Bin::from_strip(rect, self.layout.bin().base_cde.config().clone()),
);

Expand Down Expand Up @@ -162,6 +158,14 @@ impl SPProblem {
pub fn occupied_width(&self) -> fsize {
occupied_width(&self.layout)
}

pub fn strip_width(&self) -> fsize {
self.layout.bin().outer.bbox().width()
}

pub fn strip_height(&self) -> fsize {
self.layout.bin().outer.bbox().height()
}
}

impl ProblemGeneric for SPProblem {
Expand Down Expand Up @@ -257,7 +261,7 @@ impl ProblemGeneric for SPProblem {
&self.missing_item_qtys
}

fn template_layout_indices_with_stock(&self) -> impl Iterator<Item = LayoutIndex> {
fn template_layout_indices_with_stock(&self) -> impl Iterator<Item=LayoutIndex> {
iter::empty::<LayoutIndex>()
}

Expand All @@ -276,6 +280,11 @@ impl ProblemGenericPrivate for SPProblem {
self.solution_id_counter
}

fn next_layout_id(&mut self) -> usize {
self.layout_id_counter += 1;
self.layout_id_counter
}

fn missing_item_qtys_mut(&mut self) -> &mut [isize] {
&mut self.missing_item_qtys
}
Expand Down
4 changes: 2 additions & 2 deletions lbf/benches/hpg_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn hpg_query_bench(c: &mut Criterion) {
let mut problem = match instance.clone() {
Instance::BP(_) => panic!("Expected SPInstance"),
Instance::SP(instance) => {
SPProblem::new(instance, base_problem.strip_width, config.cde_config)
SPProblem::new(instance, base_problem.strip_width(), config.cde_config)
}
};
// Place the items in exactly the same way as the base problem
Expand Down Expand Up @@ -150,7 +150,7 @@ fn hpg_update_bench(c: &mut Criterion) {
let mut problem = match instance.clone() {
Instance::BP(_) => panic!("Expected SPInstance"),
Instance::SP(instance) => {
SPProblem::new(instance, base_problem.strip_width, config.cde_config)
SPProblem::new(instance, base_problem.strip_width(), config.cde_config)
}
};
// Place the items in exactly the same way as the base problem
Expand Down
7 changes: 5 additions & 2 deletions lbf/src/lbf_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl LBFOptimizer {
match &mut self.problem {
Problem::BP(_) => break,
Problem::SP(sp_problem) => {
let new_width = sp_problem.strip_width * 1.1;
let new_width = sp_problem.strip_width() * 1.1;
info!("[LBF] no placement found, extending strip width by 10% to {:.3}", new_width);
sp_problem.modify_strip_in_back(new_width);
}
Expand All @@ -117,7 +117,10 @@ impl LBFOptimizer {
Problem::BP(_) => {}
Problem::SP(sp_problem) => {
sp_problem.fit_strip();
info!("[LBF] fitted strip width to {:.3}", sp_problem.strip_width);
info!(
"[LBF] fitted strip width to {:.3}",
sp_problem.strip_width()
);
}
}

Expand Down

0 comments on commit e93e115

Please sign in to comment.