Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab.Optimization-3.3/Multi-objective/HyperVolumeTests.cs @ 17338

Last change on this file since 17338 was 17338, checked in by bwerth, 4 years ago

#2521 fixed crowding unit tests, moved CrowdingTest, HyperVolumeTests and FastHyperVolumeTests to their own subfolder

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.Linq;
23using HeuristicLab.Optimization;
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25
26namespace HeuristicLab.Optimization.Tests {
27  [TestClass]
28  public class HypervolumeTest {
29
30    /// <summary>
31    ///  /*
32    /// +-----+
33    /// |     |
34    /// |  x  |
35    /// |     |
36    /// +-----+
37    ///
38    /// box between(0,0) and(1,1) with singular point Pareto front at(0.5,0.5)
39    /// Hypervolume to each of the corners should be 0.25; 
40    ///
41    /// </summary>
42    [TestMethod]
43    [TestCategory("Optimization.MultiObjective")]
44    [TestProperty("Time", "short")]
45    public void HypervolumeTestSinglePoint() {
46
47      //Front with a single Point
48      double[] point = new double[2];
49      point[0] = 0.5;
50      point[1] = 0.5;
51      double[][] front = { point };
52
53      double[] referencePoint = new double[2];
54      bool[] maximization;
55
56      //Northeast
57      maximization = new bool[] { false, false };
58      referencePoint[0] = 1;
59      referencePoint[1] = 1;
60      double ne = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
61      Assert.AreEqual(0.25, ne);
62
63      //NorthWest
64      maximization = new bool[] { true, false };
65      referencePoint[0] = 0;
66      referencePoint[1] = 1;
67      double nw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
68      Assert.AreEqual(0.25, nw);
69
70      //SouthWest
71      maximization = new bool[] { true, true };
72      referencePoint[0] = 0;
73      referencePoint[1] = 0;
74      double sw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
75      Assert.AreEqual(0.25, sw);
76
77      //SouthEast
78      maximization = new bool[] { false, true };
79      referencePoint[0] = 1;
80      referencePoint[1] = 0;
81      double se = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
82      Assert.AreEqual(0.25, se);
83
84
85    }
86
87    /// <summary>
88    ///  /*
89    /// +-----+
90    /// | x   |
91    /// |     |
92    /// |     |
93    /// +-----+
94    ///
95    /// box between(0,0) and(1,1) with singular point Pareto front at a random Location
96    /// Sum of the Hypervolume to each of the corners should be 1; 
97    ///
98    /// </summary>
99    [TestMethod]
100    [TestCategory("Optimization.MultiObjective")]
101    [TestProperty("Time", "short")]
102    public void HypervolumeTestRandomSinglePoint() {
103      //Front with a single Point
104      double[] point = new double[2];
105      var r = new System.Random();
106
107      point[0] = r.NextDouble();
108      point[1] = r.NextDouble();
109      double[][] front = { point };
110
111      double[] referencePoint = new double[2];
112      bool[] maximization;
113
114      //Northeast
115      maximization = new bool[] { false, false };
116      referencePoint[0] = 1;
117      referencePoint[1] = 1;
118      double ne = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
119
120      //NorthWest
121      maximization = new bool[] { true, false };
122      referencePoint[0] = 0;
123      referencePoint[1] = 1;
124      double nw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
125
126      //SouthWest
127      maximization = new bool[] { true, true };
128      referencePoint[0] = 0;
129      referencePoint[1] = 0;
130      double sw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
131
132      //SouthEast
133      maximization = new bool[] { false, true };
134      referencePoint[0] = 1;
135      referencePoint[1] = 0;
136      double se = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
137      Assert.AreEqual(1.0, ne + se + nw + sw, 1e8);
138    }
139
140    /// <summary>
141    ///  /*
142    /// x-----+
143    /// |     |
144    /// |  X  |
145    /// |     |
146    /// +-----x
147    ///
148    /// box between(0,0) and(1,1) with three point (Pareto) front at (1,0), (0.5,0.5)  and (0,1)
149    /// Hypervolume to (1,0) and (0,1) of the corners should be 1 (dominated Points need to be removed beforehand and   
150    /// Hypervolume to (0,0) and (1,1) of the corners should be 0.25
151    /// </summary>
152    [TestMethod]
153    [TestCategory("Optimization.MultiObjective")]
154    [TestProperty("Time", "short")]
155    public void HypervolumeTestDiagonalPoint() {
156      //Front with three points
157      double[] point1 = new double[2];
158      point1[0] = 0;
159      point1[1] = 1;
160      double[] point2 = new double[2];
161      point2[0] = 1;
162      point2[1] = 0;
163      double[] point3 = new double[2];
164      point3[0] = 0.5;
165      point3[1] = 0.5;
166      double[][] front = { point1, point2, point3 };
167
168      double[] referencePoint = new double[2];
169      bool[] maximization;
170
171      //Northeast
172      maximization = new bool[] { false, false };
173      referencePoint[0] = 1;
174      referencePoint[1] = 1;
175      double ne = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
176      Assert.AreEqual(0.25, ne);
177
178      //NorthWest
179      maximization = new bool[] { true, false };
180      referencePoint[0] = 0;
181      referencePoint[1] = 1;
182      var nonDominatedFront = DominationCalculator.CalculateBestParetoFront(front, front, maximization, false).Select(i => i.Item2).ToList();
183      double nw = HypervolumeCalculator.CalculateHypervolume(nonDominatedFront, referencePoint, maximization);
184      Assert.AreEqual(1, nw);
185
186      //SouthWest
187      maximization = new bool[] { true, true };
188      referencePoint[0] = 0;
189      referencePoint[1] = 0;
190      double sw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
191      Assert.AreEqual(0.25, sw);
192
193      //SouthEast
194      maximization = new bool[] { false, true };
195      referencePoint[0] = 1;
196      referencePoint[1] = 0;
197      nonDominatedFront = DominationCalculator.CalculateBestParetoFront(front, front, maximization, false).Select(i => i.Item2).ToList();
198      double se = HypervolumeCalculator.CalculateHypervolume(nonDominatedFront, referencePoint, maximization);
199      Assert.AreEqual(1, se);
200
201    }
202
203    [TestMethod()]
204    [TestCategory("Optimization.MultiObjective")]
205    [TestProperty("Time", "short")]
206    public void HypervolumeTestReferencePointViolationNE() {
207      //Front with a single Point
208      double[] point = new double[2];
209      point[0] = 0.5;
210      point[1] = 0.5;
211      double[][] front = { point };
212
213      double[] referencePoint = new double[2];
214      bool[] maximization;
215
216      //Northeast
217      maximization = new bool[] { true, true };
218      referencePoint[0] = 1;
219      referencePoint[1] = 1;
220      double ne = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
221      Assert.AreEqual(0, ne);
222    }
223
224    [TestMethod()]
225    [TestCategory("Optimization.MultiObjective")]
226    [TestProperty("Time", "short")]
227    public void HypervolumeTestReferencePointViolationNW() {
228      //Front with a single Point
229      double[] point = new double[2];
230      point[0] = 0.5;
231      point[1] = 0.5;
232      double[][] front = { point };
233
234      double[] referencePoint = new double[2];
235      bool[] maximization;
236
237      //NorthWest
238      maximization = new bool[] { false, true };
239      referencePoint[0] = 0;
240      referencePoint[1] = 1;
241      double nw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
242      Assert.AreEqual(0, nw);
243    }
244
245    [TestMethod()]
246    [TestCategory("Optimization.MultiObjective")]
247    [TestProperty("Time", "short")]
248    public void HypervolumeTestReferencePointViolationSW() {
249      //Front with a single Point
250      double[] point = new double[2];
251      point[0] = 0.5;
252      point[1] = 0.5;
253      double[][] front = { point };
254
255      double[] referencePoint = new double[2];
256      bool[] maximization;
257
258      //SouthWest
259      maximization = new bool[] { false, false };
260      referencePoint[0] = 0;
261      referencePoint[1] = 0;
262      double sw = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
263      Assert.AreEqual(0, sw);
264    }
265
266    [TestMethod()]
267    [TestCategory("Optimization.MultiObjective")]
268    [TestProperty("Time", "short")]
269    public void HypervolumeTestReferencePointViolationSE() {
270      //Front with a single Point
271      double[] point = new double[2];
272      point[0] = 0.5;
273      point[1] = 0.5;
274      double[][] front = { point };
275
276      double[] referencePoint = new double[2];
277      bool[] maximization;
278
279      //SouthEast
280      maximization = new bool[] { true, false };
281      referencePoint[0] = 1;
282      referencePoint[1] = 0;
283      double se = HypervolumeCalculator.CalculateHypervolume(front, referencePoint, maximization);
284      Assert.AreEqual(0, se);
285    }
286  }
287}
Note: See TracBrowser for help on using the repository browser.