Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/13/10 20:44:31 (14 years ago)
Author:
gkronber
Message:

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs

    r3297 r3338  
    1111  [TestClass]
    1212  public class SubtreeCrossoverTest {
    13     public SubtreeCrossoverTest() {
    14     }
     13    private static ISymbolicExpressionGrammar grammar;
     14    private static List<SymbolicExpressionTree> crossoverTrees;
     15    private static double msPerCrossoverEvent;
    1516
    1617    private TestContext testContextInstance;
     
    3435      int populationSize = 1000;
    3536      int generations = 5;
    36       var grammar = new TestGrammar();
     37      int failedEvents = 0;
     38      grammar = Grammars.CreateArithmeticAndAdfGrammar();
    3739      var random = new MersenneTwister();
    3840      for (int i = 0; i < populationSize; i++) {
    39         crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
     41        crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
    4042      }
    4143      Stopwatch stopwatch = new Stopwatch();
     
    4749          var par1 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
    4850          bool success;
    49           newPopulation.Add(SubtreeCrossover.Cross(random, grammar, par0, par1, 0.9, 100, 10, out success));
     51          newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
     52          if (!success) failedEvents++;
    5053        }
    5154        crossoverTrees = newPopulation;
     
    5356      stopwatch.Stop();
    5457      foreach (var tree in crossoverTrees)
    55         Assert.IsTrue(grammar.IsValidExpression(tree));
    56       msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;
     58        Assert.IsTrue(tree.IsValidExpression());
     59      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;     
    5760    }
    5861
    5962
    6063
    61     private static List<SymbolicExpressionTree> crossoverTrees;
    62     private static double msPerCrossoverEvent;
    63 
    64     private class Addition : Symbol { }
    65     private class Subtraction : Symbol { }
    66     private class Multiplication : Symbol { }
    67     private class Division : Symbol { }
    68     private class Terminal : Symbol { }
    69 
    70     private class TestGrammar : DefaultSymbolicExpressionGrammar {
    71       public TestGrammar()
    72         : base(0, 0, 0, 0) {
    73         Initialize();
    74       }
    75 
    76       private void Initialize() {
    77         var add = new Addition();
    78         var sub = new Subtraction();
    79         var mul = new Multiplication();
    80         var div = new Division();
    81         var terminal = new Terminal();
    82 
    83         var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
    84         var functionSymbols = new List<Symbol>() { add, sub, mul, div };
    85         allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
    86 
    87         SetMinSubTreeCount(terminal, 0);
    88         SetMaxSubTreeCount(terminal, 0);
    89         int maxSubTrees = 3;
    90         foreach (var functionSymbol in functionSymbols) {
    91           SetMinSubTreeCount(functionSymbol, 1);
    92           SetMaxSubTreeCount(functionSymbol, maxSubTrees);
    93           foreach (var childSymbol in allSymbols) {
    94             for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
    95               AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
    96             }
    97           }
    98         }
    99       }
    100     }
    101 
    10264    [TestMethod()]
    10365    public void SubtreeCrossoverSpeed() {
     66
    10467      Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
    10568        Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
     
    10770
    10871    [TestMethod()]
    109     public void SubtreeCrossoverSizeDistributionTest() {
    110       int[] histogram = new int[105 / 5];
    111       for (int i = 0; i < crossoverTrees.Count; i++) {
    112         histogram[crossoverTrees[i].Size / 5]++;
    113       }
    114       StringBuilder strBuilder = new StringBuilder();
    115       for (int i = 0; i < histogram.Length; i++) {
    116         strBuilder.Append(Environment.NewLine);
    117         strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
    118         strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)crossoverTrees.Count);
    119       }
    120       Assert.Inconclusive("Size distribution of SubtreeCrossover: " + strBuilder);
     72    public void SubtreeCrossoverSizeDistributions() {
     73      Assert.Inconclusive("SubtreeCrossover: " + Util.GetSizeDistributionString(crossoverTrees, 105, 5));
    12174    }
    12275
    12376    [TestMethod()]
    12477    public void SubtreeCrossoverFunctionDistributionTest() {
    125       Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
    126       double n = 0.0;
    127       for (int i = 0; i < crossoverTrees.Count; i++) {
    128         foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
    129           if (node.SubTrees.Count > 0) {
    130             if (!occurances.ContainsKey(node.Symbol))
    131               occurances[node.Symbol] = 0;
    132             occurances[node.Symbol]++;
    133             n++;
    134           }
    135         }
    136       }
    137       StringBuilder strBuilder = new StringBuilder();
    138       foreach (var function in occurances.Keys) {
    139         strBuilder.Append(Environment.NewLine);
    140         strBuilder.Append(function.Name); strBuilder.Append(": ");
    141         strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
    142       }
    143       Assert.Inconclusive("Function distribution of SubtreeCrossover: " + strBuilder);
     78      Assert.Inconclusive("SubtreeCrossover: " + Util.GetFunctionDistributionString(crossoverTrees));
    14479    }
    14580
    14681    [TestMethod()]
    14782    public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
    148       Dictionary<int, int> occurances = new Dictionary<int, int>();
    149       double n = 0.0;
    150       for (int i = 0; i < crossoverTrees.Count; i++) {
    151         foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
    152           if (!occurances.ContainsKey(node.SubTrees.Count))
    153             occurances[node.SubTrees.Count] = 0;
    154           occurances[node.SubTrees.Count]++;
    155           n++;
    156         }
    157       }
    158       StringBuilder strBuilder = new StringBuilder();
    159       foreach (var arity in occurances.Keys) {
    160         strBuilder.Append(Environment.NewLine);
    161         strBuilder.Append(arity); strBuilder.Append(": ");
    162         strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
    163       }
    164       Assert.Inconclusive("Distribution of function arities of SubtreeCrossover: " + strBuilder);
     83      Assert.Inconclusive("SubtreeCrossover: " + Util.GetNumberOfSubTreesDistributionString(crossoverTrees));
    16584    }
    16685
     
    16887    [TestMethod()]
    16988    public void SubtreeCrossoverTerminalDistributionTest() {
    170       Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
    171       double n = 0.0;
    172       for (int i = 0; i < crossoverTrees.Count; i++) {
    173         foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
    174           if (node.SubTrees.Count == 0) {
    175             if (!occurances.ContainsKey(node.Symbol))
    176               occurances[node.Symbol] = 0;
    177             occurances[node.Symbol]++;
    178             n++;
    179           }
    180         }
    181       }
    182       StringBuilder strBuilder = new StringBuilder();
    183       foreach (var function in occurances.Keys) {
    184         strBuilder.Append(Environment.NewLine);
    185         strBuilder.Append(function.Name); strBuilder.Append(": ");
    186         strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
    187       }
    188       Assert.Inconclusive("Terminal distribution of SubtreeCrossover: " + strBuilder);
     89      Assert.Inconclusive("SubtreeCrossover: " + Util.GetTerminalDistributionString(crossoverTrees));
    18990    }
    19091  }
Note: See TracChangeset for help on using the changeset viewer.