1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HEAL.Attic;
|
---|
32 | using System.Runtime.InteropServices;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
35 | [Item("NLOpt Evaluator (with constraints)", "")]
|
---|
36 | [StorableType("5FADAE55-3516-4539-8A36-BC9B0D00880D")]
|
---|
37 | public class NLOptEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
38 | private const string ConstantOptimizationIterationsParameterName = "ConstantOptimizationIterations";
|
---|
39 | private const string ConstantOptimizationImprovementParameterName = "ConstantOptimizationImprovement";
|
---|
40 | private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability";
|
---|
41 | private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
|
---|
42 | private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
|
---|
43 | private const string UpdateVariableWeightsParameterName = "Update Variable Weights";
|
---|
44 |
|
---|
45 | private const string FunctionEvaluationsResultParameterName = "Constants Optimization Function Evaluations";
|
---|
46 | private const string GradientEvaluationsResultParameterName = "Constants Optimization Gradient Evaluations";
|
---|
47 | private const string CountEvaluationsParameterName = "Count Function and Gradient Evaluations";
|
---|
48 |
|
---|
49 | public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
|
---|
50 | get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
|
---|
51 | }
|
---|
52 | public IFixedValueParameter<DoubleValue> ConstantOptimizationImprovementParameter {
|
---|
53 | get { return (IFixedValueParameter<DoubleValue>)Parameters[ConstantOptimizationImprovementParameterName]; }
|
---|
54 | }
|
---|
55 | public IFixedValueParameter<PercentValue> ConstantOptimizationProbabilityParameter {
|
---|
56 | get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationProbabilityParameterName]; }
|
---|
57 | }
|
---|
58 | public IFixedValueParameter<PercentValue> ConstantOptimizationRowsPercentageParameter {
|
---|
59 | get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
|
---|
60 | }
|
---|
61 | public IFixedValueParameter<BoolValue> UpdateConstantsInTreeParameter {
|
---|
62 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateConstantsInTreeParameterName]; }
|
---|
63 | }
|
---|
64 | public IFixedValueParameter<BoolValue> UpdateVariableWeightsParameter {
|
---|
65 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateVariableWeightsParameterName]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public IResultParameter<IntValue> FunctionEvaluationsResultParameter {
|
---|
69 | get { return (IResultParameter<IntValue>)Parameters[FunctionEvaluationsResultParameterName]; }
|
---|
70 | }
|
---|
71 | public IResultParameter<IntValue> GradientEvaluationsResultParameter {
|
---|
72 | get { return (IResultParameter<IntValue>)Parameters[GradientEvaluationsResultParameterName]; }
|
---|
73 | }
|
---|
74 | public IFixedValueParameter<BoolValue> CountEvaluationsParameter {
|
---|
75 | get { return (IFixedValueParameter<BoolValue>)Parameters[CountEvaluationsParameterName]; }
|
---|
76 | }
|
---|
77 | public IConstrainedValueParameter<StringValue> SolverParameter {
|
---|
78 | get { return (IConstrainedValueParameter<StringValue>)Parameters["Solver"]; }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | public IntValue ConstantOptimizationIterations {
|
---|
83 | get { return ConstantOptimizationIterationsParameter.Value; }
|
---|
84 | }
|
---|
85 | public DoubleValue ConstantOptimizationImprovement {
|
---|
86 | get { return ConstantOptimizationImprovementParameter.Value; }
|
---|
87 | }
|
---|
88 | public PercentValue ConstantOptimizationProbability {
|
---|
89 | get { return ConstantOptimizationProbabilityParameter.Value; }
|
---|
90 | }
|
---|
91 | public PercentValue ConstantOptimizationRowsPercentage {
|
---|
92 | get { return ConstantOptimizationRowsPercentageParameter.Value; }
|
---|
93 | }
|
---|
94 | public bool UpdateConstantsInTree {
|
---|
95 | get { return UpdateConstantsInTreeParameter.Value.Value; }
|
---|
96 | set { UpdateConstantsInTreeParameter.Value.Value = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public bool UpdateVariableWeights {
|
---|
100 | get { return UpdateVariableWeightsParameter.Value.Value; }
|
---|
101 | set { UpdateVariableWeightsParameter.Value.Value = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public bool CountEvaluations {
|
---|
105 | get { return CountEvaluationsParameter.Value.Value; }
|
---|
106 | set { CountEvaluationsParameter.Value.Value = value; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public string Solver {
|
---|
110 | get { return SolverParameter.Value.Value; }
|
---|
111 | }
|
---|
112 | public override bool Maximization {
|
---|
113 | get { return false; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | [StorableConstructor]
|
---|
117 | protected NLOptEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
118 | protected NLOptEvaluator(NLOptEvaluator original, Cloner cloner)
|
---|
119 | : base(original, cloner) {
|
---|
120 | }
|
---|
121 | public NLOptEvaluator()
|
---|
122 | : base() {
|
---|
123 | Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
|
---|
124 | Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
|
---|
125 | Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1)));
|
---|
126 | Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1)));
|
---|
127 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true });
|
---|
128 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized.", new BoolValue(true)) { Hidden = true });
|
---|
129 |
|
---|
130 | Parameters.Add(new FixedValueParameter<BoolValue>(CountEvaluationsParameterName, "Determines if function and gradient evaluation should be counted.", new BoolValue(false)));
|
---|
131 |
|
---|
132 | var validSolvers = new ItemSet<StringValue>(new[] { "MMA", "COBYLA", "CCSAQ", "ISRES" }.Select(s => new StringValue(s).AsReadOnly()));
|
---|
133 | Parameters.Add(new ConstrainedValueParameter<StringValue>("Solver", "The solver algorithm", validSolvers, validSolvers.First()));
|
---|
134 | Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
|
---|
135 | Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
|
---|
136 | }
|
---|
137 |
|
---|
138 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
139 | return new NLOptEvaluator(this, cloner);
|
---|
140 | }
|
---|
141 |
|
---|
142 | [StorableHook(HookType.AfterDeserialization)]
|
---|
143 | private void AfterDeserialization() { }
|
---|
144 |
|
---|
145 | private static readonly object locker = new object();
|
---|
146 |
|
---|
147 | public override IOperation InstrumentedApply() {
|
---|
148 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
149 | double quality;
|
---|
150 | if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) {
|
---|
151 | IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
|
---|
152 | var counter = new EvaluationsCounter();
|
---|
153 | quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
|
---|
154 | constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, Solver, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter);
|
---|
155 |
|
---|
156 | if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
|
---|
157 | throw new NotSupportedException();
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (CountEvaluations) {
|
---|
161 | lock (locker) {
|
---|
162 | FunctionEvaluationsResultParameter.ActualValue.Value += counter.FunctionEvaluations;
|
---|
163 | GradientEvaluationsResultParameter.ActualValue.Value += counter.GradientEvaluations;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | } else {
|
---|
168 | throw new NotSupportedException();
|
---|
169 | }
|
---|
170 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
171 |
|
---|
172 | return base.InstrumentedApply();
|
---|
173 | }
|
---|
174 |
|
---|
175 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
176 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
177 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
178 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
179 | FunctionEvaluationsResultParameter.ExecutionContext = context;
|
---|
180 | GradientEvaluationsResultParameter.ExecutionContext = context;
|
---|
181 |
|
---|
182 | // MSE evaluator is used on purpose instead of the const-opt evaluator,
|
---|
183 | // because Evaluate() is used to get the quality of evolved models on
|
---|
184 | // different partitions of the dataset (e.g., best validation model)
|
---|
185 | double mse = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, double.MinValue, double.MaxValue, problemData, rows, applyLinearScaling: false);
|
---|
186 |
|
---|
187 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
188 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
189 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
190 | FunctionEvaluationsResultParameter.ExecutionContext = null;
|
---|
191 | GradientEvaluationsResultParameter.ExecutionContext = null;
|
---|
192 |
|
---|
193 | return mse;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public class EvaluationsCounter {
|
---|
197 | public int FunctionEvaluations = 0;
|
---|
198 | public int GradientEvaluations = 0;
|
---|
199 | }
|
---|
200 |
|
---|
201 | private static void GetParameterNodes(ISymbolicExpressionTree tree, out List<ISymbolicExpressionTreeNode> thetaNodes, out List<double> thetaValues) {
|
---|
202 | thetaNodes = new List<ISymbolicExpressionTreeNode>();
|
---|
203 | thetaValues = new List<double>();
|
---|
204 |
|
---|
205 | var nodes = tree.IterateNodesPrefix().ToArray();
|
---|
206 | for (int i = 0; i < nodes.Length; ++i) {
|
---|
207 | var node = nodes[i];
|
---|
208 | if (node is VariableTreeNode variableTreeNode) {
|
---|
209 | thetaValues.Add(variableTreeNode.Weight);
|
---|
210 | thetaNodes.Add(node);
|
---|
211 | } else if (node is ConstantTreeNode constantTreeNode) {
|
---|
212 | thetaNodes.Add(node);
|
---|
213 | thetaValues.Add(constantTreeNode.Value);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
220 | ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
|
---|
221 | string solver,
|
---|
222 | int maxIterations, bool updateVariableWeights = true,
|
---|
223 | double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
|
---|
224 | bool updateConstantsInTree = true, Action<double[], double, object> iterationCallback = null, EvaluationsCounter counter = null) {
|
---|
225 |
|
---|
226 | if (!updateVariableWeights) throw new NotSupportedException("not updating variable weights is not supported");
|
---|
227 | if (!updateConstantsInTree) throw new NotSupportedException("not updating tree parameters is not supported");
|
---|
228 | if (!applyLinearScaling) throw new NotSupportedException("application without linear scaling is not supported");
|
---|
229 |
|
---|
230 |
|
---|
231 | using (var state = new ConstrainedNLSInternal(solver, tree, maxIterations, problemData, 0, 0, 0)) {
|
---|
232 | state.Optimize();
|
---|
233 | return state.BestError;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | }
|
---|
239 | }
|
---|