[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;
|
---|
[3462] | 35 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
|
---|
[3294] | 36 |
|
---|
[3539] | 37 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
[3294] | 38 | /// <summary>
|
---|
| 39 | /// Manipulates a symbolic expression by deleting a preexisting function-defining branch.
|
---|
| 40 | /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 108
|
---|
| 41 | /// </summary>
|
---|
| 42 | [Item("SubroutineDeleter", "Manipulates a symbolic expression by deleting a preexisting function-defining branch.")]
|
---|
| 43 | [StorableClass]
|
---|
[3534] | 44 | public sealed class SubroutineDeleter : SymbolicExpressionTreeArchitectureManipulator {
|
---|
[3294] | 45 | public override sealed void ModifyArchitecture(
|
---|
| 46 | IRandom random,
|
---|
| 47 | SymbolicExpressionTree symbolicExpressionTree,
|
---|
| 48 | ISymbolicExpressionGrammar grammar,
|
---|
| 49 | IntValue maxTreeSize, IntValue maxTreeHeight,
|
---|
| 50 | IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
|
---|
| 51 | out bool success) {
|
---|
| 52 | success = DeleteSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public static bool DeleteSubroutine(
|
---|
| 56 | IRandom random,
|
---|
| 57 | SymbolicExpressionTree symbolicExpressionTree,
|
---|
| 58 | ISymbolicExpressionGrammar grammar,
|
---|
| 59 | int maxTreeSize, int maxTreeHeight,
|
---|
| 60 | int maxFunctionDefiningBranches, int maxFunctionArguments) {
|
---|
| 61 | var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
|
---|
| 62 |
|
---|
| 63 | if (functionDefiningBranches.Count() == 0)
|
---|
| 64 | // no ADF to delete => abort
|
---|
| 65 | return false;
|
---|
[3338] | 66 | var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
|
---|
[3294] | 67 | // remove the selected defun
|
---|
| 68 | int defunSubtreeIndex = symbolicExpressionTree.Root.SubTrees.IndexOf(selectedDefunBranch);
|
---|
| 69 | symbolicExpressionTree.Root.RemoveSubTree(defunSubtreeIndex);
|
---|
| 70 |
|
---|
| 71 | // remove references to deleted function
|
---|
[3369] | 72 | foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
|
---|
[3360] | 73 | var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
|
---|
| 74 | where symb.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 75 | select symb).SingleOrDefault();
|
---|
| 76 | if (matchingInvokeSymbol != null) {
|
---|
| 77 | subtree.Grammar.RemoveSymbol(matchingInvokeSymbol);
|
---|
[3294] | 78 | }
|
---|
| 79 | }
|
---|
[3360] | 80 |
|
---|
| 81 | DeletionByRandomRegeneration(random, symbolicExpressionTree, selectedDefunBranch);
|
---|
[3294] | 82 | return true;
|
---|
| 83 | }
|
---|
[3360] | 84 |
|
---|
| 85 | private static void DeletionByRandomRegeneration(IRandom random, SymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {
|
---|
| 86 | // find first invocation and replace it with a randomly generated tree
|
---|
| 87 | // can't find all invocations in one step because once we replaced a top level invocation
|
---|
| 88 | // the invocations below it are removed already
|
---|
| 89 | var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
|
---|
| 90 | from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
|
---|
| 91 | where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 92 | select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
|
---|
| 93 | while (invocationCutPoint != null) {
|
---|
| 94 | // deletion by random regeneration
|
---|
| 95 | SymbolicExpressionTreeNode replacementTree = null;
|
---|
| 96 | // TODO: should weight symbols by tickets
|
---|
| 97 | var selectedSymbol = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).SelectRandom(random);
|
---|
| 98 |
|
---|
| 99 | int minPossibleSize = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol);
|
---|
| 100 | int maxSize = Math.Max(minPossibleSize, invocationCutPoint.ReplacedChild.GetSize());
|
---|
| 101 | int minPossibleHeight = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);
|
---|
| 102 | int maxHeight = Math.Max(minPossibleHeight, invocationCutPoint.ReplacedChild.GetHeight());
|
---|
[3369] | 103 | replacementTree = selectedSymbol.CreateTreeNode();
|
---|
[3535] | 104 | if (replacementTree.HasLocalParameters)
|
---|
| 105 | replacementTree.ResetLocalParameters(random);
|
---|
[3360] | 106 | invocationCutPoint.Parent.RemoveSubTree(invocationCutPoint.ReplacedChildIndex);
|
---|
| 107 | invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree);
|
---|
| 108 |
|
---|
[3369] | 109 | ProbabilisticTreeCreator.PTC2(random, replacementTree, maxSize, maxHeight, 0, 0);
|
---|
| 110 |
|
---|
[3360] | 111 | invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
|
---|
| 112 | from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
|
---|
| 113 | where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 114 | select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
[3294] | 117 | }
|
---|
| 118 | }
|
---|