Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/ArgumentDeleter.cs @ 5656

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

Updated year of copyrights (#1406)

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