[10039] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[10039] | 32 | using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
|
---|
[12422] | 33 | using HeuristicLab.Random;
|
---|
[10039] | 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
| 36 | /// <summary>
|
---|
[10974] | 37 | /// Abstract base class for GenotypeToPhenotypeMappers
|
---|
[10039] | 38 | /// </summary>
|
---|
[16565] | 39 | [StorableType("427C4EB7-7888-4AB2-824A-E1F2EB1DE2FA")]
|
---|
[10039] | 40 | public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper {
|
---|
[10068] | 41 |
|
---|
[10039] | 42 | [StorableConstructor]
|
---|
[16565] | 43 | protected GenotypeToPhenotypeMapper(StorableConstructorFlag _) : base(_) { }
|
---|
[10039] | 44 | protected GenotypeToPhenotypeMapper(GenotypeToPhenotypeMapper original, Cloner cloner) : base(original, cloner) { }
|
---|
| 45 | protected GenotypeToPhenotypeMapper() : base() { }
|
---|
[10068] | 46 |
|
---|
[12915] | 47 | public abstract ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
|
---|
[10280] | 48 | ISymbolicExpressionGrammar grammar,
|
---|
[10039] | 49 | IntegerVector genotype);
|
---|
[10068] | 50 |
|
---|
[10039] | 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>
|
---|
[10228] | 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>
|
---|
[10039] | 59 | protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode,
|
---|
[10228] | 60 | ISymbolicExpressionGrammar grammar,
|
---|
| 61 | IRandom random) {
|
---|
[10075] | 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();
|
---|
[10228] | 68 |
|
---|
| 69 | // no terminal node exists for the given parent node
|
---|
[10974] | 70 | if (!possibleSymbolsList.Any()) return null;
|
---|
[10228] | 71 |
|
---|
[12422] | 72 | var newNode = possibleSymbolsList.SampleRandom(random).CreateTreeNode();
|
---|
[10228] | 73 | if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
|
---|
[10075] | 74 | return newNode;
|
---|
[10039] | 75 | }
|
---|
[10068] | 76 |
|
---|
| 77 |
|
---|
[10039] | 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>
|
---|
[10228] | 83 | /// <param name="grammar">grammar used to define the allowed child symbols</param>
|
---|
[10039] | 84 | /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param>
|
---|
[10228] | 85 | /// <param name="random">random number generator</param>
|
---|
| 86 | /// <returns>randomly chosen child node or null, if no child node exits</returns>
|
---|
[10039] | 87 | protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode,
|
---|
| 88 | IntegerVector genotype,
|
---|
| 89 | ISymbolicExpressionGrammar grammar,
|
---|
[10228] | 90 | int genotypeIndex,
|
---|
| 91 | IRandom random) {
|
---|
[10068] | 92 |
|
---|
[10075] | 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();
|
---|
[10228] | 97 |
|
---|
[10039] | 98 | int prodRuleCount = symbolList.Count();
|
---|
[10228] | 99 |
|
---|
| 100 | // no child node exists for the given parent node
|
---|
| 101 | if (prodRuleCount < 1) return null;
|
---|
| 102 |
|
---|
[10328] | 103 | // genotypeIndex % genotype.Length, if wrapping is allowed
|
---|
[10290] | 104 | int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount;
|
---|
[10068] | 105 |
|
---|
[10075] | 106 | var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode();
|
---|
[10228] | 107 | if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random);
|
---|
[10075] | 108 | return newNode;
|
---|
[10039] | 109 | }
|
---|
[10228] | 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>
|
---|
[10277] | 117 | /// <param name="grammar">symbolic expression grammar to use</param>
|
---|
[10290] | 118 | /// <returns>random arity in the interval [minArity, maxArity]</returns>
|
---|
[10228] | 119 | protected int SampleArity(IRandom random,
|
---|
| 120 | ISymbolicExpressionTreeNode node,
|
---|
[10277] | 121 | ISymbolicExpressionGrammar grammar) {
|
---|
[10228] | 122 |
|
---|
[10277] | 123 | int minArity = grammar.GetMinimumSubtreeCount(node.Symbol);
|
---|
| 124 | int maxArity = grammar.GetMaximumSubtreeCount(node.Symbol);
|
---|
| 125 |
|
---|
[10228] | 126 | if (minArity == maxArity) {
|
---|
| 127 | return minArity;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return random.Next(minArity, maxArity);
|
---|
| 131 | }
|
---|
[10039] | 132 | }
|
---|
| 133 | }
|
---|