[10039] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[10328] | 24 | using System.Collections.Generic;
|
---|
[10039] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[10290] | 27 | using HeuristicLab.Data;
|
---|
[10039] | 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[12422] | 31 | using HeuristicLab.Random;
|
---|
[10039] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// RandomMapper
|
---|
| 36 | /// </summary>
|
---|
[10328] | 37 | [Item("RandomMapper", "Randomly determines the next non-terminal symbol to expand.")]
|
---|
[10039] | 38 | [StorableClass]
|
---|
| 39 | public class RandomMapper : GenotypeToPhenotypeMapper {
|
---|
[10068] | 40 |
|
---|
[10039] | 41 | [StorableConstructor]
|
---|
| 42 | protected RandomMapper(bool deserializing) : base(deserializing) { }
|
---|
| 43 | protected RandomMapper(RandomMapper original, Cloner cloner) : base(original, cloner) { }
|
---|
| 44 | public RandomMapper() : base() { }
|
---|
| 45 |
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new RandomMapper(this, cloner);
|
---|
| 48 | }
|
---|
[10068] | 49 |
|
---|
| 50 |
|
---|
[10039] | 51 | /// <summary>
|
---|
| 52 | /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
|
---|
| 53 | /// Random approach.
|
---|
| 54 | /// </summary>
|
---|
[10280] | 55 | /// <param name="random">random number generator</param>
|
---|
[10328] | 56 | /// <param name="bounds">only used for PIGEMapper (ignore here)</param>
|
---|
| 57 | /// <param name="length">only used for PIGEMapper (ignore here)</param>
|
---|
[10039] | 58 | /// <param name="grammar">grammar definition</param>
|
---|
| 59 | /// <param name="genotype">integer vector, which should be mapped to a tree</param>
|
---|
| 60 | /// <returns>phenotype (a symbolic expression tree)</returns>
|
---|
[12915] | 61 | public override ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
|
---|
[10280] | 62 | ISymbolicExpressionGrammar grammar,
|
---|
[10039] | 63 | IntegerVector genotype) {
|
---|
[10068] | 64 |
|
---|
[10039] | 65 | SymbolicExpressionTree tree = new SymbolicExpressionTree();
|
---|
[10068] | 66 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
[10039] | 67 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
| 68 | rootNode.AddSubtree(startNode);
|
---|
| 69 | tree.Root = rootNode;
|
---|
[10068] | 70 |
|
---|
[10328] | 71 | MapRandomIteratively(startNode, genotype, grammar,
|
---|
| 72 | genotype.Length, random);
|
---|
[10068] | 73 |
|
---|
[10039] | 74 | return tree;
|
---|
| 75 | }
|
---|
[10328] | 76 |
|
---|
| 77 |
|
---|
| 78 | /// <summary>
|
---|
| 79 | /// Genotype-to-Phenotype mapper (iterative random approach, where the next non-terminal
|
---|
| 80 | /// symbol to expand is randomly determined).
|
---|
| 81 | /// </summary>
|
---|
| 82 | /// <param name="startNode">first node of the tree with arity 1</param>
|
---|
| 83 | /// <param name="genotype">integer vector, which should be mapped to a tree</param>
|
---|
| 84 | /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
|
---|
| 85 | /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
|
---|
| 86 | /// <param name="random">random number generator</param>
|
---|
| 87 | private void MapRandomIteratively(ISymbolicExpressionTreeNode startNode,
|
---|
| 88 | IntegerVector genotype,
|
---|
| 89 | ISymbolicExpressionGrammar grammar,
|
---|
| 90 | int maxSubtreeCount, IRandom random) {
|
---|
| 91 |
|
---|
| 92 | List<ISymbolicExpressionTreeNode> nonTerminals = new List<ISymbolicExpressionTreeNode>();
|
---|
| 93 |
|
---|
| 94 | int genotypeIndex = 0;
|
---|
| 95 | nonTerminals.Add(startNode);
|
---|
| 96 |
|
---|
| 97 | while (nonTerminals.Count > 0) {
|
---|
| 98 | if (genotypeIndex >= maxSubtreeCount) {
|
---|
| 99 | // if all genomes were used, only add terminal nodes to the remaining subtrees
|
---|
| 100 | ISymbolicExpressionTreeNode current = nonTerminals[0];
|
---|
| 101 | nonTerminals.RemoveAt(0);
|
---|
| 102 | current.AddSubtree(GetRandomTerminalNode(current, grammar, random));
|
---|
| 103 | } else {
|
---|
| 104 | // similar to PIGEMapper, but here the current node is determined randomly ...
|
---|
[12422] | 105 | ISymbolicExpressionTreeNode current = nonTerminals.SampleRandom(random);
|
---|
[10328] | 106 | nonTerminals.Remove(current);
|
---|
| 107 |
|
---|
| 108 | ISymbolicExpressionTreeNode newNode = GetNewChildNode(current, genotype, grammar, genotypeIndex, random);
|
---|
| 109 | int arity = SampleArity(random, newNode, grammar);
|
---|
| 110 |
|
---|
| 111 | current.AddSubtree(newNode);
|
---|
| 112 | genotypeIndex++;
|
---|
| 113 | // new node has subtrees, so add "arity" number of copies of this node to the nonTerminals list
|
---|
| 114 | for (int i = 0; i < arity; ++i) {
|
---|
| 115 | nonTerminals.Add(newNode);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[10039] | 120 | }
|
---|
| 121 | } |
---|