Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Tests/Problem/DeterministicTests.cs @ 15771

Last change on this file since 15771 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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