1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Crossovers {
|
---|
31 | [Item("SymbolicDataAnalysisExpressionDiversityPreservingCrossover", "A crossover operator which tries to preserve diversity by favoring duplicate nodes as crossover points in the tree")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
34 | private const string SimilarityCalculatorParameterName = "SimilarityCalculator";
|
---|
35 | private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
|
---|
36 |
|
---|
37 | #region Parameter Properties
|
---|
38 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
39 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
40 | }
|
---|
41 | public IValueParameter<ISymbolicDataAnalysisExpressionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
42 | get { return (IValueParameter<ISymbolicDataAnalysisExpressionSimilarityCalculator>)Parameters[SimilarityCalculatorParameterName]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | public ISymbolicDataAnalysisExpressionSimilarityCalculator SimilarityCalculator {
|
---|
47 | get { return SimilarityCalculatorParameter.Value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | }
|
---|
53 |
|
---|
54 | public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() {
|
---|
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 ValueParameter<ISymbolicDataAnalysisExpressionSimilarityCalculator>(SimilarityCalculatorParameterName, "The similarity calculator", new BottomUpSimilarityCalculator()));
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T>(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | [StorableConstructor]
|
---|
64 | private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(bool deserializing) : base(deserializing) { }
|
---|
65 |
|
---|
66 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
67 | int maximumTreeLength = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value;
|
---|
68 | int maximumTreeDepth = MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value;
|
---|
69 | double internalCrossoverPointProbability = InternalCrossoverPointProbabilityParameter.ActualValue.Value;
|
---|
70 |
|
---|
71 | return Cross(random, parent0, parent1, SimilarityCalculator, maximumTreeDepth, maximumTreeLength);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, ISymbolicDataAnalysisExpressionSimilarityCalculator similarityCalculator, int maxDepth, int maxLength) {
|
---|
75 | var actualRoot = parent0.Root.GetSubtree(0).GetSubtree(0);
|
---|
76 | while (actualRoot.SubtreeCount > 0 && actualRoot.SubtreeCount < 2) actualRoot = actualRoot.GetSubtree(0);
|
---|
77 | if (actualRoot.SubtreeCount == 0) return parent0;
|
---|
78 | var left = actualRoot.GetSubtree(0);
|
---|
79 | var right = actualRoot.GetSubtree(1);
|
---|
80 | var bus = (BottomUpSimilarityCalculator)similarityCalculator;
|
---|
81 | var map = bus.ComputeBottomUpMapping(left, right);
|
---|
82 |
|
---|
83 | var crossoverPoints = parent0.IterateNodesPostfix().Where(x => x.Parent != null && x.Parent != parent0.Root).Select(x => new CutPoint(x.Parent, x)).ToList();
|
---|
84 | var weights = crossoverPoints.Select(x => map.ContainsKey(x.Child) ? 3D : x.Child.SubtreeCount > 0 ? 2D : 1D);
|
---|
85 |
|
---|
86 | var cutpoint = crossoverPoints.SelectRandom(weights, random);
|
---|
87 | int level = parent0.Root.GetBranchLevel(cutpoint.Child);
|
---|
88 | int length = parent0.Root.GetLength() - cutpoint.Child.GetLength();
|
---|
89 |
|
---|
90 | var allowedBranches = parent1.IterateNodesPostfix().Where(x => x.Parent != null &&
|
---|
91 | x.Parent != parent1.Root &&
|
---|
92 | x.GetDepth() + level <= maxDepth &&
|
---|
93 | x.GetLength() + length <= maxLength &&
|
---|
94 | cutpoint.IsMatchingPointType(x)).ToList();
|
---|
95 | if (!allowedBranches.Any())
|
---|
96 | return parent0;
|
---|
97 |
|
---|
98 | var selectedBranch = allowedBranches.SelectRandom(random);
|
---|
99 |
|
---|
100 | // swap the node that would create the best offspring
|
---|
101 | Swap(cutpoint, selectedBranch);
|
---|
102 | return parent0;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|