Changeset 18076 for branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 11/04/21 16:58:33 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18075 r18076 24 24 private const string StructureTemplateParameterName = "Structure Template"; 25 25 private const string InterpreterParameterName = "Interpreter"; 26 27 private const string StructureTemplateDescriptionText = 26 private const string EstimationLimitsParameterName = "EstimationLimits"; 27 private const string BestTrainingSolutionParameterName = "Best Training Solution"; 28 29 private const string SymbolicExpressionTreeName = "SymbolicExpressionTree"; 30 31 private const string StructureTemplateDescriptionText = 28 32 "Enter your expression as string in infix format into the empty input field.\n" + 29 33 "By checking the \"Apply Linear Scaling\" checkbox you can add the relevant scaling terms to your expression.\n" + … … 36 40 public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName]; 37 41 public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; 42 public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; 43 public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName]; 38 44 #endregion 39 45 40 46 #region Properties 41 public IRegressionProblemData ProblemData { 42 get => ProblemDataParameter.Value; 47 public IRegressionProblemData ProblemData { 48 get => ProblemDataParameter.Value; 43 49 set { 44 50 ProblemDataParameter.Value = value; … … 54 60 IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData; 55 61 62 public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value; 63 56 64 public override bool Maximization => true; 57 65 #endregion … … 64 72 public StructuredSymbolicRegressionSingleObjectiveProblem() { 65 73 var problemData = new ShapeConstrainedRegressionProblemData(); 74 var targetInterval = problemData.VariableRanges.GetInterval(problemData.TargetVariable); 75 var estimationWidth = targetInterval.Width * 10; 76 66 77 67 78 var structureTemplate = new StructureTemplate(); … … 69 80 70 81 Parameters.Add(new ValueParameter<IRegressionProblemData>( 71 ProblemDataParameterName, 82 ProblemDataParameterName, 72 83 problemData)); 73 74 84 Parameters.Add(new FixedValueParameter<StructureTemplate>( 75 StructureTemplateParameterName, 76 StructureTemplateDescriptionText, 85 StructureTemplateParameterName, 86 StructureTemplateDescriptionText, 77 87 structureTemplate)); 78 79 88 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>( 80 InterpreterParameterName, 81 new SymbolicDataAnalysisExpressionTreeInterpreter()) 82 { Hidden = true }); 89 InterpreterParameterName, 90 new SymbolicDataAnalysisExpressionTreeInterpreter()) { Hidden = true }); 91 Parameters.Add(new FixedValueParameter<DoubleLimit>( 92 EstimationLimitsParameterName, 93 new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth))); 94 Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "")); 83 95 84 96 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 85 } 86 87 public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, 88 Cloner cloner) : base(original, cloner){ } 97 98 Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer()); 99 Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer()); 100 //TODO change to value lookup 101 //Operators.Add(new SymbolicExpressionTreeLengthAnalyzer()); 102 Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer()); 103 104 } 105 106 public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, 107 Cloner cloner) : base(original, cloner) { } 89 108 90 109 [StorableConstructor] … … 114 133 if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice 115 134 Encoding.Add(new SymbolicExpressionTreeEncoding( 116 f.Name, 117 f.Grammar, 118 f.MaximumSymbolicExpressionTreeLength, 135 f.Name, 136 f.Grammar, 137 f.MaximumSymbolicExpressionTreeLength, 119 138 f.MaximumSymbolicExpressionTreeDepth)); 120 139 } … … 124 143 base.Analyze(individuals, qualities, results, random); 125 144 126 int bestIdx = 0; 127 double bestQuality = Maximization ? double.MinValue : double.MaxValue; 128 for(int idx = 0; idx < qualities.Length; ++idx) { 129 if((Maximization && qualities[idx] > bestQuality) || 130 (!Maximization && qualities[idx] < bestQuality)) { 131 bestQuality = qualities[idx]; 132 bestIdx = idx; 133 } 134 } 135 136 if (results.TryGetValue("Best Tree", out IResult result)) { 137 var tree = BuildTree(individuals[bestIdx]); 138 if (StructureTemplate.ApplyLinearScaling) 139 AdjustLinearScalingParams(tree, Interpreter); 140 result.Value = tree; 141 } 142 else { 143 var tree = BuildTree(individuals[bestIdx]); 144 if (StructureTemplate.ApplyLinearScaling) 145 AdjustLinearScalingParams(tree, Interpreter); 146 results.Add(new Result("Best Tree", tree)); 147 } 148 } 145 var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality); 146 var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual; 147 148 if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) { 149 results.Add(new Result(BestTrainingSolutionParameter.ActualName, typeof(SymbolicRegressionSolution))); 150 } 151 152 var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName]; 153 154 var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter); 155 var solution = model.CreateRegressionSolution(ProblemData); 156 157 results[BestTrainingSolutionParameter.ActualName].Value = solution; 158 } 159 149 160 150 161 public override double Evaluate(Individual individual, IRandom random) { … … 152 163 153 164 if (StructureTemplate.ApplyLinearScaling) 154 AdjustLinearScalingParams(tree, Interpreter); 155 var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable); 165 AdjustLinearScalingParams(ProblemData, tree, Interpreter); 166 167 individual[SymbolicExpressionTreeName] = tree; 168 156 169 var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate( 157 Interpreter, tree, 158 estimationInterval.LowerBound, estimationInterval.UpperBound,170 Interpreter, tree, 171 EstimationLimits.Lower, EstimationLimits.Upper, 159 172 ProblemData, ProblemData.TrainingIndices, false); 160 173 161 174 return quality; 162 175 } 163 176 164 private void AdjustLinearScalingParams(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {177 private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) { 165 178 var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0); 166 179 var scalingNode = offsetNode.Subtrees.Where(x => !(x is ConstantTreeNode)).First(); … … 169 182 var scalingConstantNode = (ConstantTreeNode)scalingNode.Subtrees.Where(x => x is ConstantTreeNode).First(); 170 183 171 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);172 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);184 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices); 185 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 173 186 174 187 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error); 175 if (error == OnlineCalculatorError.None) {188 if (error == OnlineCalculatorError.None) { 176 189 offsetConstantNode.Value = a; 177 190 scalingConstantNode.Value = b; … … 186 199 187 200 // build main tree 188 foreach (var n in templateTree.IterateNodesPrefix()) { 189 if (n.Symbol is SubFunctionSymbol) { 190 var subFunctionTreeNode = n as SubFunctionTreeNode; 191 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name); 192 193 // add new tree 194 var subTree = subFunctionTree.Root.GetSubtree(0) // Start 195 .GetSubtree(0); // Offset 196 subFunctionTreeNode.AddSubtree(subTree); 197 } 201 foreach (var subFunctionTreeNode in templateTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) { 202 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name); 203 204 // add new tree 205 var subTree = subFunctionTree.Root.GetSubtree(0) // Start 206 .GetSubtree(0); // Offset 207 subTree = (ISymbolicExpressionTreeNode)subTree.Clone(); 208 subFunctionTreeNode.AddSubtree(subTree); 209 198 210 } 199 211 return templateTree;
Note: See TracChangeset
for help on using the changeset viewer.