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