[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 | */
|
---|
[10968] | 22 |
|
---|
[10039] | 23 | #endregion
|
---|
| 24 |
|
---|
[10228] | 25 | using System;
|
---|
| 26 | using System.Collections.Generic;
|
---|
[10039] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[10290] | 29 | using HeuristicLab.Data;
|
---|
[10039] | 30 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// DepthFirstMapper
|
---|
| 37 | /// </summary>
|
---|
[10229] | 38 | [Item("DepthFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a depth-first manner.")]
|
---|
[10039] | 39 | [StorableClass]
|
---|
| 40 | public class DepthFirstMapper : GenotypeToPhenotypeMapper {
|
---|
[10068] | 41 |
|
---|
[10039] | 42 | [StorableConstructor]
|
---|
| 43 | protected DepthFirstMapper(bool deserializing) : base(deserializing) { }
|
---|
| 44 | protected DepthFirstMapper(DepthFirstMapper original, Cloner cloner) : base(original, cloner) { }
|
---|
| 45 | public DepthFirstMapper() : base() { }
|
---|
| 46 |
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new DepthFirstMapper(this, cloner);
|
---|
| 49 | }
|
---|
[10068] | 50 |
|
---|
| 51 |
|
---|
[10039] | 52 | /// <summary>
|
---|
| 53 | /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
|
---|
| 54 | /// Depth-first approach.
|
---|
| 55 | /// </summary>
|
---|
[10280] | 56 | /// <param name="random">random number generator</param>
|
---|
[10328] | 57 | /// <param name="bounds">only used for PIGEMapper (ignore here)</param>
|
---|
| 58 | /// <param name="length">only used for PIGEMapper (ignore here)</param>
|
---|
[10039] | 59 | /// <param name="grammar">grammar definition</param>
|
---|
| 60 | /// <param name="genotype">integer vector, which should be mapped to a tree</param>
|
---|
| 61 | /// <returns>phenotype (a symbolic expression tree)</returns>
|
---|
[12915] | 62 | public override ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
|
---|
[10280] | 63 | ISymbolicExpressionGrammar grammar,
|
---|
[10039] | 64 | IntegerVector genotype) {
|
---|
[10068] | 65 |
|
---|
[10039] | 66 | SymbolicExpressionTree tree = new SymbolicExpressionTree();
|
---|
[10068] | 67 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
[10280] | 68 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[10039] | 69 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[10280] | 70 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[10039] | 71 | rootNode.AddSubtree(startNode);
|
---|
| 72 | tree.Root = rootNode;
|
---|
[10068] | 73 |
|
---|
[10228] | 74 | MapDepthFirstIteratively(startNode, genotype, grammar,
|
---|
[10280] | 75 | genotype.Length, random);
|
---|
[10039] | 76 | return tree;
|
---|
| 77 | }
|
---|
[10068] | 78 |
|
---|
| 79 |
|
---|
[10039] | 80 | /// <summary>
|
---|
[10229] | 81 | /// Genotype-to-Phenotype mapper (iterative depth-first approach, by using a stack -> LIFO).
|
---|
[10228] | 82 | /// </summary>
|
---|
| 83 | /// <param name="startNode">first node of the tree with arity 1</param>
|
---|
| 84 | /// <param name="genotype">integer vector, which should be mapped to a tree</param>
|
---|
| 85 | /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
|
---|
| 86 | /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
|
---|
| 87 | /// <param name="random">random number generator</param>
|
---|
| 88 | private void MapDepthFirstIteratively(ISymbolicExpressionTreeNode startNode,
|
---|
| 89 | IntegerVector genotype,
|
---|
| 90 | ISymbolicExpressionGrammar grammar,
|
---|
| 91 | int maxSubtreeCount, IRandom random) {
|
---|
| 92 |
|
---|
| 93 | Stack<Tuple<ISymbolicExpressionTreeNode, int>> stack
|
---|
| 94 | = new Stack<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>
|
---|
| 95 |
|
---|
| 96 | int genotypeIndex = 0;
|
---|
| 97 | stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));
|
---|
| 98 |
|
---|
[10290] | 99 | while (stack.Count > 0) {
|
---|
[10228] | 100 |
|
---|
| 101 | // get next node from stack and re-push it, if this node still has unhandled subtrees ...
|
---|
| 102 | Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop();
|
---|
| 103 | if (current.Item2 > 1) {
|
---|
| 104 | stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[10290] | 107 | if (genotypeIndex >= maxSubtreeCount) {
|
---|
| 108 | // if all genomes were used, only add terminal nodes to the remaining subtrees
|
---|
[10228] | 109 | current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
|
---|
| 110 | } else {
|
---|
[10290] | 111 | var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
|
---|
| 112 | int arity = SampleArity(random, newNode, grammar);
|
---|
| 113 |
|
---|
[10228] | 114 | current.Item1.AddSubtree(newNode);
|
---|
| 115 | genotypeIndex++;
|
---|
| 116 | if (arity > 0) {
|
---|
| 117 | // new node has subtrees so push it onto the stack
|
---|
| 118 | stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[10039] | 123 | }
|
---|
| 124 | } |
---|