[10039] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10039] | 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/>.
|
---|
[10968] | 19 | *
|
---|
| 20 | * Author: Sabine Winkler
|
---|
[10039] | 21 | */
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[10228] | 27 | using HeuristicLab.Core;
|
---|
[10290] | 28 | using HeuristicLab.Data;
|
---|
[10039] | 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>
|
---|
[10974] | 36 | /// Abstract base class for GenotypeToPhenotypeMappers
|
---|
[10039] | 37 | /// </summary>
|
---|
| 38 | public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper {
|
---|
[10068] | 39 |
|
---|
[10039] | 40 | [StorableConstructor]
|
---|
| 41 | protected GenotypeToPhenotypeMapper(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected GenotypeToPhenotypeMapper(GenotypeToPhenotypeMapper original, Cloner cloner) : base(original, cloner) { }
|
---|
| 43 | protected GenotypeToPhenotypeMapper() : base() { }
|
---|
[10068] | 44 |
|
---|
[10290] | 45 | public abstract SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
|
---|
[10280] | 46 | ISymbolicExpressionGrammar grammar,
|
---|
[10039] | 47 | IntegerVector genotype);
|
---|
[10068] | 48 |
|
---|
[10039] | 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>
|
---|
[10228] | 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>
|
---|
[10039] | 57 | protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode,
|
---|
[10228] | 58 | ISymbolicExpressionGrammar grammar,
|
---|
| 59 | IRandom random) {
|
---|
[10075] | 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();
|
---|
[10228] | 66 |
|
---|
| 67 | // no terminal node exists for the given parent node
|
---|
[10974] | 68 | if (!possibleSymbolsList.Any()) return null;
|
---|
[10228] | 69 |
|
---|
| 70 | var newNode = possibleSymbolsList.SelectRandom(random).CreateTreeNode();
|
---|
| 71 | if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
|
---|
[10075] | 72 | return newNode;
|
---|
[10039] | 73 | }
|
---|
[10068] | 74 |
|
---|
| 75 |
|
---|
[10039] | 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>
|
---|
[10228] | 81 | /// <param name="grammar">grammar used to define the allowed child symbols</param>
|
---|
[10039] | 82 | /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param>
|
---|
[10228] | 83 | /// <param name="random">random number generator</param>
|
---|
| 84 | /// <returns>randomly chosen child node or null, if no child node exits</returns>
|
---|
[10039] | 85 | protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode,
|
---|
| 86 | IntegerVector genotype,
|
---|
| 87 | ISymbolicExpressionGrammar grammar,
|
---|
[10228] | 88 | int genotypeIndex,
|
---|
| 89 | IRandom random) {
|
---|
[10068] | 90 |
|
---|
[10075] | 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();
|
---|
[10228] | 95 |
|
---|
[10039] | 96 | int prodRuleCount = symbolList.Count();
|
---|
[10228] | 97 |
|
---|
| 98 | // no child node exists for the given parent node
|
---|
| 99 | if (prodRuleCount < 1) return null;
|
---|
| 100 |
|
---|
[10328] | 101 | // genotypeIndex % genotype.Length, if wrapping is allowed
|
---|
[10290] | 102 | int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount;
|
---|
[10068] | 103 |
|
---|
[10075] | 104 | var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode();
|
---|
[10228] | 105 | if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
|
---|
[10075] | 106 | return newNode;
|
---|
[10039] | 107 | }
|
---|
[10228] | 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>
|
---|
[10277] | 115 | /// <param name="grammar">symbolic expression grammar to use</param>
|
---|
[10290] | 116 | /// <returns>random arity in the interval [minArity, maxArity]</returns>
|
---|
[10228] | 117 | protected int SampleArity(IRandom random,
|
---|
| 118 | ISymbolicExpressionTreeNode node,
|
---|
[10277] | 119 | ISymbolicExpressionGrammar grammar) {
|
---|
[10228] | 120 |
|
---|
[10277] | 121 | int minArity = grammar.GetMinimumSubtreeCount(node.Symbol);
|
---|
| 122 | int maxArity = grammar.GetMaximumSubtreeCount(node.Symbol);
|
---|
| 123 |
|
---|
[10228] | 124 | if (minArity == maxArity) {
|
---|
| 125 | return minArity;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | return random.Next(minArity, maxArity);
|
---|
| 129 | }
|
---|
[10039] | 130 | }
|
---|
| 131 | }
|
---|