[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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;
|
---|
[645] | 26 |
|
---|
[5499] | 27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3512] | 28 | [StorableClass]
|
---|
[4189] | 29 | [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
|
---|
[4722] | 30 | public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
|
---|
| 31 | [StorableConstructor]
|
---|
| 32 | private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { }
|
---|
| 33 | private ChangeNodeTypeManipulation(ChangeNodeTypeManipulation original, Cloner cloner) : base(original, cloner) { }
|
---|
| 34 | public ChangeNodeTypeManipulation() : base() { }
|
---|
| 35 |
|
---|
| 36 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 37 | return new ChangeNodeTypeManipulation(this, cloner);
|
---|
[645] | 38 | }
|
---|
| 39 |
|
---|
[5510] | 40 | protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
[5499] | 41 | ChangeNodeType(random, symbolicExpressionTree);
|
---|
[4189] | 42 | }
|
---|
[645] | 43 |
|
---|
[5510] | 44 | public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
[4189] | 45 | // select any node as parent (except the root node)
|
---|
[5567] | 46 | var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
|
---|
| 47 | let subtreeCount = parent.SubTrees.Count()
|
---|
| 48 | from subtreeIndex in Enumerable.Range(0, subtreeCount)
|
---|
| 49 | let subtree = parent.GetSubTree(subtreeIndex)
|
---|
| 50 | let existingSubtreeCount = subtree.SubTrees.Count()
|
---|
| 51 | // find possible symbols for the node (also considering the existing branches below it)
|
---|
[5686] | 52 | let allowedSymbols = (from symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, subtreeIndex)
|
---|
[5567] | 53 | // do not replace the existing symbol with itself
|
---|
| 54 | where symbol.Name != subtree.Symbol.Name
|
---|
[5686] | 55 | where existingSubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(symbol)
|
---|
| 56 | where existingSubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(symbol)
|
---|
[5567] | 57 | // keep only symbols that are still possible considering the existing sub-trees
|
---|
| 58 | where (from existingSubtreeIndex in Enumerable.Range(0, existingSubtreeCount)
|
---|
| 59 | let existingSubtree = subtree.GetSubTree(existingSubtreeIndex)
|
---|
[5686] | 60 | select parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex))
|
---|
[5567] | 61 | .All(x => x == true)
|
---|
| 62 | select symbol)
|
---|
| 63 | .ToList()
|
---|
| 64 | where allowedSymbols.Count() > 0
|
---|
| 65 | select new { Parent = parent, Child = subtree, Index = subtreeIndex, AllowedSymbols = allowedSymbols })
|
---|
[5499] | 66 | .ToList();
|
---|
[5567] | 67 | if (manipulationPoints.Count == 0) { return; }
|
---|
| 68 | var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
|
---|
[645] | 69 |
|
---|
[5567] | 70 | var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 71 | var newSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
|
---|
| 72 |
|
---|
[3512] | 73 | // replace the old node with the new node
|
---|
| 74 | var newNode = newSymbol.CreateTreeNode();
|
---|
| 75 | if (newNode.HasLocalParameters)
|
---|
| 76 | newNode.ResetLocalParameters(random);
|
---|
[5567] | 77 | foreach (var subtree in selectedManipulationPoint.Child.SubTrees)
|
---|
[3512] | 78 | newNode.AddSubTree(subtree);
|
---|
[5567] | 79 | selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
|
---|
| 80 | selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, newNode);
|
---|
[645] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|