Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored symbolic expression tree encoding and problem classes for symbolic regression. #937 , #938

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