1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
28 | public static class ParameterOptimization {
|
---|
29 | public static double OptimizeTreeParameters(IRegressionProblemData problemData, ISymbolicExpressionTree tree,
|
---|
30 | int maxIterations = 10, bool updateParametersInTree = true, bool updateVariableWeights = true,
|
---|
31 | double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
|
---|
32 | IEnumerable<int> rows = null, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = null,
|
---|
33 | Action<double[], double, object> iterationCallback = null) {
|
---|
34 |
|
---|
35 | if (rows == null) rows = problemData.TrainingIndices;
|
---|
36 | if (interpreter == null) interpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter();
|
---|
37 |
|
---|
38 | // Numeric parameters in the tree become variables for parameter optimization.
|
---|
39 | // Variables in the tree become parameters (fixed values) for parameter optimization.
|
---|
40 | // For each parameter (variable in the original tree) we store the
|
---|
41 | // variable name, variable value (for factor vars) and lag as a DataForVariable object.
|
---|
42 | // A dictionary is used to find parameters
|
---|
43 | double[] initialParameters;
|
---|
44 | var parameters = new List<TreeToAutoDiffTermConverter.DataForVariable>();
|
---|
45 |
|
---|
46 | TreeToAutoDiffTermConverter.ParametricFunction func;
|
---|
47 | TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad;
|
---|
48 | if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree, updateVariableWeights, addLinearScalingTerms: false, out parameters, out initialParameters, out func, out func_grad))
|
---|
49 | throw new NotSupportedException("Could not optimize parameters of symbolic expression tree due to not supported symbols used in the tree.");
|
---|
50 | var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
|
---|
51 |
|
---|
52 | // extract initial parameters
|
---|
53 | double[] c = (double[])initialParameters.Clone();
|
---|
54 | alglib.minlmreport rep;
|
---|
55 |
|
---|
56 | double originalQuality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(
|
---|
57 | tree, problemData, rows,
|
---|
58 | interpreter, applyLinearScaling: false,
|
---|
59 | lowerEstimationLimit, upperEstimationLimit);
|
---|
60 |
|
---|
61 |
|
---|
62 | IDataset ds = problemData.Dataset;
|
---|
63 | int n = rows.Count();
|
---|
64 | int k = parameters.Count;
|
---|
65 |
|
---|
66 | double[,] x = new double[n, k];
|
---|
67 | int row = 0;
|
---|
68 | foreach (var r in rows) {
|
---|
69 | int col = 0;
|
---|
70 | foreach (var info in parameterEntries) {
|
---|
71 | if (ds.VariableHasType<double>(info.variableName)) {
|
---|
72 | x[row, col] = ds.GetDoubleValue(info.variableName, r + info.lag);
|
---|
73 | } else if (ds.VariableHasType<string>(info.variableName)) {
|
---|
74 | x[row, col] = ds.GetStringValue(info.variableName, r) == info.variableValue ? 1 : 0;
|
---|
75 | } else throw new InvalidProgramException("found a variable of unknown type");
|
---|
76 | col++;
|
---|
77 | }
|
---|
78 | row++;
|
---|
79 | }
|
---|
80 | double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
|
---|
81 |
|
---|
82 | alglib.ndimensional_rep xrep = (p, f, obj) => iterationCallback(p, f, obj);
|
---|
83 |
|
---|
84 | try {
|
---|
85 | alglib.minlmcreatevj(y.Length, c, out var lmstate);
|
---|
86 | alglib.minlmsetcond(lmstate, 0.0, maxIterations);
|
---|
87 | alglib.minlmsetxrep(lmstate, iterationCallback != null);
|
---|
88 | // alglib.minlmoptguardgradient(lmstate, 1e-5); // for debugging gradient calculation
|
---|
89 | alglib.minlmoptimize(lmstate, CreateFunc(func, x, y), CreateJac(func_grad, x, y), xrep, null);
|
---|
90 | alglib.minlmresults(lmstate, out c, out rep);
|
---|
91 | // alglib.minlmoptguardresults(lmstate, out var optGuardReport);
|
---|
92 | } catch (ArithmeticException) {
|
---|
93 | return originalQuality;
|
---|
94 | } catch (alglib.alglibexception) {
|
---|
95 | return originalQuality;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | // * TerminationType, completion code:
|
---|
100 | // * -8 optimizer detected NAN/INF values either in the function itself,
|
---|
101 | // or in its Jacobian
|
---|
102 | // * -5 inappropriate solver was used:
|
---|
103 | // * solver created with minlmcreatefgh() used on problem with
|
---|
104 | // general linear constraints (set with minlmsetlc() call).
|
---|
105 | // * -3 constraints are inconsistent
|
---|
106 | // * 2 relative step is no more than EpsX.
|
---|
107 | // * 5 MaxIts steps was taken
|
---|
108 | // * 7 stopping conditions are too stringent,
|
---|
109 | // further improvement is impossible
|
---|
110 | // * 8 terminated by user who called MinLMRequestTermination().
|
---|
111 | // X contains point which was "current accepted" when termination
|
---|
112 | // request was submitted.
|
---|
113 | if (rep.terminationtype > 0) {
|
---|
114 | UpdateParameters(tree, c, updateVariableWeights);
|
---|
115 | }
|
---|
116 | var quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(
|
---|
117 | tree, problemData, rows,
|
---|
118 | interpreter, applyLinearScaling: false,
|
---|
119 | lowerEstimationLimit, upperEstimationLimit);
|
---|
120 |
|
---|
121 | if (!updateParametersInTree) UpdateParameters(tree, initialParameters, updateVariableWeights);
|
---|
122 |
|
---|
123 | if (originalQuality < quality || double.IsNaN(quality)) {
|
---|
124 | UpdateParameters(tree, initialParameters, updateVariableWeights);
|
---|
125 | return originalQuality;
|
---|
126 | }
|
---|
127 | return quality;
|
---|
128 | }
|
---|
129 |
|
---|
130 | private static void UpdateParameters(ISymbolicExpressionTree tree, double[] parameters, bool updateVariableWeights) {
|
---|
131 | int i = 0;
|
---|
132 | foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
|
---|
133 | NumberTreeNode numberTreeNode = node as NumberTreeNode;
|
---|
134 | VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
|
---|
135 | FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
|
---|
136 | if (numberTreeNode != null) {
|
---|
137 | if (numberTreeNode.Parent.Symbol is Power
|
---|
138 | && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimized (see TreeToAutoDiffTermConverter)
|
---|
139 | numberTreeNode.Value = parameters[i++];
|
---|
140 | } else if (updateVariableWeights && variableTreeNodeBase != null)
|
---|
141 | variableTreeNodeBase.Weight = parameters[i++];
|
---|
142 | else if (factorVarTreeNode != null) {
|
---|
143 | for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
|
---|
144 | factorVarTreeNode.Weights[j] = parameters[i++];
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private static alglib.ndimensional_fvec CreateFunc(TreeToAutoDiffTermConverter.ParametricFunction func, double[,] x, double[] y) {
|
---|
150 | int d = x.GetLength(1);
|
---|
151 | // row buffer
|
---|
152 | var xi = new double[d];
|
---|
153 | // function must return residuals, alglib optimizes resid²
|
---|
154 | return (double[] c, double[] resid, object o) => {
|
---|
155 | for (int i = 0; i < y.Length; i++) {
|
---|
156 | Buffer.BlockCopy(x, i * d * sizeof(double), xi, 0, d * sizeof(double)); // copy row. We are using BlockCopy instead of Array.Copy because x has rank 2
|
---|
157 | resid[i] = func(c, xi) - y[i];
|
---|
158 | }
|
---|
159 | };
|
---|
160 | }
|
---|
161 |
|
---|
162 | private static alglib.ndimensional_jac CreateJac(TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad, double[,] x, double[] y) {
|
---|
163 | int numParams = x.GetLength(1);
|
---|
164 | // row buffer
|
---|
165 | var xi = new double[numParams];
|
---|
166 | return (double[] c, double[] resid, double[,] jac, object o) => {
|
---|
167 | int numVars = c.Length;
|
---|
168 | for (int i = 0; i < y.Length; i++) {
|
---|
169 | Buffer.BlockCopy(x, i * numParams * sizeof(double), xi, 0, numParams * sizeof(double)); // copy row
|
---|
170 | var tuple = func_grad(c, xi);
|
---|
171 | resid[i] = tuple.Item2 - y[i];
|
---|
172 | Buffer.BlockCopy(tuple.Item1, 0, jac, i * numVars * sizeof(double), numVars * sizeof(double)); // copy the gradient to jac. BlockCopy because jac has rank 2.
|
---|
173 | }
|
---|
174 | };
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 | }
|
---|