Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDeleter.cs @ 4524

Last change on this file since 4524 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
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.")]
36  [StorableClass]
37  public sealed class SubroutineDeleter : SymbolicExpressionTreeArchitectureManipulator {
38    public override sealed void ModifyArchitecture(
39      IRandom random,
40      SymbolicExpressionTree symbolicExpressionTree,
41      ISymbolicExpressionGrammar grammar,
42      IntValue maxTreeSize, IntValue maxTreeHeight,
43      IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
44      out bool success) {
45      success = DeleteSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
46    }
47
48    public static bool DeleteSubroutine(
49      IRandom random,
50      SymbolicExpressionTree symbolicExpressionTree,
51      ISymbolicExpressionGrammar grammar,
52      int maxTreeSize, int maxTreeHeight,
53      int maxFunctionDefiningBranches, int maxFunctionArguments) {
54      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
55
56      if (functionDefiningBranches.Count() == 0)
57        // no ADF to delete => abort
58        return false;
59      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
60      // remove the selected defun
61      int defunSubtreeIndex = symbolicExpressionTree.Root.SubTrees.IndexOf(selectedDefunBranch);
62      symbolicExpressionTree.Root.RemoveSubTree(defunSubtreeIndex);
63
64      // remove references to deleted function
65      foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
66        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
67                                    where symb.FunctionName == selectedDefunBranch.FunctionName
68                                    select symb).SingleOrDefault();
69        if (matchingInvokeSymbol != null) {
70          subtree.Grammar.RemoveSymbol(matchingInvokeSymbol);
71        }
72      }
73
74      DeletionByRandomRegeneration(random, symbolicExpressionTree, selectedDefunBranch);
75      return true;
76    }
77
78    private static void DeletionByRandomRegeneration(IRandom random, SymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {
79      // find first invocation and replace it with a randomly generated tree
80      // can't find all invocations in one step because once we replaced a top level invocation
81      // the invocations below it are removed already
82      var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
83                                from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
84                                where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
85                                select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
86      while (invocationCutPoint != null) {
87        // deletion by random regeneration
88        SymbolicExpressionTreeNode replacementTree = null;
89        // TODO: should weight symbols by tickets
90        var selectedSymbol = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).SelectRandom(random);
91
92        int minPossibleSize = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol);
93        int maxSize = Math.Max(minPossibleSize, invocationCutPoint.ReplacedChild.GetSize());
94        int minPossibleHeight = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);
95        int maxHeight = Math.Max(minPossibleHeight, invocationCutPoint.ReplacedChild.GetHeight());
96        replacementTree = selectedSymbol.CreateTreeNode();
97        if (replacementTree.HasLocalParameters)
98          replacementTree.ResetLocalParameters(random);
99        invocationCutPoint.Parent.RemoveSubTree(invocationCutPoint.ReplacedChildIndex);
100        invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree);
101
102        ProbabilisticTreeCreator.PTC2(random, replacementTree, maxSize, maxHeight, 0, 0);
103
104        invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
105                              from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
106                              where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
107                              select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.