Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,
I know that I might be very wrong here as I don't understand Math well enough...
For several years I was working on and off on an Optimal Bin Packing Algorithm and was finally able to solve the problem (with approximation of course) without sorting or using combinatorics for more than the first two images.
So my question is if I'm not using combinatorics to solve the problem does it still perform in NP?
Here is a video I made while ago:
Experimenting With Optimal Bin Packing Algorithms Video1

And Here are the steps I use:
1. Get the smallest square based on the first two images
2. Find the best bin (that should be the one that the image will fill the most)
2.1 Measure the new square each time you're testing a bin.
3. Place the image in the given bin and redefine the square if necessary
4. Split bins that overlap with the image

What I have tried:

Here is part of the Photoshop automation code in JavaScript
JavaScript
function findBin(bins, image) {
        //Make two additional bins: one to the right and one to the bottom
        var x2 = rightBoundary + image.width();
        var y2 = bottomBoundary + image.height();

        //This assures there is at least one bin that can hold the image
        //Set the split flag to false as those bins should be splitted only if the image is placed in them
        //Sometimes placing the image in one of these bins gives a smaller square rather than the splittable bins
        var rightBin = { x1: rightBoundary, y1: 0, x2: x2, y2: y2, ind: 2, num: ++binNum, split: false, type: "right", from: "addRight" };
        var bottomBin = { x1: 0, y1: bottomBoundary, x2: x2, y2: y2, ind: 3, num: ++binNum, split: false, type: "bottom", from: "addBottom" };

        //Place the bins in the bins array as the last two
        bins.push(rightBin, bottomBin);

        //Set bestFit to false and find the first bin that can hold the image
        var bestFit = false;
        var fitNum = -1;

        while (bestFit === false) {
            fitNum++;
            bestFit = makeFit(bins[fitNum], image);
        }

        var fits = [bestFit];
        fits[0].ind = fitNum;

        for (var ind = fitNum + 1; ind < bins.length; ind++) {
            //Get the current bin
            var bin = bins[ind];

            //If the current bin/fit returns false, push the next one to the array of fits
            //Check if best fit holds the last one in the array, this prevents from not finding a best fit
            if (bestFit !== fits[fits.length - 1]) {
                bestFit = fits[fits.length - 1];
            }

            //Make the current bin as fit
            var fit = makeFit(bin, image);

            //If the fit is valid, compare to the best one
            if (fit !== false) {
                fit.ind = ind;

                //If the current fit forms a smaller square
                if (fit.square < bestFit.square) {
                    fits.push(fit);
                }

                //If the two squares are equal, compare to the smallest difference according to the two dimensions
                //It doesn't matter if the image exceeds the bin
                //The algotithm finds the bin that will be filled the most
                else if (fit.square === bestFit.square) {
                    if (fit.resultDimension < bestFit.resultDimension) {
                        fits.push(fit);
                    }

                    //If the two bins are going to be filled equally
                    //Find the one with the smallest x and y values as it will give a better result
                    else if (fit.resultDimension === bestFit.resultDimension) {
                        if (fit.y1 < bestFit.y1) {
                            fits.push(fit);
                        }
                        else {
                            if (fit.x1 < bestFit.x1) {
                                fits.push(fit);
                            }
                        }
                    }
                }
            }
        }

        bestFit = fits[fits.length - 1];
        var bestBin = bins[bestFit.ind];
        bestBin.square = bestFit.square;

        return bestBin;
    }
Posted
Updated 5-May-21 4:19am
v2
Comments
gggustafson 3-May-21 14:55pm    
I'm not sure that anyone here considers a code dump as a valid "What I have tried". I'd rephrase your question; point to the references on P vs NP that you used; and submit a fraction of your code that you think is appropriate.
Diliyan Vladimirov 3-May-21 15:22pm    
Hi, sorry I'm new to the forum. I'll reconstruct the question in the morning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900