[3294] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[4068] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3294] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[3539] | 28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
[3294] | 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]
|
---|
[3534] | 34 | public sealed class ArgumentDeleter : SymbolicExpressionTreeArchitectureManipulator {
|
---|
[3294] | 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;
|
---|
[3338] | 56 | var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
|
---|
[3294] | 57 | if (selectedDefunBranch.NumberOfArguments <= 1)
|
---|
| 58 | // argument deletion by consolidation is not possible => abort
|
---|
| 59 | return false;
|
---|
[3360] | 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)
|
---|
[3338] | 62 | var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
|
---|
[3360] | 63 | select sym.ArgumentIndex).Distinct().OrderBy(x => x).Last();
|
---|
[3338] | 64 | // find invocations of the manipulated funcion and remove the specified argument tree
|
---|
[3360] | 65 | var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
|
---|
| 66 | where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 67 | select node).ToList();
|
---|
[3294] | 68 | foreach (var invokeNode in invocationNodes) {
|
---|
[3338] | 69 | invokeNode.RemoveSubTree(removedArgument);
|
---|
[3294] | 70 | }
|
---|
| 71 |
|
---|
| 72 | DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
|
---|
| 73 |
|
---|
[3338] | 74 | // delete the dynamic argument symbol that matches the argument to be removed
|
---|
[3360] | 75 | var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).Single();
|
---|
[3338] | 76 | selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
|
---|
[3360] | 77 | selectedDefunBranch.NumberOfArguments--;
|
---|
[3294] | 78 | // reduce arity in known functions of all root branches
|
---|
| 79 | foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
|
---|
[3360] | 80 | var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();
|
---|
[3338] | 81 | if (matchingInvokeSymbol != null) {
|
---|
[3360] | 82 | subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
| 83 | subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
[3294] | 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
|
---|
[3338] | 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
|
---|
[3294] | 96 | select node;
|
---|
| 97 | foreach (var argNode in argNodes) {
|
---|
[3338] | 98 | var replacementSymbol = possibleArgumentSymbols.SelectRandom(random);
|
---|
| 99 | argNode.Symbol = replacementSymbol;
|
---|
[3294] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|