Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/AllArchitectureAlteringOperatorsTest.cs @ 17975

Last change on this file since 17975 was 17975, checked in by gkronber, 3 years ago

#3067: merged r17490:17492 and r17871:17872 from trunk to stable

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