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