-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streamlined how instance variants are handled (strip & bin packing)
- Loading branch information
Showing
14 changed files
with
203 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,85 @@ | ||
use enum_dispatch::enum_dispatch; | ||
use crate::entities::bin::Bin; | ||
use crate::entities::item::Item; | ||
use crate::geometry::geo_traits::Shape; | ||
use crate::util::assertions; | ||
|
||
/// Static representation of a problem instance. | ||
#[derive(Debug)] | ||
pub struct Instance { | ||
#[derive(Debug, Clone)] | ||
#[enum_dispatch] | ||
pub enum Instance { | ||
SP(SPInstance), | ||
BP(BPInstance), | ||
} | ||
|
||
#[enum_dispatch(Instance)] | ||
pub trait InstanceVariant { | ||
fn items(&self) -> &[(Item, usize)]; | ||
fn item_qty(&self, id: usize) -> usize{ | ||
self.items()[id].1 | ||
} | ||
fn item(&self, id: usize) -> &Item { | ||
&self.items()[id].0 | ||
} | ||
fn total_item_qty(&self) -> usize{ | ||
self.items().iter().map(|(_, qty)| qty).sum() | ||
} | ||
|
||
fn item_area(&self) -> f64; | ||
} | ||
|
||
|
||
#[derive(Debug, Clone)] | ||
pub struct BPInstance { | ||
/// Items to be packed in the instance, along with their requested quantities | ||
items: Vec<(Item, usize)>, | ||
pub items: Vec<(Item, usize)>, | ||
/// Total area of all items in the instance | ||
item_area: f64, | ||
packing_type: PackingType, | ||
pub item_area: f64, | ||
|
||
pub bins: Vec<(Bin, usize)>, | ||
} | ||
|
||
impl Instance { | ||
pub fn new(items: Vec<(Item, usize)>, packing_type: PackingType) -> Instance { | ||
assert!(assertions::instance_item_bin_ids_correct(&items, &packing_type)); | ||
#[derive(Debug, Clone)] | ||
pub struct SPInstance { | ||
pub items: Vec<(Item, usize)>, | ||
pub item_area: f64, | ||
pub strip_height: f64, | ||
} | ||
|
||
let item_area = items.iter().map(|(item, qty)| item.shape().area() * *qty as f64).sum(); | ||
impl BPInstance { | ||
pub fn new(items: Vec<(Item, usize)>, bins: Vec<(Bin, usize)>) -> Self { | ||
assert!(assertions::instance_item_bin_ids_correct(&items, &bins)); | ||
|
||
Instance { items, item_area, packing_type } | ||
} | ||
let item_area = items.iter().map(|(item, qty)| item.shape().area() * *qty as f64).sum(); | ||
|
||
pub fn bin(&self, id: usize) -> &Bin { | ||
match &self.packing_type { | ||
PackingType::BinPacking(bins) => &bins[id].0, | ||
PackingType::StripPacking { .. } => panic!("Instance is not a bin packing instance"), | ||
} | ||
Self { items, item_area, bins } | ||
} | ||
} | ||
|
||
pub fn items(&self) -> &Vec<(Item, usize)> { | ||
&self.items | ||
} | ||
impl SPInstance { | ||
pub fn new(items: Vec<(Item, usize)>, strip_height: f64) -> Self { | ||
let item_area = items.iter().map(|(item, qty)| item.shape().area() * *qty as f64).sum(); | ||
|
||
pub fn item_qty(&self, id: usize) -> usize { | ||
self.items[id].1 | ||
Self { items, item_area, strip_height } | ||
} | ||
} | ||
|
||
pub fn item(&self, id: usize) -> &Item { | ||
&self.items[id].0 | ||
impl InstanceVariant for BPInstance { | ||
fn items(&self) -> &[(Item, usize)] { | ||
&self.items | ||
} | ||
|
||
pub fn total_item_qty(&self) -> usize { | ||
self.items.iter().map(|(_, qty)| qty).sum() | ||
fn item_area(&self) -> f64 { | ||
self.item_area | ||
} | ||
} | ||
|
||
pub fn packing_type(&self) -> &PackingType { | ||
&self.packing_type | ||
impl InstanceVariant for SPInstance { | ||
fn items(&self) -> &[(Item, usize)] { | ||
&self.items | ||
} | ||
|
||
pub fn item_area(&self) -> f64 { | ||
fn item_area(&self) -> f64 { | ||
self.item_area | ||
} | ||
} | ||
|
||
|
||
//TODO: clean this up | ||
#[derive(Debug, Clone)] | ||
pub enum PackingType { | ||
BinPacking(Vec<(Bin, usize)>), | ||
StripPacking { height: f64 }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.