Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5567 was 5567, checked in by gkronber, 13 years ago

#1418 improved ChangeNodeTypeManipulation operator to make unit test succeed.

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