[7476] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11185] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7476] | 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[7481] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7476] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[7494] | 31 | [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees:\n" +
|
---|
| 32 | "- Take two parent individuals P0 and P1\n" +
|
---|
| 33 | "- Randomly choose a node N from P0\n" +
|
---|
| 34 | "- For each matching node M from P1, calculate the behavioral distance:\n" +
|
---|
| 35 | "\t\tD(N,M) = 0.5 * ( abs(max(N) - max(M)) + abs(min(N) - min(M)) )\n" +
|
---|
| 36 | "- Make a probabilistic weighted choice of node M from P1, based on the inversed and normalized behavioral distance")]
|
---|
[7476] | 37 | public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
| 38 | [StorableConstructor]
|
---|
| 39 | private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 40 | private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) { }
|
---|
| 42 | public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover()
|
---|
| 43 | : base() {
|
---|
[7521] | 44 | name = "ProbabilisticFunctionalCrossover";
|
---|
[7476] | 45 | }
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
|
---|
| 47 |
|
---|
[7481] | 48 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
[7476] | 49 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
| 50 | List<int> rows = GenerateRowsToEvaluate().ToList();
|
---|
| 51 | T problemData = ProblemDataParameter.ActualValue;
|
---|
| 52 | return Cross(random, parent0, parent1, interpreter, problemData,
|
---|
| 53 | rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /// <summary>
|
---|
| 57 | /// Takes two parent individuals P0 and P1.
|
---|
| 58 | /// Randomly choose a node i from the first parent, then for each matching node j from the second parent, calculate the behavioral distance based on the range:
|
---|
| 59 | /// d(i,j) = 0.5 * ( abs(max(i) - max(j)) + abs(min(i) - min(j)) ).
|
---|
[7494] | 60 | /// Next, assign probabilities for the selection of a node j based on the inversed and normalized behavioral distance, then make a random weighted choice.
|
---|
[7476] | 61 | /// </summary>
|
---|
| 62 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
| 63 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IList<int> rows, int maxDepth, int maxLength) {
|
---|
| 64 | var crossoverPoints0 = new List<CutPoint>();
|
---|
| 65 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[7494] | 66 | // the if clauses prevent the root or the startnode from being selected, although the startnode can be the parent of the node being swapped.
|
---|
| 67 | if (n.Parent != null && n.Parent != parent0.Root) {
|
---|
| 68 | crossoverPoints0.Add(new CutPoint(n.Parent, n));
|
---|
| 69 | }
|
---|
[7476] | 70 | });
|
---|
| 71 | var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
|
---|
| 72 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
| 73 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
| 74 |
|
---|
| 75 | var allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
| 76 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[7494] | 77 | if (n.Parent != null && n.Parent != parent1.Root) {
|
---|
| 78 | if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n))
|
---|
| 79 | allowedBranches.Add(n);
|
---|
| 80 | }
|
---|
[7476] | 81 | });
|
---|
| 82 |
|
---|
| 83 | if (allowedBranches.Count == 0)
|
---|
| 84 | return parent0;
|
---|
| 85 |
|
---|
| 86 | var dataset = problemData.Dataset;
|
---|
| 87 |
|
---|
| 88 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
|
---|
| 89 | var rootSymbol = new ProgramRootSymbol();
|
---|
| 90 | var startSymbol = new StartSymbol();
|
---|
| 91 | var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); // this will change crossoverPoint0.Child.Parent
|
---|
| 92 | double min0 = 0.0, max0 = 0.0;
|
---|
| 93 | foreach (double v in interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows)) {
|
---|
| 94 | if (min0 > v) min0 = v;
|
---|
| 95 | if (max0 < v) max0 = v;
|
---|
| 96 | }
|
---|
| 97 | crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore correct parent
|
---|
| 98 |
|
---|
| 99 | var weights = new List<double>();
|
---|
| 100 | foreach (var node in allowedBranches) {
|
---|
| 101 | var parent = node.Parent;
|
---|
| 102 | var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
|
---|
| 103 | double min1 = 0.0, max1 = 0.0;
|
---|
| 104 | foreach (double v in interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows)) {
|
---|
| 105 | if (min1 > v) min1 = v;
|
---|
| 106 | if (max1 < v) max1 = v;
|
---|
| 107 | }
|
---|
| 108 | double behavioralDistance = (Math.Abs(min0 - min1) + Math.Abs(max0 - max1)) / 2; // this can be NaN of Infinity because some trees are crazy like exp(exp(exp(...))), we correct that below
|
---|
| 109 | weights.Add(behavioralDistance);
|
---|
| 110 | node.Parent = parent; // restore correct node parent
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // remove branches with an infinite or NaN behavioral distance
|
---|
[7494] | 114 | for (int i = weights.Count - 1; i >= 0; --i) {
|
---|
| 115 | if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) {
|
---|
| 116 | weights.RemoveAt(i);
|
---|
| 117 | allowedBranches.RemoveAt(i);
|
---|
[7476] | 118 | }
|
---|
| 119 | }
|
---|
| 120 | // check if there are any allowed branches left
|
---|
| 121 | if (allowedBranches.Count == 0)
|
---|
| 122 | return parent0;
|
---|
| 123 |
|
---|
| 124 | ISymbolicExpressionTreeNode selectedBranch;
|
---|
| 125 | double sum = weights.Sum();
|
---|
| 126 |
|
---|
| 127 | if (sum.IsAlmost(0.0) || weights.Count == 1) // if there is only one allowed branch, or if all weights are zero
|
---|
| 128 | selectedBranch = allowedBranches[0];
|
---|
| 129 | else {
|
---|
| 130 | for (int i = 0; i != weights.Count; ++i) // normalize and invert values
|
---|
| 131 | weights[i] = 1 - weights[i] / sum;
|
---|
| 132 |
|
---|
| 133 | sum = weights.Sum(); // take new sum
|
---|
| 134 |
|
---|
| 135 | // compute the probabilities (selection weights)
|
---|
| 136 | for (int i = 0; i != weights.Count; ++i)
|
---|
| 137 | weights[i] /= sum;
|
---|
| 138 |
|
---|
| 139 | selectedBranch = allowedBranches.SelectRandom(weights, random);
|
---|
| 140 | }
|
---|
[7494] | 141 | Swap(crossoverPoint0, selectedBranch);
|
---|
[7476] | 142 | return parent0;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|