Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs @ 14927

Last change on this file since 14927 was 14927, checked in by gkronber, 7 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  [StorableType("78472a48-5ad6-417e-97ba-20e8182fa37d")]
30  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
31  public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
32    private const int MAX_TRIES = 100;
33
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      List<ISymbol> allowedSymbols = new List<ISymbol>();
49      ISymbolicExpressionTreeNode parent;
50      int childIndex;
51      ISymbolicExpressionTreeNode child;
52      // repeat until a fitting parent and child are found (MAX_TRIES times)
53      int tries = 0;
54      do {
55
56#pragma warning disable 612, 618
57        parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
58#pragma warning restore 612, 618
59
60        childIndex = random.Next(parent.SubtreeCount);
61
62        child = parent.GetSubtree(childIndex);
63        int existingSubtreeCount = child.SubtreeCount;
64        allowedSymbols.Clear();
65        foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
66          // check basic properties that the new symbol must have
67          if (symbol.Name != child.Symbol.Name &&
68            symbol.InitialFrequency > 0 &&
69            existingSubtreeCount <= parent.Grammar.GetMinimumSubtreeCount(symbol) &&
70            existingSubtreeCount >= parent.Grammar.GetMaximumSubtreeCount(symbol)) {
71            // check that all existing subtrees are also allowed for the new symbol
72            bool allExistingSubtreesAllowed = true;
73            for (int existingSubtreeIndex = 0; existingSubtreeIndex < existingSubtreeCount && allExistingSubtreesAllowed; existingSubtreeIndex++) {
74              var existingSubtree = child.GetSubtree(existingSubtreeIndex);
75              allExistingSubtreesAllowed &= parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex);
76            }
77            if (allExistingSubtreesAllowed) {
78              allowedSymbols.Add(symbol);
79            }
80          }
81        }
82        tries++;
83      } while (tries < MAX_TRIES && allowedSymbols.Count == 0);
84
85      if (tries < MAX_TRIES) {
86        var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
87#pragma warning disable 612, 618
88        var newSymbol = allowedSymbols.SelectRandom(weights, random);
89#pragma warning restore 612, 618
90
91        // replace the old node with the new node
92        var newNode = newSymbol.CreateTreeNode();
93        if (newNode.HasLocalParameters)
94          newNode.ResetLocalParameters(random);
95        foreach (var subtree in child.Subtrees)
96          newNode.AddSubtree(subtree);
97        parent.RemoveSubtree(childIndex);
98        parent.InsertSubtree(childIndex, newNode);
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.