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("ContextAwareCrossover", "An operator which performs subtree swapping in a deterministic way: it checks all possible cross points in the second parent with the aim of creating the best possible offspring.")]
|
---|
35 | public sealed class SymbolicDataAnalysisExpressionContextAwareCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
36 | [StorableConstructor]
|
---|
37 | private SymbolicDataAnalysisExpressionContextAwareCrossover(bool deserializing) : base(deserializing) { }
|
---|
38 | private SymbolicDataAnalysisExpressionContextAwareCrossover(SymbolicDataAnalysisExpressionContextAwareCrossover<T> original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | }
|
---|
41 | public SymbolicDataAnalysisExpressionContextAwareCrossover()
|
---|
42 | : base() {
|
---|
43 | }
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
45 | return new SymbolicDataAnalysisExpressionContextAwareCrossover<T>(this, cloner);
|
---|
46 | }
|
---|
47 | protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
48 | if (this.ExecutionContext == null)
|
---|
49 | throw new InvalidOperationException("ExecutionContext not set.");
|
---|
50 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
51 | T problemData = ProblemDataParameter.ActualValue;
|
---|
52 | ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue;
|
---|
53 |
|
---|
54 | return Cross(random, parent0, parent1, this.ExecutionContext, evaluator, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Takes two parent individuals P0 and P1.
|
---|
58 | /// Randomly choose a node i from the first parent, then test all nodes j from the second parent to determine the best child that would be obtained by swapping i for j.
|
---|
59 | /// </summary>
|
---|
60 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, IExecutionContext context,
|
---|
61 | ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
|
---|
62 | List<CutPoint> crossoverPoints0 = new List<CutPoint>();
|
---|
63 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
64 | if (n.Subtrees.Any() && n != parent0.Root)
|
---|
65 | foreach (var child in n.Subtrees)
|
---|
66 | crossoverPoints0.Add(new CutPoint(n, child));
|
---|
67 | });
|
---|
68 | CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
|
---|
69 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
70 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
71 |
|
---|
72 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
73 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
74 | if (n.Subtrees.Any() && n != parent1.Root)
|
---|
75 | foreach (var child in n.Subtrees)
|
---|
76 | if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
|
---|
77 | allowedBranches.Add(child);
|
---|
78 | });
|
---|
79 |
|
---|
80 | // check if empty branch is allowed
|
---|
81 | if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
|
---|
82 |
|
---|
83 | if (allowedBranches.Count == 0)
|
---|
84 | return parent0;
|
---|
85 |
|
---|
86 | var dataset = problemData.Dataset;
|
---|
87 |
|
---|
88 | // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
|
---|
89 | ISymbolicExpressionTreeNode selectedBranch = null;
|
---|
90 |
|
---|
91 | var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>();
|
---|
92 |
|
---|
93 | foreach (var node in allowedBranches) {
|
---|
94 | swap(crossoverPoint0, node);
|
---|
95 | double quality = evaluator.Evaluate(context, parent0, problemData, rows);
|
---|
96 | nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality));
|
---|
97 | }
|
---|
98 |
|
---|
99 | nodeQualities.Sort((a, b) => a.Item2.CompareTo(b.Item2)); // assuming this sorts the list in ascending order
|
---|
100 |
|
---|
101 | if (evaluator.Maximization)
|
---|
102 | selectedBranch = nodeQualities.Last().Item1;
|
---|
103 | else
|
---|
104 | selectedBranch = nodeQualities.First().Item1;
|
---|
105 |
|
---|
106 | // swap the node that would create the best offspring
|
---|
107 | swap(crossoverPoint0, selectedBranch);
|
---|
108 |
|
---|
109 | return parent0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
|
---|
113 | if (crossoverPoint.Child != null) {
|
---|
114 | // manipulate the tree of parent0 in place
|
---|
115 | // replace the branch in tree0 with the selected branch from tree1
|
---|
116 | crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
|
---|
117 | if (selectedBranch != null) {
|
---|
118 | crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
|
---|
119 | }
|
---|
120 | } else {
|
---|
121 | // child is null (additional child should be added under the parent)
|
---|
122 | if (selectedBranch != null) {
|
---|
123 | crossoverPoint.Parent.AddSubtree(selectedBranch);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|