Changeset 7193


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

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

Location:
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.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 SymbolicDataAnalysisExpressionContextAwareCrossover(bool deserializing) : base(deserializing) { }
    38     private SymbolicDataAnalysisExpressionContextAwareCrossover(SymbolicDataAnalysisExpressionContextAwareCrossover<T> original, Cloner cloner)
     36    private SymbolicDataAnalysisExpressionContextAwareCrossover(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;
     
    6462    /// </summary>
    6563    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, IExecutionContext context,
    66                                                 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
     64                                                ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, List<int> rows, int maxDepth, int maxLength) {
    6765      // randomly choose a node from the second parent
    6866      var possibleChildren = new List<ISymbolicExpressionTreeNode>();
    6967      parent1.Root.ForEachNodePostfix((n) => {
    7068        if (n.Subtrees.Any() && n != parent0.Root)
    71           foreach (var child in n.Subtrees)
    72             possibleChildren.Add(child);
     69          possibleChildren.AddRange(n.Subtrees);
    7370      });
    74       var selectedChild = possibleChildren[random.Next(possibleChildren.Count)];
     71      var selectedChild = possibleChildren.SelectRandom(random);
    7572      var crossoverPoints = new List<CutPoint>();
    7673      var qualities = new List<Tuple<CutPoint, double>>();
     
    7875      parent0.Root.ForEachNodePostfix((n) => {
    7976        if (n.Subtrees.Any() && n != parent0.Root)
    80           foreach (var child in n.Subtrees) {
    81             var crossoverPoint = new CutPoint(n, child);
    82             int totalDepth = parent0.Root.GetBranchLevel(child) + selectedChild.GetDepth();
    83             int totalLength = parent0.Root.GetLength() - child.GetLength() + selectedChild.GetLength();
    84             if (crossoverPoint.IsMatchingPointType(selectedChild) && totalDepth <= maxDepth && totalLength <= maxLength) {
    85               crossoverPoints.Add(crossoverPoint);
    86             }
    87           }
     77          crossoverPoints.AddRange(from s in n.Subtrees
     78                                   let crossoverPoint = new CutPoint(n, s)
     79                                   let totalDepth = parent0.Root.GetBranchLevel(s) + selectedChild.GetDepth()
     80                                   let totalLength = parent0.Root.GetLength() - s.GetLength() + selectedChild.GetLength()
     81                                   where crossoverPoint.IsMatchingPointType(selectedChild) && totalDepth <= maxDepth && totalLength <= maxLength
     82                                   select crossoverPoint);
    8883      });
    8984
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs

    r7119 r7193  
    3434namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3535  public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover, ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    36     private const string RandomParameterName = "Random";
    3736    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
    3837    private const string ProblemDataParameterName = "ProblemData";
     
    9796      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximum tree depth."));
    9897      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum tree length."));
     98
     99      EvaluatorParameter.Hidden = true;
     100      EstimationLimitsParameter.Hidden = true;
     101      SymbolicDataAnalysisEvaluationPartitionParameter.Hidden = true;
     102      SymbolicDataAnalysisTreeInterpreterParameter.Hidden = true;
     103      ProblemDataParameter.Hidden = true;
     104      RelativeNumberOfEvaluatedSamplesParameter.Hidden = true;
    99105    }
    100106
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs

    r7119 r7193  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2726using HeuristicLab.Common;
     
    2928using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3029using HeuristicLab.Data;
     30using HeuristicLab.Parameters;
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3333
    34   [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping restricted to a specific depth range.")]
    35   public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     34  [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range.")]
     35  public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> :
     36    SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     37    private enum Ranges { HighLevel, Standard, Lowlevel };
     38    private const string DepthRangeParameterName = "DepthRange";
     39    #region Parameter properties
     40
     41    public ConstrainedValueParameter<StringValue> DepthRangeParameter {
     42      get { return (ConstrainedValueParameter<StringValue>)Parameters[DepthRangeParameterName]; }
     43    }
     44    #endregion
     45
     46    #region Properties
     47    public StringValue DepthRange {
     48      get { return (StringValue)DepthRangeParameter.ActualValue; }
     49    }
     50    #endregion
     51
    3652    [StorableConstructor]
    3753    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
    38     private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> original, Cloner cloner)
     54    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
    3955      : base(original, cloner) { }
    40     public SymbolicDataAnalysisExpressionDepthConstrainedCrossover() : base() { }
     56    public SymbolicDataAnalysisExpressionDepthConstrainedCrossover()
     57      : base() {
     58      Parameters.Add(new ConstrainedValueParameter<StringValue>(DepthRangeParameterName, "Depth range specifier"));
     59      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.HighLevel)));
     60      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)));
     61      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Lowlevel)));
     62    }
    4163    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
    4264
    4365    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);
     66      return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, DepthRange.Value);
    4867    }
    4968
     
    5473    /// <summary>
    5574    /// Takes two parent individuals P0 and P1.
    56     /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
     75    /// Randomly choose nodes that fall within the specified depth range in both parents.
    5776    /// </summary>
    58     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength) {
    59       List<CutPoint> crossoverPoints0 = new List<CutPoint>();
     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();
     89      var depthRange = new DoubleRange();
     90      switch ((int)Enum.Parse(typeof(Ranges), mode)) {
     91        case (int)Ranges.HighLevel:
     92          depthRange.Start = 0;
     93          depthRange.End = Math.Round(depth * 0.25);
     94          break;
     95        case (int)Ranges.Standard:
     96          depthRange.Start = Math.Round(depth * 0.25);
     97          depthRange.End = Math.Round(depth * 0.75);
     98          break;
     99        case (int)Ranges.Lowlevel:
     100          depthRange.Start = Math.Round(depth * 0.75);
     101          depthRange.End = depth;
     102          break;
     103      }
     104
     105      var crossoverPoints0 = new List<CutPoint>();
    60106      parent0.Root.ForEachNodePostfix((n) => {
    61107        if (n.Subtrees.Any() && n != parent0.Root)
    62           foreach (var child in n.Subtrees)
    63             crossoverPoints0.Add(new CutPoint(n, child));
     108          crossoverPoints0.AddRange(from s in n.Subtrees
     109                                    where parent0.Root.GetBranchLevel(s) >= depthRange.Start && parent0.Root.GetBranchLevel(s) <= depthRange.End
     110                                    select new CutPoint(n, s));
    64111      });
    65       CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
     112      CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random);
    66113      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
    67114      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
    68115
    69       List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
     116      var allowedBranches = new List<ISymbolicExpressionTreeNode>();
    70117      parent1.Root.ForEachNodePostfix((n) => {
    71         if (n.Subtrees.Any() && n != parent1.Root)
    72           foreach (var child in n.Subtrees)
    73             if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    74               allowedBranches.Add(child);
     118        if (n.Subtrees.Any() && n != parent1.Root) {
     119          allowedBranches.AddRange(from s in n.Subtrees
     120                                   let branchLevel = parent1.Root.GetBranchLevel(s)
     121                                   where branchLevel >= depthRange.Start && branchLevel <= depthRange.End && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength
     122                                   select s);
     123        }
    75124      });
    76 
    77       // check if empty branch is allowed
    78       if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    79125
    80126      if (allowedBranches.Count == 0)
    81127        return parent0;
    82128
     129      var selectedBranch = allowedBranches.SelectRandom(random);
     130
     131      swap(crossoverPoint0, selectedBranch);
    83132
    84133      return parent0;
  • 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);
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.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 SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
    38     private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner)
     36    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
    3937      : base(original, cloner) { }
    4038    public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { }
     
    4341    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4442      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
    45       IEnumerable<int> rows = GenerateRowsToEvaluate();
     43      List<int> rows = GenerateRowsToEvaluate().ToList();
    4644      T problemData = ProblemDataParameter.ActualValue;
    47       var grammar = parent0.Root.Grammar;
    4845      return Cross(random, parent0, parent1, interpreter, problemData,
    4946                   rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
     
    6259    /// </summary>
    6360    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
    64                                                 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
     61                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IList<int> rows, int maxDepth, int maxLength) {
    6562      var crossoverPoints0 = new List<CutPoint>();
    6663      parent0.Root.ForEachNodePostfix((n) => {
     
    6966            crossoverPoints0.Add(new CutPoint(n, child));
    7067      });
    71       var crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
     68      var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
    7269      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
    7370      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
     
    7673      parent1.Root.ForEachNodePostfix((n) => {
    7774        if (n.Subtrees.Any() && n != parent1.Root)
    78           foreach (var child in n.Subtrees)
    79             if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    80               allowedBranches.Add(child);
     75          allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength));
    8176      });
    82 
    83       // check if empty branch is allowed
    84       if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    8577
    8678      if (allowedBranches.Count == 0)
     
    9385      var startSymbol = new StartSymbol();
    9486      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); // this will change crossoverPoint0.Child.Parent
    95       IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
     87      List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList();
    9688      double min0 = estimatedValues0.Min();
    9789      double max0 = estimatedValues0.Max();
     
    10294        var parent = node.Parent;
    10395        var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
    104         IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
     96        List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList();
    10597        double min1 = estimatedValues1.Min();
    10698        double max1 = estimatedValues1.Max();
     
    111103
    112104      // remove branches with an infinite or NaN behavioral distance
    113       for (int i = 0; i != weights.Count; ++i)
    114         if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) {
    115           weights.RemoveAt(i);
    116           allowedBranches.RemoveAt(i);
     105      int count = weights.Count, idx = 0;
     106      while (idx < count) {
     107        if (Double.IsNaN(weights[idx]) || Double.IsInfinity(weights[idx])) {
     108          weights.RemoveAt(idx);
     109          allowedBranches.RemoveAt(idx);
     110          --count;
     111        } else {
     112          ++idx;
    117113        }
     114      }
    118115
    119116      // check if there are any allowed branches left
     
    130127          weights[i] = (1 - weights[i] / sum);
    131128
     129        //selectedBranch = allowedBranches.SelectRandom(weights, random);
    132130        selectedBranch = SelectRandomBranch(random, allowedBranches, weights);
    133131      }
     
    152150    }
    153151
    154     private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, List<ISymbolicExpressionTreeNode> nodes, List<double> weights) {
     152    private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IList<ISymbolicExpressionTreeNode> nodes, IList<double> weights) {
    155153      double r = weights.Sum() * random.NextDouble();
    156154      for (int i = 0; i != nodes.Count; ++i) {
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs

    r7119 r7193  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Text;
    2624using HeuristicLab.Parameters;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3533  [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the semantic similarity between subtrees.")]
    3634  public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    37     private const string SemanticSimilarityLowerBoundParameterName = "SemanticSimilarityLowerBound";
    38     private const string SemanticSimilarityUpperBoundParameterName = "SemanticSimilarityUpperBound";
     35    private const string SemanticSimilarityRangeParameterName = "SemanticSimilarityRange";
    3936
    4037    #region Parameter properties
    41     public IValueParameter<DoubleValue> SemanticSimilarityLowerBoundParameter {
    42       get { return (IValueParameter<DoubleValue>)Parameters[SemanticSimilarityLowerBoundParameterName]; }
    43     }
    44     public IValueParameter<DoubleValue> SemanticSimilarityUpperBoundParameter {
    45       get { return (IValueParameter<DoubleValue>)Parameters[SemanticSimilarityUpperBoundParameterName]; }
     38    public IValueParameter<DoubleRange> SemanticSimilarityRangeParameter {
     39      get { return (IValueParameter<DoubleRange>)Parameters[SemanticSimilarityRangeParameterName]; }
    4640    }
    4741    #endregion
    4842
    4943    #region Properties
    50     public DoubleValue SemanticSimilarityLowerBound {
    51       get { return SemanticSimilarityLowerBoundParameter.Value; }
    52     }
    53     public DoubleValue SemanticSimilarityUpperBound {
    54       get { return SemanticSimilarityUpperBoundParameter.Value; }
     44    public DoubleRange SemanticSimilarityRange {
     45      get { return SemanticSimilarityRangeParameter.Value; }
    5546    }
    5647    #endregion
     
    5849    [StorableConstructor]
    5950    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
    60     private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { }
     51    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { }
    6152    public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
    6253      : base() {
    63       Parameters.Add(new ValueLookupParameter<DoubleValue>(SemanticSimilarityLowerBoundParameterName, "The lower bound of the semantic similarity interval."));
    64       Parameters.Add(new ValueLookupParameter<DoubleValue>(SemanticSimilarityUpperBoundParameterName, "The lower bound of the semantic similarity interval."));
    65 
    66       // Set some decent default values for the lower and upper bound parameters
    67       SemanticSimilarityLowerBoundParameter.Value = new DoubleValue(0.0001);
    68       SemanticSimilarityUpperBoundParameter.Value = new DoubleValue(10);
     54      Parameters.Add(new ValueLookupParameter<DoubleRange>(SemanticSimilarityRangeParameterName, "Semantic similarity interval.", new DoubleRange(0.0001, 10)));
    6955    }
    7056    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); }
     
    7258    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    7359      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
    74       IEnumerable<int> rows = GenerateRowsToEvaluate();
     60      List<int> rows = GenerateRowsToEvaluate().ToList();
    7561      T problemData = ProblemDataParameter.ActualValue;
    76       return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityLowerBoundParameter.Value.Value, SemanticSimilarityUpperBoundParameter.Value.Value);
     62      return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityRange);
    7763    }
    7864
     
    8571    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
    8672    /// </summary>
    87     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
    88                                                 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength, double lower, double upper) {
     73    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     74                                                T problemData, List<int> rows, int maxDepth, int maxLength, DoubleRange range) {
    8975      var crossoverPoints0 = new List<CutPoint>();
    9076      parent0.Root.ForEachNodePostfix((n) => {
     
    9379            crossoverPoints0.Add(new CutPoint(n, child));
    9480      });
    95       var crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
     81      var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
    9682      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
    9783      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
     
    10086      parent1.Root.ForEachNodePostfix((n) => {
    10187        if (n.Subtrees.Any() && n != parent1.Root)
    102           foreach (var child in n.Subtrees)
    103             if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    104               allowedBranches.Add(child);
     88          allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength));
    10589      });
    106 
    107       // check if empty branch is allowed
    108       if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    10990
    11091      if (allowedBranches.Count == 0)
     
    11798      var startSymbol = new StartSymbol();
    11899      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
    119       IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
     100      List<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows).ToList();
    120101      crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent
    121102      ISymbolicExpressionTreeNode selectedBranch = null;
     
    125106        var parent = node.Parent;
    126107        var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); // this will affect node.Parent
    127         IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
     108        List<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows).ToList();
    128109        node.Parent = parent; // restore parent
    129110
     
    131112        double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState);
    132113
    133         if (lower < ssd && ssd < upper) {
    134           selectedBranch = node;
    135           break;
    136         }
     114        if (range.Start > ssd || range.End < ssd)
     115          continue;
     116
     117        selectedBranch = node;
     118        break;
    137119      }
    138120
    139       if (selectedBranch == null)
    140         return parent0;
    141 
    142121      // perform the actual swap
    143       swap(crossoverPoint0, selectedBranch);
     122      if (selectedBranch != null)
     123        swap(crossoverPoint0, selectedBranch);
    144124
    145125      return parent0;
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSizefairCrossover.cs

    r7119 r7193  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     1using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    62using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    73using HeuristicLab.Common;
     
    139    [StorableConstructor]
    1410    protected SymbolicDataAnalysisExpressionSizefairCrossover(bool deserializing) : base(deserializing) { }
    15     protected SymbolicDataAnalysisExpressionSizefairCrossover(SymbolicDataAnalysisExpressionSizefairCrossover<T> original, Cloner cloner) : base(original, cloner) { }
     11    protected SymbolicDataAnalysisExpressionSizefairCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
    1612    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSizefairCrossover<T>(this, cloner); }
    1713
    18     public SymbolicDataAnalysisExpressionSizefairCrossover() : base() { }
     14    public SymbolicDataAnalysisExpressionSizefairCrossover()
     15      : base() {
     16      SymbolicDataAnalysisEvaluationPartitionParameter.Hidden = true;
     17    }
    1918  }
    2019}
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r7035 r7193  
    121121    <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" />
    122122    <Compile Include="Analyzers\SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs" />
     123    <Compile Include="Creators\MultiSymbolicDataAnalysisExpressionCreator.cs" />
     124    <Compile Include="Creators\SymbolicDataAnalysisExpressionFullTreeCreator.cs" />
     125    <Compile Include="Creators\SymbolicDataAnalysisExpressionGrowTreeCreator.cs" />
     126    <Compile Include="Creators\SymbolicDataAnalysisExpressionRampedHalfAndHalfTreeCreator.cs" />
     127    <Compile Include="Creators\SymbolicDataAnalysisExpressionTreeCreator.cs" />
     128    <Compile Include="Crossovers\MultiSymbolicDataAnalysisExpressionCrossover.cs" />
     129    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs" />
     130    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionContextAwareCrossover.cs" />
     131    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionCrossover.cs" />
     132    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs" />
     133    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs" />
    123134    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs" />
    124     <Compile Include="Crossovers\SymbolicDataAnalysisExpressionCrossover.cs" />
    125     <Compile Include="Crossovers\SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs" />
    126     <Compile Include="SymbolicDataAnalysisExpressionFullTreeCreator.cs" />
     135    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionSizefairCrossover.cs" />
     136    <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionCrossover.cs" />
    127137    <Compile Include="Plugin.cs" />
    128     <Compile Include="SymbolicDataAnalysisExpressionGrowTreeCreator.cs" />
    129     <Compile Include="SymbolicDataAnalysisExpressionRampedHalfAndHalfTreeCreator.cs" />
    130138    <Compile Include="SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs" />
    131139    <Compile Include="Formatters\SymbolicDataAnalysisExpressionLatexFormatter.cs" />
     
    134142    <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionTreeInterpreter.cs" />
    135143    <Compile Include="Interfaces\ISymbolicDataAnalysisProblem.cs" />
    136     <Compile Include="SymbolicDataAnalysisExpressionTreeCreator.cs" />
    137144    <Compile Include="SymbolicDataAnalysisModel.cs">
    138145      <SubType>Code</SubType>
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionCrossover.cs

    r7121 r7193  
    2727
    2828namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    29   public interface ISymbolicDataAnalysisExpressionCrossover<T> : IOperator {
     29  public interface ISymbolicDataAnalysisExpressionCrossover<T> : ISymbolicExpressionTreeCrossover, IOperator {
    3030    IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { get; }
    3131    ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
Note: See TracChangeset for help on using the changeset viewer.