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