Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs @ 3338

Last change on this file since 3338 was 3338, checked in by gkronber, 12 years ago

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File size: 3.3 KB
Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Random;
8using System.Diagnostics;
9
10namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
11  [TestClass]
12  public class SubtreeCrossoverTest {
13    private static ISymbolicExpressionGrammar grammar;
14    private static List<SymbolicExpressionTree> crossoverTrees;
15    private static double msPerCrossoverEvent;
16
17    private TestContext testContextInstance;
18
19    /// <summary>
20    ///Gets or sets the test context which provides
21    ///information about and functionality for the current test run.
22    ///</summary>
23    public TestContext TestContext {
24      get {
25        return testContextInstance;
26      }
27      set {
28        testContextInstance = value;
29      }
30    }
31
32    [ClassInitialize()]
33    public static void SubtreeCrossoverTestInitialize(TestContext testContext) {
34      crossoverTrees = new List<SymbolicExpressionTree>();
35      int populationSize = 1000;
36      int generations = 5;
37      int failedEvents = 0;
38      grammar = Grammars.CreateArithmeticAndAdfGrammar();
39      var random = new MersenneTwister();
40      for (int i = 0; i < populationSize; i++) {
41        crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
42      }
43      Stopwatch stopwatch = new Stopwatch();
44      stopwatch.Start();
45      for (int gCount = 0; gCount < generations; gCount++) {
46        var newPopulation = new List<SymbolicExpressionTree>();
47        for (int i = 0; i < populationSize; i++) {
48          var par0 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
49          var par1 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
50          bool success;
51          newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
52          if (!success) failedEvents++;
53        }
54        crossoverTrees = newPopulation;
55      }
56      stopwatch.Stop();
57      foreach (var tree in crossoverTrees)
58        Assert.IsTrue(tree.IsValidExpression());
59      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;     
60    }
61
62
63
64    [TestMethod()]
65    public void SubtreeCrossoverSpeed() {
66
67      Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
68        Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
69    }
70
71    [TestMethod()]
72    public void SubtreeCrossoverSizeDistributions() {
73      Assert.Inconclusive("SubtreeCrossover: " + Util.GetSizeDistributionString(crossoverTrees, 105, 5));
74    }
75
76    [TestMethod()]
77    public void SubtreeCrossoverFunctionDistributionTest() {
78      Assert.Inconclusive("SubtreeCrossover: " + Util.GetFunctionDistributionString(crossoverTrees));
79    }
80
81    [TestMethod()]
82    public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
83      Assert.Inconclusive("SubtreeCrossover: " + Util.GetNumberOfSubTreesDistributionString(crossoverTrees));
84    }
85
86
87    [TestMethod()]
88    public void SubtreeCrossoverTerminalDistributionTest() {
89      Assert.Inconclusive("SubtreeCrossover: " + Util.GetTerminalDistributionString(crossoverTrees));
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.