1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 | * Author: Sabine Winkler
|
---|
21 | */
|
---|
22 |
|
---|
23 | #endregion
|
---|
24 |
|
---|
25 | using System;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
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>
|
---|
38 | [Item("DepthFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a depth-first manner.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class DepthFirstMapper : GenotypeToPhenotypeMapper {
|
---|
41 |
|
---|
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 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /// <summary>
|
---|
53 | /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
|
---|
54 | /// Depth-first approach.
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="random">random number generator</param>
|
---|
57 | /// <param name="bounds">only used for PIGEMapper (ignore here)</param>
|
---|
58 | /// <param name="length">only used for PIGEMapper (ignore here)</param>
|
---|
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>
|
---|
62 | public override ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
|
---|
63 | ISymbolicExpressionGrammar grammar,
|
---|
64 | IntegerVector genotype) {
|
---|
65 |
|
---|
66 | SymbolicExpressionTree tree = new SymbolicExpressionTree();
|
---|
67 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
68 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
69 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
70 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
71 | rootNode.AddSubtree(startNode);
|
---|
72 | tree.Root = rootNode;
|
---|
73 |
|
---|
74 | MapDepthFirstIteratively(startNode, genotype, grammar,
|
---|
75 | genotype.Length, random);
|
---|
76 | return tree;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Genotype-to-Phenotype mapper (iterative depth-first approach, by using a stack -> LIFO).
|
---|
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 |
|
---|
99 | while (stack.Count > 0) {
|
---|
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 |
|
---|
107 | if (genotypeIndex >= maxSubtreeCount) {
|
---|
108 | // if all genomes were used, only add terminal nodes to the remaining subtrees
|
---|
109 | current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
|
---|
110 | } else {
|
---|
111 | var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
|
---|
112 | int arity = SampleArity(random, newNode, grammar);
|
---|
113 |
|
---|
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 | }
|
---|
123 | }
|
---|
124 | } |
---|