Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1682: Fixed a bug affecting the new crossovers

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    [StorableConstructor]
37    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
38    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> original, Cloner cloner)
39      : base(original, cloner) { }
40    public SymbolicDataAnalysisExpressionDepthConstrainedCrossover() : base() { }
41    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
42
43    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
44      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
45      IEnumerable<int> rows = GenerateRowsToEvaluate();
46      T problemData = ProblemDataParameter.ActualValue;
47      return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
48    }
49
50    /// <summary>
51    /// Takes two parent individuals P0 and P1.
52    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
53    /// </summary>
54    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength) {
55      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
56      parent0.Root.ForEachNodePostfix((n) => {
57        if (n.Subtrees.Any() && n != parent0.Root)
58          foreach (var child in n.Subtrees)
59            crossoverPoints0.Add(new CutPoint(n, child));
60      });
61      CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
62      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
63      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
64
65      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
66      parent1.Root.ForEachNodePostfix((n) => {
67        if (n.Subtrees.Any() && n != parent1.Root)
68          foreach (var child in n.Subtrees)
69            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
70              allowedBranches.Add(child);
71      });
72
73      // check if empty branch is allowed
74      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
75
76      if (allowedBranches.Count == 0)
77        return parent0;
78
79
80      return parent0;
81    }
82
83    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
84      // perform the actual swap
85      if (crossoverPoint.Child != null) {
86        // manipulate the tree of parent0 in place
87        // replace the branch in tree0 with the selected branch from tree1
88        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
89        if (selectedBranch != null) {
90          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
91        }
92      } else {
93        // child is null (additional child should be added under the parent)
94        if (selectedBranch != null) {
95          crossoverPoint.Parent.AddSubtree(selectedBranch);
96        }
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.