Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/GenotypeToPhenotypeMapper.cs @ 10328

Last change on this file since 10328 was 10328, checked in by sawinkle, 10 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: 6.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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
31
32namespace HeuristicLab.Problems.GrammaticalEvolution {
33  /// <summary>
34  /// GenotypeToPhenotypeMapper
35  /// </summary>
36  public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper {
37
38    [StorableConstructor]
39    protected GenotypeToPhenotypeMapper(bool deserializing) : base(deserializing) { }
40    protected GenotypeToPhenotypeMapper(GenotypeToPhenotypeMapper original, Cloner cloner) : base(original, cloner) { }
41    protected GenotypeToPhenotypeMapper() : base() { }
42
43    public abstract SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
44                                               ISymbolicExpressionGrammar grammar,
45                                               IntegerVector genotype);
46
47    /// <summary>
48    /// Randomly returns a terminal node for the given <paramref name="parentNode"/>.
49    /// (A terminal has got a minimum and maximum arity of 0.)
50    /// </summary>
51    /// <param name="parentNode">parent node for which a child node is returned randomly</param>
52    /// <param name="grammar">grammar to determine the allowed child symbols for parentNode</param>
53    /// <param name="random">random number generator</param>
54    /// <returns>randomly chosen terminal node with arity 0 or null, if no terminal node exists</returns>
55    protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode,
56                                                                ISymbolicExpressionGrammar grammar,
57                                                                IRandom random) {
58      // only select specific symbols, which can be interpreted ...
59      var possibleSymbolsList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol)
60                                 where s.InitialFrequency > 0.0
61                                 where s.MaximumArity == 0
62                                 where s.MinimumArity == 0
63                                 select s).ToList();
64
65      // no terminal node exists for the given parent node
66      if (possibleSymbolsList.Count() < 1) return null;
67
68      var newNode = possibleSymbolsList.SelectRandom(random).CreateTreeNode();
69      if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
70      return newNode;
71    }
72
73
74    /// <summary>
75    /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>.
76    /// </summary>
77    /// <param name="parentNode">parent node to find a child node randomly for</param>
78    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
79    /// <param name="grammar">grammar used to define the allowed child symbols</param>
80    /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param>
81    /// <param name="random">random number generator</param>
82    /// <returns>randomly chosen child node or null, if no child node exits</returns>
83    protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode,
84                                                          IntegerVector genotype,
85                                                          ISymbolicExpressionGrammar grammar,
86                                                          int genotypeIndex,
87                                                          IRandom random) {
88
89      // only select specific symbols, which can be interpreted ...
90      IEnumerable<ISymbol> symbolList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol)
91                                         where s.InitialFrequency > 0.0
92                                         select s).ToList();
93
94      int prodRuleCount = symbolList.Count();
95
96      // no child node exists for the given parent node
97      if (prodRuleCount < 1) return null;
98
99      // genotypeIndex % genotype.Length, if wrapping is allowed
100      int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount;
101
102      var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode();
103      if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
104      return newNode;
105    }
106
107
108    /// <summary>
109    /// Randomly determines an arity for the given node.
110    /// </summary>
111    /// <param name="random">random number generator</param>
112    /// <param name="node">node, for which a random arity is determined</param>
113    /// <param name="grammar">symbolic expression grammar to use</param>
114    /// <returns>random arity in the interval [minArity, maxArity]</returns>
115    protected int SampleArity(IRandom random,
116                              ISymbolicExpressionTreeNode node,
117                              ISymbolicExpressionGrammar grammar) {
118
119      int minArity = grammar.GetMinimumSubtreeCount(node.Symbol);
120      int maxArity = grammar.GetMaximumSubtreeCount(node.Symbol);
121
122      if (minArity == maxArity) {
123        return minArity;
124      }
125
126      return random.Next(minArity, maxArity);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.