Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs @ 3338

Last change on this file since 3338 was 3338, checked in by gkronber, 15 years ago

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

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;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
31using System.Collections.Generic;
32using System.Text;
33using System.Diagnostics;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators {
36  /// <summary>
37  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 112
38  /// </summary>
39  [Item("ArgumentDeleter", "Manipulates a symbolic expression by deleting an argument from an existing function defining branch.")]
40  [StorableClass]
41  public sealed class ArgumentDeleter : SymbolicExpressionTreeArchitectureAlteringOperator {
42    public override sealed void ModifyArchitecture(
43      IRandom random,
44      SymbolicExpressionTree symbolicExpressionTree,
45      ISymbolicExpressionGrammar grammar,
46      IntValue maxTreeSize, IntValue maxTreeHeight,
47      IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
48      out bool success) {
49      success = DeleteArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
50    }
51
52    public static bool DeleteArgument(
53      IRandom random,
54      SymbolicExpressionTree symbolicExpressionTree,
55      ISymbolicExpressionGrammar grammar,
56      int maxTreeSize, int maxTreeHeight,
57      int maxFunctionDefiningBranches, int maxFunctionArguments) {
58
59      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
60      if (functionDefiningBranches.Count() == 0)
61        // no function defining branch => abort
62        return false;
63      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
64      if (selectedDefunBranch.NumberOfArguments <= 1)
65        // argument deletion by consolidation is not possible => abort
66        return false;
67      var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
68                             select sym.ArgumentIndex).Distinct().SelectRandom(random);
69      // find invocations of the manipulated funcion and remove the specified argument tree
70      var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
71                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
72                            select node;
73      foreach (var invokeNode in invocationNodes) {
74        invokeNode.RemoveSubTree(removedArgument);
75      }
76
77      DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
78
79      // delete the dynamic argument symbol that matches the argument to be removed
80      var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).First();
81      selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
82      // reduce arity in known functions of all root branches
83      foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
84        var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).FirstOrDefault();
85        if (matchingInvokeSymbol != null) {
86          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
87          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
88        }
89      }
90      selectedDefunBranch.NumberOfArguments--;
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.SelectRandom(random);
104        argNode.Symbol = replacementSymbol;
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.