Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/Mappers/GenotypeToPhenotypeMapper.cs @ 13395

Last change on this file since 13395 was 13395, checked in by pfleck, 8 years ago

#2525

  • Changed unit test that it also lists types that have a StorableConstructor but are not marked as StorableClass.
  • Put both storable tests in a single StorableTest.cs.
  • Added missing StorableClassAttributes.
File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 * Author: Sabine Winkler
21 */
22#endregion
23
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.GrammaticalEvolution {
36  /// <summary>
37  /// Abstract base class for GenotypeToPhenotypeMappers
38  /// </summary>
39  [StorableClass]
40  public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper {
41
42    [StorableConstructor]
43    protected GenotypeToPhenotypeMapper(bool deserializing) : base(deserializing) { }
44    protected GenotypeToPhenotypeMapper(GenotypeToPhenotypeMapper original, Cloner cloner) : base(original, cloner) { }
45    protected GenotypeToPhenotypeMapper() : base() { }
46
47    public abstract ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
48                                               ISymbolicExpressionGrammar grammar,
49                                               IntegerVector genotype);
50
51    /// <summary>
52    /// Randomly returns a terminal node for the given <paramref name="parentNode"/>.
53    /// (A terminal has got a minimum and maximum arity of 0.)
54    /// </summary>
55    /// <param name="parentNode">parent node for which a child node is returned randomly</param>
56    /// <param name="grammar">grammar to determine the allowed child symbols for parentNode</param>
57    /// <param name="random">random number generator</param>
58    /// <returns>randomly chosen terminal node with arity 0 or null, if no terminal node exists</returns>
59    protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode,
60                                                                ISymbolicExpressionGrammar grammar,
61                                                                IRandom random) {
62      // only select specific symbols, which can be interpreted ...
63      var possibleSymbolsList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol)
64                                 where s.InitialFrequency > 0.0
65                                 where s.MaximumArity == 0
66                                 where s.MinimumArity == 0
67                                 select s).ToList();
68
69      // no terminal node exists for the given parent node
70      if (!possibleSymbolsList.Any()) return null;
71
72      var newNode = possibleSymbolsList.SampleRandom(random).CreateTreeNode();
73      if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
74      return newNode;
75    }
76
77
78    /// <summary>
79    /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>.
80    /// </summary>
81    /// <param name="parentNode">parent node to find a child node randomly for</param>
82    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
83    /// <param name="grammar">grammar used to define the allowed child symbols</param>
84    /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param>
85    /// <param name="random">random number generator</param>
86    /// <returns>randomly chosen child node or null, if no child node exits</returns>
87    protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode,
88                                                          IntegerVector genotype,
89                                                          ISymbolicExpressionGrammar grammar,
90                                                          int genotypeIndex,
91                                                          IRandom random) {
92
93      // only select specific symbols, which can be interpreted ...
94      IEnumerable<ISymbol> symbolList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol)
95                                         where s.InitialFrequency > 0.0
96                                         select s).ToList();
97
98      int prodRuleCount = symbolList.Count();
99
100      // no child node exists for the given parent node
101      if (prodRuleCount < 1) return null;
102
103      // genotypeIndex % genotype.Length, if wrapping is allowed
104      int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount;
105
106      var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode();
107      if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
108      return newNode;
109    }
110
111
112    /// <summary>
113    /// Randomly determines an arity for the given node.
114    /// </summary>
115    /// <param name="random">random number generator</param>
116    /// <param name="node">node, for which a random arity is determined</param>
117    /// <param name="grammar">symbolic expression grammar to use</param>
118    /// <returns>random arity in the interval [minArity, maxArity]</returns>
119    protected int SampleArity(IRandom random,
120                              ISymbolicExpressionTreeNode node,
121                              ISymbolicExpressionGrammar grammar) {
122
123      int minArity = grammar.GetMinimumSubtreeCount(node.Symbol);
124      int maxArity = grammar.GetMaximumSubtreeCount(node.Symbol);
125
126      if (minArity == maxArity) {
127        return minArity;
128      }
129
130      return random.Next(minArity, maxArity);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.