Example #1
0
        static Tuple <BinPackResult, decimal> TestBinPacker(IBinPacker binPacker)
        {
            var binWidth              = RandomInstance.Next(100, 5001);
            var binHeight             = RandomInstance.Next(100, 5001);
            var binDepth              = RandomInstance.Next(100, 5001);
            var binWeight             = RandomInstance.Next(100, 5001);
            var cuboidsCount          = RandomInstance.Next(50, 501);
            var allowRotateVertically = RandomInstance.Next(0, 2) == 0;
            var cuboids = new List <Cuboid>();

            for (var x = 0; x < cuboidsCount; ++x)
            {
                var width  = RandomInstance.Next(1, binWidth + 1);
                var height = RandomInstance.Next(1, binHeight + 1);
                var depth  = RandomInstance.Next(1, binDepth + 1);
                var weight = RandomInstance.Next(1, binWeight / 20 + 1);
                cuboids.Add(new Cuboid(width, height, depth, weight, null));
            }
            var parameter = new BinPackParameter(
                binWidth, binHeight, binDepth, binWeight, allowRotateVertically, cuboids);
            var result     = binPacker.Pack(parameter);
            var volumeRate = BinPacker.GetVolumeRate(parameter, result.BestResult);

            return(Tuple.Create(result, volumeRate));
        }
Example #2
0
        static void Main(string[] args)
        {
            // Define the size of bin
            var binWidth  = 1000;
            var binHeight = 1000;
            var binDepth  = 1000;
            // Define the cuboids to pack
            var parameter = new BinPackParameter(binWidth, binHeight, binDepth, new[]
            {
                new Cuboid(150, 100, 150),
                new Cuboid(500, 500, 500),
                new Cuboid(500, 550, 700),
                new Cuboid(350, 350, 350),
                new Cuboid(650, 750, 850),
            });
            // Create a bin packer instance
            // The default bin packer will test all algorithms and try to find the best result
            // BinPackerVerifyOption is used to avoid bugs, it will check whether the result is correct
            var binPacker = BinPacker.GetDefault(BinPackerVerifyOption.BestOnly);
            // The result contains bins which contains packed cuboids whith their coordinates
            var result = binPacker.Pack(parameter);

            foreach (var bins in result.BestResult)
            {
                Console.WriteLine("Bin:");
                foreach (var cuboid in bins)
                {
                    Console.WriteLine(cuboid);
                }
            }
        }
Example #3
0
 public BinPackGuillotineAlgorithm(
     BinPackParameter parameter,
     FreeCuboidChoiceHeuristic cuboidChoice,
     GuillotineSplitHeuristic splitMethod)
 {
     _parameter    = parameter;
     _cuboidChoice = cuboidChoice;
     _splitMethod  = splitMethod;
     _usedCuboids  = new List <Cuboid>();
     _freeCuboids  = new List <Cuboid>();
     AddFreeCuboid(new Cuboid(parameter.BinWidth, parameter.BinHeight, parameter.BinDepth));
 }
Example #4
0
 public BinPackShelfAlgorithm(
     BinPackParameter parameter,
     FreeRectChoiceHeuristic rectChoice,
     GuillotineSplitHeuristic splitMethod,
     ShelfChoiceHeuristic shelfChoice)
 {
     _parameter     = parameter;
     _rectChoice    = rectChoice;
     _splitMethod   = splitMethod;
     _shelfChoice   = shelfChoice;
     _currentY      = 0;
     _shelves       = new List <Shelf>();
     _packedCuboids = new List <Cuboid>();
     StartNewShelf(0);
 }
Example #5
0
        static BinPackResult TestBinPacker(IBinPacker binPacker)
        {
            var binWidth     = RandomInstance.Next(100, 5001);
            var binHeight    = RandomInstance.Next(100, 5001);
            var binDepth     = RandomInstance.Next(100, 5001);
            var cuboidsCount = RandomInstance.Next(50, 501);
            var cuboids      = new List <Cuboid>();

            for (var x = 0; x < cuboidsCount; ++x)
            {
                var width  = RandomInstance.Next(1, binWidth + 1);
                var height = RandomInstance.Next(1, binHeight + 1);
                var depth  = RandomInstance.Next(1, binDepth + 1);
                cuboids.Add(new Cuboid(width, height, depth));
            }
            var parameter = new BinPackParameter(binWidth, binHeight, binDepth, cuboids);

            return(binPacker.Pack(parameter));
        }
