Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/12 10:57:40 (12 years ago)
Author:
bburlacu
Message:

#1682: * Moved Swap method into base class

  • Simplified selection of allowed branches
  • Replaced while loop with a more simple reversed for loop
Location:
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers
Files:
2 edited

Legend:

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

    r7481 r7494  
    149149      return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
    150150    }
     151
     152    protected static void Swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
     153      if (crossoverPoint.Child != null) {
     154        // manipulate the tree of parent0 in place
     155        // replace the branch in tree0 with the selected branch from tree1
     156        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
     157        if (selectedBranch != null) {
     158          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
     159        }
     160      } else {
     161        // child is null (additional child should be added under the parent)
     162        if (selectedBranch != null) {
     163          crossoverPoint.Parent.AddSubtree(selectedBranch);
     164        }
     165      }
     166    }
    151167  }
    152168}
  • branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs

    r7481 r7494  
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3131
    32   [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees.")]
     32  [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees:\n" +
     33                                            "- Take two parent individuals P0 and P1\n" +
     34                                            "- Randomly choose a node N from P0\n" +
     35                                            "- For each matching node M from P1, calculate the behavioral distance:\n" +
     36                                            "\t\tD(N,M) = 0.5 * ( abs(max(N) - max(M)) + abs(min(N) - min(M)) )\n" +
     37                                            "- Make a probabilistic weighted choice of node M from P1, based on the inversed and normalized behavioral distance")]
    3338  public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3439    [StorableConstructor]
     
    3843    public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover()
    3944      : base() {
     45      Name = "ProbabilisticFunctionalCrossover";
    4046    }
    4147    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
     
    5359    /// Randomly choose a node i from the first parent, then for each matching node j from the second parent, calculate the behavioral distance based on the range:
    5460    /// d(i,j) = 0.5 * ( abs(max(i) - max(j)) + abs(min(i) - min(j)) ).
    55     /// Next, assign probabilities for the selection of the second cross point based on the inversed and normalized behavioral distance and
    56     /// choose the second crosspoint via a random weighted selection procedure.
     61    /// Next, assign probabilities for the selection of a node j based on the inversed and normalized behavioral distance, then make a random weighted choice.
    5762    /// </summary>
    5863    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
     
    6065      var crossoverPoints0 = new List<CutPoint>();
    6166      parent0.Root.ForEachNodePostfix((n) => {
    62         if (n.Subtrees.Any() && n != parent0.Root)
    63           crossoverPoints0.AddRange(from s in n.Subtrees
    64                                     select new CutPoint(n, s));
     67        // the if clauses prevent the root or the startnode from being selected, although the startnode can be the parent of the node being swapped.
     68        if (n.Parent != null && n.Parent != parent0.Root) {
     69          crossoverPoints0.Add(new CutPoint(n.Parent, n));
     70        }
    6571      });
    6672      var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
     
    7076      var allowedBranches = new List<ISymbolicExpressionTreeNode>();
    7177      parent1.Root.ForEachNodePostfix((n) => {
    72         if (n.Subtrees.Any() && n != parent1.Root)
    73           allowedBranches.AddRange(from s in n.Subtrees
    74                                    where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength
    75                                    select s);
     78        if (n.Parent != null && n.Parent != parent1.Root) {
     79          if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n))
     80            allowedBranches.Add(n);
     81        }
    7682      });
    7783
     
    107113
    108114      // remove branches with an infinite or NaN behavioral distance
    109       int count = weights.Count, idx = 0;
    110       while (idx < count) {
    111         if (Double.IsNaN(weights[idx]) || Double.IsInfinity(weights[idx])) {
    112           weights.RemoveAt(idx);
    113           allowedBranches.RemoveAt(idx);
    114           --count;
    115         } else {
    116           ++idx;
     115      for (int i = weights.Count - 1; i >= 0; --i) {
     116        if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) {
     117          weights.RemoveAt(i);
     118          allowedBranches.RemoveAt(i);
    117119        }
    118120      }
    119 
    120121      // check if there are any allowed branches left
    121122      if (allowedBranches.Count == 0)
     
    139140        selectedBranch = allowedBranches.SelectRandom(weights, random);
    140141      }
    141       swap(crossoverPoint0, selectedBranch);
     142      Swap(crossoverPoint0, selectedBranch);
    142143      return parent0;
    143     }
    144 
    145     private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
    146       if (crossoverPoint.Child != null) {
    147         // manipulate the tree of parent0 in place
    148         // replace the branch in tree0 with the selected branch from tree1
    149         crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    150         if (selectedBranch != null) {
    151           crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    152         }
    153       } else {
    154         // child is null (additional child should be added under the parent)
    155         if (selectedBranch != null) {
    156           crossoverPoint.Parent.AddSubtree(selectedBranch);
    157         }
    158       }
    159144    }
    160145  }
Note: See TracChangeset for help on using the changeset viewer.