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
RevLine 
[645]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[645]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
[12422]22using System.Collections.Generic;
[645]23using System.Linq;
[4722]24using HeuristicLab.Common;
[645]25using HeuristicLab.Core;
[14927]26using HeuristicLab.Persistence;
[645]27
[5499]28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[14927]29  [StorableType("78472a48-5ad6-417e-97ba-20e8182fa37d")]
[4189]30  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
[4722]31  public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
[7662]32    private const int MAX_TRIES = 100;
33
[4722]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);
[645]41    }
42
[5510]43    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
[5499]44      ChangeNodeType(random, symbolicExpressionTree);
[4189]45    }
[645]46
[5510]47    public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
[7662]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 {
[12422]55
56#pragma warning disable 612, 618
[7662]57        parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
[12422]58#pragma warning restore 612, 618
59
[7662]60        childIndex = random.Next(parent.SubtreeCount);
[645]61
[7662]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);
[5567]84
[7662]85      if (tries < MAX_TRIES) {
86        var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
[12422]87#pragma warning disable 612, 618
[7662]88        var newSymbol = allowedSymbols.SelectRandom(weights, random);
[12422]89#pragma warning restore 612, 618
[7662]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      }
[645]100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.