Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs

Last change on this file was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 8.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Random;
32using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
35  [Item("DiversityCrossover", "Simple crossover operator preventing swap between subtrees with the same hash value.")]
36  [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")]
37  public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
38
39    private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
40    private const string WindowingParameterName = "Windowing";
41    private const string ProportionalSamplingParameterName = "ProportionalSampling";
42    private const string StrictHashingParameterName = "StrictHashing";
43
44    private static readonly Func<byte[], ulong> hashFunction = HashUtil.DJBHash;
45
46    #region Parameter Properties
47    public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
48      get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
49    }
50
51    public IValueLookupParameter<BoolValue> WindowingParameter {
52      get { return (IValueLookupParameter<BoolValue>)Parameters[WindowingParameterName]; }
53    }
54
55    public IValueLookupParameter<BoolValue> ProportionalSamplingParameter {
56      get { return (IValueLookupParameter<BoolValue>)Parameters[ProportionalSamplingParameterName]; }
57    }
58
59    public IFixedValueParameter<BoolValue> StrictHashingParameter {
60      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictHashingParameterName]; }
61    }
62    #endregion
63
64    #region Properties
65    public PercentValue InternalCrossoverPointProbability {
66      get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
67    }
68
69    public BoolValue Windowing {
70      get { return WindowingParameter.ActualValue; }
71    }
72
73    public BoolValue ProportionalSampling {
74      get { return ProportionalSamplingParameter.ActualValue; }
75    }
76
77    bool StrictHashing {
78      get { return StrictHashingParameter.Value.Value; }
79    }
80    #endregion
81
82
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      if (!Parameters.ContainsKey(StrictHashingParameterName)) {
86        Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
87      }
88    }
89
90    public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() : base() {
91      Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
92      Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false)));
93      Parameters.Add(new ValueLookupParameter<BoolValue>(ProportionalSamplingParameterName, "Select cutpoints proportionally using probabilities as weights instead of randomly.", new BoolValue(true)));
94      Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
95    }
96
97    private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner) : base(original, cloner) { }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T>(this, cloner);
101    }
102
103    [StorableConstructor]
104    private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(StorableConstructorFlag _) : base(_) { }
105
106    private static ISymbolicExpressionTreeNode ActualRoot(ISymbolicExpressionTree tree) {
107      return tree.Root.GetSubtree(0).GetSubtree(0);
108    }
109
110    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false, bool strictHashing = false) {
111      var nodes0 = ActualRoot(parent0).MakeNodes(strictHashing).Sort(hashFunction);
112      var nodes1 = ActualRoot(parent1).MakeNodes(strictHashing).Sort(hashFunction);
113
114      IList<HashNode<ISymbolicExpressionTreeNode>> sampled0;
115      IList<HashNode<ISymbolicExpressionTreeNode>> sampled1;
116
117      if (proportionalSampling) {
118        var p = internalCrossoverPointProbability;
119        var weights0 = nodes0.Select(x => x.IsLeaf ? 1 - p : p);
120        sampled0 = nodes0.SampleProportionalWithoutRepetition(random, nodes0.Length, weights0, windowing: windowing).ToArray();
121
122        var weights1 = nodes1.Select(x => x.IsLeaf ? 1 - p : p);
123        sampled1 = nodes1.SampleProportionalWithoutRepetition(random, nodes1.Length, weights1, windowing: windowing).ToArray();
124      } else {
125        sampled0 = ChooseNodes(random, nodes0, internalCrossoverPointProbability).ShuffleInPlace(random);
126        sampled1 = ChooseNodes(random, nodes1, internalCrossoverPointProbability).ShuffleInPlace(random);
127      }
128
129      foreach (var selected in sampled0) {
130        var cutpoint = new CutPoint(selected.Data.Parent, selected.Data);
131
132        var maxAllowedDepth = maxDepth - parent0.Root.GetBranchLevel(selected.Data);
133        var maxAllowedLength = maxLength - (parent0.Length - selected.Data.GetLength());
134
135        foreach (var candidate in sampled1) {
136          if (candidate.CalculatedHashValue == selected.CalculatedHashValue
137            || candidate.Data.GetDepth() > maxAllowedDepth
138            || candidate.Data.GetLength() > maxAllowedLength
139            || !cutpoint.IsMatchingPointType(candidate.Data)) {
140            continue;
141          }
142
143          Swap(cutpoint, candidate.Data);
144          return parent0;
145        }
146      }
147      return parent0;
148    }
149
150    public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
151      if (this.ExecutionContext == null) {
152        throw new InvalidOperationException("ExecutionContext not set.");
153      }
154
155      var maxDepth = MaximumSymbolicExpressionTreeDepth.Value;
156      var maxLength = MaximumSymbolicExpressionTreeLength.Value;
157
158      var internalCrossoverPointProbability = InternalCrossoverPointProbability.Value;
159      var windowing = Windowing.Value;
160      var proportionalSampling = ProportionalSampling.Value;
161
162      return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling, StrictHashing);
163    }
164
165    private static List<HashNode<ISymbolicExpressionTreeNode>> ChooseNodes(IRandom random, IEnumerable<HashNode<ISymbolicExpressionTreeNode>> nodes, double internalCrossoverPointProbability) {
166      var list = new List<HashNode<ISymbolicExpressionTreeNode>>();
167
168      var chooseInternal = random.NextDouble() < internalCrossoverPointProbability;
169
170      if (chooseInternal) {
171        list.AddRange(nodes.Where(x => !x.IsLeaf && x.Data.Parent != null));
172      }
173      if (!chooseInternal || list.Count == 0) {
174        list.AddRange(nodes.Where(x => x.IsLeaf && x.Data.Parent != null));
175      }
176
177      return list;
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.