- Timestamp:
- 10/17/17 13:52:29 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RandomInstanceProviderWithSRand.cs
r15418 r15423 26 26 using System.IO; 27 27 using System.Linq; 28 using System.Reflection; 28 29 using HeuristicLab.Common; 29 30 using HeuristicLab.Core; … … 33 34 namespace HeuristicLab.Problems.BinPacking3D { 34 35 // make sure that for each class we have a separate entry in the problem instance providers 36 35 37 public class RandomInstanceClass1ProviderWithSRand : RandomInstanceProviderWithSRand { 36 38 public RandomInstanceClass1ProviderWithSRand() : base() { @class = 1; binWidth = binHeight = binDepth = 100; } 37 } 39 40 } 41 38 42 public class RandomInstanceClass2ProviderWithSRand : RandomInstanceProviderWithSRand { 39 43 public RandomInstanceClass2ProviderWithSRand() : base() { @class = 2; binWidth = binHeight = binDepth = 100; } 44 40 45 } 41 46 public class RandomInstanceClass3ProviderWithSRand : RandomInstanceProviderWithSRand { 42 47 public RandomInstanceClass3ProviderWithSRand() : base() { @class = 3; binWidth = binHeight = binDepth = 100; } 48 43 49 } 44 50 public class RandomInstanceClass4ProviderWithSRand : RandomInstanceProviderWithSRand { 45 51 public RandomInstanceClass4ProviderWithSRand() : base() { @class = 4; binWidth = binHeight = binDepth = 100; } 52 46 53 } 47 54 public class RandomInstanceClass5ProviderWithSRand : RandomInstanceProviderWithSRand { 48 55 public RandomInstanceClass5ProviderWithSRand() : base() { @class = 5; binWidth = binHeight = binDepth = 100; } 56 49 57 } 50 58 … … 55 63 } 56 64 protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) { 57 w = rand.Next(1, 1 1);58 h = rand.Next(1, 1 1);59 d = rand.Next(1, 1 1);65 w = rand.Next(1, 10); 66 h = rand.Next(1, 10); 67 d = rand.Next(1, 10); 60 68 } 61 69 } … … 66 74 } 67 75 protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) { 68 w = rand.Next(1, 3 6);69 h = rand.Next(1, 3 6);70 d = rand.Next(1, 3 6);76 w = rand.Next(1, 35); 77 h = rand.Next(1, 35); 78 d = rand.Next(1, 35); 71 79 } 72 80 } … … 77 85 } 78 86 protected override void SampleItemParameters(IRandom rand, out int w, out int h, out int d) { 79 w = rand.Next(1, 10 1);80 h = rand.Next(1, 10 1);81 d = rand.Next(1, 10 1);87 w = rand.Next(1, 100); 88 h = rand.Next(1, 100); 89 d = rand.Next(1, 100); 82 90 } 83 91 } … … 87 95 88 96 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 89 108 #region Random Generator srand48 90 109 protected class SRand48 : Item, IRandom { … … 173 192 protected int binWidth, binHeight, binDepth; 174 193 194 #region Common information for displaying it on the ui 195 175 196 public override string Name { 176 197 get { return string.Format("Martello, Pisinger, Vigo (class={0})", @class); } … … 189 210 } 190 211 212 #endregion 191 213 public RandomInstanceProviderWithSRand() : base() { } 192 214 215 216 /// <summary> 217 /// Returns a collection of data descriptors. Each descriptor contains the seed for the random generator. 218 /// </summary> 219 /// <returns></returns> 193 220 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 194 221 // 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++) { 201 224 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 208 235 public override BPPData LoadData(IDataDescriptor dd) { 209 236 var randDd = dd as RandomDataDescriptor; … … 224 251 } 225 252 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> 227 261 protected virtual void SampleItemParameters(IRandom rand, out int w, out int h, out int d) { 228 // for classes 1 - 5229 262 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 }; 231 264 weights[@class - 1] = 0.6; 232 265 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 248 277 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: 295 294 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 299 331 300 332 public override bool CanImportData { … … 323 355 else 324 356 writer.WriteLine("{0,-5} {1,-5} {2,-5}", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth); 325 326 357 } 327 358 writer.Flush(); 328 359 } 329 360 } 330 331 361 } 332 362 }
Note: See TracChangeset
for help on using the changeset viewer.