Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/Mappers/BreathFirstMapper.cs @ 15448

Last change on this file since 15448 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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#endregion
23
24using System;
25using System.Collections.Generic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.GrammaticalEvolution {
34  /// <summary>
35  /// BreathFirstMapper
36  /// </summary>
37  [Item("BreathFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a breath-first manner.")]
38  [StorableClass]
39  public class BreathFirstMapper : GenotypeToPhenotypeMapper {
40
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    }
49
50
51    /// <summary>
52    /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
53    /// Breath-first approach.
54    /// </summary>
55    /// <param name="random">random number generator</param>
56    /// <param name="bounds">only used for PIGEMapper (ignore here)</param>
57    /// <param name="length">only used for PIGEMapper (ignore here)</param>
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>
61    public override ISymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length,
62                                               ISymbolicExpressionGrammar grammar,
63                                               IntegerVector genotype) {
64
65      SymbolicExpressionTree tree = new SymbolicExpressionTree();
66      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
67      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
68      rootNode.AddSubtree(startNode);
69      tree.Root = rootNode;
70
71      MapBreathFirstIteratively(startNode, genotype, grammar,
72                                genotype.Length, random);
73
74      return tree;
75    }
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
96      while (queue.Count > 0) {
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
103          if (genotypeIndex >= maxSubtreeCount) {
104            // if all genomes were used, only add terminal nodes to the remaining subtrees
105            current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));
106          } else {
107            var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);
108            int arity = SampleArity(random, newNode, grammar);
109
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    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.