Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/BreathFirstMapper.cs @ 10280

Last change on this file since 10280 was 10280, checked in by sawinkle, 10 years ago

#2109: Fetched random number generator in Evaluator implementations from scope, so that the same results are produced using the same seed.

File size: 5.5 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.GrammaticalEvolution {
31  /// <summary>
32  /// BreathFirstMapper
33  /// </summary>
34  [Item("BreathFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a breath-first manner.")]
35  [StorableClass]
36  public class BreathFirstMapper : GenotypeToPhenotypeMapper {
37
38    [StorableConstructor]
39    protected BreathFirstMapper(bool deserializing) : base(deserializing) { }
40    protected BreathFirstMapper(BreathFirstMapper original, Cloner cloner) : base(original, cloner) { }
41    public BreathFirstMapper() : base() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new BreathFirstMapper(this, cloner);
45    }
46
47
48    /// <summary>
49    /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
50    /// Breath-first approach.
51    /// </summary>
52    /// <param name="random">random number generator</param>
53    /// <param name="grammar">grammar definition</param>
54    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
55    /// <returns>phenotype (a symbolic expression tree)</returns>
56    public override SymbolicExpressionTree Map(IRandom random,
57                                               ISymbolicExpressionGrammar grammar,
58                                               IntegerVector genotype) {
59
60      SymbolicExpressionTree tree = new SymbolicExpressionTree();
61      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
62      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
63      rootNode.AddSubtree(startNode);
64      tree.Root = rootNode;
65
66      MapBreathFirstIteratively(startNode, genotype, grammar,
67                                genotype.Length, random);
68
69      return tree;
70    }
71
72    /// <summary>
73    /// Genotype-to-Phenotype mapper (iterative breath-first approach, by using a queue -> FIFO).
74    /// </summary>
75    /// <param name="startNode">first node of the tree with arity 1</param>
76    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
77    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
78    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
79    /// <param name="random">random number generator</param>
80    private void MapBreathFirstIteratively(ISymbolicExpressionTreeNode startNode,
81                                          IntegerVector genotype,
82                                          ISymbolicExpressionGrammar grammar,
83                                          int maxSubtreeCount, IRandom random) {
84
85      Queue<Tuple<ISymbolicExpressionTreeNode, int>> queue
86        = new Queue<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>
87
88      int genotypeIndex = 0;
89      int currSubtreeCount = 1;
90
91      queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));
92
93      while ((currSubtreeCount < maxSubtreeCount) && (queue.Count > 0)) {
94
95        Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();
96
97        // foreach subtree of the current node, create a new node and enqueue it, if it is no terminal node
98        for (int i = 0; i < current.Item2; ++i) {
99
100          var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
101          int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount, grammar);
102
103          if (arity < 0) {
104            current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
105          } else {
106            current.Item1.AddSubtree(newNode);
107            genotypeIndex++;
108            currSubtreeCount += arity;
109            if (arity > 0) {
110              // new node has subtrees so enqueue the node
111              queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));
112            }
113          }
114        }
115      }
116
117      // maximum allowed subtree count was already reached, but there are still
118      // incomplete subtrees (non-terminal symbols) in the tree
119      // -> fill them with terminal symbols
120      while (queue.Count > 0) {
121        Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();
122        for (int i = 0; i < current.Item2; ++i) {
123          current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
124        }
125      }
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.