Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/SubtreeCrossoverTest.cs @ 9785

Last change on this file since 9785 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: 3.2 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.Random;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests {
29  [TestClass]
30  public class SubtreeCrossoverTest {
31    private const int POPULATION_SIZE = 1000;
32    private TestContext testContextInstance;
33
34    [TestMethod]
35    [TestCategory("Encodings.SymbolicExpressionTree")]
36    [TestProperty("Time", "long")]
37    public void SubtreeCrossoverDistributionsTest() {
38      int generations = 5;
39      var trees = new List<ISymbolicExpressionTree>();
40      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
41      var random = new MersenneTwister(31415);
42      double msPerCrossoverEvent;
43
44      for (int i = 0; i < POPULATION_SIZE; i++) {
45        trees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
46        for (int j = random.Next(3); j < 3; j++)
47          SubroutineCreater.CreateSubroutine(random, trees[i], 100, 10, 3, 3);
48      }
49      Stopwatch stopwatch = new Stopwatch();
50      stopwatch.Start();
51      for (int gCount = 0; gCount < generations; gCount++) {
52        for (int i = 0; i < POPULATION_SIZE; i++) {
53          var par0 = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
54          var par1 = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
55          SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10);
56        }
57      }
58      stopwatch.Stop();
59      foreach (var tree in trees)
60        Util.IsValid(tree);
61
62      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)generations;
63
64      Console.WriteLine("SubtreeCrossover: " + Environment.NewLine +
65        msPerCrossoverEvent + " ms per crossover event (~" + Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)" + Environment.NewLine +
66        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
67        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
68        Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
69        Util.GetTerminalDistributionString(trees) + Environment.NewLine
70        );
71
72      //mkommend: commented due to performance issues on the builder
73      //Assert.IsTrue(Math.Round(1000.0 / (msPerCrossoverEvent)) > 2000); // must achieve more than 2000 x-overs/s
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.