1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
|
---|
27 | using HeuristicLab.Random;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
|
---|
31 | [TestClass]
|
---|
32 | public class ProbabilisticTreeCreaterTest {
|
---|
33 | private const int POPULATION_SIZE = 10000;
|
---|
34 | private const int MAX_TREE_SIZE = 100;
|
---|
35 | private const int MAX_TREE_HEIGHT = 10;
|
---|
36 | private TestContext testContextInstance;
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | ///Gets or sets the test context which provides
|
---|
40 | ///information about and functionality for the current test run.
|
---|
41 | ///</summary>
|
---|
42 | public TestContext TestContext {
|
---|
43 | get {
|
---|
44 | return testContextInstance;
|
---|
45 | }
|
---|
46 | set {
|
---|
47 | testContextInstance = value;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [TestMethod()]
|
---|
52 | public void ProbabilisticTreeCreaterDistributionsTest() {
|
---|
53 | var randomTrees = new List<SymbolicExpressionTree>();
|
---|
54 | var grammar = Grammars.CreateSimpleArithmeticGrammar();
|
---|
55 | var random = new MersenneTwister(31415);
|
---|
56 | var stopwatch = new Stopwatch();
|
---|
57 | stopwatch.Start();
|
---|
58 | for (int i = 0; i < POPULATION_SIZE; i++) {
|
---|
59 | randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 0, 0));
|
---|
60 | }
|
---|
61 | stopwatch.Stop();
|
---|
62 |
|
---|
63 | foreach (var tree in randomTrees) {
|
---|
64 | Util.IsValid(tree);
|
---|
65 | }
|
---|
66 | double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
|
---|
67 |
|
---|
68 | Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
|
---|
69 | msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
|
---|
70 | Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
|
---|
71 | Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
|
---|
72 | Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine +
|
---|
73 | Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
|
---|
74 | );
|
---|
75 | Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 500); // must achieve more than 2000 random trees / s
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | [TestMethod()]
|
---|
80 | public void ProbabilisticTreeCreaterWithAdfDistributionsTest() {
|
---|
81 | var randomTrees = new List<SymbolicExpressionTree>();
|
---|
82 | var grammar = Grammars.CreateArithmeticAndAdfGrammar();
|
---|
83 | var random = new MersenneTwister(31415);
|
---|
84 | var stopwatch = new Stopwatch();
|
---|
85 | stopwatch.Start();
|
---|
86 | for (int i = 0; i < POPULATION_SIZE; i++) {
|
---|
87 | randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3));
|
---|
88 | }
|
---|
89 | stopwatch.Stop();
|
---|
90 | foreach (var tree in randomTrees)
|
---|
91 | Util.IsValid(tree);
|
---|
92 |
|
---|
93 | double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
|
---|
94 |
|
---|
95 | Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
|
---|
96 | msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
|
---|
97 | Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
|
---|
98 | Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
|
---|
99 | Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine +
|
---|
100 | Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
|
---|
101 | );
|
---|
102 |
|
---|
103 | Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 2000 random trees / s
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|