Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/17/17 13:52:29 (7 years ago)
Author:
rhanghof
Message:

#2817: - Now the new RandomInstanceProvider creates the same instances as in the specification given by Martello, Pisinger and Vigo.

  • Now the unit tests are testing the new RandomInstanceProvider.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RandomInstanceProviderWithSRand.cs

    r15418 r15423  
    2626using System.IO;
    2727using System.Linq;
     28using System.Reflection;
    2829using HeuristicLab.Common;
    2930using HeuristicLab.Core;
     
    3334namespace HeuristicLab.Problems.BinPacking3D {
    3435  // make sure that for each class we have a separate entry in the problem instance providers
     36 
    3537  public class RandomInstanceClass1ProviderWithSRand : RandomInstanceProviderWithSRand {
    3638    public RandomInstanceClass1ProviderWithSRand() : base() { @class = 1; binWidth = binHeight = binDepth = 100; }
    37   }
     39   
     40  }
     41
    3842  public class RandomInstanceClass2ProviderWithSRand : RandomInstanceProviderWithSRand {
    3943    public RandomInstanceClass2ProviderWithSRand() : base() { @class = 2; binWidth = binHeight = binDepth = 100; }
     44   
    4045  }
    4146  public class RandomInstanceClass3ProviderWithSRand : RandomInstanceProviderWithSRand {
    4247    public RandomInstanceClass3ProviderWithSRand() : base() { @class = 3; binWidth = binHeight = binDepth = 100; }
     48   
    4349  }
    4450  public class RandomInstanceClass4ProviderWithSRand : RandomInstanceProviderWithSRand {
    4551    public RandomInstanceClass4ProviderWithSRand() : base() { @class = 4; binWidth = binHeight = binDepth = 100; }
     52   
    4653  }
    4754  public class RandomInstanceClass5ProviderWithSRand : RandomInstanceProviderWithSRand {
    4855    public RandomInstanceClass5ProviderWithSRand() : base() { @class = 5; binWidth = binHeight = binDepth = 100; }
     56   
    4957  }
    5058
     
    5563    }
    5664    protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) {
    57       w = rand.Next(1, 11);
    58       h = rand.Next(1, 11);
    59       d = rand.Next(1, 11);
     65      w = rand.Next(1, 10);
     66      h = rand.Next(1, 10);
     67      d = rand.Next(1, 10);
    6068    }
    6169  }
     
    6674    }
    6775    protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) {
    68       w = rand.Next(1, 36);
    69       h = rand.Next(1, 36);
    70       d = rand.Next(1, 36);
     76      w = rand.Next(1, 35);
     77      h = rand.Next(1, 35);
     78      d = rand.Next(1, 35);
    7179    }
    7280  }
     
    7785    }
    7886    protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) {
    79       w = rand.Next(1, 101);
    80       h = rand.Next(1, 101);
    81       d = rand.Next(1, 101);
     87      w = rand.Next(1, 100);
     88      h = rand.Next(1, 100);
     89      d = rand.Next(1, 100);
    8290    }
    8391  }
     
    8795
    8896  public abstract class RandomInstanceProviderWithSRand : ProblemInstanceProvider<BPPData>, IProblemInstanceProvider<BPPData> {
     97
     98    /// <summary>
     99    /// Number of created test items. This items are used for packing them into the bin
     100    /// </summary>
     101    private static readonly int[] numberOfGeneratedTestItems = new int[] { 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 150, 200 };   
     102
     103    /// <summary>
     104    /// Number of instance for which should be created for each instance
     105    /// </summary>
     106    private static readonly int numberOfGeneratedInstances = 30;
     107
    89108    #region Random Generator srand48
    90109    protected class SRand48 : Item, IRandom {
     
    173192    protected int binWidth, binHeight, binDepth;
    174193
     194    #region Common information for displaying it on the ui
     195   
    175196    public override string Name {
    176197      get { return string.Format("Martello, Pisinger, Vigo (class={0})", @class); }
     
    189210    }
    190211
     212    #endregion
    191213    public RandomInstanceProviderWithSRand() : base() { }
    192214
     215   
     216    /// <summary>
     217    /// Returns a collection of data descriptors. Each descriptor contains the seed for the random generator.
     218    /// </summary>
     219    /// <returns></returns>
    193220    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
    194221      // 10 classes
    195       //var rand = new MersenneTwister(1234); // fixed seed to makes sure that instances are always the same
    196       foreach (int numItems in new int[] { 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 150, 200 }) {
    197         // get class parameters
    198         // generate 30 different instances for each class
    199         foreach (int instance in Enumerable.Range(1, 30)) {
    200           var rand = new SRand48((uint)(numItems + instance));
     222      foreach (int numItems in numberOfGeneratedTestItems) {
     223        for (int instance = 1; instance <= numberOfGeneratedInstances; instance++) {
    201224          string name = string.Format("n={0}-id={1:00} (class={2})", numItems, instance, @class);
    202           var dd = new RandomDataDescriptor(name, name, numItems, @class, seed: rand.Next());
    203           yield return dd;
    204         }
    205       }
    206     }
    207 
     225          /* As in the test programm of Silvano Martello, David Pisinger, Daniele Vigo given,
     226           * the seed of the instance provider will be calculated by adding the number of generated items and teh instance number.
     227           * This guarantees that the instances will always be the same
     228           */
     229          yield return new RandomDataDescriptor(name, name, numItems, @class, seed: numItems + instance);
     230        }
     231      }
     232    }
     233
     234   
    208235    public override BPPData LoadData(IDataDescriptor dd) {
    209236      var randDd = dd as RandomDataDescriptor;
     
    224251    }
    225252
    226     // default implementation for class 1 .. 5
     253   
     254    /// <summary>
     255    /// Generates the dimensions for a item by using the given random generator
     256    /// </summary>
     257    /// <param name="rand">Given random generator</param>
     258    /// <param name="w">Calculated width of the item</param>
     259    /// <param name="h">Calculated height of the item</param>
     260    /// <param name="d">Calculated depth of the item</param>
    227261    protected virtual void SampleItemParameters(IRandom rand, out int w, out int h, out int d) {
    228       // for classes 1 - 5
    229262      Contract.Assert(@class >= 1 && @class <= 5);
    230       var weights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 };
     263      /*var weights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 };
    231264      weights[@class - 1] = 0.6;
    232265      var type = Enumerable.Range(1, 5).SampleProportional(rand, 1, weights).First();
    233 
    234       int minW, maxW;
    235       int minH, maxH;
    236       int minD, maxD;
    237       GetItemParameters(type, rand, binWidth, binHeight, binDepth,
    238         out minW, out maxW, out minH, out maxH, out minD, out maxD);
    239 
    240       w = rand.Next(minW, maxW + 1);
    241       h = rand.Next(minH, maxH + 1);
    242       d = rand.Next(minD, maxD + 1);
    243     }
    244 
    245     private void GetItemParameters(int type, IRandom rand,
    246       int w, int h, int d,
    247       out int minW, out int maxW, out int minH, out int maxH, out int minD, out int maxD) {
     266      */
     267
     268      // as by Martello and Vigo
     269      int type = @class;
     270      if (type <= 5) {
     271        var t = rand.Next(1, 10);
     272        if (t <= 5) {
     273          type = t;
     274        }
     275      }
     276
    248277      switch (type) {
    249         case 1: {
    250             minW = 1;
    251             maxW = w / 2; // integer division on purpose (see paper)
    252             minH = h * 2 / 3;
    253             maxH = h;
    254             minD = d * 2 / 3;
    255             maxD = d;
    256             break;
    257           }
    258         case 2: {
    259             minW = w * 2 / 3;
    260             maxW = w;
    261             minH = 1;
    262             maxH = h / 2;
    263             minD = d * 2 / 3;
    264             maxD = d;
    265             break;
    266           }
    267         case 3: {
    268             minW = w * 2 / 3;
    269             maxW = w;
    270             minH = h * 2 / 3;
    271             maxH = h;
    272             minD = 1;
    273             maxD = d / 2;
    274             break;
    275           }
    276         case 4: {
    277             minW = w / 2;
    278             maxW = w;
    279             minH = h / 2;
    280             maxH = h;
    281             minD = d / 2;
    282             maxD = d;
    283             break;
    284           }
    285         case 5: {
    286             minW = 1;
    287             maxW = w / 2;
    288             minH = 1;
    289             maxH = h / 2;
    290             minD = 1;
    291             maxD = d / 2;
    292             break;
    293           }
    294         default: {
     278        case 1:
     279          CreateInstanceDimensionsType1(rand, out w, out h, out d);
     280          break;
     281        case 2:
     282          CreateInstanceDimensionsType2(rand, out w, out h, out d);
     283          break;
     284        case 3:
     285          CreateInstanceDimensionsType3(rand, out w, out h, out d);
     286          break;
     287        case 4:
     288          CreateInstanceDimensionsType4(rand, out w, out h, out d);
     289          break;
     290        case 5:
     291          CreateInstanceDimensionsType5(rand, out w, out h, out d);
     292          break;
     293        default:
    295294            throw new InvalidProgramException();
    296           }
    297       }
    298     }
     295      }
     296    }
     297
     298   
     299    #region Instance dimensions generators for type 1 - 5
     300    private void CreateInstanceDimensionsType1(IRandom rand, out int w, out int h, out int d) {
     301      w = rand.Next(1, binWidth / 2);
     302      h = rand.Next((binHeight * 2) / 3, binHeight);
     303      d = rand.Next((binDepth * 2) / 3, binDepth);
     304    }
     305
     306    private void CreateInstanceDimensionsType2(IRandom rand, out int w, out int h, out int d) {
     307      w = rand.Next(((binWidth * 2) / 3), binWidth);
     308      h = rand.Next(1, binHeight / 2);
     309      d = rand.Next(((binDepth * 2) / 3), binDepth);
     310    }
     311
     312    private void CreateInstanceDimensionsType3(IRandom rand, out int w, out int h, out int d) {
     313      w = rand.Next(((binWidth * 2) / 3), binWidth);
     314      h = rand.Next(((binHeight * 2) / 3), binHeight);
     315      d = rand.Next(1, binDepth / 2);
     316    }
     317    private void CreateInstanceDimensionsType4(IRandom rand, out int w, out int h, out int d) {
     318      w = rand.Next(binWidth / 2, binWidth);
     319      h = rand.Next(binHeight / 2, binHeight);
     320      d = rand.Next(binDepth / 2, binDepth);
     321    }
     322    private void CreateInstanceDimensionsType5(IRandom rand, out int w, out int h, out int d) {
     323      w = rand.Next(1, binWidth / 2);
     324      h = rand.Next(1, binHeight / 2);
     325      d = rand.Next(1, binDepth / 2);
     326    }
     327   
     328    #endregion
     329
     330
    299331
    300332    public override bool CanImportData {
     
    323355          else
    324356            writer.WriteLine("{0,-5} {1,-5} {2,-5}", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth);
    325 
    326357        }
    327358        writer.Flush();
    328359      }
    329360    }
    330 
    331361  }
    332362}
Note: See TracChangeset for help on using the changeset viewer.