Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs @ 5686

Last change on this file since 5686 was 5686, checked in by mkommend, 13 years ago

#1418: Finally added results from the grammar refactoring.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  [StorableClass]
29  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
30  public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
31    [StorableConstructor]
32    private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { }
33    private ChangeNodeTypeManipulation(ChangeNodeTypeManipulation original, Cloner cloner) : base(original, cloner) { }
34    public ChangeNodeTypeManipulation() : base() { }
35
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new ChangeNodeTypeManipulation(this, cloner);
38    }
39
40    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
41      ChangeNodeType(random, symbolicExpressionTree);
42    }
43
44    public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
45      // select any node as parent (except the root node)
46      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
47                                let subtreeCount = parent.SubTrees.Count()
48                                from subtreeIndex in Enumerable.Range(0, subtreeCount)
49                                let subtree = parent.GetSubTree(subtreeIndex)
50                                let existingSubtreeCount = subtree.SubTrees.Count()
51                                // find possible symbols for the node (also considering the existing branches below it)
52                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, subtreeIndex)
53                                                      // do not replace the existing symbol with itself
54                                                      where symbol.Name != subtree.Symbol.Name
55                                                      where existingSubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(symbol)
56                                                      where existingSubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(symbol)
57                                                      // keep only symbols that are still possible considering the existing sub-trees
58                                                      where (from existingSubtreeIndex in Enumerable.Range(0, existingSubtreeCount)
59                                                             let existingSubtree = subtree.GetSubTree(existingSubtreeIndex)
60                                                             select parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex))
61                                                             .All(x => x == true)
62                                                      select symbol)
63                                                      .ToList()
64                                where allowedSymbols.Count() > 0
65                                select new { Parent = parent, Child = subtree, Index = subtreeIndex, AllowedSymbols = allowedSymbols })
66                               .ToList();
67      if (manipulationPoints.Count == 0) { return; }
68      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
69
70      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
71      var newSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
72
73      // replace the old node with the new node
74      var newNode = newSymbol.CreateTreeNode();
75      if (newNode.HasLocalParameters)
76        newNode.ResetLocalParameters(random);
77      foreach (var subtree in selectedManipulationPoint.Child.SubTrees)
78        newNode.AddSubTree(subtree);
79      selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
80      selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, newNode);
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.