1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
33 |
|
---|
34 | [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on behavioral similarity")]
|
---|
35 | public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
|
---|
36 | {
|
---|
37 | [StorableConstructor]
|
---|
38 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
|
---|
39 | private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner)
|
---|
40 | : base(original, cloner) {
|
---|
41 | }
|
---|
42 | public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
|
---|
43 | : base() {
|
---|
44 | }
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
46 | return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner);
|
---|
47 | }
|
---|
48 | protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
49 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
50 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
51 | T problemData = ProblemDataParameter.ActualValue;
|
---|
52 | return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
|
---|
53 | }
|
---|
54 | /// <summary>
|
---|
55 | /// Takes two parent individuals P0 and P1.
|
---|
56 | /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
|
---|
57 | /// </summary>
|
---|
58 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
59 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
|
---|
60 | List<CutPoint> crossoverPoints0 = new List<CutPoint>();
|
---|
61 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
62 | if (n.Subtrees.Any() && n != parent0.Root)
|
---|
63 | foreach (var child in n.Subtrees)
|
---|
64 | crossoverPoints0.Add(new CutPoint(n, child));
|
---|
65 | });
|
---|
66 | CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
|
---|
67 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
68 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
69 |
|
---|
70 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
71 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
72 | if (n.Subtrees.Any() && n != parent1.Root)
|
---|
73 | foreach (var child in n.Subtrees)
|
---|
74 | if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
|
---|
75 | allowedBranches.Add(n);
|
---|
76 | });
|
---|
77 |
|
---|
78 | // check if empty branch is allowed
|
---|
79 | if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
|
---|
80 |
|
---|
81 | if (allowedBranches.Count == 0)
|
---|
82 | return parent0;
|
---|
83 |
|
---|
84 | var dataset = problemData.Dataset;
|
---|
85 |
|
---|
86 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
|
---|
87 | var rootSymbol = new ProgramRootSymbol();
|
---|
88 | var startSymbol = new StartSymbol();
|
---|
89 | var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
|
---|
90 | IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
|
---|
91 |
|
---|
92 | ISymbolicExpressionTreeNode selectedBranch = null;
|
---|
93 |
|
---|
94 | // pick the first node that fulfills the semantic similarity conditions
|
---|
95 | foreach (var node in allowedBranches) {
|
---|
96 | var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
|
---|
97 | IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
|
---|
98 |
|
---|
99 | //double ssd = SSD(estimatedValues0, estimatedValues1);
|
---|
100 | OnlineCalculatorError errorState;
|
---|
101 | double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState);
|
---|
102 |
|
---|
103 | // TODO: introduce parameters alpha and beta for establishing the interval boundaries. the best values need to be determined experimentally
|
---|
104 | if (0.0001 < ssd && ssd < 10) {
|
---|
105 | selectedBranch = node;
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | // perform the actual swap
|
---|
111 | if (crossoverPoint0.Child != null) {
|
---|
112 | // manipulate the tree of parent0 in place
|
---|
113 | // replace the branch in tree0 with the selected branch from tree1
|
---|
114 | crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
|
---|
115 | if (selectedBranch != null) {
|
---|
116 | crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
|
---|
117 | }
|
---|
118 | } else {
|
---|
119 | // child is null (additional child should be added under the parent)
|
---|
120 | if (selectedBranch != null) {
|
---|
121 | crossoverPoint0.Parent.AddSubtree(selectedBranch);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return parent0;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|