[15289] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 |
|
---|
| 4 | namespace 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;
|
---|
[15771] | 13 | using HeuristicLab.Problems.ProgramSynthesis;
|
---|
[15289] | 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");
|
---|
[15366] | 41 | Assert.AreEqual(bestQuality1, bestQuality2, 0.000001);
|
---|
[15289] | 42 |
|
---|
[15366] | 43 | //var currentAverageQuality1 = GetDoubleResult(ga1, "CurrentAverageQuality");
|
---|
| 44 | //var currentAverageQuality2 = GetDoubleResult(ga2, "CurrentAverageQuality");
|
---|
| 45 | //Assert.AreEqual(currentAverageQuality1, currentAverageQuality2, 0.01);
|
---|
[15289] | 46 |
|
---|
| 47 | var currentWorstQuality1 = GetDoubleResult(ga1, "CurrentWorstQuality");
|
---|
| 48 | var currentWorstQuality2 = GetDoubleResult(ga2, "CurrentWorstQuality");
|
---|
[15366] | 49 | Assert.AreEqual(currentWorstQuality1, currentWorstQuality2, 0.000001);
|
---|
[15289] | 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 | }
|
---|