Changeset 5499 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
- Timestamp:
- 02/16/11 19:01:00 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Property svn:ignore
-
old new 2 2 obj 3 3 HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs 4 *.user
-
- Property svn:ignore
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/CrossoverPoint.cs
r5445 r5499 21 21 22 22 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Crossovers{24 publicclass CrossoverPoint {23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 internal class CrossoverPoint { 25 25 public SymbolicExpressionTreeNode Parent { get; set; } 26 26 public SymbolicExpressionTreeNode Child { get; set; } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5445 r5499 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 30 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Crossovers{31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 32 32 /// <summary> 33 33 /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1. … … 38 38 [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")] 39 39 [StorableClass] 40 public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover { 40 public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator { 41 private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability"; 42 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 43 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 44 #region Parameter Properties 41 45 public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter { 42 get { return (IValueLookupParameter<PercentValue>)Parameters["InternalCrossoverPointProbability"]; } 43 } 46 get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; } 47 } 48 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 49 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 50 } 51 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 52 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 53 } 54 #endregion 55 #region Properties 56 public PercentValue InternalCrossoverPointProbability { 57 get { return InternalCrossoverPointProbabilityParameter.ActualValue; } 58 } 59 public IntValue MaximumSymbolicExpressionTreeLength { 60 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 61 } 62 public IntValue MaximumSymbolicExpressionTreeDepth { 63 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 64 } 65 #endregion 44 66 [StorableConstructor] 45 67 private SubtreeCrossover(bool deserializing) : base(deserializing) { } … … 47 69 public SubtreeCrossover() 48 70 : base() { 49 Parameters.Add(new ValueLookupParameter<PercentValue>("InternalCrossoverPointProbability", "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9))); 71 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 72 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 73 Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9))); 50 74 } 51 75 … … 55 79 56 80 protected override SymbolicExpressionTree Cross(IRandom random, 57 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1 ,58 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {59 return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);81 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1) { 82 return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value, 83 MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 60 84 } 61 85 62 86 public static SymbolicExpressionTree Cross(IRandom random, 63 87 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 64 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight , out bool success) {88 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) { 65 89 // select a random crossover point in the first parent 66 90 SymbolicExpressionTreeNode crossoverPoint0; … … 81 105 82 106 if (allowedBranches.Count == 0) { 83 success = false;84 107 return parent0; 85 108 } else { … … 90 113 crossoverPoint0.RemoveSubTree(replacedSubtreeIndex); 91 114 crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch); 92 success = true;93 115 return parent0; 94 116 } … … 190 212 } 191 213 192 private static int GetBranchLevel( SymbolicExpressionTreeNode root,SymbolicExpressionTreeNode point) {214 private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) { 193 215 if (root == point) return 0; 194 216 foreach (var subtree in root.SubTrees) { -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs
r5445 r5499 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;27 26 using HeuristicLab.Parameters; 28 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Crossovers{29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 31 30 /// <summary> 32 31 /// A base class for operators that perform a crossover of symbolic expression trees. … … 37 36 private const string ParentsParameterName = "Parents"; 38 37 private const string ChildParameterName = "Child"; 39 private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents";38 #region Parameter Properties 40 39 public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter { 41 40 get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; } … … 44 43 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; } 45 44 } 46 public IValueParameter<IntValue> FailedCrossoverEventsParameter { 47 get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; } 45 #endregion 46 #region Properties 47 public ItemArray<SymbolicExpressionTree> Parents { 48 get { return ParentsParameter.ActualValue; } 48 49 } 49 50 public IntValue FailedCrossoverEvents {51 get { return FailedCrossoverEventsParameter.Value; }50 public SymbolicExpressionTree Child { 51 get { return ChildParameter.ActualValue; } 52 set { ChildParameter.ActualValue = value; } 52 53 } 54 #endregion 53 55 [StorableConstructor] 54 56 protected SymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { } … … 58 60 Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 59 61 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); 60 Parameters.Add(new ValueParameter<IntValue>(FailedCrossoverEventsParameterName, "The number of failed crossover events (child is an exact copy of a parent)", new IntValue()));61 62 } 62 63 63 64 public sealed override IOperation Apply() { 64 if (Parents Parameter.ActualValue.Length != 2)65 if (Parents.Length != 2) 65 66 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 66 67 67 SymbolicExpressionTree parent0 = ParentsParameter.ActualValue[0]; 68 SymbolicExpressionTree parent1 = ParentsParameter.ActualValue[1]; 68 SymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]); 69 69 70 IRandom random = RandomParameter.ActualValue; 71 72 bool success; 73 SymbolicExpressionTree result = Cross(random, parent0, parent1, 74 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success); 75 76 if (!success) FailedCrossoverEvents.Value++; 77 78 ChildParameter.ActualValue = result; 70 Child = result; 79 71 return base.Apply(); 80 72 } 81 73 82 74 protected abstract SymbolicExpressionTree Cross(IRandom random, 83 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 84 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success); 75 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1); 85 76 } 86 77 }
Note: See TracChangeset
for help on using the changeset viewer.