Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.GrammaticalEvolution/3.4/Mappers/RandomMapper.cs @ 17514

Last change on this file since 17514 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HEAL.Attic;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Problems.GrammaticalEvolution {
34  /// <summary>
35  /// RandomMapper
36  /// </summary>
37  [Item("RandomMapper", "Randomly determines the next non-terminal symbol to expand.")]
38  [StorableType("FD52470F-EA9A-4E95-8DE0-66C9B9F5F4EB")]
39  public class RandomMapper : GenotypeToPhenotypeMapper {
40
41    [StorableConstructor]
42    protected RandomMapper(StorableConstructorFlag _) : base(_) { }
43    protected RandomMapper(RandomMapper original, Cloner cloner) : base(original, cloner) { }
44    public RandomMapper() : base() { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new RandomMapper(this, cloner);
48    }
49
50
51    /// <summary>
52    /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree).
53    /// Random 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      MapRandomIteratively(startNode, genotype, grammar,
72                           genotype.Length, random);
73
74      return tree;
75    }
76
77
78    /// <summary>
79    /// Genotype-to-Phenotype mapper (iterative random approach, where the next non-terminal
80    /// symbol to expand is randomly determined).
81    /// </summary>
82    /// <param name="startNode">first node of the tree with arity 1</param>
83    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
84    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
85    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
86    /// <param name="random">random number generator</param>
87    private void MapRandomIteratively(ISymbolicExpressionTreeNode startNode,
88                                     IntegerVector genotype,
89                                     ISymbolicExpressionGrammar grammar,
90                                     int maxSubtreeCount, IRandom random) {
91
92      List<ISymbolicExpressionTreeNode> nonTerminals = new List<ISymbolicExpressionTreeNode>();
93
94      int genotypeIndex = 0;
95      nonTerminals.Add(startNode);
96
97      while (nonTerminals.Count > 0) {
98        if (genotypeIndex >= maxSubtreeCount) {
99          // if all genomes were used, only add terminal nodes to the remaining subtrees
100          ISymbolicExpressionTreeNode current = nonTerminals[0];
101          nonTerminals.RemoveAt(0);
102          current.AddSubtree(GetRandomTerminalNode(current, grammar, random));
103        } else {
104          // similar to PIGEMapper, but here the current node is determined randomly ...
105          ISymbolicExpressionTreeNode current = nonTerminals.SampleRandom(random);
106          nonTerminals.Remove(current);
107
108          ISymbolicExpressionTreeNode newNode = GetNewChildNode(current, genotype, grammar, genotypeIndex, random);
109          int arity = SampleArity(random, newNode, grammar);
110
111          current.AddSubtree(newNode);
112          genotypeIndex++;
113          // new node has subtrees, so add "arity" number of copies of this node to the nonTerminals list
114          for (int i = 0; i < arity; ++i) {
115            nonTerminals.Add(newNode);
116          }
117        }
118      }
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.