Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/AllArchitectureAlteringOperatorsTest.cs @ 9770

Last change on this file since 9770 was 9770, checked in by mkommend, 11 years ago

#2088: Removed useless brackets in the TestMethod attribute and added test categories and properties for SymExpTreeEncoding unit test.

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