[7035] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 31 |
|
---|
[7119] | 32 | [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees.")]
|
---|
[7089] | 33 | public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
[7035] | 34 | [StorableConstructor]
|
---|
| 35 | private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
|
---|
[7193] | 36 | private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
|
---|
[7075] | 37 | : base(original, cloner) { }
|
---|
[7303] | 38 | public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover()
|
---|
| 39 | : base() {
|
---|
| 40 | Name = "ProbabilisticFunctionalCrossover";
|
---|
| 41 | }
|
---|
[7075] | 42 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
|
---|
[7089] | 43 |
|
---|
[7035] | 44 | protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
| 45 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
[7193] | 46 | List<int> rows = GenerateRowsToEvaluate().ToList();
|
---|
[7035] | 47 | T problemData = ProblemDataParameter.ActualValue;
|
---|
[7089] | 48 | return Cross(random, parent0, parent1, interpreter, problemData,
|
---|
[7075] | 49 | rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
|
---|
[7035] | 50 | }
|
---|
| 51 |
|
---|
[7119] | 52 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
| 53 | return Cross(random, parent0, parent1);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[7035] | 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)) ).
|
---|
| 60 | /// Next, assign probabilities for the selection of the second cross point based on the inversed and normalized behavioral distance and
|
---|
| 61 | /// choose the second crosspoint via a random weighted selection procedure.
|
---|
| 62 | /// </summary>
|
---|
[7089] | 63 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
[7193] | 64 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IList<int> rows, int maxDepth, int maxLength) {
|
---|
[7105] | 65 | var crossoverPoints0 = new List<CutPoint>();
|
---|
[7035] | 66 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[7089] | 67 | if (n.Subtrees.Any() && n != parent0.Root)
|
---|
| 68 | foreach (var child in n.Subtrees)
|
---|
[7035] | 69 | crossoverPoints0.Add(new CutPoint(n, child));
|
---|
| 70 | });
|
---|
[7193] | 71 | var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
|
---|
[7035] | 72 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
| 73 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
| 74 |
|
---|
[7105] | 75 | var allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[7035] | 76 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[7089] | 77 | if (n.Subtrees.Any() && n != parent1.Root)
|
---|
[7193] | 78 | allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength));
|
---|
[7035] | 79 | });
|
---|
| 80 |
|
---|
[7089] | 81 | if (allowedBranches.Count == 0)
|
---|
[7035] | 82 | return parent0;
|
---|
| 83 |
|
---|
| 84 | var dataset = problemData.Dataset;
|
---|
| 85 |
|
---|
| 86 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
|
---|
[7089] | 87 | var rootSymbol = new ProgramRootSymbol();
|
---|
| 88 | var startSymbol = new StartSymbol();
|
---|
[7105] | 89 | var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); // this will change crossoverPoint0.Child.Parent
|
---|
[7193] | 90 | List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList();
|
---|
[7035] | 91 | double min0 = estimatedValues0.Min();
|
---|
| 92 | double max0 = estimatedValues0.Max();
|
---|
[7105] | 93 | crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore correct parent
|
---|
[7035] | 94 |
|
---|
[7105] | 95 | var weights = new List<double>();
|
---|
[7035] | 96 | foreach (var node in allowedBranches) {
|
---|
[7105] | 97 | var parent = node.Parent;
|
---|
[7089] | 98 | var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
|
---|
[7193] | 99 | List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList();
|
---|
[7035] | 100 | double min1 = estimatedValues1.Min();
|
---|
| 101 | double max1 = estimatedValues1.Max();
|
---|
[7105] | 102 | 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
|
---|
| 103 | weights.Add(behavioralDistance);
|
---|
| 104 | node.Parent = parent; // restore correct node parent
|
---|
| 105 | }
|
---|
[7035] | 106 |
|
---|
[7105] | 107 | // remove branches with an infinite or NaN behavioral distance
|
---|
[7193] | 108 | int count = weights.Count, idx = 0;
|
---|
| 109 | while (idx < count) {
|
---|
| 110 | if (Double.IsNaN(weights[idx]) || Double.IsInfinity(weights[idx])) {
|
---|
| 111 | weights.RemoveAt(idx);
|
---|
| 112 | allowedBranches.RemoveAt(idx);
|
---|
| 113 | --count;
|
---|
| 114 | } else {
|
---|
| 115 | ++idx;
|
---|
[7105] | 116 | }
|
---|
[7193] | 117 | }
|
---|
[7035] | 118 |
|
---|
[7105] | 119 | // check if there are any allowed branches left
|
---|
| 120 | if (allowedBranches.Count == 0)
|
---|
| 121 | return parent0;
|
---|
| 122 |
|
---|
| 123 | ISymbolicExpressionTreeNode selectedBranch;
|
---|
| 124 | double sum = weights.Sum();
|
---|
| 125 | if (sum == 0.0)
|
---|
| 126 | selectedBranch = allowedBranches[0]; // just return the first, since we don't care (all weights are zero)
|
---|
| 127 | else {
|
---|
| 128 | // transform similarity distances into probabilities by normalizing and inverting the values
|
---|
| 129 | for (int i = 0; i != weights.Count; ++i)
|
---|
| 130 | weights[i] = (1 - weights[i] / sum);
|
---|
| 131 |
|
---|
[7193] | 132 | //selectedBranch = allowedBranches.SelectRandom(weights, random);
|
---|
[7105] | 133 | selectedBranch = SelectRandomBranch(random, allowedBranches, weights);
|
---|
[7035] | 134 | }
|
---|
[7075] | 135 | swap(crossoverPoint0, selectedBranch);
|
---|
| 136 | return parent0;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
|
---|
| 140 | if (crossoverPoint.Child != null) {
|
---|
[7035] | 141 | // manipulate the tree of parent0 in place
|
---|
| 142 | // replace the branch in tree0 with the selected branch from tree1
|
---|
[7075] | 143 | crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
|
---|
[7035] | 144 | if (selectedBranch != null) {
|
---|
[7075] | 145 | crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
|
---|
[7035] | 146 | }
|
---|
| 147 | } else {
|
---|
| 148 | // child is null (additional child should be added under the parent)
|
---|
| 149 | if (selectedBranch != null) {
|
---|
[7075] | 150 | crossoverPoint.Parent.AddSubtree(selectedBranch);
|
---|
[7035] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[7193] | 155 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IList<ISymbolicExpressionTreeNode> nodes, IList<double> weights) {
|
---|
[7035] | 156 | double r = weights.Sum() * random.NextDouble();
|
---|
| 157 | for (int i = 0; i != nodes.Count; ++i) {
|
---|
[7089] | 158 | if (r < weights[i])
|
---|
| 159 | return nodes[i];
|
---|
[7035] | 160 | r -= weights[i];
|
---|
| 161 | }
|
---|
[7105] | 162 | return nodes.Last();
|
---|
[7035] | 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|