1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
32 |
|
---|
33 | [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the semantic similarity between subtrees.")]
|
---|
34 | public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
35 | private const string SemanticSimilarityRangeParameterName = "SemanticSimilarityRange";
|
---|
36 |
|
---|
37 | #region Parameter properties
|
---|
38 | public IValueParameter<DoubleRange> SemanticSimilarityRangeParameter {
|
---|
39 | get { return (IValueParameter<DoubleRange>)Parameters[SemanticSimilarityRangeParameterName]; }
|
---|
40 | }
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | #region Properties
|
---|
44 | public DoubleRange SemanticSimilarityRange {
|
---|
45 | get { return SemanticSimilarityRangeParameter.Value; }
|
---|
46 | }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
|
---|
51 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new ValueLookupParameter<DoubleRange>(SemanticSimilarityRangeParameterName, "Semantic similarity interval.", new DoubleRange(0.0001, 10)));
|
---|
55 | Name = "SemanticSimilarityCrossover";
|
---|
56 | }
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); }
|
---|
58 |
|
---|
59 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
60 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
61 | List<int> rows = GenerateRowsToEvaluate().ToList();
|
---|
62 | T problemData = ProblemDataParameter.ActualValue;
|
---|
63 | return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityRange);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Takes two parent individuals P0 and P1.
|
---|
68 | /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
|
---|
69 | /// </summary>
|
---|
70 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
71 | T problemData, List<int> rows, int maxDepth, int maxLength, DoubleRange range) {
|
---|
72 | var crossoverPoints0 = new List<CutPoint>();
|
---|
73 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
74 | if (n.Subtrees.Any() && n != parent0.Root)
|
---|
75 | foreach (var child in n.Subtrees)
|
---|
76 | crossoverPoints0.Add(new CutPoint(n, child));
|
---|
77 | });
|
---|
78 | var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
|
---|
79 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
80 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
81 |
|
---|
82 | var allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
83 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
84 | if (n.Subtrees.Any() && n != parent1.Root)
|
---|
85 | allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength));
|
---|
86 | });
|
---|
87 |
|
---|
88 | if (allowedBranches.Count == 0)
|
---|
89 | return parent0;
|
---|
90 |
|
---|
91 | var dataset = problemData.Dataset;
|
---|
92 |
|
---|
93 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
|
---|
94 | var rootSymbol = new ProgramRootSymbol();
|
---|
95 | var startSymbol = new StartSymbol();
|
---|
96 | var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
|
---|
97 | List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList();
|
---|
98 | crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent
|
---|
99 | ISymbolicExpressionTreeNode selectedBranch = null;
|
---|
100 |
|
---|
101 | // pick the first node that fulfills the semantic similarity conditions
|
---|
102 | foreach (var node in allowedBranches) {
|
---|
103 | var parent = node.Parent;
|
---|
104 | var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); // this will affect node.Parent
|
---|
105 | List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList();
|
---|
106 | node.Parent = parent; // restore parent
|
---|
107 |
|
---|
108 | OnlineCalculatorError errorState;
|
---|
109 | double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState);
|
---|
110 |
|
---|
111 | if (range.Start > ssd || range.End < ssd)
|
---|
112 | continue;
|
---|
113 |
|
---|
114 | selectedBranch = node;
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // perform the actual swap
|
---|
119 | if (selectedBranch != null)
|
---|
120 | swap(crossoverPoint0, selectedBranch);
|
---|
121 |
|
---|
122 | return parent0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
|
---|
126 | // perform the actual swap
|
---|
127 | if (crossoverPoint.Child != null) {
|
---|
128 | // manipulate the tree of parent0 in place
|
---|
129 | // replace the branch in tree0 with the selected branch from tree1
|
---|
130 | crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
|
---|
131 | if (selectedBranch != null) {
|
---|
132 | crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
|
---|
133 | }
|
---|
134 | } else {
|
---|
135 | // child is null (additional child should be added under the parent)
|
---|
136 | if (selectedBranch != null) {
|
---|
137 | crossoverPoint.Parent.AddSubtree(selectedBranch);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|