[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;
|
---|
| 23 | using System.Linq;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[3294] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3462] | 31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3294] | 32 | using System.Collections.Generic;
|
---|
| 33 | using System.Text;
|
---|
| 34 | using System.Diagnostics;
|
---|
| 35 |
|
---|
[3539] | 36 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
[3294] | 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]
|
---|
[3534] | 42 | public sealed class ArgumentDeleter : SymbolicExpressionTreeArchitectureManipulator {
|
---|
[3294] | 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;
|
---|
[3338] | 64 | var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
|
---|
[3294] | 65 | if (selectedDefunBranch.NumberOfArguments <= 1)
|
---|
| 66 | // argument deletion by consolidation is not possible => abort
|
---|
| 67 | return false;
|
---|
[3360] | 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)
|
---|
[3338] | 70 | var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
|
---|
[3360] | 71 | select sym.ArgumentIndex).Distinct().OrderBy(x => x).Last();
|
---|
[3338] | 72 | // find invocations of the manipulated funcion and remove the specified argument tree
|
---|
[3360] | 73 | var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
|
---|
| 74 | where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 75 | select node).ToList();
|
---|
[3294] | 76 | foreach (var invokeNode in invocationNodes) {
|
---|
[3338] | 77 | invokeNode.RemoveSubTree(removedArgument);
|
---|
[3294] | 78 | }
|
---|
| 79 |
|
---|
| 80 | DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
|
---|
| 81 |
|
---|
[3338] | 82 | // delete the dynamic argument symbol that matches the argument to be removed
|
---|
[3360] | 83 | var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).Single();
|
---|
[3338] | 84 | selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
|
---|
[3360] | 85 | selectedDefunBranch.NumberOfArguments--;
|
---|
[3294] | 86 | // reduce arity in known functions of all root branches
|
---|
| 87 | foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
|
---|
[3360] | 88 | var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();
|
---|
[3338] | 89 | if (matchingInvokeSymbol != null) {
|
---|
[3360] | 90 | subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
| 91 | subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
[3294] | 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
|
---|
[3338] | 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
|
---|
[3294] | 104 | select node;
|
---|
| 105 | foreach (var argNode in argNodes) {
|
---|
[3338] | 106 | var replacementSymbol = possibleArgumentSymbols.SelectRandom(random);
|
---|
| 107 | argNode.Symbol = replacementSymbol;
|
---|
[3294] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|