Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/12 13:53:28 (12 years ago)
Author:
bburlacu
Message:

#1682: Added missing files (that were previously incorrectly referencing the old branch), added unit tests, recommitted lost changes.

Location:
branches/HeuristicLab.Crossovers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Crossovers

    • Property svn:ignore
      •  

        old new  
        1919protoc.exe
        2020*.user
         21_ReSharper.HeuristicLab.Crossovers
  • branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.bak
  • branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers

    • Property svn:ignore set to
      *.bak
  • branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs

    r7259 r7477  
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.PluginInfrastructure;
    3031
    3132namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    3738  /// </summary> 
    3839  [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")]
     40  [NonDiscoverableType]
    3941  [StorableClass]
    40   public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
     42  public class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
    4143    private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
    4244    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
    4345    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     46    private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition";
     47
    4448    #region Parameter Properties
    4549    public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
     
    5155    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
    5256      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     57    }
     58    public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter {
     59      get { return (IValueLookupParameter<IntRange>)Parameters[SymbolicDataAnalysisEvaluationPartitionParameterName]; }
    5360    }
    5461    #endregion
     
    6572    #endregion
    6673    [StorableConstructor]
    67     private SubtreeCrossover(bool deserializing) : base(deserializing) { }
    68     private SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
     74    protected SubtreeCrossover(bool deserializing) : base(deserializing) { }
     75    protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
    6976    public SubtreeCrossover()
    7077      : base() {
     
    7279      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    7380      Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
     81      Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
    7482    }
    7583
     
    8290      return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
    8391        MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     92    }
     93
     94    public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
     95      return Cross(random, parent0, parent1);
    8496    }
    8597
     
    94106      // calculate the max length and depth that the inserted branch can have
    95107      int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength);
    96       int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0.Parent);
     108      int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent);
    97109
    98110      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    99111      parent1.Root.ForEachNodePostfix((n) => {
    100112        if (n.GetLength() <= maxInsertedBranchLength &&
    101             n.GetDepth() <= maxInsertedBranchDepth &&
    102             IsMatchingPointType(crossoverPoint0, n))
     113            n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n))
    103114          allowedBranches.Add(n);
    104115      });
    105116      // empty branch
    106       if (IsMatchingPointType(crossoverPoint0, null)) allowedBranches.Add(null);
     117      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    107118
    108119      if (allowedBranches.Count == 0) {
     
    125136        }
    126137        return parent0;
    127       }
    128     }
    129 
    130     private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) {
    131       var parent = cutPoint.Parent;
    132       if (newChild == null) {
    133         // make sure that one subtree can be removed and that only the last subtree is removed
    134         return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&
    135           cutPoint.ChildIndex == parent.SubtreeCount - 1;
    136       } else {
    137         // check syntax constraints of direct parent - child relation
    138         if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||
    139             !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false;
    140 
    141         bool result = true;
    142         // check point type for the whole branch
    143         newChild.ForEachNodePostfix((n) => {
    144           result =
    145             result &&
    146             parent.Grammar.ContainsSymbol(n.Symbol) &&
    147             n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
    148             n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
    149         });
    150         return result;
    151138      }
    152139    }
     
    226213      }
    227214    }
    228 
    229     private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
    230       if (root == point) return 0;
    231       foreach (var subtree in root.Subtrees) {
    232         int branchLevel = GetBranchLevel(subtree, point);
    233         if (branchLevel < int.MaxValue) return 1 + branchLevel;
    234       }
    235       return int.MaxValue;
    236     }
    237215  }
    238216}
Note: See TracChangeset for help on using the changeset viewer.