source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator.cs @ 17660

Last change on this file since 17660 was 17660, checked in by chaider, 2 months ago

#3076

  • Set worst nmse value to 1.0 in SymbolicRegressionSingleObjectiveConstraintConstOptNmseEvaluator
  • Added SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator which allows to use linear scaling within IA
File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using HEAL.Attic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Parameters;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.SingleObjective.Evaluators {
11  [Item("Constraint Scaling NMSE Evaluator", "")]
12  [StorableType("5B69083F-74EE-446C-A7D1-9DEAAA128AC6")]
13  public class SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator
14    : SymbolicRegressionSingleObjectiveEvaluator {
15
16    [StorableConstructor]
17    protected SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator(StorableConstructorFlag _) : base(_) {}
18
19    protected SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator(
20      SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator original, Cloner cloner) :
21      base(original, cloner) { }
22
23    public override IDeepCloneable Clone(Cloner cloner) {
24      return new SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator(this, cloner);
25    }
26
27    public SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator() { }
28
29    public override bool Maximization => false;
30
31
32    public override IOperation InstrumentedApply() {
33      var rows                                      = GenerateRowsToEvaluate();
34      var solution                                  = SymbolicExpressionTreeParameter.ActualValue;
35      var problemData                               = ProblemDataParameter.ActualValue;
36      var interpreter                               = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
37      var estimationLimits                          = EstimationLimitsParameter.ActualValue;
38      var applyLinearScaling                        = false;
39     
40     //Check for interval arithmetic grammar
41     //remove scaling nodes for linear scaling evaluation
42     var rootNode = new ProgramRootSymbol().CreateTreeNode();
43     var startNode = new StartSymbol().CreateTreeNode();
44     SymbolicExpressionTree newTree = null;
45     foreach (var node in solution.IterateNodesPrefix()) {
46       if (node.Symbol.Name == "Scaling") {
47         for (var i = 0; i < node.SubtreeCount; ++i) {
48           startNode.AddSubtree(node.GetSubtree(i));
49         }
50         rootNode.AddSubtree(startNode);
51         newTree = new SymbolicExpressionTree(rootNode);
52         break;
53       }
54     }
55     //calculate alpha and beta for scaling
56     var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
57     var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
58     OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta, out var errorState);
59     var a = alpha;
60     var b = beta;
61     //Set alpha and beta to the scaling nodes from ia grammar
62     foreach (var node in solution.IterateNodesPrefix()) {
63       if (node.Symbol.Name == "Offset") {
64         node.RemoveSubtree(1);
65         var alphaNode = new ConstantTreeNode(new Constant()) {Value = alpha};
66         node.AddSubtree(alphaNode);
67       } else if (node.Symbol.Name == "Scaling") {
68         node.RemoveSubtree(1);
69         var betaNode = new ConstantTreeNode(new Constant()) {Value = beta};
70         node.AddSubtree(betaNode);
71       }
72     }
73
74      var quality = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows,
75        applyLinearScaling);
76      QualityParameter.ActualValue = new DoubleValue(quality);
77      return base.InstrumentedApply();
78    }
79
80    public static double Calculate(
81      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
82      ISymbolicExpressionTree solution, double lowerEstimationLimit,
83      double upperEstimationLimit,
84      IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
85      var estimatedValues =
86        interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
87      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
88      var constraints = problemData.IntervalConstraints.EnabledConstraints;
89      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
90
91      if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, solution))
92        return 1.0;
93
94      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
95      var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out var errorState);
96      if (errorState != OnlineCalculatorError.None) nmse = 1.0;
97
98      return nmse;
99    }
100   
101    public override double Evaluate(
102      IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
103      IEnumerable<int> rows) {
104      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
105      EstimationLimitsParameter.ExecutionContext                    = context;
106      ApplyLinearScalingParameter.ExecutionContext                  = context;
107
108      var nmse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
109        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
110        problemData, rows,
111        ApplyLinearScalingParameter.ActualValue.Value);
112
113      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
114      EstimationLimitsParameter.ExecutionContext                    = null;
115      ApplyLinearScalingParameter.ExecutionContext                  = null;
116
117      return nmse;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.