Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs @ 3360

Last change on this file since 3360 was 3360, checked in by gkronber, 14 years ago

Fixed bugs in ADF operators and added test classes for ADF operators. #290 (Implement ADFs)

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