Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs @ 10290

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

#2109:

  • Implemented PIGEMapper. Due to that, it was necessary to modify the Map method interface to additionally take the bounds and length of the genotype.
  • Corrected and simplified the different mappers. Simplified the SampleArity method of /Mappers/GenotypeToPhenotypeMapper.cs
File size: 5.3 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.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GrammaticalEvolution {
32  /// <summary>
33  /// DepthFirstMapper
34  /// </summary>
35  [Item("DepthFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a depth-first manner.")]
36  [StorableClass]
37  public class DepthFirstMapper : GenotypeToPhenotypeMapper {
38
39    [StorableConstructor]
40    protected DepthFirstMapper(bool deserializing) : base(deserializing) { }
41    protected DepthFirstMapper(DepthFirstMapper original, Cloner cloner) : base(original, cloner) { }
42    public DepthFirstMapper() : base() { }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new DepthFirstMapper(this, cloner);
46    }
47
48
49    /// <summary>
50    /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
51    /// Depth-first approach.
52    /// </summary>
53    /// <param name="random">random number generator</param>
54    /// <param name="grammar">grammar definition</param>
55    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
56    /// <returns>phenotype (a symbolic expression tree)</returns>
57    public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
58                                               ISymbolicExpressionGrammar grammar,
59                                               IntegerVector genotype) {
60
61      SymbolicExpressionTree tree = new SymbolicExpressionTree();
62      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
63      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
64      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
65      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
66      rootNode.AddSubtree(startNode);
67      tree.Root = rootNode;
68
69      MapDepthFirstIteratively(startNode, genotype, grammar,
70                               genotype.Length, random);
71      return tree;
72    }
73
74
75    /// <summary>
76    /// Genotype-to-Phenotype mapper (iterative depth-first approach, by using a stack -> LIFO).
77    /// </summary>
78    /// <param name="startNode">first node of the tree with arity 1</param>
79    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
80    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
81    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
82    /// <param name="random">random number generator</param>
83    private void MapDepthFirstIteratively(ISymbolicExpressionTreeNode startNode,
84                                          IntegerVector genotype,
85                                          ISymbolicExpressionGrammar grammar,
86                                          int maxSubtreeCount, IRandom random) {
87
88      Stack<Tuple<ISymbolicExpressionTreeNode, int>> stack
89        = new Stack<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>
90
91      int genotypeIndex = 0;
92      stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));
93
94      while (stack.Count > 0) {
95
96        // get next node from stack and re-push it, if this node still has unhandled subtrees ...
97        Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop();
98        if (current.Item2 > 1) {
99          stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1));
100        }
101
102        if (genotypeIndex >= maxSubtreeCount) {
103          // if all genomes were used, only add terminal nodes to the remaining subtrees
104          current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
105        } else {
106          var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
107          int arity = SampleArity(random, newNode, grammar);
108
109          current.Item1.AddSubtree(newNode);
110          genotypeIndex++;
111          if (arity > 0) {
112            // new node has subtrees so push it onto the stack
113            stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));
114          }
115        }
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.