Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10884 was 10328, checked in by sawinkle, 11 years ago

#2109:

  • Added method comments + refactoring.
  • Implemented RandomMapper.
  • Changed InitialTreeLength (genotype length) of Symbolic Regression problem from 25 to 30, equally to the Artificial Ant problem with also 30.
File size: 5.4 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="bounds">only used for PIGEMapper (ignore here)</param>
55    /// <param name="length">only used for PIGEMapper (ignore here)</param>
56    /// <param name="grammar">grammar definition</param>
57    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
58    /// <returns>phenotype (a symbolic expression tree)</returns>
59    public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
60                                               ISymbolicExpressionGrammar grammar,
61                                               IntegerVector genotype) {
62
63      SymbolicExpressionTree tree = new SymbolicExpressionTree();
64      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
65      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
66      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
67      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
68      rootNode.AddSubtree(startNode);
69      tree.Root = rootNode;
70
71      MapDepthFirstIteratively(startNode, genotype, grammar,
72                               genotype.Length, random);
73      return tree;
74    }
75
76
77    /// <summary>
78    /// Genotype-to-Phenotype mapper (iterative depth-first approach, by using a stack -> LIFO).
79    /// </summary>
80    /// <param name="startNode">first node of the tree with arity 1</param>
81    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
82    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
83    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
84    /// <param name="random">random number generator</param>
85    private void MapDepthFirstIteratively(ISymbolicExpressionTreeNode startNode,
86                                          IntegerVector genotype,
87                                          ISymbolicExpressionGrammar grammar,
88                                          int maxSubtreeCount, IRandom random) {
89
90      Stack<Tuple<ISymbolicExpressionTreeNode, int>> stack
91        = new Stack<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>
92
93      int genotypeIndex = 0;
94      stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));
95
96      while (stack.Count > 0) {
97
98        // get next node from stack and re-push it, if this node still has unhandled subtrees ...
99        Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop();
100        if (current.Item2 > 1) {
101          stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1));
102        }
103
104        if (genotypeIndex >= maxSubtreeCount) {
105          // if all genomes were used, only add terminal nodes to the remaining subtrees
106          current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
107        } else {
108          var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
109          int arity = SampleArity(random, newNode, grammar);
110
111          current.Item1.AddSubtree(newNode);
112          genotypeIndex++;
113          if (arity > 0) {
114            // new node has subtrees so push it onto the stack
115            stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));
116          }
117        }
118      }
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.