Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs @ 5549

Last change on this file since 5549 was 5549, checked in by gkronber, 13 years ago

#1418 unified size/height vs. length/depth terminology and adapted unit tests for symbolic expression tree encoding version 3.4

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System.Collections.Generic;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  /// <summary>
32  /// Manipulates a symbolic expression by deleting a preexisting function-defining branch.
33  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 108
34  /// </summary>
35  [Item("SubroutineDeleter", "Manipulates a symbolic expression by deleting a preexisting function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 108")]
36  [StorableClass]
37  public sealed class SubroutineDeleter : SymbolicExpressionTreeArchitectureManipulator {
38    [StorableConstructor]
39    private SubroutineDeleter(bool deserializing) : base(deserializing) { }
40    private SubroutineDeleter(SubroutineDeleter original, Cloner cloner) : base(original, cloner) { }
41    public SubroutineDeleter() : base() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new SubroutineDeleter(this, cloner);
45    }
46
47    public override sealed void ModifyArchitecture(
48      IRandom random,
49      ISymbolicExpressionTree symbolicExpressionTree,
50      IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
51      DeleteSubroutine(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
52    }
53
54    public static bool DeleteSubroutine(
55      IRandom random,
56      ISymbolicExpressionTree symbolicExpressionTree,
57      int maxFunctionDefinitions, int maxFunctionArguments) {
58      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
59
60      if (functionDefiningBranches.Count() == 0)
61        // no ADF to delete => abort
62        return false;
63      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
64      // remove the selected defun
65      int defunSubtreeIndex = symbolicExpressionTree.Root.IndexOfSubTree(selectedDefunBranch);
66      symbolicExpressionTree.Root.RemoveSubTree(defunSubtreeIndex);
67
68      // remove references to deleted function
69      foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
70        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
71                                    where symb.FunctionName == selectedDefunBranch.FunctionName
72                                    select symb).SingleOrDefault();
73        if (matchingInvokeSymbol != null) {
74          subtree.Grammar.RemoveSymbol(matchingInvokeSymbol);
75        }
76      }
77
78      DeletionByRandomRegeneration(random, symbolicExpressionTree, selectedDefunBranch);
79      return true;
80    }
81
82    private static void DeletionByRandomRegeneration(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {
83      // find first invocation and replace it with a randomly generated tree
84      // can't find all invocations in one step because once we replaced a top level invocation
85      // the invocations below it are removed already
86      var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
87                                from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
88                                where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
89                                select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).FirstOrDefault();
90      while (invocationCutPoint != null) {
91        // deletion by random regeneration
92        ISymbolicExpressionTreeNode replacementTree = null;
93        var allowedSymbolsList = invocationCutPoint.Parent.Grammar.GetAllowedSymbols(invocationCutPoint.Parent.Symbol, invocationCutPoint.ReplacedChildIndex).ToList();
94        var weights = allowedSymbolsList.Select(s => s.InitialFrequency);
95        var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random);
96
97        int minPossibleLength = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol);
98        int maxLength = Math.Max(minPossibleLength, invocationCutPoint.ReplacedChild.GetLength());
99        int minPossibleDepth = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);
100        int maxDepth = Math.Max(minPossibleDepth, invocationCutPoint.ReplacedChild.GetDepth());
101        replacementTree = selectedSymbol.CreateTreeNode();
102        if (replacementTree.HasLocalParameters)
103          replacementTree.ResetLocalParameters(random);
104        invocationCutPoint.Parent.RemoveSubTree(invocationCutPoint.ReplacedChildIndex);
105        invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree);
106
107        ProbabilisticTreeCreator.PTC2(random, replacementTree, maxLength, maxDepth, 0, 0);
108
109        invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
110                              from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
111                              where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
112                              select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).FirstOrDefault();
113      }
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.