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