Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/ProbabilisticTreeCreatorTest.cs @ 16824

Last change on this file since 16824 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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;
23using System.Collections.Generic;
24using System.Diagnostics;
25using HeuristicLab.Random;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests {
29  [TestClass]
30  public class ProbabilisticTreeCreatorTest {
31    private const int POPULATION_SIZE = 10000;
32    private const int MAX_TREE_LENGTH = 100;
33    private const int MAX_TREE_DEPTH = 10;
34
35    [TestMethod()]
36    [TestCategory("Encodings.SymbolicExpressionTree")]
37    [TestProperty("Time", "long")]
38    public void ProbabilisticTreeCreaterDistributionsTest() {
39      var randomTrees = new List<ISymbolicExpressionTree>();
40      var grammar = Grammars.CreateSimpleArithmeticGrammar();
41      var random = new MersenneTwister(31415);
42      var stopwatch = new Stopwatch();
43      stopwatch.Start();
44      for (int i = 0; i < POPULATION_SIZE; i++) {
45        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH));
46      }
47      stopwatch.Stop();
48
49      int count = 0;
50      int depth = 0;
51      foreach (var tree in randomTrees) {
52        Util.IsValid(tree);
53        count += tree.Length;
54        depth += tree.Depth;
55      }
56      double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
57
58      Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
59        msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
60        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
61        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
62        Util.GetNumberOfSubtreesDistributionString(randomTrees) + Environment.NewLine +
63        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine +
64        "Average tree depth: " + depth / POPULATION_SIZE + Environment.NewLine +
65        "Average tree length: " + count / POPULATION_SIZE + Environment.NewLine +
66        "Total nodes created: " + count + Environment.NewLine
67        );
68      //mkommend: commented due to performance issues on the builder
69      // Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 250); // must achieve more than 250 random trees / s
70    }
71
72    [TestMethod]
73    [TestCategory("Encodings.SymbolicExpressionTree")]
74    [TestProperty("Time", "short")]
75    public void ProbabilisticTreeCreatorSpecificTreeSizesTest() {
76      var grammar = Grammars.CreateSimpleArithmeticGrammar();
77      var random = new MersenneTwister(31415);
78
79      for (int targetTreeSize = 3; targetTreeSize <= 100; targetTreeSize++) {
80        var tree = ProbabilisticTreeCreator.CreateExpressionTree(random, grammar, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 2);
81        Assert.AreEqual(targetTreeSize, tree.Length);
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.