[16263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 |
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Data;
|
---|
| 8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 9 | using HeuristicLab.Parameters;
|
---|
[16565] | 10 | using HEAL.Attic;
|
---|
[16263] | 11 | using HeuristicLab.Random;
|
---|
| 12 | using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions;
|
---|
| 13 |
|
---|
| 14 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 15 | [Item("DiversityCrossover", "Simple crossover operator prioritizing internal nodes according to the given probability.")]
|
---|
[16565] | 16 | [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")]
|
---|
[16263] | 17 | public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
| 18 |
|
---|
| 19 | private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
|
---|
| 20 | private const string WindowingParameterName = "Windowing";
|
---|
| 21 | private const string ProportionalSamplingParameterName = "ProportionalSampling";
|
---|
| 22 |
|
---|
[16272] | 23 | private static readonly Func<byte[], ulong> hashFunction = HashUtil.JSHash;
|
---|
| 24 |
|
---|
[16263] | 25 | #region Parameter Properties
|
---|
| 26 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
| 27 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public IValueLookupParameter<BoolValue> WindowingParameter {
|
---|
| 31 | get { return (IValueLookupParameter<BoolValue>)Parameters[WindowingParameterName]; }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public IValueLookupParameter<BoolValue> ProportionalSamplingParameter {
|
---|
| 35 | get { return (IValueLookupParameter<BoolValue>)Parameters[ProportionalSamplingParameterName]; }
|
---|
| 36 | }
|
---|
| 37 | #endregion
|
---|
| 38 |
|
---|
| 39 | #region Properties
|
---|
| 40 | public PercentValue InternalCrossoverPointProbability {
|
---|
| 41 | get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public BoolValue Windowing {
|
---|
| 45 | get { return WindowingParameter.ActualValue; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public BoolValue ProportionalSampling {
|
---|
| 49 | get { return ProportionalSamplingParameter.ActualValue; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() {
|
---|
| 54 | name = "DiversityCrossover";
|
---|
| 55 | Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
|
---|
| 56 | Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false)));
|
---|
| 57 | Parameters.Add(new ValueLookupParameter<BoolValue>(ProportionalSamplingParameterName, "Select cutpoints proportionally using probabilities as weights instead of randomly.", new BoolValue(true)));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner) : base(original, cloner) {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T>(this, cloner);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | [StorableConstructor]
|
---|
[16565] | 68 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[16263] | 69 |
|
---|
| 70 | private static ISymbolicExpressionTreeNode ActualRoot(ISymbolicExpressionTree tree) {
|
---|
| 71 | return tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false) {
|
---|
| 75 | var leafCrossoverPointProbability = 1 - internalCrossoverPointProbability;
|
---|
| 76 |
|
---|
[16272] | 77 | var nodes0 = ActualRoot(parent0).MakeNodes().Sort(hashFunction);
|
---|
| 78 | var nodes1 = ActualRoot(parent1).MakeNodes().Sort(hashFunction);
|
---|
[16263] | 79 |
|
---|
| 80 | IList<HashNode<ISymbolicExpressionTreeNode>> sampled0;
|
---|
| 81 | IList<HashNode<ISymbolicExpressionTreeNode>> sampled1;
|
---|
| 82 |
|
---|
| 83 | if (proportionalSampling) {
|
---|
| 84 | var p = internalCrossoverPointProbability;
|
---|
[16270] | 85 | var weights0 = nodes0.Select(x => x.IsLeaf ? 1 - p : p);
|
---|
[16263] | 86 | sampled0 = nodes0.SampleProportionalWithoutRepetition(random, nodes0.Length, weights0, windowing: windowing).ToArray();
|
---|
| 87 |
|
---|
[16270] | 88 | var weights1 = nodes1.Select(x => x.IsLeaf ? 1 - p : p);
|
---|
[16263] | 89 | sampled1 = nodes1.SampleProportionalWithoutRepetition(random, nodes1.Length, weights1, windowing: windowing).ToArray();
|
---|
| 90 | } else {
|
---|
| 91 | sampled0 = ChooseNodes(random, nodes0, internalCrossoverPointProbability).ShuffleInPlace(random);
|
---|
| 92 | sampled1 = ChooseNodes(random, nodes1, internalCrossoverPointProbability).ShuffleInPlace(random);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | foreach (var selected in sampled0) {
|
---|
| 96 | var cutpoint = new CutPoint(selected.Data.Parent, selected.Data);
|
---|
| 97 |
|
---|
| 98 | var maxAllowedDepth = maxDepth - parent0.Root.GetBranchLevel(selected.Data);
|
---|
| 99 | var maxAllowedLength = maxLength - (parent0.Length - selected.Data.GetLength());
|
---|
| 100 |
|
---|
| 101 | foreach (var candidate in sampled1) {
|
---|
| 102 | if (candidate.CalculatedHashValue == selected.CalculatedHashValue
|
---|
| 103 | || candidate.Data.GetDepth() > maxAllowedDepth
|
---|
| 104 | || candidate.Data.GetLength() > maxAllowedLength
|
---|
| 105 | || !cutpoint.IsMatchingPointType(candidate.Data)) {
|
---|
| 106 | continue;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | Swap(cutpoint, candidate.Data);
|
---|
| 110 | return parent0;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | return parent0;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
| 117 | if (this.ExecutionContext == null) {
|
---|
| 118 | throw new InvalidOperationException("ExecutionContext not set.");
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | var maxDepth = MaximumSymbolicExpressionTreeDepth.Value;
|
---|
| 122 | var maxLength = MaximumSymbolicExpressionTreeLength.Value;
|
---|
| 123 |
|
---|
| 124 | var internalCrossoverPointProbability = InternalCrossoverPointProbability.Value;
|
---|
| 125 | var windowing = Windowing.Value;
|
---|
| 126 | var proportionalSampling = ProportionalSampling.Value;
|
---|
| 127 |
|
---|
| 128 | return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | private static List<HashNode<ISymbolicExpressionTreeNode>> ChooseNodes(IRandom random, IEnumerable<HashNode<ISymbolicExpressionTreeNode>> nodes, double internalCrossoverPointProbability) {
|
---|
| 132 | var list = new List<HashNode<ISymbolicExpressionTreeNode>>();
|
---|
| 133 |
|
---|
| 134 | var chooseInternal = random.NextDouble() < internalCrossoverPointProbability;
|
---|
| 135 |
|
---|
| 136 | if (chooseInternal) {
|
---|
[16270] | 137 | list.AddRange(nodes.Where(x => !x.IsLeaf && x.Data.Parent != null));
|
---|
[16263] | 138 | }
|
---|
| 139 | if (!chooseInternal || list.Count == 0) {
|
---|
[16270] | 140 | list.AddRange(nodes.Where(x => x.IsLeaf && x.Data.Parent != null));
|
---|
[16263] | 141 | }
|
---|
| 142 |
|
---|
| 143 | return list;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|