我写了一个3D bin打包算法,但我仍然不确定它是否正确。
我没有遵循任何代码或伪代码,这就是为什么我想知道它是否是3D包装问题的有效算法。
每个容器都有长度,高度和宽度
每个项目都有长度,高度和宽度。
这是我写的代码逐个打包而不超过容器的长度,高度或广度的代码:
private double x,y,z=0;
private double[] remainingLength;
private double[] remainingHeight;
private double[] remainingBreadth;
//----initialize the remaining dimensions' arrays
public void init(int n) {
remainingLength=new double[n];
remainingHeight=new double[n];
remainingBreadth=new double[n];
for (int i=0; i<n; i++) {
remainingLength[i]=length;
remainingHeight[i]=height;
remainingBreadth[i]=breadth;
}
}
public boolean put3D(ItemsUnit item, int p,int n) {
init(n);
if(x<length){
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
if(y<breadth) {
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
if(z<height){
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false;
}
public boolean putL(ItemsUnit item, int p) {
//remaining dimensions arrays already initialized in the optimization algorithm
double minRemL=remainingLength[0];
int i=0;
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j]!=0)&&(minRemL>=remainingLength[j])&&(remainingLength[j]>=item.getLength())){
i=j; //choosing the item to which we should put the new packed item next to
minRemL=remainingLength[j]; //minimum length left
}else {
return false;
}
}
remainingLength[p]=remainingLength[i]-item.getLength();
remainingBreadth[p]-=item.getBreadth();
remainingHeight[p]-=item.getHeight();
remainingLength[i]=0;
x+=item.getLength(); //increment x
return true;
}
public boolean putB(ItemsUnit item, int p) {
//remaining dimensions arrays already initialized in the optimization algorithm
double minRemB=remainingBreadth[0];
int i=0;
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j]!=0)&&(minRemB>=remainingBreadth[j])&&(remainingBreadth[j]>=item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
else {
return false;
}
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth();
remainingHeight[p]-=item.getHeight();
remainingLength[p]-=item.getLength();
remainingBreadth[i]=0;
y+=item.getBreadth(); //increment y
return true;
}
public boolean putH(ItemsUnit item, int p) {
//remaining dimensions arrays already initialized in the optimization algorithm
double minRemH=remainingHeight[0];
int i=0;
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j]!=0)&&(minRemH>=remainingHeight[j])&&(remainingHeight[j]>=item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
else {
return false;
}
}
remainingHeight[p]=remainingHeight[i]-item.getHeight();
remainingBreadth[p]-=item.getBreadth();
remainingLength[p]-=item.getLength();
remainingHeight[i]=0;
z+=item.getHeight(); //increment z
return true;
}
我测试了算法并且它工作正常而没有超出容器的尺寸但是我不完全确定代码是否正确。
任何人都可以阅读代码并告诉我某处是否有问题或是否正确?