[16980] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16980] | 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;
|
---|
[16263] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[16980] | 25 | using HEAL.Attic;
|
---|
[16263] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Random;
|
---|
| 32 | using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16980] | 35 | [Item("DiversityCrossover", "Simple crossover operator preventing swap between subtrees with the same hash value.")]
|
---|
[16565] | 36 | [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")]
|
---|
[16263] | 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";
|
---|
[16980] | 42 | private const string StrictHashingParameterName = "StrictHashing";
|
---|
[16263] | 43 |
|
---|
[17076] | 44 | private static readonly Func<byte[], ulong> hashFunction = HashUtil.DJBHash;
|
---|
[16272] | 45 |
|
---|
[16263] | 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 | }
|
---|
[16980] | 58 |
|
---|
| 59 | public IFixedValueParameter<BoolValue> StrictHashingParameter {
|
---|
| 60 | get { return (IFixedValueParameter<BoolValue>)Parameters[StrictHashingParameterName]; }
|
---|
| 61 | }
|
---|
[16263] | 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 | }
|
---|
[16980] | 76 |
|
---|
| 77 | bool StrictHashing {
|
---|
| 78 | get { return StrictHashingParameter.Value.Value; }
|
---|
| 79 | }
|
---|
[16263] | 80 | #endregion
|
---|
| 81 |
|
---|
[16980] | 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 |
|
---|
[17076] | 90 | public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() : base() {
|
---|
[16263] | 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)));
|
---|
[16980] | 94 | Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
|
---|
[16263] | 95 | }
|
---|
| 96 |
|
---|
[17076] | 97 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
[16263] | 98 |
|
---|
| 99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 100 | return new SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T>(this, cloner);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | [StorableConstructor]
|
---|
[16565] | 104 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[16263] | 105 |
|
---|
| 106 | private static ISymbolicExpressionTreeNode ActualRoot(ISymbolicExpressionTree tree) {
|
---|
| 107 | return tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[16980] | 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);
|
---|
[16263] | 113 |
|
---|
| 114 | IList<HashNode<ISymbolicExpressionTreeNode>> sampled0;
|
---|
| 115 | IList<HashNode<ISymbolicExpressionTreeNode>> sampled1;
|
---|
| 116 |
|
---|
| 117 | if (proportionalSampling) {
|
---|
| 118 | var p = internalCrossoverPointProbability;
|
---|
[16270] | 119 | var weights0 = nodes0.Select(x => x.IsLeaf ? 1 - p : p);
|
---|
[16263] | 120 | sampled0 = nodes0.SampleProportionalWithoutRepetition(random, nodes0.Length, weights0, windowing: windowing).ToArray();
|
---|
| 121 |
|
---|
[16270] | 122 | var weights1 = nodes1.Select(x => x.IsLeaf ? 1 - p : p);
|
---|
[16263] | 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 |
|
---|
[16980] | 162 | return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling, StrictHashing);
|
---|
[16263] | 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) {
|
---|
[16270] | 171 | list.AddRange(nodes.Where(x => !x.IsLeaf && x.Data.Parent != null));
|
---|
[16263] | 172 | }
|
---|
| 173 | if (!chooseInternal || list.Count == 0) {
|
---|
[16270] | 174 | list.AddRange(nodes.Where(x => x.IsLeaf && x.Data.Parent != null));
|
---|
[16263] | 175 | }
|
---|
| 176 |
|
---|
| 177 | return list;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|