[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 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 |
|
---|
| 22 | using System.Linq;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[645] | 24 | using HeuristicLab.Core;
|
---|
[3512] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7662] | 26 | using System.Collections.Generic;
|
---|
[645] | 27 |
|
---|
[5499] | 28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3512] | 29 | [StorableClass]
|
---|
[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 {
|
---|
| 55 | parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
|
---|
| 56 | childIndex = random.Next(parent.SubtreeCount);
|
---|
[645] | 57 |
|
---|
[7662] | 58 | child = parent.GetSubtree(childIndex);
|
---|
| 59 | int existingSubtreeCount = child.SubtreeCount;
|
---|
| 60 | allowedSymbols.Clear();
|
---|
| 61 | foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
|
---|
| 62 | // check basic properties that the new symbol must have
|
---|
| 63 | if (symbol.Name != child.Symbol.Name &&
|
---|
| 64 | symbol.InitialFrequency > 0 &&
|
---|
| 65 | existingSubtreeCount <= parent.Grammar.GetMinimumSubtreeCount(symbol) &&
|
---|
| 66 | existingSubtreeCount >= parent.Grammar.GetMaximumSubtreeCount(symbol)) {
|
---|
| 67 | // check that all existing subtrees are also allowed for the new symbol
|
---|
| 68 | bool allExistingSubtreesAllowed = true;
|
---|
| 69 | for (int existingSubtreeIndex = 0; existingSubtreeIndex < existingSubtreeCount && allExistingSubtreesAllowed; existingSubtreeIndex++) {
|
---|
| 70 | var existingSubtree = child.GetSubtree(existingSubtreeIndex);
|
---|
| 71 | allExistingSubtreesAllowed &= parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex);
|
---|
| 72 | }
|
---|
| 73 | if (allExistingSubtreesAllowed) {
|
---|
| 74 | allowedSymbols.Add(symbol);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | tries++;
|
---|
| 79 | } while (tries < MAX_TRIES && allowedSymbols.Count == 0);
|
---|
[5567] | 80 |
|
---|
[7662] | 81 | if (tries < MAX_TRIES) {
|
---|
| 82 | var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 83 | var newSymbol = allowedSymbols.SelectRandom(weights, random);
|
---|
| 84 |
|
---|
| 85 | // replace the old node with the new node
|
---|
| 86 | var newNode = newSymbol.CreateTreeNode();
|
---|
| 87 | if (newNode.HasLocalParameters)
|
---|
| 88 | newNode.ResetLocalParameters(random);
|
---|
| 89 | foreach (var subtree in child.Subtrees)
|
---|
| 90 | newNode.AddSubtree(subtree);
|
---|
| 91 | parent.RemoveSubtree(childIndex);
|
---|
| 92 | parent.InsertSubtree(childIndex, newNode);
|
---|
| 93 | }
|
---|
[645] | 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|