[3360] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.Diagnostics;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[3360] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[4068] | 27 | using HeuristicLab.Random;
|
---|
| 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[3360] | 29 |
|
---|
[7915] | 30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_34.Tests {
|
---|
[3360] | 31 | [TestClass]
|
---|
| 32 | public class AllArchitectureAlteringOperatorsTest {
|
---|
| 33 | private const int POPULATION_SIZE = 1000;
|
---|
[5792] | 34 | private const int N_ITERATIONS = 200;
|
---|
[5549] | 35 | private const int MAX_TREE_LENGTH = 100;
|
---|
| 36 | private const int MAX_TREE_DEPTH = 10;
|
---|
[3360] | 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()]
|
---|
[6923] | 53 | [Timeout(3600000)]
|
---|
[3360] | 54 | public void AllArchitectureAlteringOperatorsDistributionTest() {
|
---|
[5549] | 55 | var trees = new List<ISymbolicExpressionTree>();
|
---|
| 56 | var newTrees = new List<ISymbolicExpressionTree>();
|
---|
[3360] | 57 | var grammar = Grammars.CreateArithmeticAndAdfGrammar();
|
---|
| 58 | var random = new MersenneTwister(31415);
|
---|
[5686] | 59 | SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
|
---|
[5549] | 60 | IntValue maxTreeSize = new IntValue(MAX_TREE_LENGTH);
|
---|
| 61 | IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH);
|
---|
[3360] | 62 | IntValue maxDefuns = new IntValue(3);
|
---|
| 63 | IntValue maxArgs = new IntValue(3);
|
---|
| 64 | for (int i = 0; i < POPULATION_SIZE; i++) {
|
---|
[5686] | 65 | var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
|
---|
[3360] | 66 | Util.IsValid(tree);
|
---|
| 67 | trees.Add(tree);
|
---|
| 68 | }
|
---|
| 69 | Stopwatch stopwatch = new Stopwatch();
|
---|
[5367] | 70 | int failedEvents = 0;
|
---|
[3360] | 71 | for (int g = 0; g < N_ITERATIONS; g++) {
|
---|
| 72 | for (int i = 0; i < POPULATION_SIZE; i++) {
|
---|
[3369] | 73 | if (random.NextDouble() < 0.5) {
|
---|
| 74 | // manipulate
|
---|
[5686] | 75 | stopwatch.Start();
|
---|
[5549] | 76 | var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
|
---|
[5686] | 77 | var oldTree = (ISymbolicExpressionTree)selectedTree.Clone();
|
---|
[3534] | 78 | bool success = false;
|
---|
[5686] | 79 | int sw = random.Next(6);
|
---|
| 80 | switch (sw) {
|
---|
[5549] | 81 | case 0: success = ArgumentCreater.CreateNewArgument(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
|
---|
| 82 | case 1: success = ArgumentDeleter.DeleteArgument(random, selectedTree, 3, 3); break;
|
---|
| 83 | case 2: success = ArgumentDuplicater.DuplicateArgument(random, selectedTree, 3, 3); break;
|
---|
| 84 | case 3: success = SubroutineCreater.CreateSubroutine(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
|
---|
| 85 | case 4: success = SubroutineDuplicater.DuplicateSubroutine(random, selectedTree, 3, 3); break;
|
---|
| 86 | case 5: success = SubroutineDeleter.DeleteSubroutine(random, selectedTree, 3, 3); break;
|
---|
| 87 | }
|
---|
[5686] | 88 | stopwatch.Stop();
|
---|
[5549] | 89 | if (!success) failedEvents++;
|
---|
[3369] | 90 | Util.IsValid(selectedTree);
|
---|
| 91 | newTrees.Add(selectedTree);
|
---|
| 92 | } else {
|
---|
[5918] | 93 | stopwatch.Start();
|
---|
[3369] | 94 | // crossover
|
---|
[5367] | 95 | SymbolicExpressionTree par0 = null;
|
---|
| 96 | SymbolicExpressionTree par1 = null;
|
---|
| 97 | do {
|
---|
| 98 | par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
|
---|
| 99 | par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
|
---|
[5549] | 100 | } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH);
|
---|
[5792] | 101 | var newTree = SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
|
---|
[5918] | 102 | stopwatch.Stop();
|
---|
[5792] | 103 | Util.IsValid(newTree);
|
---|
| 104 | newTrees.Add(newTree);
|
---|
[3369] | 105 | }
|
---|
[3360] | 106 | }
|
---|
[5792] | 107 | trees = new List<ISymbolicExpressionTree>(newTrees);
|
---|
| 108 | newTrees.Clear();
|
---|
[3360] | 109 | }
|
---|
[5917] | 110 | var msPerOperation = stopwatch.ElapsedMilliseconds / ((double)POPULATION_SIZE * (double)N_ITERATIONS);
|
---|
[5367] | 111 | Console.WriteLine("AllArchitectureAlteringOperators: " + Environment.NewLine +
|
---|
[3360] | 112 | "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine +
|
---|
[5917] | 113 | "Failed events: " + failedEvents * 100.0 / (double)(POPULATION_SIZE * N_ITERATIONS / 2.0) + "%" + Environment.NewLine +
|
---|
[3360] | 114 | Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
|
---|
| 115 | Util.GetFunctionDistributionString(trees) + Environment.NewLine +
|
---|
[5733] | 116 | Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
|
---|
[3360] | 117 | Util.GetTerminalDistributionString(trees) + Environment.NewLine
|
---|
| 118 | );
|
---|
[5411] | 119 |
|
---|
[5918] | 120 | Assert.IsTrue(failedEvents * 100.0 / (POPULATION_SIZE * N_ITERATIONS / 2.0) < 75.0); // 25% of architecture operations must succeed
|
---|
[6824] | 121 | Assert.IsTrue(Math.Round(1000.0 / (msPerOperation)) > 800); // must achieve more than 800 ops per second
|
---|
[3360] | 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|