Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs @ 5499

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

#1418 changes in symbolic expression tree encoding.

File size: 5.7 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  /// <summary>
30  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 112
31  /// </summary>
32  [Item("ArgumentDeleter", "Manipulates a symbolic expression by deleting an argument from an existing function defining branch.")]
33  [StorableClass]
34  public sealed class ArgumentDeleter : SymbolicExpressionTreeArchitectureManipulator {
35    [StorableConstructor]
36    private ArgumentDeleter(bool deserializing) : base(deserializing) { }
37    private ArgumentDeleter(ArgumentDeleter original, Cloner cloner) : base(original, cloner) { }
38    public ArgumentDeleter() : base() { }
39
40    public override sealed void ModifyArchitecture(
41      IRandom random,
42      SymbolicExpressionTree symbolicExpressionTree,
43      ISymbolicExpressionGrammar grammar,
44      IntValue maxTreeSize, IntValue maxTreeHeight,
45      IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
46      out bool success) {
47      success = DeleteArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new ArgumentDeleter(this, cloner);
52    }
53
54    public static bool DeleteArgument(
55      IRandom random,
56      SymbolicExpressionTree symbolicExpressionTree,
57      ISymbolicExpressionGrammar grammar,
58      int maxTreeSize, int maxTreeHeight,
59      int maxFunctionDefiningBranches, int maxFunctionArguments) {
60
61      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
62      if (functionDefiningBranches.Count() == 0)
63        // no function defining branch => abort
64        return false;
65      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
66      if (selectedDefunBranch.NumberOfArguments <= 1)
67        // argument deletion by consolidation is not possible => abort
68        return false;
69      // the argument to be removed is always the one with the largest index
70      // (otherwise we would have to decrement the index of the larger argument symbols)
71      var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
72                             select sym.ArgumentIndex).Distinct().OrderBy(x => x).Last();
73      // find invocations of the manipulated funcion and remove the specified argument tree
74      var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
75                             where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
76                             select node).ToList();
77      foreach (var invokeNode in invocationNodes) {
78        invokeNode.RemoveSubTree(removedArgument);
79      }
80
81      DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
82
83      // delete the dynamic argument symbol that matches the argument to be removed
84      var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).Single();
85      selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
86      selectedDefunBranch.NumberOfArguments--;
87      // reduce arity in known functions of all root branches
88      foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
89        var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();
90        if (matchingInvokeSymbol != null) {
91          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
92          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
93        }
94      }
95      return true;
96    }
97
98    private static void DeleteArgumentByConsolidation(IRandom random, DefunTreeNode branch, int removedArgumentIndex) {
99      // replace references to the deleted argument with random references to existing arguments
100      var possibleArgumentSymbols = (from sym in branch.Grammar.Symbols.OfType<Argument>()
101                                     where sym.ArgumentIndex != removedArgumentIndex
102                                     select sym).ToList();
103      var argNodes = from node in branch.IterateNodesPrefix().OfType<ArgumentTreeNode>()
104                     where node.Symbol.ArgumentIndex == removedArgumentIndex
105                     select node;
106      foreach (var argNode in argNodes) {
107        var replacementSymbol = possibleArgumentSymbols.SelectRandom(random);
108        argNode.Symbol = replacementSymbol;
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.