Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/MultiObjectiveTestfunctionTests/HyperVolumeTests.cs @ 13765

Last change on this file since 13765 was 13729, checked in by bwerth, 9 years ago

#1087 annotated unit tests

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Problems.MultiObjectiveTestFunctions;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24
25namespace MultiObjectiveTestfunctionTests {
26  [TestClass]
27  public class HypervolumeTest {
28
29
30
31
32    /// <summary>
33    ///  /*
34    /// +-----+
35    /// |     |
36    /// |  x  |
37    /// |     |
38    /// +-----+
39    ///
40    /// box between(0,0) and(1,1) with singular point pareto front at(0.5,0.5)
41    /// Hypervolume to each of the corners should be 0.25; 
42    ///
43    /// </summary>
44    [TestMethod]
45    [TestCategory("Problems.TestFunctions")]
46    [TestProperty("Time", "short")]
47    public void SinglePointTest() {
48
49      //Front with a single Point
50      double[] point = new double[2];
51      point[0] = 0.5;
52      point[1] = 0.5;
53      double[][] front = { point };
54
55      double[] referencePoint = new double[2];
56      bool[] maximization;
57
58      //Northeast
59      maximization = new bool[] { false, false };
60      referencePoint[0] = 1;
61      referencePoint[1] = 1;
62      double ne = Hypervolume.Calculate(front, referencePoint, maximization);
63      Assert.AreEqual(0.25, ne);
64
65      //NorthWest
66      maximization = new bool[] { true, false };
67      referencePoint[0] = 0;
68      referencePoint[1] = 1;
69      double nw = Hypervolume.Calculate(front, referencePoint, maximization);
70      Assert.AreEqual(0.25, nw);
71
72      //SouthWest
73      maximization = new bool[] { true, true };
74      referencePoint[0] = 0;
75      referencePoint[1] = 0;
76      double sw = Hypervolume.Calculate(front, referencePoint, maximization);
77      Assert.AreEqual(0.25, sw);
78
79      //SouthEast
80      maximization = new bool[] { false, true };
81      referencePoint[0] = 1;
82      referencePoint[1] = 0;
83      double se = Hypervolume.Calculate(front, referencePoint, maximization);
84      Assert.AreEqual(0.25, se);
85
86
87    }
88
89    /// <summary>
90    ///  /*
91    /// +-----+
92    /// | x   |
93    /// |     |
94    /// |     |
95    /// +-----+
96    ///
97    /// box between(0,0) and(1,1) with singular point pareto front at a random Location
98    /// Sum of the Hypervolume to each of the corners should be 1; 
99    ///
100    /// </summary>
101    [TestMethod]
102    [TestCategory("Problems.TestFunctions")]
103    [TestProperty("Time", "short")]
104    public void RandomSinglePointTest() {
105      //Front with a single Point
106      double[] point = new double[2];
107      Random r = new Random();
108
109      point[0] = r.NextDouble();
110      point[1] = r.NextDouble();
111      double[][] front = { point };
112
113      double[] referencePoint = new double[2];
114      bool[] maximization;
115
116      //Northeast
117      maximization = new bool[] { false, false };
118      referencePoint[0] = 1;
119      referencePoint[1] = 1;
120      double ne = Hypervolume.Calculate(front, referencePoint, maximization);
121
122      //NorthWest
123      maximization = new bool[] { true, false };
124      referencePoint[0] = 0;
125      referencePoint[1] = 1;
126      double nw = Hypervolume.Calculate(front, referencePoint, maximization);
127
128      //SouthWest
129      maximization = new bool[] { true, true };
130      referencePoint[0] = 0;
131      referencePoint[1] = 0;
132      double sw = Hypervolume.Calculate(front, referencePoint, maximization);
133
134      //SouthEast
135      maximization = new bool[] { false, true };
136      referencePoint[0] = 1;
137      referencePoint[1] = 0;
138      double se = Hypervolume.Calculate(front, referencePoint, maximization);
139      Assert.AreEqual(1.0, ne + se + nw + sw, 1e8);
140    }
141
142    /// <summary>
143    ///  /*
144    /// x-----+
145    /// |     |
146    /// |  X  |
147    /// |     |
148    /// +-----x
149    ///
150    /// box between(0,0) and(1,1) with three point (pareto) front at (1,0), (0.5,0.5)  and (0,1)
151    /// Hypervolume to (1,0) and (0,1) of the corners should be 1 (dominated Points need to be reemoved beforehand and   
152    /// Hypervolume to (0,0) and (1,1) of the corners should be 0.25
153    /// </summary>
154    [TestMethod]
155    [TestCategory("Problems.TestFunctions")]
156    [TestProperty("Time", "short")]
157    public void DiagonalPointTest() {
158      //Front with three points
159      double[] point1 = new double[2];
160      point1[0] = 0;
161      point1[1] = 1;
162      double[] point2 = new double[2];
163      point2[0] = 1;
164      point2[1] = 0;
165      double[] point3 = new double[2];
166      point3[0] = 0.5;
167      point3[1] = 0.5;
168      double[][] front = { point1, point2, point3 };
169
170      double[] referencePoint = new double[2];
171      bool[] maximization;
172
173      //Northeast
174      maximization = new bool[] { false, false };
175      referencePoint[0] = 1;
176      referencePoint[1] = 1;
177      double ne = Hypervolume.Calculate(front, referencePoint, maximization);
178      Assert.AreEqual(0.25, ne);
179
180      //NorthWest
181      maximization = new bool[] { true, false };
182      referencePoint[0] = 0;
183      referencePoint[1] = 1;
184      double nw = Hypervolume.Calculate(NonDominatedSelect.selectNonDominatedVectors(front, maximization, true), referencePoint, maximization);
185      Assert.AreEqual(1, nw);
186
187      //SouthWest
188      maximization = new bool[] { true, true };
189      referencePoint[0] = 0;
190      referencePoint[1] = 0;
191      double sw = Hypervolume.Calculate(front, referencePoint, maximization);
192      Assert.AreEqual(0.25, sw);
193
194      //SouthEast
195      maximization = new bool[] { false, true };
196      referencePoint[0] = 1;
197      referencePoint[1] = 0;
198      double se = Hypervolume.Calculate(NonDominatedSelect.selectNonDominatedVectors(front, maximization, true), referencePoint, maximization);
199      Assert.AreEqual(1, se);
200
201    }
202
203    [TestMethod()]
204    [ExpectedException(typeof(ArgumentException))]
205    [TestCategory("Problems.TestFunctions")]
206    [TestProperty("Time", "short")]
207    public void ReferencePointViolationNE() {
208      //Front with a single Point
209      double[] point = new double[2];
210      point[0] = 0.5;
211      point[1] = 0.5;
212      double[][] front = { point };
213
214      double[] referencePoint = new double[2];
215      bool[] maximization;
216
217      //Northeast
218      maximization = new bool[] { true, true };
219      referencePoint[0] = 1;
220      referencePoint[1] = 1;
221      double ne = Hypervolume.Calculate(front, referencePoint, maximization);
222    }
223
224    [TestMethod()]
225    [ExpectedException(typeof(ArgumentException))]
226    [TestCategory("Problems.TestFunctions")]
227    [TestProperty("Time", "short")]
228    public void ReferencePointViolationNW() {
229      //Front with a single Point
230      double[] point = new double[2];
231      point[0] = 0.5;
232      point[1] = 0.5;
233      double[][] front = { point };
234
235      double[] referencePoint = new double[2];
236      bool[] maximization;
237
238      //NorthWest
239      maximization = new bool[] { false, true };
240      referencePoint[0] = 0;
241      referencePoint[1] = 1;
242      double nw = Hypervolume.Calculate(front, referencePoint, maximization);
243      Assert.AreEqual(0.25, nw);
244    }
245
246    [TestMethod()]
247    [ExpectedException(typeof(ArgumentException))]
248    [TestCategory("Problems.TestFunctions")]
249    [TestProperty("Time", "short")]
250    public void ReferencePointViolationSW() {
251      //Front with a single Point
252      double[] point = new double[2];
253      point[0] = 0.5;
254      point[1] = 0.5;
255      double[][] front = { point };
256
257      double[] referencePoint = new double[2];
258      bool[] maximization;
259
260      //SouthWest
261      maximization = new bool[] { false, false };
262      referencePoint[0] = 0;
263      referencePoint[1] = 0;
264      double sw = Hypervolume.Calculate(front, referencePoint, maximization);
265      Assert.AreEqual(0.25, sw);
266    }
267
268    [TestMethod()]
269    [ExpectedException(typeof(ArgumentException))]
270    [TestCategory("Problems.TestFunctions")]
271    [TestProperty("Time", "short")]
272    public void ReferencePointViolationSE() {
273      //Front with a single Point
274      double[] point = new double[2];
275      point[0] = 0.5;
276      point[1] = 0.5;
277      double[][] front = { point };
278
279      double[] referencePoint = new double[2];
280      bool[] maximization;
281
282      //SouthEast
283      maximization = new bool[] { true, false };
284      referencePoint[0] = 1;
285      referencePoint[1] = 0;
286      double se = Hypervolume.Calculate(front, referencePoint, maximization);
287      Assert.AreEqual(0.25, se);
288    }
289
290  }
291
292
293}
Note: See TracBrowser for help on using the repository browser.