Changeset 7106 for branches/gp-crossover
- Timestamp:
- 11/30/11 17:23:19 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
r7089 r7106 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 using HeuristicLab.Common; … … 34 35 [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on semantic similarity.")] 35 36 public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 37 private const string SemanticSimilarityLowerBoundParameterName = "SemanticSimilarityLowerBound"; 38 private const string SemanticSimilarityUpperBoundParameterName = "SemanticSimilarityUpperBound"; 39 40 #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]; } 46 } 47 #endregion 48 49 #region Properties 50 public DoubleValue SemanticSimilarityLowerBound { 51 get { return SemanticSimilarityLowerBoundParameter.Value; } 52 } 53 public DoubleValue SemanticSimilarityUpperBound { 54 get { return SemanticSimilarityUpperBoundParameter.Value; } 55 } 56 #endregion 57 36 58 [StorableConstructor] 37 59 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { } 38 60 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { } 39 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { } 61 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() 62 : 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); 69 } 40 70 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); } 41 71 … … 44 74 IEnumerable<int> rows = GenerateRowsToEvaluate(); 45 75 T problemData = ProblemDataParameter.ActualValue; 46 return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value );76 return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityLowerBoundParameter.Value.Value, SemanticSimilarityUpperBoundParameter.Value.Value); 47 77 } 48 78 /// <summary> … … 51 81 /// </summary> 52 82 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 53 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength ) {54 List<CutPoint>crossoverPoints0 = new List<CutPoint>();83 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength, double lower, double upper) { 84 var crossoverPoints0 = new List<CutPoint>(); 55 85 parent0.Root.ForEachNodePostfix((n) => { 56 86 if (n.Subtrees.Any() && n != parent0.Root) … … 58 88 crossoverPoints0.Add(new CutPoint(n, child)); 59 89 }); 60 CutPointcrossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];90 var crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)]; 61 91 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 62 92 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); 63 93 64 List<ISymbolicExpressionTreeNode>allowedBranches = new List<ISymbolicExpressionTreeNode>();94 var allowedBranches = new List<ISymbolicExpressionTreeNode>(); 65 95 parent1.Root.ForEachNodePostfix((n) => { 66 96 if (n.Subtrees.Any() && n != parent1.Root) … … 83 113 var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); 84 114 IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows); 85 115 crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent 86 116 ISymbolicExpressionTreeNode selectedBranch = null; 87 117 88 118 // pick the first node that fulfills the semantic similarity conditions 89 119 foreach (var node in allowedBranches) { 90 var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); 120 var parent = node.Parent; 121 var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); // this will affect node.Parent 91 122 IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows); 123 node.Parent = parent; // restore parent 92 124 93 //double ssd = SSD(estimatedValues0, estimatedValues1);94 125 OnlineCalculatorError errorState; 95 126 double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState); 96 127 97 // TODO: introduce parameters alpha and beta for establishing the interval boundaries. the best values need to be determined experimentally 98 if (0.0001 < ssd && ssd < 10) { 128 if (lower < ssd && ssd < upper) { 99 129 selectedBranch = node; 100 130 break; 101 131 } 102 132 } 133 134 if (selectedBranch == null) 135 return parent0; 103 136 104 137 // perform the actual swap
Note: See TracChangeset
for help on using the changeset viewer.