コード例 #1
0
    /// <summary>Benchmarks the provided rectangle packer using random data</summary>
    /// <param name="buildPacker">
    ///   Rectangle packer build method returning new rectangle packers
    ///   with an area of 1024 x 1024
    /// </param>
    /// <returns>The achieved benchmark score</returns>
    protected float Benchmark(RectanglePackerBuilder buildPacker) {
      // How many runs to perform for getting a stable average
      const int averagingRuns = 1;

      // Generates the random number seeds. This is used so that each run produces
      // the same number sequences and makes the comparison of different algorithms
      // a little bit more stable.
      Random seedGenerator = new Random(12345);
      int rectanglesPacked = 0;

      // Perform a number of  runs to get a semi-stable average score
      for(int averagingRun = 0; averagingRun < averagingRuns; ++averagingRun) {
        Random dimensionGenerator = new Random(seedGenerator.Next());
        RectanglePacker packer = buildPacker();

        // Try to cramp as many rectangles into the packing area as possible
        for(; ; ++rectanglesPacked) {
          Point placement;

          int width = dimensionGenerator.Next(16, 64);
          int height = dimensionGenerator.Next(16, 64);

          // As soon as the packer rejects the first rectangle, the run is over
          if(!packer.TryPack(width, height, out placement))
            break;
        }
      }

      // Return the average score achieved by the packer
      return (float)rectanglesPacked / (float)averagingRuns;
    }
コード例 #2
0
ファイル: RectanglePacker.Test.cs プロジェクト: minskowl/MY
        /// <summary>Benchmarks the provided rectangle packer using random data</summary>
        /// <param name="buildPacker">
        ///   Rectangle packer build method returning new rectangle packers
        ///   with an area of 1024 x 1024
        /// </param>
        /// <returns>The achieved benchmark score</returns>
        protected float Benchmark(RectanglePackerBuilder buildPacker)
        {
            // How many runs to perform for getting a stable average
            const int averagingRuns = 1;

            // Generates the random number seeds. This is used so that each run produces
            // the same number sequences and makes the comparison of different algorithms
            // a little bit more stable.
            Random seedGenerator    = new Random(12345);
            int    rectanglesPacked = 0;

            // Perform a number of  runs to get a semi-stable average score
            for (int averagingRun = 0; averagingRun < averagingRuns; ++averagingRun)
            {
                Random          dimensionGenerator = new Random(seedGenerator.Next());
                RectanglePacker packer             = buildPacker();

                // Try to cramp as many rectangles into the packing area as possible
                for (; ; ++rectanglesPacked)
                {
                    Point placement;

                    int width  = dimensionGenerator.Next(16, 64);
                    int height = dimensionGenerator.Next(16, 64);

                    // As soon as the packer rejects the first rectangle, the run is over
                    if (!packer.TryPack(width, height, out placement))
                    {
                        break;
                    }
                }
            }

            // Return the average score achieved by the packer
            return((float)rectanglesPacked / (float)averagingRuns);
        }