Example #6
0
        public List <HSolution> BuildSolutions(AnalysisHetero analysis)
        {
            // dim container + offset
            Vector3D dimContainer = analysis.DimContainer(0), offset = analysis.Offset(0);
            // content items
            List <ContentItem> contentItems = new List <ContentItem>(analysis.Content);
            // solutions
            List <HSolution> solutions = new List <HSolution>();

            // *** Sharp3DBinPacking : begin
            // create cuboid list
            List <Cuboid> listCuboids           = new List <Cuboid>();
            bool          bAllowAllOrientations = true;

            foreach (ContentItem ci in contentItems)
            {
                for (int i = 0; i < ci.Number; ++i)
                {
                    if (!ci.AllowOrientX && !ci.AllowOrientY && !ci.AllowOrientZ)
                    {
                        continue;
                    }
                    if (ci.Pack is BoxProperties b)
                    {
                        listCuboids.Add(
                            new Cuboid((decimal)b.Length, (decimal)b.Width, (decimal)b.Height)
                        {
                            Tag            = b
                            , AllowOrientX = ci.AllowOrientX
                            , AllowOrientY = ci.AllowOrientY
                            , AllowOrientZ = ci.AllowOrientZ
                        }
                            );
                    }
                }
                if (!ci.AllowOrientX || !ci.AllowOrientY || !ci.AllowOrientZ)
                {
                    bAllowAllOrientations = false;
                }
            }

            // Create a bin packer instance
            // The default bin packer will test all algorithms and try to find the best result
            // BinPackerVerifyOption is used to avoid bugs, it will check whether the result is correct
            var binPacker = BinPacker.GetDefault(BinPackerVerifyOption.BestOnly, bAllowAllOrientations);
            // The result contains bins which contains packed cuboids whith their coordinates
            var parameter = new BinPackParameter(
                (decimal)dimContainer.X, (decimal)dimContainer.Y, (decimal)dimContainer.Z,
                listCuboids.ToArray())
            {
            };

            var binPackResult = binPacker.Pack(parameter);
            {
                HSolution sol = new HSolution("Sharp3DBinPacking")
                {
                    Analysis = analysis
                };
                foreach (var bins in binPackResult.BestResult)
                {
                    HSolItem hSolItem = sol.CreateSolItem();
                    foreach (var cuboid in bins)
                    {
                        CuboidToSolItem(contentItems, offset, cuboid, out int index, out BoxPosition pos);
                        hSolItem.InsertContainedElt(index, pos);
                    }
                }
                solutions.Add(sol);
            }
            // *** Sharp3DBinPacking : end

            // *** BoxoLogic : begin
            List <BoxItem> listItems = new List <BoxItem>();

            foreach (ContentItem ci in contentItems)
            {
                for (int i = 0; i < ci.Number; ++i)
                {
                    if (ci.Pack is BoxProperties b)
                    {
                        listItems.Add(
                            new BoxItem()
                        {
                            ID     = BoxToID(b),
                            Boxx   = (decimal)b.Length,
                            Boxy   = (decimal)b.Width,
                            Boxz   = (decimal)b.Height,
                            AllowX = ci.AllowOrientX,
                            AllowY = ci.AllowOrientY,
                            AllowZ = ci.AllowOrientZ,
                            N      = 1
                        }
                            );
                    }
                }
            }
            var bl = new Boxlogic()
            {
                OutputFilePath = string.Empty
            };
            var solArray = new SolutionArray();

            bl.Run(listItems.ToArray(), (decimal)dimContainer.X, (decimal)dimContainer.Y, (decimal)dimContainer.Z, ref solArray);
            foreach (var solution in solArray.Solutions)
            {
                HSolution sol = new HSolution($"Boxologic - Variant {solution.Variant}")
                {
                    Analysis = analysis
                };
                HSolItem hSolItem = sol.CreateSolItem();

                Transform3D transform;
                switch (solution.Variant)
                {
                case 1: transform = Transform3D.Translation(new Vector3D(0.0, dimContainer.Y, 0.0)) * Transform3D.RotationX(90.0); break;

                case 2: transform = Transform3D.Translation(new Vector3D(dimContainer.X, 0.0, 0.0)) * Transform3D.RotationZ(90.0); break;

                case 3: transform = Transform3D.Translation(new Vector3D(dimContainer.X, 0.0, 0.0)) * Transform3D.RotationZ(90.0); break;

                case 4: transform = Transform3D.Translation(new Vector3D(dimContainer.X, 0.0, 0.0)) * Transform3D.RotationY(-90.0); break;

                case 5: transform = Transform3D.Translation(new Vector3D(0.0, dimContainer.Y, 0.0)) * Transform3D.RotationX(90.0); break;

                default: transform = Transform3D.Identity; break;
                }

                foreach (var item in solution.ItemsPacked)
                {
                    BoxInfoToSolItem(contentItems, offset, item, transform, out int index, out BoxPosition pos);
                    hSolItem.InsertContainedElt(index, pos.Adjusted(new Vector3D((double)item.DimX, (double)item.DimY, (double)item.DimZ)));
                }
                solutions.Add(sol);
            }
            // *** BoxoLogic : end

            return(solutions);
        }
