Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Problem/DeterministicTests.cs @ 17603

Last change on this file since 17603 was 15366, checked in by pkimmesw, 7 years ago

#2665 Solution Cleanup

File size: 3.1 KB
Line 
1using System;
2using System.Linq;
3
4namespace HeuristicLab.Tests.Problem {
5  using System.Threading;
6
7  using HeuristicLab.Algorithms.GeneticAlgorithm;
8  using HeuristicLab.BenchmarkSuite.Problems;
9  using HeuristicLab.Data;
10  using HeuristicLab.Encodings.IntegerVectorEncoding;
11  using HeuristicLab.Optimization;
12  using HeuristicLab.ParallelEngine;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Selector;
15
16  using Microsoft.VisualStudio.TestTools.UnitTesting;
17
18  [TestClass]
19  public class DeterministicTests {
20
21    [TestMethod]
22    [TestCategory("Problem")]
23    [TestProperty("Time", "long")]
24    public void TestMedian() {
25      var ga = GetAlgorithm();
26
27      RunAlgorithm(ga);
28    }
29
30    [TestMethod]
31    [TestCategory("Problem")]
32    [TestProperty("Time", "long")]
33    public void TestSimilarResult() {
34      var ga1 = GetAlgorithm();
35      var ga2 = GetAlgorithm();
36
37      RunAlgorithm(ga1);
38      RunAlgorithm(ga2);
39
40      var bestQuality1 = GetDoubleResult(ga1, "BestQuality");
41      var bestQuality2 = GetDoubleResult(ga2, "BestQuality");
42      Assert.AreEqual(bestQuality1, bestQuality2, 0.000001);
43
44      //var currentAverageQuality1 = GetDoubleResult(ga1, "CurrentAverageQuality");
45      //var currentAverageQuality2 = GetDoubleResult(ga2, "CurrentAverageQuality");
46      //Assert.AreEqual(currentAverageQuality1, currentAverageQuality2, 0.01);
47
48      var currentWorstQuality1 = GetDoubleResult(ga1, "CurrentWorstQuality");
49      var currentWorstQuality2 = GetDoubleResult(ga2, "CurrentWorstQuality");
50      Assert.AreEqual(currentWorstQuality1, currentWorstQuality2, 0.000001);
51    }
52
53    public static IAlgorithm GetAlgorithm() {
54      var ga = new GeneticAlgorithm();
55
56      var instance = new Median();
57      var problemData = instance.CreateProblemData();
58      var pushProblem = new IntegerVectorPushBenchmarkSuiteProblem();
59
60      pushProblem.Load(problemData);
61
62      ga.Problem = pushProblem;
63      pushProblem.Encoding.Length = 100;
64
65      ga.Crossover = ga.CrossoverParameter.ValidValues.OfType<SinglePointCrossover>().First();
66      ga.Selector = ga.SelectorParameter.ValidValues.OfType<LexicaseSelector>().First();
67      ga.Mutator = ga.MutatorParameter.ValidValues.OfType<UniformSomePositionsManipulator>().First();
68
69      ga.Seed.Value = 1337;
70      ga.SetSeedRandomly.Value = false;
71      ga.MutationProbability.Value = 0.05;
72      ga.PopulationSize.Value = 1000;
73      ga.MaximumGenerations.Value = 10;
74
75      ga.Engine = new ParallelEngine();
76
77      return ga;
78    }
79
80    public static void RunAlgorithm(IAlgorithm a) {
81      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
82      Exception ex = null;
83      a.Stopped += (src, e) => trigger.Set();
84      a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
85      a.Prepare();
86      a.Start();
87      trigger.WaitOne();
88
89      Assert.AreEqual(ex, null);
90    }
91
92    public static double GetDoubleResult(IAlgorithm a, string resultName) {
93      return ((DoubleValue)a.Results[resultName].Value).Value;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.