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