Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/16/11 09:43:02 (12 years ago)
Author:
bburlacu
Message:

#1682: Overhauled the crossover operators, fixed bug in the DeterministicBestCrossover.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs

    r7119 r7193  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2726using HeuristicLab.Common;
    2827using HeuristicLab.Core;
    2928using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    30 using HeuristicLab.Data;
    3129
    3230namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    3634    [StorableConstructor]
    3735    private SymbolicDataAnalysisExpressionDeterministicBestCrossover(bool deserializing) : base(deserializing) { }
    38     private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> original, Cloner cloner)
     36    private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
    3937      : base(original, cloner) {
    4038    }
     
    4846      if (this.ExecutionContext == null)
    4947        throw new InvalidOperationException("ExecutionContext not set.");
    50       IEnumerable<int> rows = GenerateRowsToEvaluate();
     48      List<int> rows = GenerateRowsToEvaluate().ToList();
    5149      T problemData = ProblemDataParameter.ActualValue;
    5250      ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue;
     
    6361    /// </summary>
    6462    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, IExecutionContext context,
    65                                                 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
     63                                                ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, List<int> rows, int maxDepth, int maxLength) {
    6664      var crossoverPoints0 = new List<CutPoint>();
    6765      parent0.Root.ForEachNodePostfix((n) => {
    6866        if (n.Subtrees.Any() && n != parent0.Root)
    69           foreach (var child in n.Subtrees)
    70             crossoverPoints0.Add(new CutPoint(n, child));
     67          crossoverPoints0.AddRange(from s in n.Subtrees select new CutPoint(n, s));
    7168      });
    72       CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
     69      CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random);
    7370      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
    7471      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
     
    7774      parent1.Root.ForEachNodePostfix((n) => {
    7875        if (n.Subtrees.Any() && n != parent1.Root)
    79           foreach (var child in n.Subtrees)
    80             if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    81               allowedBranches.Add(child);
     76          allowedBranches.AddRange(from s in n.Subtrees
     77                                   where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength
     78                                   select s);
    8279      });
    83 
    84       // check if empty branch is allowed
    85       if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    8680
    8781      if (allowedBranches.Count == 0)
     
    9387      var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>();
    9488
     89      var originalChild = crossoverPoint0.Child;
     90
    9591      foreach (var node in allowedBranches) {
    9692        var parent = node.Parent;
    9793        swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent
    9894        double quality = evaluator.Evaluate(context, parent0, problemData, rows);
     95        swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1)
    9996        nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality));
    10097        node.Parent = parent; // restore correct parent
     
    103100      nodeQualities.Sort((a, b) => a.Item2.CompareTo(b.Item2)); // assuming this sorts the list in ascending order
    104101      selectedBranch = evaluator.Maximization ? nodeQualities.Last().Item1 : nodeQualities.First().Item1;
     102
     103
     104      if (selectedBranch == null)
     105        throw new Exception("Selected branch is null");
     106
     107      if (selectedBranch.Parent == null)
     108        throw new Exception("Parent is null");
     109
     110
    105111      // swap the node that would create the best offspring
    106112      swap(crossoverPoint0, selectedBranch);
Note: See TracChangeset for help on using the changeset viewer.