Skip to content

Commit

Permalink
polishing of ProblemGeneric trait
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 23, 2024
1 parent ff545a6 commit 96aaa2f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
14 changes: 6 additions & 8 deletions jagua-rs/src/entities/problems/bin_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::util::assertions;
#[derive(Clone)]
pub struct BPProblem {
pub instance: BPInstance,
layouts: Vec<Layout>,
pub layouts: Vec<Layout>,
template_layouts: Vec<Layout>,
missing_item_qtys: Vec<isize>,
bin_qtys: Vec<usize>,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl ProblemGeneric for BPProblem {

fn create_solution(&mut self, old_solution: &Option<Solution>) -> 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) => {
Expand Down Expand Up @@ -269,15 +269,13 @@ impl ProblemGeneric for BPProblem {
&self.missing_item_qtys
}

fn included_item_qtys(&self) -> Vec<usize> {
(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
}
}


Expand Down
13 changes: 7 additions & 6 deletions jagua-rs/src/entities/problems/problem.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -79,17 +80,17 @@ impl ProblemGeneric for Problem {
}
}

fn included_item_qtys(&self) -> Vec<usize> {
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(),
}
}
}
Expand Down
29 changes: 13 additions & 16 deletions jagua-rs/src/entities/problems/problem_generic.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Item=usize>{
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;
Expand All @@ -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<usize>;

/// Returns the `LayoutIndex` of all layouts.
fn layout_indices(&self) -> impl Iterator<Item=LayoutIndex> {
(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<Item=LayoutIndex> {
self.template_layouts().iter().enumerate().filter_map(|(i, l)| {
match self.bin_qtys()[l.bin().id] {
Expand All @@ -70,22 +76,13 @@ pub trait ProblemGeneric: ProblemGenericPrivate {
}
}

fn min_usage_layout_index(&mut self) -> Option<usize> {
(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 {
Expand Down
14 changes: 6 additions & 8 deletions jagua-rs/src/entities/problems/strip_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<isize>,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl ProblemGeneric for SPProblem {

fn create_solution(&mut self, _old_solution: &Option<Solution>) -> 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();
Expand Down Expand Up @@ -154,19 +154,17 @@ impl ProblemGeneric for SPProblem {
&self.missing_item_qtys
}

fn included_item_qtys(&self) -> Vec<usize> {
(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<Item=LayoutIndex> {
iter::empty::<LayoutIndex>()
}

fn bin_qtys(&self) -> &[usize] {
&[0]
}

fn instance(&self) -> &dyn InstanceGeneric {
&self.instance
}
}


Expand Down
2 changes: 1 addition & 1 deletion lbf/src/lbf_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>() >= ITEM_LIMIT {
if self.problem.placed_item_qtys().sum::<usize>() >= ITEM_LIMIT {
break 'outer;
}
}
Expand Down

0 comments on commit 96aaa2f

Please sign in to comment.