Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/DeleteSubTreeManipulation.cs @ 2210

Last change on this file since 2210 was 2210, checked in by gkronber, 15 years ago

GP Refactoring #713

  • introduced a plugin for GP interfaces
  • created a new interface IGeneticProgrammingModel which represents GP models in HL scopes instead of IFunctionTree
  • changed interfaces IFunction and IFunctionTree
  • moved some files to new directories (general housekeeping)
  • changed all GP operators and engines to work with IGeneticProgrammingModels
  • removed parameters TreeSize and TreeHeight in all GP operators
  • changed parameter OperatorLibrary to FunctionLibrary in all GP operators
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Random;
28using System.Diagnostics;
29using HeuristicLab.GP.Interfaces;
30
31namespace HeuristicLab.GP {
32  public class DeleteSubTreeManipulation : GPManipulatorBase {
33    public override string Description {
34      get {
35        return @"Deletes a random sub-tree of the input tree. If the remaining tree is not valid
36the operator tries to fix the tree by generating random subtrees where necessary.";
37      }
38    }
39
40    public DeleteSubTreeManipulation()
41      : base() {
42    }
43
44    internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) {
45      TreeGardener gardener = new TreeGardener(random, library);
46      IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
47
48      // parent==null means the whole tree should be deleted.
49      // => return a new minimal random tree
50      if (parent == null) {
51        IFunctionTree newTree = gardener.CreateBalancedRandomTree(1, 1);
52        // check if the tree is ok
53        Debug.Assert(gardener.IsValidTree(newTree));
54        gpModel.FunctionTree = newTree;
55        // schedule an operation to initialize the newly created operator
56        return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTree), scope);
57      }
58
59      // select a branch to prune
60      int childIndex = random.Next(parent.SubTrees.Count);
61      if (parent.SubTrees.Count > parent.Function.MinArity) {
62        parent.RemoveSubTree(childIndex);
63        // actually since the next sub-trees are shifted in the place of the removed branch
64        // it might be possible that these sub-trees are not allowed in the place of the old branch
65        // we ignore this problem for now.
66        // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted
67        // 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)
68
69        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
70        // recalculate size and height
71        gpModel.Size = gpModel.FunctionTree.GetSize();
72        gpModel.Height = gpModel.FunctionTree.GetHeight();
73        // root hasn't changed so don't need to update 'FunctionTree' variable
74        return null;
75      } else {
76        // replace with a minimal random seedling
77        parent.RemoveSubTree(childIndex);
78        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
79        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1);
80        parent.InsertSubTree(childIndex, newFunctionTree);
81        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
82        // recalculate size and height
83        gpModel.Size = gpModel.FunctionTree.GetSize();
84        gpModel.Height = gpModel.FunctionTree.GetHeight();
85        // return an initialization operation for the newly created tree
86        return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope);
87      }
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.