Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Tests/HeuristicLab.Problems.Bin-Packing-3.3/3D/AlgorithmTest.cs @ 15463

Last change on this file since 15463 was 15463, checked in by rhanghof, 6 years ago

#2817
-Added unit tests for bin packing 3d.
-The items can now be sorted by the material.

File size: 13.4 KB
Line 
1using System;
2using System.IO;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4using HeuristicLab.Problems.BinPacking3D;
5using HeuristicLab.Problems.BinPacking3D.Instances;
6using System.Collections.Generic;
7using System.Linq;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.Problems.BinPacking._3D.Tests {
11  [TestClass]
12  public class AlgorithmTest {
13
14    private struct Dimension {
15      public int Id { get; set; }
16      public int Width { get; set; }
17      public int Height { get; set; }
18      public int Depth { get; set; }
19    }
20
21    #region TestRandomInstanceProvider
22
23
24    /// <summary>
25    /// Tests if the generated random instance equals to the references generated by david pisinger
26    /// http://www.diku.dk/~pisinger/new3dbpp/test3dbpp.c
27    /// </summary>
28    [TestMethod]
29    [TestCategory("Problems.BinPacking.3D")]
30    public void TestRandomInstanceProvider() {
31
32      var referenceItemLists = ReadReferenceItemLists();
33      TestRandomInstanceProviderByClass(new RandomInstanceClass1Provider(), referenceItemLists);
34      TestRandomInstanceProviderByClass(new RandomInstanceClass2Provider(), referenceItemLists);
35      TestRandomInstanceProviderByClass(new RandomInstanceClass3Provider(), referenceItemLists);
36      TestRandomInstanceProviderByClass(new RandomInstanceClass4Provider(), referenceItemLists);
37      TestRandomInstanceProviderByClass(new RandomInstanceClass5Provider(), referenceItemLists);
38      TestRandomInstanceProviderByClass(new RandomInstanceClass6Provider(), referenceItemLists);
39      TestRandomInstanceProviderByClass(new RandomInstanceClass7Provider(), referenceItemLists);
40      TestRandomInstanceProviderByClass(new RandomInstanceClass8Provider(), referenceItemLists);
41
42    }
43
44    private IDictionary<string, List<Dimension>> ReadReferenceItemLists() {
45      var itemList = new Dictionary<string, List<Dimension>>();
46      string path = @".\..\HeuristicLab.Tests\HeuristicLab.Problems.Bin-Packing-3.3\ReferenceInstances";
47
48      string[] files = Directory.GetFiles(path);
49      foreach (string filePath in files) {
50        string key = Path.GetFileNameWithoutExtension(filePath);
51
52        using (StreamReader reader = new StreamReader(filePath)) {
53          int lineNumber = 1;
54          List<Dimension> dimensionList = new List<Dimension>();
55          while (!reader.EndOfStream) {
56            string line = reader.ReadLine();
57            if (lineNumber > 2) {
58              string[] lineValues = line.Split('\t');
59              int id;
60              int depth;
61              int width;
62              int height;
63              Int32.TryParse(lineValues[0], out id);
64              Int32.TryParse(lineValues[1], out depth);
65              Int32.TryParse(lineValues[2], out width);
66              Int32.TryParse(lineValues[3], out height);
67              dimensionList.Add(new Dimension() {
68                Id = id,
69                Depth = depth,
70                Width = width,
71                Height = height
72              });
73            }
74            lineNumber++;
75          }
76          itemList.Add(key, dimensionList);
77        }
78      }
79      return itemList;
80    }
81
82    private void TestRandomInstanceProviderByClass(RandomInstanceProvider randomInstanceProvider, IDictionary<string, List<Dimension>> referenceItems) {
83
84      var dataDescriptors = randomInstanceProvider.GetDataDescriptors();
85      foreach (var dataDescriptor in dataDescriptors) {
86        List<Dimension> testItemDimensions = null;
87        if (referenceItems.TryGetValue(dataDescriptor.Name, out testItemDimensions)) {
88          var packingItems = randomInstanceProvider.LoadData(dataDescriptor).Items;
89          Assert.IsNotNull(packingItems);
90          Assert.AreEqual(testItemDimensions.Count, packingItems.Length);
91          for (int i = 0; i < packingItems.Length; i++) {
92            Assert.AreEqual(testItemDimensions[i].Width, packingItems[i].Width);
93            Assert.AreEqual(testItemDimensions[i].Height, packingItems[i].Height);
94            Assert.AreEqual(testItemDimensions[i].Depth, packingItems[i].Depth);
95          }
96        }
97      }
98    }
99    #endregion
100
101    #region TestExtremePointAlgorithm
102
103    /// <summary>
104    /// Constants for testing the algorithm
105    /// The test parameters are defined in the paper
106    /// </summary>
107    private const int NUMBER_OF_TEST_INSTANCES = 10;
108    private static readonly int[] TEST_CLASSES = { 1, 2 };
109    private static readonly int[] NUMBER_OF_TEST_ITEMS = { 50, 100, 150, 200 };
110
111    [TestMethod]
112    [TestCategory("Problems.BinPacking.3D")]
113    public void TestExtremePointAlgorithmClass1() {
114      TestExtremePointAlgorithm(new RandomInstanceClass1Provider(), 1);
115    }
116
117    [TestMethod]
118    [TestCategory("Problems.BinPacking.3D")]
119    public void TestExtremePointAlgorithmClass2() {
120      TestExtremePointAlgorithm(new RandomInstanceClass2Provider(), 2);
121    }
122
123    [TestMethod]
124    [TestCategory("Problems.BinPacking.3D")]
125    public void TestExtremePointAlgorithmClass3() {
126      TestExtremePointAlgorithm(new RandomInstanceClass3Provider(), 3);
127    }
128
129    [TestMethod]
130    [TestCategory("Problems.BinPacking.3D")]
131    public void TestExtremePointAlgorithmClass4() {
132      TestExtremePointAlgorithm(new RandomInstanceClass4Provider(), 4);
133    }
134
135    [TestMethod]
136    [TestCategory("Problems.BinPacking.3D")]
137    public void TestExtremePointAlgorithmClass5() {
138      TestExtremePointAlgorithm(new RandomInstanceClass5Provider(), 5);
139    }
140
141    [TestMethod]
142    [TestCategory("Problems.BinPacking.3D")]
143    public void TestExtremePointAlgorithmClass6() {
144      TestExtremePointAlgorithm(new RandomInstanceClass6Provider(), 6);
145    }
146
147    [TestMethod]
148    [TestCategory("Problems.BinPacking.3D")]
149    public void TestExtremePointAlgorithmClass7() {
150      TestExtremePointAlgorithm(new RandomInstanceClass7Provider(), 7);
151    }
152
153    [TestMethod]
154    [TestCategory("Problems.BinPacking.3D")]
155    public void TestExtremePointAlgorithmClass8() {
156      TestExtremePointAlgorithm(new RandomInstanceClass8Provider(), 8);
157    }
158
159    private void TestExtremePointAlgorithm(RandomInstanceProvider randomInstanceProvider, int @class) {
160      foreach (SortingMethod sortingMethod in Enum.GetValues(typeof(SortingMethod))) {
161        //foreach (FittingMethod fittingMethod in Enum.GetValues(typeof(FittingMethod))) {
162        FittingMethod fittingMethod = FittingMethod.FirstFit;
163        TestExtremePointAlgorithmByParameters(randomInstanceProvider, @class, sortingMethod, fittingMethod);
164        //}
165      }
166    }
167
168
169
170
171    private void TestExtremePointAlgorithmByParameters(RandomInstanceProvider randomInstanceProvider, int @class, SortingMethod sortingMethod, FittingMethod fittingMethod) {
172      var dataDescriptors = randomInstanceProvider.GetDataDescriptors();
173      var referenceValues = GetReferenceAlgorithmValues();
174      foreach (var numItems in NUMBER_OF_TEST_ITEMS) {
175        int sumNumberOfBins = 0;
176        for (int instance = 1; instance <= NUMBER_OF_TEST_INSTANCES; instance++) {
177          string name = string.Format("n={0}-id={1:00} (class={2})", numItems, instance, @class);
178          var selectedDataDescriptor = dataDescriptors.Where(dataDescriptor => dataDescriptor.Name == name);
179          Assert.IsNotNull(selectedDataDescriptor?.First());
180          var packingData = randomInstanceProvider.LoadData(selectedDataDescriptor.First());
181
182          ExtremePointAlgorithm algorithm = new ExtremePointAlgorithm();
183          algorithm.SortingMethodParameter.Value.Value = sortingMethod;
184          algorithm.FittingMethodParameter.Value.Value = fittingMethod;
185          algorithm.Problem.Load(packingData);
186
187          algorithm.Start();
188
189          PackingPlan<BinPacking3D.PackingPosition, PackingShape, PackingItem> bestPackingPlan = null;
190          foreach (Optimization.IResult result in algorithm.Results) {
191            if (result.Name == "Best Solution") {
192              bestPackingPlan = (PackingPlan<BinPacking3D.PackingPosition, PackingShape, PackingItem>)result.Value;
193              break;
194            }
195          }
196
197          sumNumberOfBins += bestPackingPlan.NrOfBins;
198        }
199
200        double referenceValue = 0.0;
201
202        if (referenceValues.TryGetValue(new Tuple<int, int, SortingMethod>(@class, numItems, sortingMethod), out referenceValue)) {
203          Console.WriteLine($"{numItems}-{@class}-{sortingMethod}: \t{referenceValue} \t {(double)sumNumberOfBins / (double)NUMBER_OF_TEST_INSTANCES} \t{(referenceValue - ((double)sumNumberOfBins / (double)NUMBER_OF_TEST_INSTANCES)):F2}");
204          Assert.AreEqual(referenceValue, (double)sumNumberOfBins / (double)NUMBER_OF_TEST_INSTANCES, 20.0);
205        }
206      }
207    }
208
209
210    /// <summary>
211    /// Returns a dictionary which contains the reference values from table 2 given by the paper https://www.cirrelt.ca/DocumentsTravail/CIRRELT-2007-41.pdf
212    /// Dictionary<Tuple<int, int, SortingMethod>, double> -> Dictionary<Tuple<@class, number of items, SortingMethod>, value>
213    /// </summary>
214    /// <returns></returns>
215    private Dictionary<Tuple<int, int, SortingMethod>, double> GetReferenceAlgorithmValues() {
216      Dictionary<Tuple<int, int, SortingMethod>, double> referenceValues = new Dictionary<Tuple<int, int, SortingMethod>, double>();
217
218      AddToReferenceAlgorithmValuesDict(referenceValues, 1, SortingMethod.Given, new double[] { 14.6, 29.2, 40.1, 55.9});
219      AddToReferenceAlgorithmValuesDict(referenceValues, 1, SortingMethod.HeightVolume, new double[] { 15, 29.2, 39.9, 55.6 });
220      AddToReferenceAlgorithmValuesDict(referenceValues, 1, SortingMethod.VolumeHeight, new double[] { 14.4, 29.5, 40.3, 55.7 });
221      AddToReferenceAlgorithmValuesDict(referenceValues, 1, SortingMethod.AreaHeight, new double[] { 14.4, 28.3, 39.2, 53.2 });
222      AddToReferenceAlgorithmValuesDict(referenceValues, 1, SortingMethod.HeightArea, new double[] { 15, 29, 39.8, 55.1});
223
224      AddToReferenceAlgorithmValuesDict(referenceValues, 4, SortingMethod.Given, new double[] { 29.7, 60.2, 88.5, 119.9 });
225      AddToReferenceAlgorithmValuesDict(referenceValues, 4, SortingMethod.HeightVolume, new double[] { 30.1, 59.6, 88.3, 120.1 });
226      AddToReferenceAlgorithmValuesDict(referenceValues, 4, SortingMethod.VolumeHeight, new double[] { 29.9, 60.4, 88.6, 119.6 });
227      AddToReferenceAlgorithmValuesDict(referenceValues, 4, SortingMethod.AreaHeight, new double[] { 30, 59.7, 88.4, 120.3 });
228      AddToReferenceAlgorithmValuesDict(referenceValues, 4, SortingMethod.HeightArea, new double[] { 30, 59.6, 88.3, 120 });
229
230      AddToReferenceAlgorithmValuesDict(referenceValues, 5, SortingMethod.Given, new double[] { 10.1, 18.1, 24.4, 32.5 });
231      AddToReferenceAlgorithmValuesDict(referenceValues, 5, SortingMethod.HeightVolume, new double[] { 9, 16.7, 22.9, 30.7});
232      AddToReferenceAlgorithmValuesDict(referenceValues, 5, SortingMethod.VolumeHeight, new double[] { 10, 17.8, 24.5, 32.6 });
233      AddToReferenceAlgorithmValuesDict(referenceValues, 5, SortingMethod.AreaHeight, new double[] { 9.2, 16.1, 21.9, 29.5 });
234      AddToReferenceAlgorithmValuesDict(referenceValues, 5, SortingMethod.HeightArea, new double[] { 9, 16.6, 22.6, 30.5 });
235
236      AddToReferenceAlgorithmValuesDict(referenceValues, 6, SortingMethod.Given, new double[] { 11.7, 21.7, 33, 44.4});
237      AddToReferenceAlgorithmValuesDict(referenceValues, 6, SortingMethod.HeightVolume, new double[] { 10.9, 21.2, 31.8, 41.5 });
238      AddToReferenceAlgorithmValuesDict(referenceValues, 6, SortingMethod.VolumeHeight, new double[] { 11.7, 22, 34.2, 44 });
239      AddToReferenceAlgorithmValuesDict(referenceValues, 6, SortingMethod.AreaHeight, new double[] { 10.6, 20.2, 30.8, 39.5});
240      AddToReferenceAlgorithmValuesDict(referenceValues, 6, SortingMethod.HeightArea, new double[] { 10.9, 20.5, 31, 39.8 });
241
242      AddToReferenceAlgorithmValuesDict(referenceValues, 7, SortingMethod.Given, new double[] { 9.4, 15.9, 19.3, 30 });
243      AddToReferenceAlgorithmValuesDict(referenceValues, 7, SortingMethod.HeightVolume, new double[] { 8.2, 14.6, 19.2, 28.1 });
244      AddToReferenceAlgorithmValuesDict(referenceValues, 7, SortingMethod.VolumeHeight, new double[] { 9.3, 15.6, 19.7, 30.2 });
245      AddToReferenceAlgorithmValuesDict(referenceValues, 7, SortingMethod.AreaHeight, new double[] { 8.1, 14.1, 18.2, 26.2 });
246      AddToReferenceAlgorithmValuesDict(referenceValues, 7, SortingMethod.HeightArea, new double[] { 8.1, 14.1, 18.9, 27,2 });
247
248      AddToReferenceAlgorithmValuesDict(referenceValues, 8, SortingMethod.Given, new double[] { 11.6, 22, 28.5, 35.4 });
249      AddToReferenceAlgorithmValuesDict(referenceValues, 8, SortingMethod.HeightVolume, new double[] { 10.5, 20.9, 27.4, 33.9 });
250      AddToReferenceAlgorithmValuesDict(referenceValues, 8, SortingMethod.VolumeHeight, new double[] { 11.6, 22.1, 28.4, 35.4});
251      AddToReferenceAlgorithmValuesDict(referenceValues, 8, SortingMethod.AreaHeight, new double[] { 10.1, 20.3, 26.4, 32.2 });
252      AddToReferenceAlgorithmValuesDict(referenceValues, 8, SortingMethod.HeightArea, new double[] { 10.5, 20.8, 37.7, 33.9 });
253      return referenceValues;
254    }
255
256    private void AddToReferenceAlgorithmValuesDict(Dictionary<Tuple<int, int, SortingMethod>, double> referenceValues, int @class, SortingMethod sortingMethod, double[] values) {
257      for (int i = 0; i < values.Length; i++) {
258        referenceValues.Add(new Tuple<int, int, SortingMethod>(@class, 50 + (50 * i), sortingMethod), values[i]);
259      }
260     
261    }
262
263
264    #endregion
265  }
266
267}
Note: See TracBrowser for help on using the repository browser.