Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/ArgumentDuplicater.cs @ 4068

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

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

File size: 6.1 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  /// Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch.
31  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 94
32  /// </summary>
33  [Item("ArgumentDuplicater", "Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch.")]
34  [StorableClass]
35  public sealed class ArgumentDuplicater : SymbolicExpressionTreeArchitectureManipulator {
36    public override sealed void ModifyArchitecture(
37      IRandom random,
38      SymbolicExpressionTree symbolicExpressionTree,
39      ISymbolicExpressionGrammar grammar,
40      IntValue maxTreeSize, IntValue maxTreeHeight,
41      IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
42      out bool success) {
43      success = DuplicateArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
44    }
45
46    public static bool DuplicateArgument(
47      IRandom random,
48      SymbolicExpressionTree symbolicExpressionTree,
49      ISymbolicExpressionGrammar grammar,
50      int maxTreeSize, int maxTreeHeight,
51      int maxFunctionDefiningBranches, int maxFunctionArguments) {
52      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
53
54      var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
55      if (functionDefiningBranches.Count() == 0)
56        // no function defining branches => abort
57        return false;
58
59      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
60      var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>();
61      if (argumentSymbols.Count() == 0 || argumentSymbols.Count() >= maxFunctionArguments)
62        // when no argument or number of arguments is already at max allowed value => abort
63        return false;
64      var selectedArgumentSymbol = argumentSymbols.SelectRandom(random);
65      var takenIndexes = argumentSymbols.Select(s => s.ArgumentIndex);
66      var newArgumentIndex = allowedArgumentIndexes.Except(takenIndexes).First();
67
68      var newArgSymbol = new Argument(newArgumentIndex);
69
70      // replace existing references to the original argument with references to the new argument randomly in the selectedBranch
71      var argumentNodes = selectedDefunBranch.IterateNodesPrefix().OfType<ArgumentTreeNode>();
72      foreach (var argNode in argumentNodes) {
73        if (argNode.Symbol == selectedArgumentSymbol) {
74          if (random.NextDouble() < 0.5) {
75            argNode.Symbol = newArgSymbol;
76          }
77        }
78      }
79      // find invocations of the functions and duplicate the matching argument branch
80      var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
81                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
82                            select node;
83      foreach (var invokeNode in invocationNodes) {
84        var argumentBranch = invokeNode.SubTrees[selectedArgumentSymbol.ArgumentIndex];
85        var clonedArgumentBranch = (SymbolicExpressionTreeNode)argumentBranch.Clone();
86        invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch);
87      }
88      // register the new argument symbol and increase the number of arguments of the ADF
89      selectedDefunBranch.Grammar.AddSymbol(newArgSymbol);
90      selectedDefunBranch.Grammar.SetMinSubtreeCount(newArgSymbol, 0);
91      selectedDefunBranch.Grammar.SetMaxSubtreeCount(newArgSymbol, 0);
92      // allow the argument as child of any other symbol
93      foreach (var symb in selectedDefunBranch.Grammar.Symbols)
94        for (int i = 0; i < selectedDefunBranch.Grammar.GetMaxSubtreeCount(symb); i++) {
95          selectedDefunBranch.Grammar.SetAllowedChild(symb, newArgSymbol, i);
96        }
97      selectedDefunBranch.NumberOfArguments++;
98
99      // increase the arity of the changed ADF in all branches that can use this ADF
100      foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
101        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
102                                    where symb.FunctionName == selectedDefunBranch.FunctionName
103                                    select symb).SingleOrDefault();
104        if (matchingInvokeSymbol != null) {
105          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
106          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
107          foreach (var child in subtree.GetAllowedSymbols(0)) {
108            for (int i = 0; i < subtree.Grammar.GetMaxSubtreeCount(matchingInvokeSymbol); i++) {
109              subtree.Grammar.SetAllowedChild(matchingInvokeSymbol, child, i);
110            }
111          }
112        }
113      }
114      return true;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.