HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

3D bin packing algorithm using Java?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
binpackingjavaalgorithmusing

Problem

I wrote a 3D bin packing algorithm but I am still not sure if it is correct or not.

I did not follow any code or pseudo-code that's why I would like to know if it is an efficient algorithm for the 3D bin packing problem or not.

each container has a length, height and breadth

each item has a length , height and breadth.

This is the code I wrote to pack items one by one without exceeding the container's length, height or breadth:

```
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=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[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();
remaining

Solution

Each of the putL, putB and putH methods is public without needing to be. A call to any of these methods can throw an exception if the put3D method hasn't been called before because the array haven't been initialised.
The call to init should be done in the constructor to avoid such side effects.

Having spaces before and after (conditional) operators will increase the readability. E.g this

if ((remainingHeight[j]!=0)&&(minRemH>=remainingHeight[j])&&(remainingHeight[j]>=item.getHeight())){


would be better like this

if ((remainingHeight[j] != 0) && (minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){

Code Snippets

if ((remainingHeight[j]!=0)&&(minRemH>=remainingHeight[j])&&(remainingHeight[j]>=item.getHeight())){
if ((remainingHeight[j] != 0) && (minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){

Context

StackExchange Code Review Q#124835, answer score: 2

Revisions (0)

No revisions yet.