Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11176 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
37    [TestMethod]
38    [Timeout(3600000)]
39    [TestCategory("Encodings.SymbolicExpressionTree")]
40    [TestProperty("Time", "long")]
41    public void AllArchitectureAlteringOperatorsDistributionTest() {
42      var trees = new List<ISymbolicExpressionTree>();
43      var newTrees = new List<ISymbolicExpressionTree>();
44      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
45      var random = new MersenneTwister(31415);
46      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
47      IntValue maxTreeSize = new IntValue(MAX_TREE_LENGTH);
48      IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH);
49      IntValue maxDefuns = new IntValue(3);
50      IntValue maxArgs = new IntValue(3);
51      for (int i = 0; i < POPULATION_SIZE; i++) {
52        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
53        Util.IsValid(tree);
54        trees.Add(tree);
55      }
56      Stopwatch stopwatch = new Stopwatch();
57      int failedEvents = 0;
58      for (int g = 0; g < N_ITERATIONS; g++) {
59        for (int i = 0; i < POPULATION_SIZE; i++) {
60          if (random.NextDouble() < 0.5) {
61            // manipulate
62            stopwatch.Start();
63            var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
64            var oldTree = (ISymbolicExpressionTree)selectedTree.Clone();
65            bool success = false;
66            int sw = random.Next(6);
67            switch (sw) {
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            }
75            stopwatch.Stop();
76            if (!success) failedEvents++;
77            Util.IsValid(selectedTree);
78            newTrees.Add(selectedTree);
79          } else {
80            stopwatch.Start();
81            // crossover
82            SymbolicExpressionTree par0 = null;
83            SymbolicExpressionTree par1 = null;
84            do {
85              par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
86              par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
87            } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH);
88            var newTree = SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
89            stopwatch.Stop();
90            Util.IsValid(newTree);
91            newTrees.Add(newTree);
92          }
93        }
94        trees = new List<ISymbolicExpressionTree>(newTrees);
95        newTrees.Clear();
96      }
97      var msPerOperation = stopwatch.ElapsedMilliseconds / ((double)POPULATION_SIZE * (double)N_ITERATIONS);
98      Console.WriteLine("AllArchitectureAlteringOperators: " + Environment.NewLine +
99        "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine +
100        "Failed events: " + failedEvents * 100.0 / (double)(POPULATION_SIZE * N_ITERATIONS / 2.0) + "%" + Environment.NewLine +
101        Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
102        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
103        Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
104        Util.GetTerminalDistributionString(trees) + Environment.NewLine
105        );
106
107      Assert.IsTrue(failedEvents * 100.0 / (POPULATION_SIZE * N_ITERATIONS / 2.0) < 75.0); // 25% of architecture operations must succeed
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
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.