[10039] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
[10229] | 24 | using System;
|
---|
| 25 | using System.Collections.Generic;
|
---|
[10039] | 26 | using HeuristicLab.Common;
|
---|
| 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 |
|
---|
| 33 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// BreathFirstMapper
|
---|
| 36 | /// </summary>
|
---|
[10229] | 37 | [Item("BreathFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a breath-first manner.")]
|
---|
[10039] | 38 | [StorableClass]
|
---|
| 39 | public class BreathFirstMapper : GenotypeToPhenotypeMapper {
|
---|
[10068] | 40 |
|
---|
[10039] | 41 | [StorableConstructor]
|
---|
| 42 | protected BreathFirstMapper(bool deserializing) : base(deserializing) { }
|
---|
| 43 | protected BreathFirstMapper(BreathFirstMapper original, Cloner cloner) : base(original, cloner) { }
|
---|
| 44 | public BreathFirstMapper() : base() { }
|
---|
| 45 |
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new BreathFirstMapper(this, cloner);
|
---|
| 48 | }
|
---|
[10068] | 49 |
|
---|
| 50 |
|
---|
[10039] | 51 | /// <summary>
|
---|
| 52 | /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
|
---|
| 53 | /// Breath-first 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 |
|
---|
[10229] | 71 | MapBreathFirstIteratively(startNode, genotype, grammar,
|
---|
[10280] | 72 | genotype.Length, random);
|
---|
[10068] | 73 |
|
---|
[10039] | 74 | return tree;
|
---|
| 75 | }
|
---|
[10229] | 76 |
|
---|
| 77 | /// <summary>
|
---|
| 78 | /// Genotype-to-Phenotype mapper (iterative breath-first approach, by using a queue -> FIFO).
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <param name="startNode">first node of the tree with arity 1</param>
|
---|
| 81 | /// <param name="genotype">integer vector, which should be mapped to a tree</param>
|
---|
| 82 | /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
|
---|
| 83 | /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
|
---|
| 84 | /// <param name="random">random number generator</param>
|
---|
| 85 | private void MapBreathFirstIteratively(ISymbolicExpressionTreeNode startNode,
|
---|
| 86 | IntegerVector genotype,
|
---|
| 87 | ISymbolicExpressionGrammar grammar,
|
---|
| 88 | int maxSubtreeCount, IRandom random) {
|
---|
| 89 |
|
---|
| 90 | Queue<Tuple<ISymbolicExpressionTreeNode, int>> queue
|
---|
| 91 | = new Queue<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>
|
---|
| 92 |
|
---|
| 93 | int genotypeIndex = 0;
|
---|
| 94 | queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));
|
---|
| 95 |
|
---|
[10290] | 96 | while (queue.Count > 0) {
|
---|
[10229] | 97 |
|
---|
| 98 | Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();
|
---|
| 99 |
|
---|
| 100 | // foreach subtree of the current node, create a new node and enqueue it, if it is no terminal node
|
---|
| 101 | for (int i = 0; i < current.Item2; ++i) {
|
---|
| 102 |
|
---|
[10290] | 103 | if (genotypeIndex >= maxSubtreeCount) {
|
---|
| 104 | // if all genomes were used, only add terminal nodes to the remaining subtrees
|
---|
[10229] | 105 | current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
|
---|
| 106 | } else {
|
---|
[10290] | 107 | var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
|
---|
| 108 | int arity = SampleArity(random, newNode, grammar);
|
---|
| 109 |
|
---|
[10229] | 110 | current.Item1.AddSubtree(newNode);
|
---|
| 111 | genotypeIndex++;
|
---|
| 112 | if (arity > 0) {
|
---|
| 113 | // new node has subtrees so enqueue the node
|
---|
| 114 | queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[10039] | 120 | }
|
---|
| 121 | } |
---|