- Timestamp:
- 11/25/11 11:41:48 (13 years ago)
- Location:
- branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.cs
r7072 r7075 88 88 89 89 // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated 90 var rootSymbol = new ProgramRootSymbol();91 var startSymbol = new StartSymbol();92 93 90 ISymbolicExpressionTreeNode selectedBranch = null; 94 91 -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs
r7035 r7075 33 33 34 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 35 public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover where T : class, IDataAnalysisProblemData {35 public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover, ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 36 36 private const string RandomParameterName = "Random"; 37 37 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 38 38 private const string ProblemDataParameterName = "ProblemData"; 39 39 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 private const string EvaluationPartitionParameterName = "EvaluationPartition"; 40 private const string EvaluatorParameterName = "Evaluator"; 41 private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition"; 41 42 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 42 43 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; … … 52 53 get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; } 53 54 } 54 public IValueLookupParameter<IntRange> EvaluationPartitionParameter { 55 get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; } 55 public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter { 56 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>) Parameters[EvaluatorParameterName]; } 57 } 58 public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { 59 get { return (IValueLookupParameter<IntRange>)Parameters[SymbolicDataAnalysisEvaluationPartitionParameterName]; } 56 60 } 57 61 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { … … 86 90 : base() { 87 91 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree.")); 92 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The single objective solution evaluator")); 88 93 Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated.")); 89 Parameters.Add(new ValueLookupParameter<IntRange>( EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));94 Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated.")); 90 95 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees.")); 91 96 Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.")); 97 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximum tree depth.")); 98 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum tree length.")); 92 99 } 93 100 … … 100 107 /// <param name="startSymbol"></param> 101 108 /// <returns></returns> 102 protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ProgramRootSymbol rootSymbol, StartSymbol startSymbol) { 103 ISymbolicExpressionTreeNode rootNode = rootSymbol.CreateTreeNode(); 109 protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ISymbolicExpressionGrammar grammar) { 110 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 111 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 104 112 if (rootNode.HasLocalParameters) 105 113 rootNode.ResetLocalParameters(random); 106 ISymbolicExpressionTreeNode startNode = startSymbol.CreateTreeNode(); 114 rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar); 115 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 116 startNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar); 107 117 if (startNode.HasLocalParameters) 108 118 startNode.ResetLocalParameters(random); 109 119 rootNode.AddSubtree(startNode); 110 startNode.AddSubtree(node); 120 121 rootNode.AddSubtree(startNode); 122 if (startNode.Grammar.IsAllowedChildSymbol(startNode.Symbol, node.Symbol)) 123 startNode.AddSubtree(node); 124 else 125 throw new InvalidOperationException("The given node cannot be a child of the start symbol."); 111 126 return new SymbolicExpressionTree(rootNode); 112 127 } … … 118 133 protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) { 119 134 IEnumerable<int> rows; 120 int samplesStart = EvaluationPartitionParameter.ActualValue.Start;121 int samplesEnd = EvaluationPartitionParameter.ActualValue.End;135 int samplesStart = SymbolicDataAnalysisEvaluationPartitionParameter.ActualValue.Start; 136 int samplesEnd = SymbolicDataAnalysisEvaluationPartitionParameter.ActualValue.End; 122 137 int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start; 123 138 int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End; -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs
r7035 r7075 37 37 [StorableConstructor] 38 38 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { } 39 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner) 40 : base(original, cloner) { 41 } 42 public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() 43 : base() { 44 } 45 public override IDeepCloneable Clone(Cloner cloner) { 46 return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); 47 } 39 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner) 40 : base(original, cloner) { } 41 public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { } 42 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); } 43 48 44 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 49 45 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; 50 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 51 47 T problemData = ProblemDataParameter.ActualValue; 52 return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value); 48 var grammar = parent0.Root.Grammar; 49 return Cross(random, parent0, parent1, interpreter, problemData, 50 rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value); 53 51 } 54 52 … … 60 58 /// choose the second crosspoint via a random weighted selection procedure. 61 59 /// </summary> 62 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 60 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 63 61 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) { 64 62 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); … … 89 87 90 88 // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated 91 var rootSymbol = new ProgramRootSymbol(); 92 var startSymbol = new StartSymbol(); 93 var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol); 89 var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar); 94 90 IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows); 95 91 double min0 = estimatedValues0.Min(); … … 98 94 List<double> weights = new List<double>(); 99 95 foreach (var node in allowedBranches) { 100 var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);96 var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar); 101 97 IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows); 102 98 double min1 = estimatedValues1.Min(); … … 109 105 110 106 ISymbolicExpressionTreeNode selectedBranch = SelectRandomBranch(random, allowedBranches, weights); 107 swap(crossoverPoint0, selectedBranch); 111 108 112 // perform the actual swap 113 if (crossoverPoint0.Child != null) { 109 return parent0; 110 } 111 112 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) { 113 if (crossoverPoint.Child != null) { 114 114 // manipulate the tree of parent0 in place 115 115 // replace the branch in tree0 with the selected branch from tree1 116 crossoverPoint 0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);116 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex); 117 117 if (selectedBranch != null) { 118 crossoverPoint 0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);118 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch); 119 119 } 120 120 } else { 121 121 // child is null (additional child should be added under the parent) 122 122 if (selectedBranch != null) { 123 crossoverPoint 0.Parent.AddSubtree(selectedBranch);123 crossoverPoint.Parent.AddSubtree(selectedBranch); 124 124 } 125 125 } 126 127 return parent0;128 126 } 129 127 -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
r7036 r7075 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 34 [Item(" ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on behavioral similarity")]34 [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on semantic similarity.")] 35 35 public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData 36 36 { 37 37 [StorableConstructor] 38 38 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { } 39 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) 40 : base(original, cloner) { 41 } 42 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() 43 : base() { 44 } 45 public override IDeepCloneable Clone(Cloner cloner) { 46 return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); 47 } 39 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { } 40 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { } 41 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); } 42 48 43 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 49 44 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; … … 56 51 /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria. 57 52 /// </summary> 58 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 53 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 59 54 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) { 60 55 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); … … 82 77 return parent0; 83 78 84 var dataset = problemData.Dataset;79 var dataset = problemData.Dataset; 85 80 86 81 // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated 87 var rootSymbol = new ProgramRootSymbol();88 var startSymbol = new StartSymbol();89 var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);82 //var rootSymbol = new ProgramRootSymbol(); 83 //var startSymbol = new StartSymbol(); 84 var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar); 90 85 IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows); 91 86 … … 94 89 // pick the first node that fulfills the semantic similarity conditions 95 90 foreach (var node in allowedBranches) { 96 var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);91 var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar); 97 92 IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows); 98 93 … … 109 104 110 105 // perform the actual swap 111 if (crossoverPoint0.Child != null) { 106 swap(crossoverPoint0, selectedBranch); 107 108 return parent0; 109 } 110 111 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) { 112 // perform the actual swap 113 if (crossoverPoint.Child != null) { 112 114 // manipulate the tree of parent0 in place 113 115 // replace the branch in tree0 with the selected branch from tree1 114 crossoverPoint 0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);116 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex); 115 117 if (selectedBranch != null) { 116 crossoverPoint 0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);118 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch); 117 119 } 118 120 } else { 119 121 // child is null (additional child should be added under the parent) 120 122 if (selectedBranch != null) { 121 crossoverPoint 0.Parent.AddSubtree(selectedBranch);123 crossoverPoint.Parent.AddSubtree(selectedBranch); 122 124 } 123 125 } 124 return parent0;125 126 } 127 126 128 } 127 129 } -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionCrossover.cs
r7072 r7075 27 27 28 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 29 public interface ISymbolicDataAnalysisExpressionCrossover : INamedItem{29 public interface ISymbolicDataAnalysisExpressionCrossover<T> : IOperator { 30 30 IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { get; } 31 31 }
Note: See TracChangeset
for help on using the changeset viewer.