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 | |
---|
22 | using System.Collections.Generic; |
---|
23 | using System.Linq; |
---|
24 | using HeuristicLab.Common; |
---|
25 | using HeuristicLab.Core; |
---|
26 | using HeuristicLab.Data; |
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; |
---|
28 | using HeuristicLab.Parameters; |
---|
29 | using HEAL.Attic; |
---|
30 | using HeuristicLab.Random; |
---|
31 | |
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic { |
---|
33 | [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the notion semantic similarity between subtrees\n" + |
---|
34 | "(criteria: mean of the absolute differences between the estimated output values of the two subtrees, falling into a user-defined range)\n" + |
---|
35 | "- Take two parent individuals P0 and P1\n" + |
---|
36 | "- Randomly choose a node N from the P0\n" + |
---|
37 | "- Find the first node M that satisfies the semantic similarity criteria\n" + |
---|
38 | "- Swap N for M and return P0")] |
---|
39 | [StorableType("8AB40F60-5664-42AB-9DD9-78707476C1B2")] |
---|
40 | public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { |
---|
41 | private const string SemanticSimilarityRangeParameterName = "SemanticSimilarityRange"; |
---|
42 | |
---|
43 | #region Parameter properties |
---|
44 | public IValueParameter<DoubleRange> SemanticSimilarityRangeParameter { |
---|
45 | get { return (IValueParameter<DoubleRange>)Parameters[SemanticSimilarityRangeParameterName]; } |
---|
46 | } |
---|
47 | #endregion |
---|
48 | |
---|
49 | #region Properties |
---|
50 | public DoubleRange SemanticSimilarityRange { |
---|
51 | get { return SemanticSimilarityRangeParameter.Value; } |
---|
52 | } |
---|
53 | #endregion |
---|
54 | |
---|
55 | [StorableConstructor] |
---|
56 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(StorableConstructorFlag _) : base(_) { } |
---|
57 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { } |
---|
58 | public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() |
---|
59 | : base() { |
---|
60 | Parameters.Add(new ValueLookupParameter<DoubleRange>(SemanticSimilarityRangeParameterName, "Semantic similarity interval.", new DoubleRange(0.0001, 10))); |
---|
61 | name = "SemanticSimilarityCrossover"; |
---|
62 | } |
---|
63 | public override IDeepCloneable Clone(Cloner cloner) { |
---|
64 | return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); |
---|
65 | } |
---|
66 | |
---|
67 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { |
---|
68 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; |
---|
69 | List<int> rows = GenerateRowsToEvaluate().ToList(); |
---|
70 | T problemData = ProblemDataParameter.ActualValue; |
---|
71 | return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityRange); |
---|
72 | } |
---|
73 | |
---|
74 | /// <summary> |
---|
75 | /// Takes two parent individuals P0 and P1. |
---|
76 | /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria. |
---|
77 | /// </summary> |
---|
78 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, |
---|
79 | T problemData, List<int> rows, int maxDepth, int maxLength, DoubleRange range) { |
---|
80 | var crossoverPoints0 = new List<CutPoint>(); |
---|
81 | parent0.Root.ForEachNodePostfix((n) => { |
---|
82 | if (n.Parent != null && n.Parent != parent0.Root) |
---|
83 | crossoverPoints0.Add(new CutPoint(n.Parent, n)); |
---|
84 | }); |
---|
85 | |
---|
86 | var crossoverPoint0 = crossoverPoints0.SampleRandom(random); |
---|
87 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); |
---|
88 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); |
---|
89 | |
---|
90 | var allowedBranches = new List<ISymbolicExpressionTreeNode>(); |
---|
91 | parent1.Root.ForEachNodePostfix((n) => { |
---|
92 | if (n.Parent != null && n.Parent != parent1.Root) { |
---|
93 | if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n)) |
---|
94 | allowedBranches.Add(n); |
---|
95 | } |
---|
96 | }); |
---|
97 | |
---|
98 | if (allowedBranches.Count == 0) |
---|
99 | return parent0; |
---|
100 | |
---|
101 | var dataset = problemData.Dataset; |
---|
102 | |
---|
103 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated |
---|
104 | var rootSymbol = new ProgramRootSymbol(); |
---|
105 | var startSymbol = new StartSymbol(); |
---|
106 | var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); |
---|
107 | List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList(); |
---|
108 | crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent |
---|
109 | ISymbolicExpressionTreeNode selectedBranch = null; |
---|
110 | |
---|
111 | // pick the first node that fulfills the semantic similarity conditions |
---|
112 | foreach (var node in allowedBranches) { |
---|
113 | var parent = node.Parent; |
---|
114 | var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); // this will affect node.Parent |
---|
115 | List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList(); |
---|
116 | node.Parent = parent; // restore parent |
---|
117 | |
---|
118 | OnlineCalculatorError errorState; |
---|
119 | double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState); |
---|
120 | |
---|
121 | if (range.Start <= ssd && ssd <= range.End) { |
---|
122 | selectedBranch = node; |
---|
123 | break; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | // perform the actual swap |
---|
128 | if (selectedBranch != null) |
---|
129 | Swap(crossoverPoint0, selectedBranch); |
---|
130 | return parent0; |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|