Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator.cs @ 17821

Last change on this file since 17821 was 17821, checked in by chaider, 3 years ago

#3076 Refactoring Evaluators and Analyzers

File size: 6.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HEAL.Attic;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
11  [StorableType("9397A63D-0C6B-4733-BD1A-59AAE9A9F006")]
12  public class SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
13    private const string BoundsEstimatorParameterName = "Bounds estimator";
14    public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
15      (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
16
17    public IBoundsEstimator BoundsEstimator {
18      get => BoundsEstimatorParameter.Value;
19      set => BoundsEstimatorParameter.Value = value;
20    }
21
22    #region Constructors
23
24    public SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator() { }
25
26    [StorableConstructor]
27    protected SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator(StorableConstructorFlag _) : base(_) { }
28
29    protected SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator(
30      SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator original, Cloner cloner) :
31      base(original, cloner) { }
32
33    #endregion
34
35    public override IDeepCloneable Clone(Cloner cloner) {
36      return new SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator(this, cloner);
37    }
38
39
40    public override IOperation InstrumentedApply() {
41      var rows               = GenerateRowsToEvaluate();
42      var solution           = SymbolicExpressionTreeParameter.ActualValue;
43      var problemData        = ProblemDataParameter.ActualValue;
44      var interpreter        = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
45      var estimationLimits   = EstimationLimitsParameter.ActualValue;
46      var applyLinearScaling = false;
47
48      if (UseConstantOptimization)
49        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows,
50                                                                          applyLinearScaling,
51                                                                          ConstantOptimizationIterations,
52                                                                          updateVariableWeights:
53                                                                          ConstantOptimizationUpdateVariableWeights,
54                                                                          lowerEstimationLimit: estimationLimits.Lower,
55                                                                          upperEstimationLimit: estimationLimits.Upper);
56
57      var qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData,
58                                rows, applyLinearScaling, DecimalPlaces, BoundsEstimator);
59      QualitiesParameter.ActualValue = new DoubleArray(qualities);
60      return base.InstrumentedApply();
61    }
62
63    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
64                                      IRegressionProblemData problemData,
65                                      IEnumerable<int> rows) {
66      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
67      EstimationLimitsParameter.ExecutionContext                    = context;
68      ApplyLinearScalingParameter.ExecutionContext                  = context;
69
70      var quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
71                              EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
72                              problemData, rows,
73                              ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces, BoundsEstimator);
74
75      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
76      EstimationLimitsParameter.ExecutionContext                    = null;
77      ApplyLinearScalingParameter.ExecutionContext                  = null;
78
79      return quality;
80    }
81
82    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
83                                     ISymbolicExpressionTree solution, double lowerEstimationLimit,
84                                     double upperEstimationLimit,
85                                     IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
86                                     int decimalPlaces, IBoundsEstimator estimator) {
87      OnlineCalculatorError errorState;
88      var estimatedValues =
89        interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
90      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
91
92      double nmse;
93
94      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
95      nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
96      if (errorState != OnlineCalculatorError.None) nmse = double.NaN;
97
98      if (nmse > 1)
99        nmse = 1;
100
101      var constraints    = problemData.IntervalConstraints.Constraints.Where(c => c.Enabled);
102      var intervalCollection = problemData.VariableRanges;
103
104      var objectives = new List<double> {nmse}; //Add r² to resultlist
105      var constraintObjectives = constraints.Select(constraint =>
106        IntervalUtil.IntervalConstraintViolation(constraint, estimator, intervalCollection, solution) > 0 ? 0.0 : 1.0);
107
108      objectives.AddRange(constraintObjectives); //Add hardconstraints for each constraint
109
110      return objectives.ToArray();
111    }
112
113
114    /*
115     * First objective is to maximize the Pearson R² value
116     * All following objectives have to be minimized ==> Constraints
117     */
118    public override IEnumerable<bool> Maximization {
119      get {
120        var objectives = new List<bool> {true};           //Max the pearson r² value
121        objectives.AddRange(Enumerable.Repeat(false, 6)); //Min the constraints
122        return objectives;
123      }
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.