I’m trying to create a class that will pack products into boxes.
There are different products and box sizes.
If more than one product then they needed to be fitted in a box that will hold both items. if a box is not big enough then the order must be split into a new box.
This problem is also called:
3D Bin Packer
The Knapsack Problem
I have not found any PHP examples. This is what I have got so far:
Box class
final class Box {
private $packaging_id;
private $length;
private $width;
private $height;
private $weight;
private $product = array();
function __construct($packaging_id, $length, $width, $height, $weight) {
$this->packaging_id = $packaging_id;
$this->length = $length;
$this->width = $width;
$this->height = $height;
}
function addProduct($product) {
$this->product[] = $product;
}
function check($product) {
$status = TRUE;
if ($product['width'] > $width) {
}
}
function getPrice() {
$price = 0;
foreach ($this->product as $product) {
$price += $product['price'];
}
return $price;
}
function getWeight() {
$weight = 0;
foreach ($this->product as $product) {
$weight += $product['weight'];
}
return $weight;
}
}
Packaging Class
final class Packaging {
private $box = array();
function __construct($product, $package) {
for ($i = 0; $i < count($product); $i++) {
$product[$i]['volume'] = $product[$i]['length'] * $product[$i]['width'] * $product[$i]['height'];
}
for ($i = 0; $i < count($package); $i++) {
$package[$i]['volume'] = $package[$i]['length'] * $package[$i]['width'] * $package[$i]['height'];
}
$sort_order = array();
foreach ($package as $key => $value) {
$sort_order[$key] = $value['volume'];
}
array_multisort($sort_order, SORT_ASC, $package);
foreach ($product as $key => $value) {
if (($package[$i]['volume'] > $product[$i]['volume']) && ($package[$i]['weight'] > $product[$i]['weight'])) {
$box[$k] = array(
'packaging_id' => $package[$i]['packaging_id'],
'length' => $package[$i]['length'],
'width' => $package[$i]['width'],
'height' => $package[$i]['height'],
'product' => $product[$i],
'volume' => $package[$i]['volume'] - $product[$i]['volume'],
'weight' => $package[$i]['weight'] - $product[$i]['weight']
);
$k++;
continue;
}
for ($j = 0; $j < count($package); $j++) {
if ($package[$i]['volume'] < $product['volume']) {
continue;
}
if ($package[$i]['weight'] < $product['weight']) {
continue;
}
}
}
$box = array();
$k = 0;
for ($i = 0; $i < count($product); $i++) {
if (!$box) {
for ($j = 0; $j < count($package); $j++) {
if ($package[$i]['volume'] < $product[$i]['volume']) {
continue;
}
if ($package[$i]['weight'] < $product[$i]['weight']) {
continue;
}
if (($package[$i]['volume'] > $product[$i]['volume']) && ($package[$i]['weight'] > $product[$i]['weight'])) {
$box[$k] = array(
'packaging_id' => $package[$i]['packaging_id'],
'length' => $package[$i]['length'],
'width' => $package[$i]['width'],
'height' => $package[$i]['height'],
'product' => $product[$i],
'remaining_volume' => $package[$i]['volume'] - $product[$i]['volume'],
'remaining_weight' => $package[$i]['weight'] - $product[$i]['weight']
);
$k++;
}
}
} else {
for ($j = 0; $j < count($box); $j++) {
}
}
}
}
function addItem($length, $width, $height, $weight, $price = 0) {
if ((float)$weight < 1.0) {
$weight = 1;
} else {
$weight = round($weight, 1);
}
$index = $this->items_qty;
$this->item[$index]['length'] = ($length ? (string)$length : 0);
$this->item[$index]['width'] = ($width ? (string)$width : 0);
$this->item[$index]['height'] = ($height ? (string)$height : 0);
$this->item[$index]['weight'] = ($weight ? (string)$weight : 0);
$this->item[$index]['price'] = $price;
$this->items_qty++;
}
}
Data
$product_data = array();
$product_data[] = array(
'product_id' => 1,
'name' => 'Product 1',
'quantity' => 2,
'price' => 10,
'length' => 12,
'width' => 6,
'height' => 6,
'weight' => 1,
'ready_to_ship' => false
);
$product_data[] = array(
'product_id' => 2,
'name' => 'Product 2',
'quantity' => 1,
'price' => 10,
'length' => 5,
'width' => 6,
'height' => 6,
'weight' => 1,
'ready_to_ship' => false
);
$product_data[] = array(
'product_id' => 3,
'name' => 'Product 3',
'quantity' => 1,
'price' => 10,
'length' => 9,
'width' => 14,
'height' => 12,
'weight' => 1,
'ready_to_ship' => false
);
$product_data[] = array(
'product_id' => 3,
'name' => 'Product 3',
'quantity' => 1,
'price' => 10,
'length' => 14,
'width' => 16,
'height' => 20,
'weight' => 1,
'ready_to_ship' => false
);
$package_data = array();
$package_data[] = array(
'packaging_id' => 1,
'name' => '9x8x9',
'description' => '9x8x9',
'length' => 9,
'width' => 8,
'height' => 9,
'weight' => 5
);
$package_data[] = array(
'packaging_id' => 2,
'name' => '12x14x13',
'description' => '12x14x13',
'length' => 12,
'width' => 14,
'height' => 13,
'weight' => 10,
);
$package_data[] = array(
'packaging_id' => 3,
'name' => '18x19x22',
'description' => '18x19x22',
'length' => 18,
'width' => 19,
'height' => 22,
'weight' => 25
);
$package_data[] = array(
'packaging_id' => 3,
'name' => '25x29x28',
'description' => '25x29x28',
'length' => 25,
'width' => 29,
'height' => 28,
'weight' => 25
);
$package_data[] = array(
'packaging_id' => 3,
'name' => '36x30x38',
'description' => '36x30x38',
'length' => 36,
'width' => 30,
'height' => 38,
'weight' => 25
);
error_reporting(E_ALL);
$packaging = new Packaging($product_data, $package_data);
echo '<pre>';
//print_r($package_data);
//print_r($empty_box_data);
print_r($packaging);
echo '</pre>';
Please help!