我需要一个可以帮助我将多个包裹组合在一起的公式。
我有一个像这样的包裹对象:
<?php
class Parcel {
function __construct($length,$width,$height,$weight) {
$this->length=$length;
$this->width=$width;
$this->height=$height;
$this->weight=$weight;
}
private $length;
private $width;
private $height;
private $weight;
public function getParcelDetails()
{
echo "length=".$this->length."<br>";
echo "width =".$this->width."<br>";
echo "height=".$this->height."<br>";
echo "weight=".$this->weight ."<br>";
}
public static function mergeParcels($parcels){
$new_parcel_length=0;
$new_parcel_width=0;
$new_parcel_height=0;
$new_parcel_weight=0;
foreach ($parcels as $key => $parcel) {
# What is the formula that can create a new parcel which is enable to contain the parcels $parcels ?
# The weight will be just additionned
$new_parcel_weight+=$parcel->$weight;
}
$new_parcel= new static($new_parcel_length,$new_parcel_width,$new_parcel_height,$new_parcel_weight);
return $new_parcel;
}
}
目标是将parcel合并为一个,所以在主脚本中我会有这样的东西:
$parcel1=new Parcel(10,10,10,1);
$parcel2=new Parcel(5,5,5,1);
//$parcel1->getParcelDetails();
//$parcel2->getParcelDetails();
$new_parcel=Parcel::mergeParcels([$parcel1,$parcel2]);
$new_parcel->getParcelDetails();
请注意,问题是当我有多个尺寸不等于的包裹时。
如果尺寸等于,我可以将它们相互添加,但是当尺寸不相同时,我真的没有解决方案。
您遇到的编程问题称为knapsack packing problem,它是组合优化问题的一个子集。
使用动态编程可以解决小问题,使用启发式编程可以解决较大问题。
请注意,大规模组合求解器本身就是一个整个行业,需要多年的研发。