From ad06b11d3cdc857ae3ac304212766d1a665256c1 Mon Sep 17 00:00:00 2001 From: spsarras Date: Fri, 21 Jan 2022 16:12:08 +0000 Subject: [PATCH] exposing the seed on the use of random function --- Sharp3DBinPacking.RandomTest/Program.cs | 2 +- Sharp3DBinPacking/BinPackParameter.cs | 8 +++++--- Sharp3DBinPacking/BinPacker.cs | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Sharp3DBinPacking.RandomTest/Program.cs b/Sharp3DBinPacking.RandomTest/Program.cs index b3a1580..91bfb84 100644 --- a/Sharp3DBinPacking.RandomTest/Program.cs +++ b/Sharp3DBinPacking.RandomTest/Program.cs @@ -48,7 +48,7 @@ static Tuple TestBinPacker(IBinPacker binPacker) cuboids.Add(new Cuboid(width, height, depth, weight, null)); } var parameter = new BinPackParameter( - binWidth, binHeight, binDepth, binWeight, allowRotateVertically, cuboids); + binWidth, binHeight, binDepth, binWeight, allowRotateVertically, cuboids, 0); var result = binPacker.Pack(parameter); var volumeRate = BinPacker.GetVolumeRate(parameter, result.BestResult); return Tuple.Create(result, volumeRate); diff --git a/Sharp3DBinPacking/BinPackParameter.cs b/Sharp3DBinPacking/BinPackParameter.cs index 83abd23..0ee9ba7 100644 --- a/Sharp3DBinPacking/BinPackParameter.cs +++ b/Sharp3DBinPacking/BinPackParameter.cs @@ -13,14 +13,15 @@ public class BinPackParameter public bool AllowRotateVertically { get; private set; } public IEnumerable Cuboids { get; private set; } public int ShuffleCount { get; set; } + public int Seed { get; set; } public BinPackParameter( - decimal binWidth, decimal binHeight, decimal binDepth, IEnumerable cuboids) : - this(binWidth, binHeight, binDepth, 0, true, cuboids) { } + decimal binWidth, decimal binHeight, decimal binDepth, IEnumerable cuboids, int seed = 0) : + this(binWidth, binHeight, binDepth, 0, true, cuboids, seed) { } public BinPackParameter( decimal binWidth, decimal binHeight, decimal binDepth, decimal binWeight, - bool allowRotateVertically, IEnumerable cuboids) + bool allowRotateVertically, IEnumerable cuboids, int seed) { BinWidth = binWidth; BinHeight = binHeight; @@ -29,6 +30,7 @@ public BinPackParameter( AllowRotateVertically = allowRotateVertically; Cuboids = cuboids; ShuffleCount = 5; + Seed = seed; } } } diff --git a/Sharp3DBinPacking/BinPacker.cs b/Sharp3DBinPacking/BinPacker.cs index 86386e4..3a657bb 100644 --- a/Sharp3DBinPacking/BinPacker.cs +++ b/Sharp3DBinPacking/BinPacker.cs @@ -32,9 +32,10 @@ public BinPackResult Pack(BinPackParameter parameter) IList singleBestRemain = null; decimal singleBestVolumeRate = 0; string singleBestAlgorihm = null; - foreach (var factory in _factories) + for (int i = 0; i < _factories.Length; i++) { - foreach (var cuboids in GetCuboidsPermutations(pendingCuboids, parameter.ShuffleCount)) + BinPackAlgorithmFactory factory = _factories[i]; + foreach (var cuboids in GetCuboidsPermutations(pendingCuboids, parameter.ShuffleCount, parameter.Seed + i)) { var targetCuboids = cuboids.Select(c => c.CloneWithoutPlaceInformation()).ToList(); var algorithm = factory(parameter); @@ -152,14 +153,14 @@ public static decimal GetVolumeRate(BinPackParameter parameter, IList> GetCuboidsPermutations( - IEnumerable cuboids, int shuffleCount) + IEnumerable cuboids, int shuffleCount, int seed) { yield return cuboids; yield return cuboids.OrderByDescending(x => Math.Max(Math.Max(x.Width, x.Height), x.Depth)); yield return cuboids.OrderByDescending(x => x.Width * x.Height * x.Depth); if (shuffleCount > 0) { - var random = new Random(); + var random = new Random(seed); for (var x = 0; x < shuffleCount; ++x) { yield return cuboids.OrderBy(_ => random.Next(int.MaxValue));