Free cookie consent management tool by TermsFeed Policy Generator

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

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

Cosmetic name-space rename. #937 (Data types and operators for symbolic expression tree encoding)

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