Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDeleter.cs @ 17456

Last change on this file since 17456 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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