Example #7
0
        public List <HSolution> BuildSolutions(HAnalysis analysis)
        {
            List <ContentItem> contentItems = new List <ContentItem>(analysis.Content);
            // *** Sharp3DBinPacking : begin
            // create cuboid list
            List <Cuboid> listCuboids           = new List <Cuboid>();
            bool          bAllowAllOrientations = true;

            foreach (ContentItem ci in contentItems)
            {
                for (int i = 0; i < ci.Number; ++i)
                {
                    if (ci.Pack is BoxProperties b)
                    {
                        listCuboids.Add(
                            new Cuboid((decimal)b.Length, (decimal)b.Width, (decimal)b.Height)
                        {
                            Tag          = b,
                            AllowOrientX = ci.AllowOrientX,
                            AllowOrientY = ci.AllowOrientY,
                            AllowOrientZ = ci.AllowOrientZ
                        }
                            );
                    }
                    if (!ci.AllowOrientX || !ci.AllowOrientY || !ci.AllowOrientZ)
                    {
                        bAllowAllOrientations = false;
                    }
                }
            }
            // dim container + offset
            Vector3D dimContainer = analysis.DimContainer(0), offset = analysis.Offset(0);

            // Create a bin packer instance
            // The default bin packer will test all algorithms and try to find the best result
            // BinPackerVerifyOption is used to avoid bugs, it will check whether the result is correct
            var binPacker = BinPacker.GetDefault(BinPackerVerifyOption.BestOnly, bAllowAllOrientations);
            // The result contains bins which contains packed cuboids whith their coordinates
            var parameter = new BinPackParameter(
                (decimal)dimContainer.X, (decimal)dimContainer.Y, (decimal)dimContainer.Z,
                listCuboids.ToArray())
            {
            };

            var binPackResult = binPacker.Pack(parameter);

            List <HSolution> solutions = new List <HSolution>();
            //foreach (var result in binPackResult.BestResult)
            //{
            HSolution sol = new HSolution("")
            {
                Analysis = analysis
            };

            foreach (var bins in binPackResult.BestResult)
            {
                HSolItem hSolItem = sol.CreateSolItem();
                foreach (var cuboid in bins)
                {
                    CuboidToSolItem(contentItems, offset, cuboid, out int index, out BoxPosition pos);
                    hSolItem.InsertContainedElt(index, pos);
                }
            }
            solutions.Add(sol);
            //}
            // *** Sharp3DBinPacking : end

            return(solutions);
        }
Example #8
0
        public List <HSolution> BuildSolutions(AnalysisHetero analysis)
        {
            // dim container + offset
            Vector3D dimContainer = analysis.DimContainer(0), offset = analysis.Offset(0);
            // content items
            List <ContentItem> contentItems = new List <ContentItem>(analysis.Content);
            // solutions
            List <HSolution> solutions = new List <HSolution>();

            // *** Sharp3DBinPacking : begin
            // create cuboid list
            List <Cuboid> listCuboids           = new List <Cuboid>();
            bool          bAllowAllOrientations = true;

            foreach (ContentItem ci in contentItems)
            {
                for (int i = 0; i < ci.Number; ++i)
                {
                    if (!ci.AllowOrientX && !ci.AllowOrientY && !ci.AllowOrientZ)
                    {
                        continue;
                    }
                    if (ci.Pack is BoxProperties b)
                    {
                        listCuboids.Add(
                            new Cuboid((decimal)b.Length, (decimal)b.Width, (decimal)b.Height)
                        {
                            Tag             = BoxToID(ci.Pack as BoxProperties)
                            , AllowOrientX  = ci.AllowOrientX
                            , AllowOrientY  = ci.AllowOrientY
                            , AllowOrientZ  = ci.AllowOrientZ
                            , PriorityLevel = ci.PriorityLevel
                        }
                            );
                    }
                }
                if (!ci.AllowOrientX || !ci.AllowOrientY || !ci.AllowOrientZ)
                {
                    bAllowAllOrientations = false;
                }
            }

            // Create a bin packer instance
            // The default bin packer will test all algorithms and try to find the best result
            // BinPackerVerifyOption is used to avoid bugs, it will check whether the result is correct
            var binPacker = BinPacker.GetDefault(BinPackerVerifyOption.BestOnly, bAllowAllOrientations);
            // The result contains bins which contains packed cuboids whith their coordinates
            var parameter = new BinPackParameter(
                (decimal)dimContainer.X, (decimal)dimContainer.Y, (decimal)dimContainer.Z,
                listCuboids.ToArray())
            {
                ShuffleCount = 0
            };

            var binPackResult = binPacker.Pack(parameter);
            {
                HSolution sol = new HSolution("Sharp3DBinPacking")
                {
                    Analysis = analysis
                };
                foreach (var bins in binPackResult.BestResult)
                {
                    HSolItem hSolItem = sol.CreateSolItem();
                    foreach (var cuboid in bins)
                    {
                        CuboidToSolItem(contentItems, offset, cuboid, out int index, out BoxPosition pos);
                        hSolItem.InsertContainedElt(index, pos);
                    }
                }
                solutions.Add(sol);
            }
            // *** Sharp3DBinPacking : end

            // *** BoxoLogic : begin

            bool singleSol = true;

            for (int variant = singleSol? 5 : 1; variant < 6; ++variant)
            {
                HSolution sol = new HSolution($"Boxologic - Variant {variant}")
                {
                    Analysis = analysis
                };
                RunBoxologic(variant, sol, dimContainer, offset, contentItems);
                solutions.Add(sol);
            } // for
            // *** BoxoLogic : end
            return(solutions);
        }