Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7495


Ignore:
Timestamp:
02/21/12 11:01:02 (13 years ago)
Author:
bburlacu
Message:

#1693:

  • Simplified selection of cutpoints and reordered criteria for cutpoint selection
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.cs

    r7481 r7495  
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3131
    32   [Item("ContextAwareCrossover", "An operator which deterministically choses the best insertion point for a randomly selected node.")]
     32  [Item("ContextAwareCrossover", "An operator which deterministically choses the best insertion point for a randomly selected node:\n" +
     33                                 "- Take two parent individuals P0 and P1\n" +
     34                                 "- Randomly choose a node N from P1\n" +
     35                                 "- Test all crossover points from P0 to determine the best location for N to be inserted")]
    3336  public sealed class SymbolicDataAnalysisExpressionContextAwareCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3437    [StorableConstructor]
     
    6366      var possibleChildren = new List<ISymbolicExpressionTreeNode>();
    6467      parent1.Root.ForEachNodePostfix((n) => {
    65         if (n.Subtrees.Any() && n != parent0.Root)
    66           possibleChildren.AddRange(n.Subtrees);
     68        if (n.Parent != null && n.Parent != parent1.Root)
     69          possibleChildren.Add(n);
    6770      });
    6871      var selectedChild = possibleChildren.SelectRandom(random);
     
    7174
    7275      parent0.Root.ForEachNodePostfix((n) => {
    73         if (n.Subtrees.Any() && n != parent0.Root)
    74           crossoverPoints.AddRange(from s in n.Subtrees
    75                                    let crossoverPoint = new CutPoint(n, s)
    76                                    let totalDepth = parent0.Root.GetBranchLevel(s) + selectedChild.GetDepth()
    77                                    let totalLength = parent0.Root.GetLength() - s.GetLength() + selectedChild.GetLength()
    78                                    where crossoverPoint.IsMatchingPointType(selectedChild) && totalDepth <= maxDepth && totalLength <= maxLength
    79                                    select crossoverPoint);
     76        if (n.Parent != null && n.Parent != parent0.Root) {
     77          var totalDepth = parent0.Root.GetBranchLevel(n) + selectedChild.GetDepth();
     78          var totalLength = parent0.Root.GetLength() - n.GetLength() + selectedChild.GetLength();
     79          if (totalDepth <= maxDepth && totalLength <= maxLength) {
     80            var crossoverPoint = new CutPoint(n.Parent, n);
     81            if (crossoverPoint.IsMatchingPointType(selectedChild))
     82              crossoverPoints.Add(crossoverPoint);
     83          }
     84        }
    8085      });
    8186
     
    8691          var parent = selectedChild.Parent;
    8792          // perform a swap and check the quality of the solution
    88           swap(crossoverPoint, selectedChild);
     93          Swap(crossoverPoint, selectedChild);
    8994          double quality = evaluator.Evaluate(context, parent0, problemData, rows);
    9095          qualities.Add(new Tuple<CutPoint, double>(crossoverPoint, quality));
     
    9297          selectedChild.Parent = parent;
    9398          // swap the replaced subtree back into the tree so that the structure is preserved
    94           swap(crossoverPoint, crossoverPoint.Child);
     99          Swap(crossoverPoint, crossoverPoint.Child);
    95100        }
    96101
     
    99104        // swap the node that would create the best offspring
    100105        // this last swap makes a total of (2 * crossoverPoints.Count() + 1) swap operations.
    101         swap(crossoverPoint0, selectedChild);
     106        Swap(crossoverPoint0, selectedChild);
    102107      }
    103108
    104109      return parent0;
    105110    }
    106 
    107     private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
    108       if (crossoverPoint.Child != null) {
    109         // manipulate the tree of parent0 in place
    110         // replace the branch in tree0 with the selected branch from tree1
    111         crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    112         if (selectedBranch != null) {
    113           crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    114         }
    115       } else {
    116         // child is null (additional child should be added under the parent)
    117         if (selectedBranch != null) {
    118           crossoverPoint.Parent.AddSubtree(selectedBranch);
    119         }
    120       }
    121     }
    122111  }
    123112}
Note: See TracChangeset for help on using the changeset viewer.