1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022013 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.Encodings.IntegerVectorEncoding;


27  using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;


28  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


29  using HeuristicLab.Random;


30 


31  namespace HeuristicLab.Problems.GrammaticalEvolution {


32  /// <summary>


33  /// BreathFirstMapper


34  /// </summary>


35  [Item("BreathFirstMapper", "Resolves the nonterminal symbols of the resulting phenotypic syntax tree in a breathfirst 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  /// Breathfirst approach.


52  /// </summary>


53  /// <param name="grammar">grammar definition</param>


54  /// <param name="genotype">integer vector, which should be mapped to a tree</param>


55  /// <returns>phenotype (a symbolic expression tree)</returns>


56  public override SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar,


57  IntegerVector genotype) {


58 


59  SymbolicExpressionTree tree = new SymbolicExpressionTree();


60  var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();


61  var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();


62  rootNode.AddSubtree(startNode);


63  tree.Root = rootNode;


64 


65  MapBreathFirstIteratively(startNode, genotype, grammar,


66  genotype.Length, new MersenneTwister());


67 


68  return tree;


69  }


70 


71  /// <summary>


72  /// GenotypetoPhenotype mapper (iterative breathfirst approach, by using a queue > FIFO).


73  /// </summary>


74  /// <param name="startNode">first node of the tree with arity 1</param>


75  /// <param name="genotype">integer vector, which should be mapped to a tree</param>


76  /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>


77  /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>


78  /// <param name="random">random number generator</param>


79  private void MapBreathFirstIteratively(ISymbolicExpressionTreeNode startNode,


80  IntegerVector genotype,


81  ISymbolicExpressionGrammar grammar,


82  int maxSubtreeCount, IRandom random) {


83 


84  Queue<Tuple<ISymbolicExpressionTreeNode, int>> queue


85  = new Queue<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity>


86 


87  int genotypeIndex = 0;


88  int currSubtreeCount = 1;


89 


90  queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1));


91 


92  while ((currSubtreeCount < maxSubtreeCount) && (queue.Count > 0)) {


93 


94  Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();


95 


96  // foreach subtree of the current node, create a new node and enqueue it, if it is no terminal node


97  for (int i = 0; i < current.Item2; ++i) {


98 


99  var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random);


100  int arity = SampleArity(random, newNode, maxSubtreeCount  currSubtreeCount);


101 


102  if (arity < 0) {


103  current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));


104  } else {


105  current.Item1.AddSubtree(newNode);


106  genotypeIndex++;


107  currSubtreeCount += arity;


108  if (arity > 0) {


109  // new node has subtrees so enqueue the node


110  queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity));


111  }


112  }


113  }


114  }


115 


116  // maximum allowed subtree count was already reached, but there are still


117  // incomplete subtrees (nonterminal symbols) in the tree


118  // > fill them with terminal symbols


119  while (queue.Count > 0) {


120  Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();


121  for (int i = 0; i < current.Item2; ++i) {


122  current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));


123  }


124  }


125  }


126  }


127  } 
