Skip to content

Commit

Permalink
placing an item in a problem now returns the LayoutIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 23, 2024
1 parent 6790675 commit 9892f2d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions jagua-rs/src/entities/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::util::assertions;
///It is a mutable representation, and can be modified by placing or removing items.
#[derive(Clone)]
pub struct Layout {
/// The unique identifier of the layout, only used for restoring from a snapshot.
id: usize,
bin: Bin,
placed_items: Vec<PlacedItem>,
Expand Down
31 changes: 18 additions & 13 deletions jagua-rs/src/entities/problems/bin_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::util::assertions;
pub struct BPProblem {
pub instance: BPInstance,
layouts: Vec<Layout>,
// TODO: document empty layouts
empty_layouts: Vec<Layout>,
missing_item_qtys: Vec<isize>,
bin_qtys: Vec<usize>,
Expand All @@ -30,9 +31,9 @@ pub struct BPProblem {
impl BPProblem {
pub fn new(instance: BPInstance) -> Self {
let missing_item_qtys = instance.items.iter().map(|(_, qty)| *qty as isize).collect_vec();
let bin_qtys = instance.bins.iter().map(|(_, qty)| *qty).collect_vec();
let bin_qtys = instance.bins.iter().map(|(_, qty)| *qty).collect_vec();
let layouts = vec![];
let empty_layouts = instance.bins.iter().enumerate().map(|(i, (bin, _))| {
let empty_layouts = instance.bins.iter().enumerate().map(|(i, (bin, _))| {
Layout::new(i, bin.clone())
}).collect_vec();
let layout_id_counter = empty_layouts.len();
Expand All @@ -58,14 +59,15 @@ impl BPProblem {
self.unregister_layout(layout_index);
}

pub fn register_layout(&mut self, layout: Layout) {
pub fn register_layout(&mut self, layout: Layout) -> LayoutIndex {
self.register_bin(layout.bin().id);
layout.placed_items().iter().for_each(
|p_i| {
self.register_included_item(p_i.item_id())
}
);
self.layouts.push(layout);
LayoutIndex::Existing(self.layouts.len() - 1)
}

pub fn unregister_layout(&mut self, layout_index: LayoutIndex) {
Expand Down Expand Up @@ -110,26 +112,29 @@ impl BPProblem {
}

impl ProblemGeneric for BPProblem {
fn place_item(&mut self, i_opt: &PlacingOption) {
let item_id = i_opt.item_id;
let layout = match &i_opt.layout_index {
LayoutIndex::Existing(i) => {
&mut self.layouts[*i]
}
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex {
let layout_index = match &i_opt.layout_index {
LayoutIndex::Existing(i) => LayoutIndex::Existing(*i),
LayoutIndex::Empty(i) => {
//Layout is empty, clone it and add it to `layouts`
let next_layout_id = self.next_layout_id();
let empty_layout = &self.empty_layouts[*i];
let copy = empty_layout.clone_with_id(next_layout_id);
self.register_layout(copy);
self.layouts.last_mut().unwrap()
self.register_layout(copy)
}
};
let item = self.instance.item(item_id);
let layout = match layout_index {
LayoutIndex::Existing(i) => &mut self.layouts[i],
LayoutIndex::Empty(_) => unreachable!("cannot place item in empty layout")
};
let item = self.instance.item(i_opt.item_id);
layout.place_item(item, &i_opt.d_transf);
let layout_id = layout.id();

self.register_included_item(item_id);
self.register_included_item(i_opt.item_id);
self.layout_has_changed(layout_id);

layout_index
}

fn remove_item(&mut self, layout_index: LayoutIndex, pi_uid: &PlacedItemUID, commit_instantly: bool) {
Expand Down
2 changes: 1 addition & 1 deletion jagua-rs/src/entities/problems/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum Problem {
}

impl ProblemGeneric for Problem {
fn place_item(&mut self, i_opt: &PlacingOption) {
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex{
match self {
Problem::BP(bp) => bp.place_item(i_opt),
Problem::SP(sp) => sp.place_item(i_opt),
Expand Down
3 changes: 2 additions & 1 deletion jagua-rs/src/entities/problems/problem_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::entities::solution::Solution;
pub trait ProblemGeneric: ProblemGenericPrivate {

/// Places an item into the problem instance according to the given `PlacingOption`.
fn place_item(&mut self, i_opt: &PlacingOption);
/// Returns the index of the layout where the item was placed.
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex;

/// Removes an item with a specific `PlacedItemUID` from a specific `Layout`
fn remove_item(&mut self, layout_index: LayoutIndex, pi_uid: &PlacedItemUID, commit_instantly: bool);
Expand Down
3 changes: 2 additions & 1 deletion jagua-rs/src/entities/problems/strip_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ impl SPProblem {
}

impl ProblemGeneric for SPProblem {
fn place_item(&mut self, i_opt: &PlacingOption) {
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex {
assert_eq!(i_opt.layout_index, LayoutIndex::Existing(0), "strip packing problems only have a single layout");
let item_id = i_opt.item_id;
let item = self.instance.item(item_id);
self.layout.place_item(item, &i_opt.d_transf);

self.register_included_item(item_id);
LayoutIndex::Existing(0)
}

fn remove_item(&mut self, layout_index: LayoutIndex, pi_uid: &PlacedItemUID, commit_instantly: bool) {
Expand Down

0 comments on commit 9892f2d

Please sign in to comment.