Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Manipulators/ChangeNodeTypeManipulation.cs @ 12770

Last change on this file since 12770 was 3512, checked in by gkronber, 14 years ago

Implemented manipulation operators. #937 (Data types and operators for symbolic expression tree encoding)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Random;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
30  [StorableClass]
31  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol size.")]
32  public class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
33
34    public ChangeNodeTypeManipulation()
35      : base() {
36    }
37
38    protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
39
40      // select any node except the with a parent where the parent is not the root node)
41      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
42                               from subtree in parent.SubTrees
43                               select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random);
44      // find possible symbols for the node (also considering the existing branches below it)
45      var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
46                           where manipulationPoint.Node.SubTrees.Count <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)
47                           where manipulationPoint.Node.SubTrees.Count >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)
48                           select symbol;
49
50      if (allowedSymbols.Count() <= 1) {
51        success = false;
52        return;
53      }
54      var node = manipulationPoint.Node;
55      // keep only symbols that are still possible considering the existing sub-trees
56      var constrainedSymbols = from symbol in allowedSymbols
57                               let disallowedSubtrees =
58                                     from subtree in node.SubTrees
59                                     where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.SubTrees.IndexOf(subtree))
60                                     select subtree
61                               where disallowedSubtrees.Count() == 0
62                               select symbol;
63      if (constrainedSymbols.Count() <= 1) {
64        success = false;
65        return;
66      }
67      var newSymbol = constrainedSymbols.SelectRandom(random);
68
69      // replace the old node with the new node
70      var newNode = newSymbol.CreateTreeNode();
71      if (newNode.HasLocalParameters)
72        newNode.ResetLocalParameters(random);
73      foreach (var subtree in node.SubTrees)
74        newNode.AddSubTree(subtree);
75      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
76      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode);
77      success = true;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.