Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/RandomMapper.cs @ 10923

Last change on this file since 10923 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.1 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.GrammaticalEvolution {
31  /// <summary>
32  /// RandomMapper
33  /// </summary>
34  [Item("RandomMapper", "Randomly determines the next non-terminal symbol to expand.")]
35  [StorableClass]
36  public class RandomMapper : GenotypeToPhenotypeMapper {
37
38    [StorableConstructor]
39    protected RandomMapper(bool deserializing) : base(deserializing) { }
40    protected RandomMapper(RandomMapper original, Cloner cloner) : base(original, cloner) { }
41    public RandomMapper() : base() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new RandomMapper(this, cloner);
45    }
46
47
48    /// <summary>
49    /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
50    /// Random approach.
51    /// </summary>
52    /// <param name="random">random number generator</param>
53    /// <param name="bounds">only used for PIGEMapper (ignore here)</param>
54    /// <param name="length">only used for PIGEMapper (ignore here)</param>
55    /// <param name="grammar">grammar definition</param>
56    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
57    /// <returns>phenotype (a symbolic expression tree)</returns>
58    public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
59                                               ISymbolicExpressionGrammar grammar,
60                                               IntegerVector genotype) {
61
62      SymbolicExpressionTree tree = new SymbolicExpressionTree();
63      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
64      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
65      rootNode.AddSubtree(startNode);
66      tree.Root = rootNode;
67
68      MapRandomIteratively(startNode, genotype, grammar,
69                           genotype.Length, random);
70
71      return tree;
72    }
73
74
75    /// <summary>
76    /// Genotype-to-Phenotype mapper (iterative random approach, where the next non-terminal
77    /// symbol to expand is randomly determined).
78    /// </summary>
79    /// <param name="startNode">first node of the tree with arity 1</param>
80    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
81    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
82    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
83    /// <param name="random">random number generator</param>
84    private void MapRandomIteratively(ISymbolicExpressionTreeNode startNode,
85                                     IntegerVector genotype,
86                                     ISymbolicExpressionGrammar grammar,
87                                     int maxSubtreeCount, IRandom random) {
88
89      List<ISymbolicExpressionTreeNode> nonTerminals = new List<ISymbolicExpressionTreeNode>();
90
91      int genotypeIndex = 0;
92      nonTerminals.Add(startNode);
93
94      while (nonTerminals.Count > 0) {
95        if (genotypeIndex >= maxSubtreeCount) {
96          // if all genomes were used, only add terminal nodes to the remaining subtrees
97          ISymbolicExpressionTreeNode current = nonTerminals[0];
98          nonTerminals.RemoveAt(0);
99          current.AddSubtree(GetRandomTerminalNode(current, grammar, random));
100        } else {
101          // similar to PIGEMapper, but here the current node is determined randomly ...
102          ISymbolicExpressionTreeNode current = nonTerminals.SelectRandom(random);
103          nonTerminals.Remove(current);
104
105          ISymbolicExpressionTreeNode newNode = GetNewChildNode(current, genotype, grammar, genotypeIndex, random);
106          int arity = SampleArity(random, newNode, grammar);
107
108          current.AddSubtree(newNode);
109          genotypeIndex++;
110          // new node has subtrees, so add "arity" number of copies of this node to the nonTerminals list
111          for (int i = 0; i < arity; ++i) {
112            nonTerminals.Add(newNode);
113          }
114        }
115      }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.