1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Linq;
|
---|
27 | using HEAL.Attic;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.MultiObjective {
|
---|
35 | [Item("Multi Soft Constraints Evaluator",
|
---|
36 | "Calculates the Person R² and the constraints violations of a symbolic regression solution.")]
|
---|
37 | [StorableType("8E9D76B7-ED9C-43E7-9898-01FBD3633880")]
|
---|
38 | public class
|
---|
39 | SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator : SymbolicRegressionMultiObjectiveSplittingEvaluator {
|
---|
40 | public const string DimensionsParameterName = "Dimensions";
|
---|
41 | private const string BoundsEstimatorParameterName = "Bounds estimator";
|
---|
42 |
|
---|
43 | public IFixedValueParameter<IntValue> DimensionsParameter =>
|
---|
44 | (IFixedValueParameter<IntValue>) Parameters[DimensionsParameterName];
|
---|
45 |
|
---|
46 | public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
|
---|
47 | (IValueParameter<IBoundsEstimator>) Parameters[BoundsEstimatorParameterName];
|
---|
48 |
|
---|
49 | public IBoundsEstimator BoundsEstimator {
|
---|
50 | get => BoundsEstimatorParameter.Value;
|
---|
51 | set => BoundsEstimatorParameter.Value = value;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #region Constructors
|
---|
55 |
|
---|
56 | public SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator() {
|
---|
57 | Parameters.Add(new FixedValueParameter<IntValue>(DimensionsParameterName, new IntValue(2)));
|
---|
58 | Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName, new IABoundsEstimator()));
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | protected SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
63 |
|
---|
64 | protected SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(
|
---|
65 | SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
66 |
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | [StorableHook(HookType.AfterDeserialization)]
|
---|
70 | private void AfterDeserialization() {
|
---|
71 | if (!Parameters.ContainsKey(DimensionsParameterName))
|
---|
72 | Parameters.Add(new FixedValueParameter<IntValue>(DimensionsParameterName, new IntValue(2)));
|
---|
73 |
|
---|
74 | if (!Parameters.ContainsKey(BoundsEstimatorParameterName))
|
---|
75 | Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName, new IABoundsEstimator()));
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
79 | return new SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(this, cloner);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | public override IOperation InstrumentedApply() {
|
---|
84 | var rows = GenerateRowsToEvaluate();
|
---|
85 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
86 | var problemData = ProblemDataParameter.ActualValue;
|
---|
87 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
88 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
89 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
90 |
|
---|
91 | if (UseConstantOptimization) {
|
---|
92 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows,
|
---|
93 | false,
|
---|
94 | ConstantOptimizationIterations,
|
---|
95 | ConstantOptimizationUpdateVariableWeights,
|
---|
96 | estimationLimits.Lower,
|
---|
97 | estimationLimits.Upper);
|
---|
98 | } else {
|
---|
99 | if (applyLinearScaling) {
|
---|
100 | //Check for interval arithmetic grammar
|
---|
101 | //remove scaling nodes for linear scaling evaluation
|
---|
102 | var rootNode = new ProgramRootSymbol().CreateTreeNode();
|
---|
103 | var startNode = new StartSymbol().CreateTreeNode();
|
---|
104 | SymbolicExpressionTree newTree = null;
|
---|
105 | foreach (var node in solution.IterateNodesPrefix())
|
---|
106 | if (node.Symbol.Name == "Scaling") {
|
---|
107 | for (var i = 0; i < node.SubtreeCount; ++i) startNode.AddSubtree(node.GetSubtree(i));
|
---|
108 | rootNode.AddSubtree(startNode);
|
---|
109 | newTree = new SymbolicExpressionTree(rootNode);
|
---|
110 | break;
|
---|
111 | }
|
---|
112 |
|
---|
113 | //calculate alpha and beta for scaling
|
---|
114 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
|
---|
115 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
116 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
|
---|
117 | out var errorState);
|
---|
118 | //Set alpha and beta to the scaling nodes from ia grammar
|
---|
119 | foreach (var node in solution.IterateNodesPrefix())
|
---|
120 | if (node.Symbol.Name == "Offset") {
|
---|
121 | node.RemoveSubtree(1);
|
---|
122 | var alphaNode = new ConstantTreeNode(new Constant()) {Value = alpha};
|
---|
123 | node.AddSubtree(alphaNode);
|
---|
124 | } else if (node.Symbol.Name == "Scaling") {
|
---|
125 | node.RemoveSubtree(1);
|
---|
126 | var betaNode = new ConstantTreeNode(new Constant()) {Value = beta};
|
---|
127 | node.AddSubtree(betaNode);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | var qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData,
|
---|
133 | rows, BoundsEstimator);
|
---|
134 | QualitiesParameter.ActualValue = new DoubleArray(qualities);
|
---|
135 | return base.InstrumentedApply();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public override double[] Evaluate(
|
---|
139 | IExecutionContext context, ISymbolicExpressionTree tree,
|
---|
140 | IRegressionProblemData problemData,
|
---|
141 | IEnumerable<int> rows) {
|
---|
142 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
143 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
144 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
145 |
|
---|
146 | var quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
|
---|
147 | EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
|
---|
148 | problemData, rows, BoundsEstimator);
|
---|
149 |
|
---|
150 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
151 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
152 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
153 |
|
---|
154 | return quality;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | public static double[] Calculate(
|
---|
159 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
160 | ISymbolicExpressionTree solution, double lowerEstimationLimit,
|
---|
161 | double upperEstimationLimit,
|
---|
162 | IRegressionProblemData problemData, IEnumerable<int> rows, IBoundsEstimator estimator) {
|
---|
163 | OnlineCalculatorError errorState;
|
---|
164 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
165 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
166 | var constraints = problemData.IntervalConstraints.Constraints.Where(c => c.Enabled);
|
---|
167 | var intervalCollection = problemData.VariableRanges;
|
---|
168 |
|
---|
169 | double nmse;
|
---|
170 |
|
---|
171 | var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
172 | nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
|
---|
173 | if (errorState != OnlineCalculatorError.None) nmse = 1.0;
|
---|
174 |
|
---|
175 | if (nmse > 1)
|
---|
176 | nmse = 1.0;
|
---|
177 |
|
---|
178 |
|
---|
179 | var objectives = new List<double> {nmse};
|
---|
180 | ////var intervalInterpreter = new IntervalInterpreter() {UseIntervalSplitting = splitting};
|
---|
181 |
|
---|
182 | //var constraintObjectives = new List<double>();
|
---|
183 | //foreach (var c in constraints) {
|
---|
184 | // var penalty = ConstraintExceeded(c, intervalInterpreter, variableRanges,
|
---|
185 | // solution);
|
---|
186 | // var maxP = 0.1;
|
---|
187 |
|
---|
188 | // if (double.IsNaN(penalty) || double.IsInfinity(penalty) || penalty > maxP)
|
---|
189 | // penalty = maxP;
|
---|
190 |
|
---|
191 | // constraintObjectives.Add(penalty);
|
---|
192 | //}
|
---|
193 |
|
---|
194 | //objectives.AddRange(constraintObjectives);
|
---|
195 |
|
---|
196 | //return objectives.ToArray();
|
---|
197 |
|
---|
198 | var results = IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution);
|
---|
199 | objectives.AddRange(results);
|
---|
200 | return objectives.ToArray();
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * First objective is to maximize the Pearson R² value
|
---|
205 | * All following objectives have to be minimized ==> Constraints
|
---|
206 | */
|
---|
207 |
|
---|
208 | public override IEnumerable<bool> Maximization {
|
---|
209 | get {
|
---|
210 | var objectives = new List<bool> {false}; //First NMSE ==> min
|
---|
211 | objectives.AddRange(Enumerable.Repeat(false, DimensionsParameter.Value.Value)); //Constraints ==> min
|
---|
212 |
|
---|
213 | return objectives;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | } |
---|