Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/RealVectorSolutionCacheTest.cs @ 11528

Last change on this file since 11528 was 11182, checked in by ascheibe, 10 years ago

#1886 added another unit test

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
21
22using System.Linq;
23using HeuristicLab.Analysis.SolutionCaching;
24using HeuristicLab.Analysis.SolutionCaching.RealVectorEncoding;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace AlgorithmBehaviorUnitTests {
29  [TestClass]
30  public class RealVectorSolutionCacheTest {
31    [TestMethod]
32    public void TestComparer() {
33      var rand = new HeuristicLab.Random.FastRandom();
34      int nrOfVectors = 500000;
35      int vectorLength = 4;
36
37      RealVectorEqualityComparer comp = new RealVectorEqualityComparer();
38
39      for (int i = 0; i < nrOfVectors; i++) {
40        RealVector vector1 = new RealVector(vectorLength, rand, -20, 30);
41        RealVector vector2 = new RealVector(vectorLength, rand, -20, 30);
42        RealVector vector3 = SinglePointCrossover.Apply(rand, vector1, vector2);
43
44        bool result = comp.Equals(vector1, vector2);
45        Assert.AreEqual(result, comp.GetHashCode(vector1) == comp.GetHashCode(vector2));
46
47        result = comp.Equals(vector1, vector3);
48        Assert.AreEqual(result, comp.GetHashCode(vector1) == comp.GetHashCode(vector3));
49
50        result = comp.Equals(vector3, vector2);
51        Assert.AreEqual(result, comp.GetHashCode(vector3) == comp.GetHashCode(vector2));
52
53        result = comp.Equals(vector3, vector3);
54        Assert.AreEqual(result, comp.GetHashCode(vector3) == comp.GetHashCode(vector3));
55
56
57      }
58    }
59
60    [TestMethod]
61    public void TestCache() {
62      var rand = new HeuristicLab.Random.FastRandom();
63      int nrOfVectors = 200;
64      int vectorLength = 50;
65
66      RealVectorSolutionCache solutionCache = new RealVectorSolutionCache();
67      RealVectorEqualityComparer comp = new RealVectorEqualityComparer();
68
69      for (int i = 0; i < nrOfVectors; i++) {
70        RealVector vector = new RealVector(vectorLength, rand, -20, 30);
71        RealVectorSolutionInformation info = new RealVectorSolutionInformation();
72
73        //should be added
74        info.ProducedBy = ProducedBy.Crossover;
75        solutionCache.Add(vector, info);
76
77        //should be added
78        info = new RealVectorSolutionInformation();
79        info.ProducedBy = ProducedBy.Move;
80        solutionCache.Add(vector, info);
81
82        //should be added
83        info = new RealVectorSolutionInformation();
84        info.ProducedBy = ProducedBy.Move;
85        info.Iteration = 20;
86        solutionCache.Add(vector, info);
87
88        //should not be added
89        info = new RealVectorSolutionInformation();
90        info.ProducedBy = ProducedBy.Crossover;
91        solutionCache.Add(vector, info);
92      }
93
94      foreach (var value in solutionCache.Values()) {
95        Assert.AreEqual(3, value.Count);
96      }
97
98      foreach (var key in solutionCache.Keys()) {
99        int cnt = solutionCache.Keys().Count(x => comp.Equals(key, x));
100        Assert.AreEqual(1, cnt);
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.