Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs @ 16659

Last change on this file since 16659 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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