Beispiel #1
0
 private static Bin[] InitializeBins(int numberOfBins, int numberOfBytes)
 {
     var bins = new Bin[numberOfBins];
     for (int i = 0; i < numberOfBins; i++)
     {
         bins[i] = new Bin(numberOfBytes);
     }
     return bins;
 }
Beispiel #2
0
        private static Bin FindLeastFilledBin(Bin[] bins, double itemSize)
        {
            if (bins == null || bins.Length < 1)
                throw new ArgumentException("The bins array may not be null or empty", "bins");

            var candidate = bins.OrderBy(x => x.Fill).First();

            if (candidate.Fill + itemSize > candidate.Size)
                throw new Exception("There are not enough bins to hold all the items");

            return candidate;
        }
Beispiel #3
0
        /// <summary>
        /// Longest Processing Time algorith (from scheduling theory):
        /// Sort the items by their processing time and then assigns them to the bin with the lowest fill level so far
        /// </summary>
        private static void AllocateItemsToBins(Bin[] bins, List<Item> items)
        {
            if (bins == null || bins.Length < 1)
                throw new ArgumentException("The bins array may not be null or empty", "bins");
            if(items == null)
                throw new ArgumentException("The list of items cannot be uninitialized", "sortedItems");

            List<Item> sortedItems = items.OrderByDescending(x => x.Size).ToList();

            foreach (var item in sortedItems)
            {
                Bin bin = FindLeastFilledBin(bins, item.Size);
                bin.Fill += item.Size;
                bin.Items.Add(item);
            }
        }
Beispiel #4
0
        private static void PrintBinContents(Bin[] bins)
        {
            if (bins == null || bins.Length < 1)
                return;

            for (int i = 0; i < bins.Length; i++)
            {
                var header = string.Format("Bin #{0} ({1}%)", i + 1, Math.Round(bins[i].Fill / bins[i].Size * 100, 1));
                Console.WriteLine(header);

                foreach (var item in bins[i].Items)
                {
                    Console.WriteLine("\t" + item.Name);
                }
            }
        }