Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4501 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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