Changeset 18095
- Timestamp:
- 11/25/21 15:32:01 (3 years ago)
- Location:
- branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r18084 r18095 267 267 <Private>False</Private> 268 268 </ProjectReference> 269 <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj"> 270 <Project>{f4539fb6-4708-40c9-be64-0a1390aea197}</Project> 271 <Name>HeuristicLab.Random-3.3</Name> 272 </ProjectReference> 269 273 </ItemGroup> 270 274 <ItemGroup> -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Interfaces/ISymbolicRegressionSingleObjectiveEvaluator.cs
r17180 r18095 1 using HEAL.Attic; 1 using System.Collections.Generic; 2 using System.Linq; 3 using HEAL.Attic; 4 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 2 5 #region License Information 3 6 /* HeuristicLab … … 24 27 [StorableType("5dd2601a-b884-48c0-85bc-bc1f437187a3")] 25 28 public interface ISymbolicRegressionSingleObjectiveEvaluator : ISymbolicRegressionEvaluator, ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> { 29 double Evaluate( 30 IRegressionProblemData problemData, 31 ISymbolicExpressionTree tree, 32 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 33 IEnumerable<int> rows = null, 34 bool applyLinearScaling = true, 35 double lowerEstimationLimit = double.MinValue, 36 double upperEstimationLimit = double.MaxValue); 26 37 } 27 38 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs
r17958 r18095 233 233 return nmse; 234 234 } 235 236 public override double Evaluate(IRegressionProblemData problemData, 237 ISymbolicExpressionTree solution, 238 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 239 IEnumerable<int> rows = null, 240 bool applyLinearScaling = true, 241 double lowerEstimationLimit = double.MinValue, 242 double upperEstimationLimit = double.MaxValue) { 243 244 if (OptimizeParameters) { 245 SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants( 246 interpreter, 247 solution, 248 problemData, 249 rows, 250 false, 251 ConstantOptimizationIterations, 252 true, 253 lowerEstimationLimit, 254 upperEstimationLimit); 255 } else { 256 if (applyLinearScaling) { 257 var rootNode = new ProgramRootSymbol().CreateTreeNode(); 258 var startNode = new StartSymbol().CreateTreeNode(); 259 var offset = solution.Root.GetSubtree(0) //Start 260 .GetSubtree(0); //Offset 261 var scaling = offset.GetSubtree(0); 262 263 //Check if tree contains offset and scaling nodes 264 if (!(offset.Symbol is Addition) || !(scaling.Symbol is Multiplication)) 265 throw new ArgumentException($"{ItemName} can only be used with LinearScalingGrammar."); 266 267 var t = (ISymbolicExpressionTreeNode)scaling.GetSubtree(0).Clone(); 268 rootNode.AddSubtree(startNode); 269 startNode.AddSubtree(t); 270 var newTree = new SymbolicExpressionTree(rootNode); 271 272 //calculate alpha and beta for scaling 273 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows); 274 275 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 276 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta, 277 out var errorState); 278 279 if (errorState == OnlineCalculatorError.None) { 280 //Set alpha and beta to the scaling nodes from ia grammar 281 var offsetParameter = offset.GetSubtree(1) as ConstantTreeNode; 282 offsetParameter.Value = alpha; 283 var scalingParameter = scaling.GetSubtree(1) as ConstantTreeNode; 284 scalingParameter.Value = beta; 285 } 286 } // else: alpha and beta are evolved 287 } 288 return Calculate( 289 interpreter, 290 solution, 291 lowerEstimationLimit, 292 upperEstimationLimit, 293 problemData, 294 rows ?? problemData.TrainingIndices, 295 BoundsEstimator, 296 UseSoftConstraints, 297 PenalityFactor); 298 } 235 299 } 236 300 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r17944 r18095 30 30 using HeuristicLab.Optimization; 31 31 using HeuristicLab.Parameters; 32 using HeuristicLab.Random; 32 33 33 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { … … 175 176 176 177 return base.InstrumentedApply(); 178 } 179 180 public override double Evaluate(IRegressionProblemData problemData, 181 ISymbolicExpressionTree solution, 182 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 183 IEnumerable<int> rows = null, 184 bool applyLinearScaling = true, 185 double lowerEstimationLimit = double.MinValue, 186 double upperEstimationLimit = double.MaxValue) { 187 188 189 var random = RandomParameter?.Value ?? new MersenneTwister((uint)DateTime.Now.Millisecond); 190 double quality; 191 192 var propability = random.NextDouble(); 193 if (propability < ConstantOptimizationProbability.Value) { 194 var counter = new EvaluationsCounter(); 195 quality = OptimizeConstants( 196 interpreter, 197 solution, 198 problemData, 199 rows ?? problemData.TrainingIndices, 200 applyLinearScaling, 201 ConstantOptimizationIterations.Value, 202 updateVariableWeights: UpdateVariableWeights, 203 lowerEstimationLimit: lowerEstimationLimit, 204 upperEstimationLimit: upperEstimationLimit, 205 updateConstantsInTree: UpdateConstantsInTree, 206 counter: counter); 207 208 if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { 209 var evaluationRows = GenerateRowsToEvaluate(); 210 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate( 211 interpreter, 212 solution, 213 lowerEstimationLimit, 214 upperEstimationLimit, 215 problemData, 216 evaluationRows, 217 applyLinearScaling); 218 } 219 220 } else { 221 var evaluationRows = GenerateRowsToEvaluate(); 222 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate( 223 interpreter, 224 solution, 225 lowerEstimationLimit, 226 upperEstimationLimit, 227 problemData, 228 evaluationRows, 229 applyLinearScaling); 230 } 231 return quality; 177 232 } 178 233 … … 311 366 FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode; 312 367 if (constantTreeNode != null) { 313 if (constantTreeNode.Parent.Symbol is Power 368 if (constantTreeNode.Parent.Symbol is Power 314 369 && constantTreeNode.Parent.GetSubtree(1) == constantTreeNode) continue; // exponents in powers are not optimizated (see TreeToAutoDiffTermConverter) 315 370 constantTreeNode.Value = constants[i++]; -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionLogResidualEvaluator.cs
r17180 r18095 90 90 return mlr; 91 91 } 92 93 public override double Evaluate(IRegressionProblemData problemData, 94 ISymbolicExpressionTree solution, 95 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 96 IEnumerable<int> rows = null, 97 bool applyLinearScaling = true, 98 double lowerEstimationLimit = double.MinValue, 99 double upperEstimationLimit = double.MaxValue) { 100 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices); 101 } 92 102 } 93 103 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionMeanRelativeErrorEvaluator.cs
r17180 r18095 83 83 return mre; 84 84 } 85 86 public override double Evaluate(IRegressionProblemData problemData, 87 ISymbolicExpressionTree solution, 88 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 89 IEnumerable<int> rows = null, 90 bool applyLinearScaling = true, 91 double lowerEstimationLimit = double.MinValue, 92 double upperEstimationLimit = double.MaxValue) { 93 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices); 94 } 85 95 } 86 96 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r17180 r18095 22 22 using HeuristicLab.Common; 23 23 using HEAL.Attic; 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using System.Collections.Generic; 26 24 27 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 25 28 [StorableType("7EB90F03-4385-474F-BDE7-3B133E8FEAAB")] … … 28 31 protected SymbolicRegressionSingleObjectiveEvaluator(StorableConstructorFlag _) : base(_) { } 29 32 protected SymbolicRegressionSingleObjectiveEvaluator(SymbolicRegressionSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { } 30 protected SymbolicRegressionSingleObjectiveEvaluator(): base() {} 33 protected SymbolicRegressionSingleObjectiveEvaluator(): base() {} 34 public abstract double Evaluate( 35 IRegressionProblemData problemData, 36 ISymbolicExpressionTree tree, 37 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 38 IEnumerable<int> rows = null, 39 bool applyLinearScaling = true, 40 double lowerEstimationLimit = double.MinValue, 41 double upperEstimationLimit = double.MaxValue); 31 42 } 32 43 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs
r17180 r18095 84 84 return mse; 85 85 } 86 87 public override double Evaluate(IRegressionProblemData problemData, 88 ISymbolicExpressionTree solution, 89 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 90 IEnumerable<int> rows = null, 91 bool applyLinearScaling = true, 92 double lowerEstimationLimit = double.MinValue, 93 double upperEstimationLimit = double.MaxValue) { 94 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling); 95 } 86 96 } 87 97 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs
r17180 r18095 84 84 return mse; 85 85 } 86 87 public override double Evaluate(IRegressionProblemData problemData, 88 ISymbolicExpressionTree solution, 89 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 90 IEnumerable<int> rows = null, 91 bool applyLinearScaling = true, 92 double lowerEstimationLimit = double.MinValue, 93 double upperEstimationLimit = double.MaxValue) { 94 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling); 95 } 86 96 } 87 97 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs
r17180 r18095 84 84 return mse; 85 85 } 86 87 public override double Evaluate(IRegressionProblemData problemData, 88 ISymbolicExpressionTree solution, 89 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 90 IEnumerable<int> rows = null, 91 bool applyLinearScaling = true, 92 double lowerEstimationLimit = double.MinValue, 93 double upperEstimationLimit = double.MaxValue) { 94 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling); 95 } 86 96 } 87 97 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs
r17180 r18095 86 86 return r2; 87 87 } 88 89 public override double Evaluate(IRegressionProblemData problemData, 90 ISymbolicExpressionTree solution, 91 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 92 IEnumerable<int> rows = null, 93 bool applyLinearScaling = true, 94 double lowerEstimationLimit = double.MinValue, 95 double upperEstimationLimit = double.MaxValue) { 96 return Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows ?? problemData.TrainingIndices, applyLinearScaling); 97 } 88 98 } 89 99 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18084 r18095 31 31 32 32 private const string SymbolicExpressionTreeName = "SymbolicExpressionTree"; 33 private const string VariableName = "Variable"; 33 34 34 35 private const string StructureTemplateDescriptionText = … … 94 95 95 96 Parameters.Add(new ConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>( 96 TreeEvaluatorParameterName, 97 TreeEvaluatorParameterName, 97 98 evaluators, 98 99 evaluators.First())); … … 114 115 Parameters.Add(new FixedValueParameter<DoubleLimit>( 115 116 EstimationLimitsParameterName, 116 new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth))); 117 EstimationLimitsParameter.Hidden = true; 118 119 Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "")); 120 this.BestTrainingSolutionParameter.Hidden = true; 117 new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth)) { Hidden = true }); 118 119 Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true }); 121 120 122 121 this.EvaluatorParameter.Hidden = true; 123 124 125 122 126 123 Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer()); 127 124 Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer()); … … 175 172 base.Analyze(individuals, qualities, results, random); 176 173 177 var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality); 178 var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual; 174 var best = GetBestIndividual(individuals, qualities).Item1; 179 175 180 176 if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) { … … 199 195 individual[SymbolicExpressionTreeName] = tree; 200 196 201 //TreeEvaluatorParameter.Value.EstimationLimitsParameter.ActualValue = EstimationLimits; 202 //TreeEvaluatorParameter.Value.EstimationLimitsParameter.Value = EstimationLimits; 203 //var quality = TreeEvaluatorParameter.Value.Evaluate(new ExecutionContext(null, this, new Scope("Test")), tree, ProblemData, ProblemData.TrainingIndices); 204 205 var quality = double.MaxValue; 206 var evaluatorGUID = TreeEvaluatorParameter.Value.GetType().GUID; 207 208 // TODO: use Evaluate method instead of static Calculate -> a fake ExecutionContext is needed 209 if (evaluatorGUID == typeof(NMSESingleObjectiveConstraintsEvaluator).GUID) { 210 quality = NMSESingleObjectiveConstraintsEvaluator.Calculate( 211 Interpreter, tree, 212 EstimationLimits.Lower, EstimationLimits.Upper, 213 ProblemData, ProblemData.TrainingIndices, new IntervalArithBoundsEstimator()); 214 } else if (evaluatorGUID == typeof(SymbolicRegressionLogResidualEvaluator).GUID) { 215 quality = SymbolicRegressionLogResidualEvaluator.Calculate( 216 Interpreter, tree, 217 EstimationLimits.Lower, EstimationLimits.Upper, 218 ProblemData, ProblemData.TrainingIndices); 219 } else if (evaluatorGUID == typeof(SymbolicRegressionMeanRelativeErrorEvaluator).GUID) { 220 quality = SymbolicRegressionMeanRelativeErrorEvaluator.Calculate( 221 Interpreter, tree, 222 EstimationLimits.Lower, EstimationLimits.Upper, 223 ProblemData, ProblemData.TrainingIndices); 224 } else if (evaluatorGUID == typeof(SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator).GUID) { 225 quality = SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.Calculate( 226 Interpreter, tree, 227 EstimationLimits.Lower, EstimationLimits.Upper, 228 ProblemData, ProblemData.TrainingIndices, false); 229 } else if (evaluatorGUID == typeof(SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator).GUID) { 230 quality = SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.Calculate( 231 Interpreter, tree, 232 EstimationLimits.Lower, EstimationLimits.Upper, 233 ProblemData, ProblemData.TrainingIndices, false); 234 } else { // SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator 235 quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate( 236 Interpreter, tree, 237 EstimationLimits.Lower, EstimationLimits.Upper, 238 ProblemData, ProblemData.TrainingIndices, false); 239 } 240 241 return quality; 197 return TreeEvaluatorParameter.Value.Evaluate( 198 ProblemData, 199 tree, 200 Interpreter, 201 ProblemData.TrainingIndices, 202 StructureTemplate.ApplyLinearScaling, 203 EstimationLimits.Lower, 204 EstimationLimits.Upper); 242 205 } 243 206 … … 280 243 281 244 private void SetupVariables(SubFunction subFunction) { 282 var varSym = (Variable)subFunction.Grammar.GetSymbol( "Variable");245 var varSym = (Variable)subFunction.Grammar.GetSymbol(VariableName); 283 246 if (varSym == null) { 284 247 varSym = new Variable();
Note: See TracChangeset
for help on using the changeset viewer.