#region License Information /* HeuristicLab * Copyright (C) 2002-2008 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 HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Random; using HeuristicLab.Functions; namespace HeuristicLab.StructureIdentification { public class DeleteSubTreeManipulation : OperatorBase { public override string Description { get { return @"Deletes a random sub-tree of the input tree. If the remaining tree is not valid the operator tries to fix the tree by generating random subtrees where necessary."; } } public DeleteSubTreeManipulation() : base() { AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In)); AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In)); AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In)); AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); } public override IOperation Apply(IScope scope) { IFunctionTree root = GetVariableValue("FunctionTree", scope, true); MersenneTwister random = GetVariableValue("Random", scope, true); GPOperatorLibrary library = GetVariableValue("OperatorLibrary", scope, true); TreeGardener gardener = new TreeGardener(random, library); IFunctionTree parent = gardener.GetRandomParentNode(root); // parent==null means the whole tree should be deleted. // => return a new minimal random tree if(parent == null) { IFunctionTree newTree = gardener.CreateRandomTree(1, 1, false); // check if the tree is ok if(!gardener.IsValidTree(newTree)) { throw new InvalidOperationException(); } // update sizes to match the new tree GetVariableValue("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree); GetVariableValue("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree); scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; // schedule an operation to initialize the newly created operator return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); } // select a branch to prune int childIndex = random.Next(parent.SubTrees.Count); int min; int max; gardener.GetMinMaxArity(parent.Function, out min, out max); if(parent.SubTrees.Count > min) { parent.RemoveSubTree(childIndex); // actually since the next sub-trees are shifted in the place of the removed branch // it might be possible that these sub-trees are not allowed in the place of the old branch // we ignore this problem for now. // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted // and find the first one that doesn't fit. At this position we insert a new randomly initialized subtree of matching type (gkronber 25.12.07) if(!gardener.IsValidTree(root)) { throw new InvalidOperationException(); } GetVariableValue("TreeSize", scope, true).Data = gardener.GetTreeSize(root); GetVariableValue("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); // root hasn't changed so don't need to update 'FunctionTree' variable return null; } else { // replace with a minimal random seedling parent.RemoveSubTree(childIndex); ICollection allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1, true); parent.InsertSubTree(childIndex, newFunctionTree); if(!gardener.IsValidTree(root)) { throw new InvalidProgramException(); } GetVariableValue("TreeSize", scope, true).Data = gardener.GetTreeSize(root); GetVariableValue("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); // again the root hasn't changed so we don't need to update the 'FunctionTree' variable // but we have to return an initialization operation for the newly created tree return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope); } } } }