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

Last change on this file since 17705 was 17705, checked in by chaider, 7 weeks ago

#3076

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