Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/AllArchitectureAlteringOperatorsTest.cs @ 5551

Last change on this file since 5551 was 5549, checked in by gkronber, 14 years ago

#1418 unified size/height vs. length/depth terminology and adapted unit tests for symbolic expression tree encoding version 3.4

File size: 5.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Random;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
31  [TestClass]
32  public class AllArchitectureAlteringOperatorsTest {
33    private const int POPULATION_SIZE = 1000;
34    private const int N_ITERATIONS = 20;
35    private const int MAX_TREE_LENGTH = 100;
36    private const int MAX_TREE_DEPTH = 10;
37    private TestContext testContextInstance;
38
39    /// <summary>
40    ///Gets or sets the test context which provides
41    ///information about and functionality for the current test run.
42    ///</summary>
43    public TestContext TestContext {
44      get {
45        return testContextInstance;
46      }
47      set {
48        testContextInstance = value;
49      }
50    }
51
52    [TestMethod()]
53    public void AllArchitectureAlteringOperatorsDistributionTest() {
54      var trees = new List<ISymbolicExpressionTree>();
55      var newTrees = new List<ISymbolicExpressionTree>();
56      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
57      var random = new MersenneTwister(31415);
58      IntValue maxTreeSize = new IntValue(MAX_TREE_LENGTH);
59      IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH);
60      IntValue maxDefuns = new IntValue(3);
61      IntValue maxArgs = new IntValue(3);
62      for (int i = 0; i < POPULATION_SIZE; i++) {
63        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
64        Util.IsValid(tree);
65        trees.Add(tree);
66      }
67      Stopwatch stopwatch = new Stopwatch();
68      stopwatch.Start();
69      int failedEvents = 0;
70      for (int g = 0; g < N_ITERATIONS; g++) {
71        for (int i = 0; i < POPULATION_SIZE; i++) {
72          if (random.NextDouble() < 0.5) {
73            // manipulate
74            var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
75            bool success = false;
76            switch (random.Next(6)) {
77              case 0: success = ArgumentCreater.CreateNewArgument(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
78              case 1: success = ArgumentDeleter.DeleteArgument(random, selectedTree, 3, 3); break;
79              case 2: success = ArgumentDuplicater.DuplicateArgument(random, selectedTree, 3, 3); break;
80              case 3: success = SubroutineCreater.CreateSubroutine(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
81              case 4: success = SubroutineDuplicater.DuplicateSubroutine(random, selectedTree, 3, 3); break;
82              case 5: success = SubroutineDeleter.DeleteSubroutine(random, selectedTree, 3, 3); break;
83            }
84            if (!success) failedEvents++;
85            Util.IsValid(selectedTree);
86            newTrees.Add(selectedTree);
87          } else {
88            // crossover
89            SymbolicExpressionTree par0 = null;
90            SymbolicExpressionTree par1 = null;
91            do {
92              par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
93              par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
94            } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH);
95            newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH));
96          }
97        }
98        trees = newTrees;
99      }
100      stopwatch.Stop();
101      var msPerOperation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)N_ITERATIONS;
102      Console.WriteLine("AllArchitectureAlteringOperators: " + Environment.NewLine +
103        "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine +
104        "Failed events: " + failedEvents * 100.0 / (double)(POPULATION_SIZE * N_ITERATIONS * 2.0) + "%" + Environment.NewLine +
105        Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
106        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
107        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
108        Util.GetTerminalDistributionString(trees) + Environment.NewLine
109        );
110
111      Assert.IsTrue(failedEvents * 100.0 / (POPULATION_SIZE * N_ITERATIONS * 2.0) < 25.0); // 75% of architecture operations must succeed
112      Assert.IsTrue(Math.Round(1000.0 / (msPerOperation)) > 1000); // must achieve more than 1000 ops per second
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.