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