#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Parameters; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { [StorableClass] [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")] public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator { private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; #region Parameter Properties public IValueLookupParameter MaximumSymbolicExpressionTreeLengthParameter { get { return (IValueLookupParameter)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } } public IValueLookupParameter MaximumSymbolicExpressionTreeDepthParameter { get { return (IValueLookupParameter)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } } #endregion #region Properties public IntValue MaximumSymbolicExpressionTreeLength { get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } } public IntValue MaximumSymbolicExpressionTreeDepth { get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } } #endregion [StorableConstructor] private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { } private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { } public ReplaceBranchManipulation() : base() { Parameters.Add(new ValueLookupParameter(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); Parameters.Add(new ValueLookupParameter(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); } public override IDeepCloneable Clone(Cloner cloner) { return new ReplaceBranchManipulation(this, cloner); } protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); } public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) { // select any node as parent (except the root node) var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1) from subtree in parent.SubTrees select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) }) .SelectRandom(random); int maxLength = maxTreeLength - symbolicExpressionTree.Length + manipulationPoint.Node.GetLength(); int maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + manipulationPoint.Node.GetDepth(); // find possible symbols for the node (also considering the existing branches below it) var allowedSymbols = (from symbol in manipulationPoint.Parent.Grammar.GetAllowedSymbols(manipulationPoint.Parent.Symbol, manipulationPoint.Index) where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength select symbol).ToList(); if (allowedSymbols.Count() == 0) return; var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); var seedSymbol = allowedSymbols.SelectRandom(weights, random); // replace the old node with the new node var seedNode = seedSymbol.CreateTreeNode(); if (seedNode.HasLocalParameters) seedNode.ResetLocalParameters(random); manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index); manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode); seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0); } } }