Changeset 7193 for branches/gp-crossover
- Timestamp:
- 12/16/11 09:43:02 (13 years ago)
- 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 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Data;31 29 32 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 36 34 [StorableConstructor] 37 35 private SymbolicDataAnalysisExpressionContextAwareCrossover(bool deserializing) : base(deserializing) { } 38 private SymbolicDataAnalysisExpressionContextAwareCrossover(SymbolicDataAnalysisExpressionC ontextAwareCrossover<T> original, Cloner cloner)36 private SymbolicDataAnalysisExpressionContextAwareCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) 39 37 : base(original, cloner) { 40 38 } … … 48 46 if (this.ExecutionContext == null) 49 47 throw new InvalidOperationException("ExecutionContext not set."); 50 IEnumerable<int> rows = GenerateRowsToEvaluate();48 List<int> rows = GenerateRowsToEvaluate().ToList(); 51 49 T problemData = ProblemDataParameter.ActualValue; 52 50 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue; … … 64 62 /// </summary> 65 63 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) { 67 65 // randomly choose a node from the second parent 68 66 var possibleChildren = new List<ISymbolicExpressionTreeNode>(); 69 67 parent1.Root.ForEachNodePostfix((n) => { 70 68 if (n.Subtrees.Any() && n != parent0.Root) 71 foreach (var child in n.Subtrees) 72 possibleChildren.Add(child); 69 possibleChildren.AddRange(n.Subtrees); 73 70 }); 74 var selectedChild = possibleChildren [random.Next(possibleChildren.Count)];71 var selectedChild = possibleChildren.SelectRandom(random); 75 72 var crossoverPoints = new List<CutPoint>(); 76 73 var qualities = new List<Tuple<CutPoint, double>>(); … … 78 75 parent0.Root.ForEachNodePostfix((n) => { 79 76 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); 88 83 }); 89 84 -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs
r7119 r7193 34 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 35 35 public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover, ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 36 private const string RandomParameterName = "Random";37 36 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 38 37 private const string ProblemDataParameterName = "ProblemData"; … … 97 96 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximum tree depth.")); 98 97 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; 99 105 } 100 106 -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs
r7119 r7193 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.Common; … … 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 29 using HeuristicLab.Data; 30 using HeuristicLab.Parameters; 31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 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 36 52 [StorableConstructor] 37 53 private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { } 38 private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpression DepthConstrainedCrossover<T> original, Cloner cloner)54 private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) 39 55 : 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 } 41 63 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); } 42 64 43 65 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); 48 67 } 49 68 … … 54 73 /// <summary> 55 74 /// 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. 57 76 /// </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>(); 60 106 parent0.Root.ForEachNodePostfix((n) => { 61 107 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)); 64 111 }); 65 CutPoint crossoverPoint0 = crossoverPoints0 [random.Next(crossoverPoints0.Count)];112 CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random); 66 113 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 67 114 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); 68 115 69 List<ISymbolicExpressionTreeNode>allowedBranches = new List<ISymbolicExpressionTreeNode>();116 var allowedBranches = new List<ISymbolicExpressionTreeNode>(); 70 117 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 } 75 124 }); 76 77 // check if empty branch is allowed78 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);79 125 80 126 if (allowedBranches.Count == 0) 81 127 return parent0; 82 128 129 var selectedBranch = allowedBranches.SelectRandom(random); 130 131 swap(crossoverPoint0, selectedBranch); 83 132 84 133 return parent0; -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
r7119 r7193 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Data;31 29 32 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 36 34 [StorableConstructor] 37 35 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(bool deserializing) : base(deserializing) { } 38 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpression DeterministicBestCrossover<T> original, Cloner cloner)36 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) 39 37 : base(original, cloner) { 40 38 } … … 48 46 if (this.ExecutionContext == null) 49 47 throw new InvalidOperationException("ExecutionContext not set."); 50 IEnumerable<int> rows = GenerateRowsToEvaluate();48 List<int> rows = GenerateRowsToEvaluate().ToList(); 51 49 T problemData = ProblemDataParameter.ActualValue; 52 50 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue; … … 63 61 /// </summary> 64 62 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) { 66 64 var crossoverPoints0 = new List<CutPoint>(); 67 65 parent0.Root.ForEachNodePostfix((n) => { 68 66 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)); 71 68 }); 72 CutPoint crossoverPoint0 = crossoverPoints0 [random.Next(crossoverPoints0.Count)];69 CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random); 73 70 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 74 71 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); … … 77 74 parent1.Root.ForEachNodePostfix((n) => { 78 75 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); 82 79 }); 83 84 // check if empty branch is allowed85 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);86 80 87 81 if (allowedBranches.Count == 0) … … 93 87 var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>(); 94 88 89 var originalChild = crossoverPoint0.Child; 90 95 91 foreach (var node in allowedBranches) { 96 92 var parent = node.Parent; 97 93 swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent 98 94 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) 99 96 nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality)); 100 97 node.Parent = parent; // restore correct parent … … 103 100 nodeQualities.Sort((a, b) => a.Item2.CompareTo(b.Item2)); // assuming this sorts the list in ascending order 104 101 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 105 111 // swap the node that would create the best offspring 106 112 swap(crossoverPoint0, selectedBranch); -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs
r7119 r7193 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Data;31 29 32 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 36 34 [StorableConstructor] 37 35 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { } 38 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpression ProbabilisticFunctionalCrossover<T> original, Cloner cloner)36 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) 39 37 : base(original, cloner) { } 40 38 public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { } … … 43 41 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 44 42 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; 45 IEnumerable<int> rows = GenerateRowsToEvaluate();43 List<int> rows = GenerateRowsToEvaluate().ToList(); 46 44 T problemData = ProblemDataParameter.ActualValue; 47 var grammar = parent0.Root.Grammar;48 45 return Cross(random, parent0, parent1, interpreter, problemData, 49 46 rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value); … … 62 59 /// </summary> 63 60 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 64 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, I Enumerable<int> rows, int maxDepth, int maxLength) {61 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IList<int> rows, int maxDepth, int maxLength) { 65 62 var crossoverPoints0 = new List<CutPoint>(); 66 63 parent0.Root.ForEachNodePostfix((n) => { … … 69 66 crossoverPoints0.Add(new CutPoint(n, child)); 70 67 }); 71 var crossoverPoint0 = crossoverPoints0 [random.Next(crossoverPoints0.Count)];68 var crossoverPoint0 = crossoverPoints0.SelectRandom(random); 72 69 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 73 70 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); … … 76 73 parent1.Root.ForEachNodePostfix((n) => { 77 74 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)); 81 76 }); 82 83 // check if empty branch is allowed84 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);85 77 86 78 if (allowedBranches.Count == 0) … … 93 85 var startSymbol = new StartSymbol(); 94 86 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(); 96 88 double min0 = estimatedValues0.Min(); 97 89 double max0 = estimatedValues0.Max(); … … 102 94 var parent = node.Parent; 103 95 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(); 105 97 double min1 = estimatedValues1.Min(); 106 98 double max1 = estimatedValues1.Max(); … … 111 103 112 104 // 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; 117 113 } 114 } 118 115 119 116 // check if there are any allowed branches left … … 130 127 weights[i] = (1 - weights[i] / sum); 131 128 129 //selectedBranch = allowedBranches.SelectRandom(weights, random); 132 130 selectedBranch = SelectRandomBranch(random, allowedBranches, weights); 133 131 } … … 152 150 } 153 151 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) { 155 153 double r = weights.Sum() * random.NextDouble(); 156 154 for (int i = 0; i != nodes.Count; ++i) { -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
r7119 r7193 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; 25 using System.Text;26 24 using HeuristicLab.Parameters; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 35 33 [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the semantic similarity between subtrees.")] 36 34 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"; 39 36 40 37 #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]; } 46 40 } 47 41 #endregion 48 42 49 43 #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; } 55 46 } 56 47 #endregion … … 58 49 [StorableConstructor] 59 50 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { } 60 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpression SemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { }51 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) : base(original, cloner) { } 61 52 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() 62 53 : 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))); 69 55 } 70 56 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); } … … 72 58 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 73 59 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; 74 IEnumerable<int> rows = GenerateRowsToEvaluate();60 List<int> rows = GenerateRowsToEvaluate().ToList(); 75 61 T problemData = ProblemDataParameter.ActualValue; 76 return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarity LowerBoundParameter.Value.Value, SemanticSimilarityUpperBoundParameter.Value.Value);62 return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityRange); 77 63 } 78 64 … … 85 71 /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria. 86 72 /// </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) { 89 75 var crossoverPoints0 = new List<CutPoint>(); 90 76 parent0.Root.ForEachNodePostfix((n) => { … … 93 79 crossoverPoints0.Add(new CutPoint(n, child)); 94 80 }); 95 var crossoverPoint0 = crossoverPoints0 [random.Next(crossoverPoints0.Count)];81 var crossoverPoint0 = crossoverPoints0.SelectRandom(random); 96 82 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 97 83 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); … … 100 86 parent1.Root.ForEachNodePostfix((n) => { 101 87 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)); 105 89 }); 106 107 // check if empty branch is allowed108 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);109 90 110 91 if (allowedBranches.Count == 0) … … 117 98 var startSymbol = new StartSymbol(); 118 99 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(); 120 101 crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent 121 102 ISymbolicExpressionTreeNode selectedBranch = null; … … 125 106 var parent = node.Parent; 126 107 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(); 128 109 node.Parent = parent; // restore parent 129 110 … … 131 112 double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState); 132 113 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; 137 119 } 138 120 139 if (selectedBranch == null)140 return parent0;141 142 121 // perform the actual swap 143 swap(crossoverPoint0, selectedBranch); 122 if (selectedBranch != null) 123 swap(crossoverPoint0, selectedBranch); 144 124 145 125 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; 1 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 2 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 7 3 using HeuristicLab.Common; … … 13 9 [StorableConstructor] 14 10 protected SymbolicDataAnalysisExpressionSizefairCrossover(bool deserializing) : base(deserializing) { } 15 protected SymbolicDataAnalysisExpressionSizefairCrossover(S ymbolicDataAnalysisExpressionSizefairCrossover<T>original, Cloner cloner) : base(original, cloner) { }11 protected SymbolicDataAnalysisExpressionSizefairCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { } 16 12 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSizefairCrossover<T>(this, cloner); } 17 13 18 public SymbolicDataAnalysisExpressionSizefairCrossover() : base() { } 14 public SymbolicDataAnalysisExpressionSizefairCrossover() 15 : base() { 16 SymbolicDataAnalysisEvaluationPartitionParameter.Hidden = true; 17 } 19 18 } 20 19 } -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r7035 r7193 121 121 <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" /> 122 122 <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" /> 123 134 <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" /> 127 137 <Compile Include="Plugin.cs" /> 128 <Compile Include="SymbolicDataAnalysisExpressionGrowTreeCreator.cs" />129 <Compile Include="SymbolicDataAnalysisExpressionRampedHalfAndHalfTreeCreator.cs" />130 138 <Compile Include="SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs" /> 131 139 <Compile Include="Formatters\SymbolicDataAnalysisExpressionLatexFormatter.cs" /> … … 134 142 <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionTreeInterpreter.cs" /> 135 143 <Compile Include="Interfaces\ISymbolicDataAnalysisProblem.cs" /> 136 <Compile Include="SymbolicDataAnalysisExpressionTreeCreator.cs" />137 144 <Compile Include="SymbolicDataAnalysisModel.cs"> 138 145 <SubType>Code</SubType> -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionCrossover.cs
r7121 r7193 27 27 28 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 29 public interface ISymbolicDataAnalysisExpressionCrossover<T> : I Operator {29 public interface ISymbolicDataAnalysisExpressionCrossover<T> : ISymbolicExpressionTreeCrossover, IOperator { 30 30 IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { get; } 31 31 ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
Note: See TracChangeset
for help on using the changeset viewer.