Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs @ 7072

Last change on this file since 7072 was 7072, checked in by bburlacu, 12 years ago

#1682: New versions of crossover (work-in-progress).

File size: 4.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Data;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33
34  [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping only within a specific depth range.")]
35  public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
36  {
37    [StorableConstructor]
38    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
39    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> original, Cloner cloner)
40      : base(original, cloner) { }
41    public SymbolicDataAnalysisExpressionDepthConstrainedCrossover() : base() { }
42    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
43 
44    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
45      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
46      IEnumerable<int> rows = GenerateRowsToEvaluate();
47      T problemData = ProblemDataParameter.ActualValue;
48      return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
49    }
50
51    /// <summary>
52    /// Takes two parent individuals P0 and P1.
53    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
54    /// </summary>
55    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength) {
56      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
57      parent0.Root.ForEachNodePostfix((n) => {
58        if (n.Subtrees.Any() && n != parent0.Root)
59          foreach (var child in n.Subtrees)
60            crossoverPoints0.Add(new CutPoint(n, child));
61      });
62      CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
63      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
64      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
65
66      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
67      parent1.Root.ForEachNodePostfix((n) => {
68        if (n.Subtrees.Any() && n != parent1.Root)
69          foreach (var child in n.Subtrees)
70            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
71              allowedBranches.Add(n);
72      });
73
74      // check if empty branch is allowed
75      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
76
77      if (allowedBranches.Count == 0)
78        return parent0;
79
80
81      return parent0;
82    }
83
84    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
85      // perform the actual swap
86      if (crossoverPoint.Child != null) {
87        // manipulate the tree of parent0 in place
88        // replace the branch in tree0 with the selected branch from tree1
89        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
90        if (selectedBranch != null) {
91          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
92        }
93      } else {
94        // child is null (additional child should be added under the parent)
95        if (selectedBranch != null) {
96          crossoverPoint.Parent.AddSubtree(selectedBranch);
97        }
98      }
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.