Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sharp3DBinPacking.RandomTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static Tuple<BinPackResult, decimal> 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);
Expand Down
8 changes: 5 additions & 3 deletions Sharp3DBinPacking/BinPackParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public class BinPackParameter
public bool AllowRotateVertically { get; private set; }
public IEnumerable<Cuboid> Cuboids { get; private set; }
public int ShuffleCount { get; set; }
public int Seed { get; set; }

public BinPackParameter(
decimal binWidth, decimal binHeight, decimal binDepth, IEnumerable<Cuboid> cuboids) :
this(binWidth, binHeight, binDepth, 0, true, cuboids) { }
decimal binWidth, decimal binHeight, decimal binDepth, IEnumerable<Cuboid> 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<Cuboid> cuboids)
bool allowRotateVertically, IEnumerable<Cuboid> cuboids, int seed)
{
BinWidth = binWidth;
BinHeight = binHeight;
Expand All @@ -29,6 +30,7 @@ public BinPackParameter(
AllowRotateVertically = allowRotateVertically;
Cuboids = cuboids;
ShuffleCount = 5;
Seed = seed;
}
}
}
9 changes: 5 additions & 4 deletions Sharp3DBinPacking/BinPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public BinPackResult Pack(BinPackParameter parameter)
IList<Cuboid> 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);
Expand Down Expand Up @@ -152,14 +153,14 @@ public static decimal GetVolumeRate(BinPackParameter parameter, IList<IList<Cubo
}

public static IEnumerable<IEnumerable<Cuboid>> GetCuboidsPermutations(
IEnumerable<Cuboid> cuboids, int shuffleCount)
IEnumerable<Cuboid> 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));
Expand Down