Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/ProbabilisticTreeCreatorTest.cs @ 16613

Last change on this file since 16613 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 3.7 KB
RevLine 
[3360]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3360]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;
[3294]23using System.Collections.Generic;
[5686]24using System.Diagnostics;
[3294]25using HeuristicLab.Random;
[4068]26using Microsoft.VisualStudio.TestTools.UnitTesting;
[3294]27
[9764]28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests {
[3294]29  [TestClass]
[13236]30  public class ProbabilisticTreeCreatorTest {
[5549]31    private const int POPULATION_SIZE = 10000;
32    private const int MAX_TREE_LENGTH = 100;
33    private const int MAX_TREE_DEPTH = 10;
[3294]34
35    [TestMethod()]
[9770]36    [TestCategory("Encodings.SymbolicExpressionTree")]
37    [TestProperty("Time", "long")]
[3338]38    public void ProbabilisticTreeCreaterDistributionsTest() {
[5549]39      var randomTrees = new List<ISymbolicExpressionTree>();
[3338]40      var grammar = Grammars.CreateSimpleArithmeticGrammar();
[3369]41      var random = new MersenneTwister(31415);
[5549]42      var stopwatch = new Stopwatch();
43      stopwatch.Start();
[3338]44      for (int i = 0; i < POPULATION_SIZE; i++) {
[5686]45        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH));
[3294]46      }
[5549]47      stopwatch.Stop();
[3294]48
[6949]49      int count = 0;
50      int depth = 0;
[3360]51      foreach (var tree in randomTrees) {
52        Util.IsValid(tree);
[6949]53        count += tree.Length;
54        depth += tree.Depth;
[3360]55      }
[5549]56      double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
57
[5367]58      Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
[5549]59        msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
[3338]60        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
61        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
[5733]62        Util.GetNumberOfSubtreesDistributionString(randomTrees) + Environment.NewLine +
[6949]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
[3338]67        );
[9322]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
[3294]70    }
[6911]71
72    [TestMethod]
[9770]73    [TestCategory("Encodings.SymbolicExpressionTree")]
74    [TestProperty("Time", "short")]
[6911]75    public void ProbabilisticTreeCreatorSpecificTreeSizesTest() {
76      var grammar = Grammars.CreateSimpleArithmeticGrammar();
77      var random = new MersenneTwister(31415);
78
[13227]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);
[6911]82      }
83    }
[3294]84  }
85}
Note: See TracBrowser for help on using the repository browser.