Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418 Fixed compiler errors in symbolic expression tree encoding

File size: 4.3 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
49      // select any node as parent (except the root node)
50      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
51                               from subtree in parent.SubTrees
52                               select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
53                               .SelectRandom(random);
54      int subTreeCount = manipulationPoint.Node.SubTrees.Count();
55      // find possible symbols for the node (also considering the existing branches below it)
56      var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
57                           where subTreeCount <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)
58                           where subTreeCount >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)
59                           select symbol;
60
61      if (allowedSymbols.Count() == 0) {
62        return;
63      }
64      var node = manipulationPoint.Node;
65      // keep only symbols that are still possible considering the existing sub-trees
66      var constrainedSymbols = (from symbol in allowedSymbols
67                                let disallowedSubtrees =
68                                      from subtree in node.SubTrees
69                                      where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.IndexOfSubTree(subtree))
70                                      select subtree
71                                where disallowedSubtrees.Count() == 0
72                                select symbol)
73                               .ToList();
74      if (constrainedSymbols.Count() == 0) {
75        return;
76      }
77      var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList();
78      var newSymbol =  constrainedSymbols.SelectRandom(weights, random);
79
80      // replace the old node with the new node
81      var newNode = newSymbol.CreateTreeNode();
82      if (newNode.HasLocalParameters)
83        newNode.ResetLocalParameters(random);
84      foreach (var subtree in node.SubTrees)
85        newNode.AddSubTree(subtree);
86      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
87      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.