Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs @ 7303

Last change on this file since 7303 was 7303, checked in by bburlacu, 13 years ago

#1682: Override of the CanChangeName property, added friendly names for the crossover operators.

File size: 8.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31
32  [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees.")]
33  public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
34    [StorableConstructor]
35    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
36    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
37      : base(original, cloner) { }
38    public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover()
39      : base() {
40      Name = "ProbabilisticFunctionalCrossover";
41    }
42    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
43
44    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
45      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
46      List<int> rows = GenerateRowsToEvaluate().ToList();
47      T problemData = ProblemDataParameter.ActualValue;
48      return Cross(random, parent0, parent1, interpreter, problemData,
49                   rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
50    }
51
52    public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
53      return Cross(random, parent0, parent1);
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)) ).
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>
63    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
64                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IList<int> rows, int maxDepth, int maxLength) {
65      var crossoverPoints0 = new List<CutPoint>();
66      parent0.Root.ForEachNodePostfix((n) => {
67        if (n.Subtrees.Any() && n != parent0.Root)
68          foreach (var child in n.Subtrees)
69            crossoverPoints0.Add(new CutPoint(n, child));
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) => {
77        if (n.Subtrees.Any() && n != parent1.Root)
78          allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength));
79      });
80
81      if (allowedBranches.Count == 0)
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
87      var rootSymbol = new ProgramRootSymbol();
88      var startSymbol = new StartSymbol();
89      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); // this will change crossoverPoint0.Child.Parent
90      List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList();
91      double min0 = estimatedValues0.Min();
92      double max0 = estimatedValues0.Max();
93      crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore correct parent
94
95      var weights = new List<double>();
96      foreach (var node in allowedBranches) {
97        var parent = node.Parent;
98        var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
99        List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList();
100        double min1 = estimatedValues1.Min();
101        double max1 = estimatedValues1.Max();
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      }
106
107      // remove branches with an infinite or NaN behavioral distance
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;
116        }
117      }
118
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
132        //selectedBranch = allowedBranches.SelectRandom(weights, random);
133        selectedBranch = SelectRandomBranch(random, allowedBranches, weights);
134      }
135      swap(crossoverPoint0, selectedBranch);
136      return parent0;
137    }
138
139    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
140      if (crossoverPoint.Child != null) {
141        // manipulate the tree of parent0 in place
142        // replace the branch in tree0 with the selected branch from tree1
143        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
144        if (selectedBranch != null) {
145          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
146        }
147      } else {
148        // child is null (additional child should be added under the parent)
149        if (selectedBranch != null) {
150          crossoverPoint.Parent.AddSubtree(selectedBranch);
151        }
152      }
153    }
154
155    private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IList<ISymbolicExpressionTreeNode> nodes, IList<double> weights) {
156      double r = weights.Sum() * random.NextDouble();
157      for (int i = 0; i != nodes.Count; ++i) {
158        if (r < weights[i])
159          return nodes[i];
160        r -= weights[i];
161      }
162      return nodes.Last();
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.