Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs @ 12440

Last change on this file since 12440 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range. The range parameter controls the crossover behavior:\n" +
34                                     "- HighLevel (upper 25% of the tree)\n" +
35                                     "- Standard (mid 50% of the tree)\n" +
36                                     "- LowLevel (lower 25% of the tree)")]
37  public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> :
38    SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
39    private enum Ranges { HighLevel, Standard, LowLevel };
40    private const string DepthRangeParameterName = "DepthRange";
41
42    #region Parameter properties
43    public IConstrainedValueParameter<StringValue> DepthRangeParameter {
44      get { return (IConstrainedValueParameter<StringValue>)Parameters[DepthRangeParameterName]; }
45    }
46    #endregion
47
48    #region Properties
49    public StringValue DepthRange {
50      get { return (StringValue)DepthRangeParameter.ActualValue; }
51    }
52    #endregion
53
54    [StorableConstructor]
55    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
56    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
57      : base(original, cloner) { }
58    public SymbolicDataAnalysisExpressionDepthConstrainedCrossover()
59      : base() {
60      Parameters.Add(new ConstrainedValueParameter<StringValue>(DepthRangeParameterName, "Depth range specifier"));
61      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.HighLevel)).AsReadOnly());
62      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)).AsReadOnly());
63      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.LowLevel)).AsReadOnly());
64      name = "DepthConstrainedCrossover";
65    }
66    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
67
68    public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
69      return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, DepthRange.Value);
70    }
71
72
73    /// <summary>
74    /// Takes two parent individuals P0 and P1.
75    /// Randomly choose nodes that fall within the specified depth range in both parents.
76    /// </summary>
77    /// <param name="random">Pseudo-random number generator.</param>
78    /// <param name="parent0">First parent.</param>
79    /// <param name="parent1">Second parent.</param>
80    /// <param name="maxDepth">Maximum allowed length depth.</param>
81    /// <param name="maxLength">Maximum allowed tree length.</param>
82    /// <param name="mode">Controls the crossover behavior:
83    /// - HighLevel (upper 25% of the tree),
84    /// - Standard (mid 50%)
85    /// - LowLevel (low 25%)</param>
86    /// <returns></returns>
87    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength, string mode) {
88      int depth = parent0.Root.GetDepth() - 1; // substract 1 because the tree levels are counted from 0
89      var depthRange = new IntRange();
90      const int depthOffset = 2; // skip the first 2 levels (root + startNode)
91      switch ((Ranges)Enum.Parse(typeof(Ranges), mode)) {
92        case Ranges.HighLevel:
93          depthRange.Start = depthOffset; // skip the first 2 levels (root + startNode)
94          depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.25);
95          break;
96        case Ranges.Standard:
97          depthRange.Start = depthOffset + (int)Math.Round(depth * 0.25);
98          depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.5);
99          break;
100        case Ranges.LowLevel:
101          depthRange.Start = depthOffset + (int)Math.Round(depth * 0.75);
102          depthRange.End = Math.Max(depthRange.Start, depth);
103          break;
104      }
105
106      // make sure that the depth range does not exceeded the actual depth of parent0
107      if (depthRange.Start > depth)
108        depthRange.Start = depth;
109      if (depthRange.End < depthRange.Start)
110        depthRange.End = depthRange.Start;
111
112      var crossoverPoints0 = (from node in GetNodesAtDepth(parent0, depthRange) select new CutPoint(node.Parent, node)).ToList();
113
114      if (crossoverPoints0.Count == 0)
115        throw new Exception("No crossover points available in the first parent");
116
117      var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
118
119      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
120      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
121
122      var allowedBranches = (from s in GetNodesAtDepth(parent1, depthRange)
123                             where s.GetDepth() + level <= maxDepth
124                             where s.GetLength() + length <= maxLength
125                             where crossoverPoint0.IsMatchingPointType(s)
126                             select s).ToList();
127      if (allowedBranches.Count == 0) return parent0;
128      var selectedBranch = allowedBranches.SelectRandom(random);
129      Swap(crossoverPoint0, selectedBranch);
130      return parent0;
131    }
132
133    private static IEnumerable<ISymbolicExpressionTreeNode> GetNodesAtDepth(ISymbolicExpressionTree tree, IntRange depthRange) {
134      var treeDepth = tree.Root.GetDepth();
135      return from node in tree.Root.IterateNodesPostfix()
136             let depth = treeDepth - node.GetDepth()
137             where depthRange.Start <= depth
138             where depth <= depthRange.End
139             select node;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.