Hi everyone!
So I am implementing a
Java program that shows the best combination of boxes to fit into a container. Just similar to the knapsack problem but with boxes and a container instead of objects and a rucksack. To do this, I have implemented a genetic algorithm. First, I create "Individuals": each "Individual" is an array that can contain 0 or 1 depending on the absence or presence of each box.
Example: Individual1 = [1,0,0,1,1]
in this individual the boxes n.2 and 3 are absent.
Then i create some more individuals, put them into a population and create n new generations starting from the first population in order to find eventually a population which is better than the first one. Anyway, this is not the problem. My problem is to find a suitable algorithm to calculate the fitness of each individual.
Initially I implemented it in such a way that the best combination of boxes is the one with the highest number of boxes, without the total volume and weight of these boxes exceeding the volume and weight of the container. Let's suppose we have two individuals
individual1 [1,0,0,1,0]
individual2 [1,1,1,0,1]
both not exceeding the container's weight and volume: the best one is individual2 because it stores more boxes: its fitness will be higher that individual1's fitness.
Now I want to complicate the problem by also taking into account the shape of the boxes (for now I consider them to be two-dimensional, so I take into account the shape of the base of the boxes, without considering how tall they are: I therefore imagine a container of indefinite height into which all the boxes can fit by height).
So now each box has a weight, a volume (i used these constraints to calculate the simplier version of the fitness) and also a width and a height (the two sides of the box base).
This is my code:
Note that the first "else if()" exchanges the first side of the box with the second side of the box and the second side of the box with the first side of the box, in order to simulate a rotation of the box (since it may be possible that a box doesn't fit if it's placed in a certain position but could fit if rotated, hence I have to check both orientations).
What this code does:
- i have created an arraylist called remainingArea in which, at the beginning, there's only the whole area of the container. Then
- i consider the first box to place: i check if its dimension are smaller than the container dimension
- if so, i split the available area into two new available areas: you can see that from the link to the photo below.
- i remove the initial area from the arraylist and i add the two new areas, and then i continue with the next box.
Now i thought this was working fine, but then i found a problem. To understand this problem it would be really helpful to check this visual representation of the problem.
Image here:
As seen by the figure, the algorithm works fine when i add new boxes that do not overlap the two remaining planes created after putting into the container the previous box. If the next box overlap the two remaining planes, I would need to modify both the previous remaining areas, and this is where I'm stuck.
I have thought about a "fast" solution to avoid this problem: to sort the boxes by height, so that after putting the first box, all the remaining boxes will have a smaller height, so that no more box will ever overlap the two remaining areas (hope this is clear).
But the problem of the overlapping remaining areas is still there if the algorithm reaches the first "else if" where i rotate the box in order to make it fit into the container: here, by exchanging first and second side of the box, it may still be possible that the next box overlaps the two areas.
How can i avoid this problem?
Thanks in advance