Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NonlinearLeastSquaresConstantOptimizationEvaluator.cs @ 17472

Last change on this file since 17472 was 17472, checked in by pfleck, 4 years ago

#3040 Moved Alglib+AutoDiff constant optimizer in own class and created base class to provide multiple constant-opt implementations.

File size: 9.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HEAL.Attic;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
34  [StorableType("24B68851-036D-4446-BD6F-3823E9028FF4")]
35  [Item("NonlinearLeastSquaresOptimizer", "")]
36  public class NonlinearLeastSquaresConstantOptimizationEvaluator : SymbolicRegressionConstantOptimizationEvaluator {
37
38    private const string ConstantOptimizationIterationsName = "ConstantOptimizationIterations";
39
40    #region Parameter Properties
41    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsName]; }
43    }
44    #endregion
45
46    #region Properties
47    public int ConstantOptimizationIterations {
48      get { return ConstantOptimizationIterationsParameter.Value.Value; }
49    }
50    #endregion
51
52    public NonlinearLeastSquaresConstantOptimizationEvaluator()
53      : base() {
54      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsName, "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)));
55    }
56
57    protected NonlinearLeastSquaresConstantOptimizationEvaluator(NonlinearLeastSquaresConstantOptimizationEvaluator original, Cloner cloner)
58      : base(original, cloner) {
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new NonlinearLeastSquaresConstantOptimizationEvaluator(this, cloner);
62    }
63    [StorableConstructor]
64    protected NonlinearLeastSquaresConstantOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
65
66    protected override ISymbolicExpressionTree OptimizeConstants(
67      ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows,
68      CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter = null) {
69      return OptimizeTree(tree,
70        problemData, rows,
71        ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations, UpdateVariableWeights,
72        cancellationToken, counter);
73    }
74
75    public static ISymbolicExpressionTree OptimizeTree(
76      ISymbolicExpressionTree tree,
77      IRegressionProblemData problemData, IEnumerable<int> rows,
78      bool applyLinearScaling, int maxIterations, bool updateVariableWeights,
79      CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter = null, Action<double[], double, object> iterationCallback = null) {
80
81      // numeric constants in the tree become variables for constant opt
82      // variables in the tree become parameters (fixed values) for constant opt
83      // for each parameter (variable in the original tree) we store the
84      // variable name, variable value (for factor vars) and lag as a DataForVariable object.
85      // A dictionary is used to find parameters
86      bool success = TreeToAutoDiffTermConverter.TryConvertToAutoDiff(
87        tree, updateVariableWeights, applyLinearScaling,
88        out var parameters, out var initialConstants, out var func, out var func_grad);
89      if (!success)
90        throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
91      if (parameters.Count == 0) return (ISymbolicExpressionTree)tree.Clone();
92      var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
93
94      //extract initial constants
95      double[] c;
96      if (applyLinearScaling) {
97        c = new double[initialConstants.Length + 2];
98        c[0] = 0.0;
99        c[1] = 1.0;
100        Array.Copy(initialConstants, 0, c, 2, initialConstants.Length);
101      } else {
102        c = (double[])initialConstants.Clone();
103      }
104
105      IDataset ds = problemData.Dataset;
106      double[,] x = new double[rows.Count(), parameters.Count];
107      int row = 0;
108      foreach (var r in rows) {
109        int col = 0;
110        foreach (var info in parameterEntries) {
111          if (ds.VariableHasType<double>(info.variableName)) {
112            x[row, col] = ds.GetDoubleValue(info.variableName, r + info.lag);
113          } else if (ds.VariableHasType<string>(info.variableName)) {
114            x[row, col] = ds.GetStringValue(info.variableName, r) == info.variableValue ? 1 : 0;
115          } else throw new InvalidProgramException("found a variable of unknown type");
116          col++;
117        }
118        row++;
119      }
120      double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
121      int n = x.GetLength(0);
122      int m = x.GetLength(1);
123      int k = c.Length;
124
125      alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(func);
126      alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(func_grad);
127      alglib.ndimensional_rep xrep = (p, f, obj) => {
128        iterationCallback?.Invoke(p, f, obj);
129        cancellationToken.ThrowIfCancellationRequested();
130      };
131      var rowEvaluationsCounter = new EvaluationsCounter();
132
133      try {
134        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out var state);
135        alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
136        alglib.lsfitsetxrep(state, iterationCallback != null || cancellationToken != default(CancellationToken));
137        //alglib.lsfitsetgradientcheck(state, 0.001);
138        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, xrep, rowEvaluationsCounter);
139        alglib.lsfitresults(state, out var retVal, out c, out alglib.lsfitreport rep);
140
141        //retVal == -7  => constant optimization failed due to wrong gradient
142        if (retVal == -1)
143          return (ISymbolicExpressionTree)tree.Clone();
144      } catch (ArithmeticException) {
145        return (ISymbolicExpressionTree)tree.Clone();
146      } catch (alglib.alglibexception) {
147        return (ISymbolicExpressionTree)tree.Clone();
148      }
149
150      if (counter != null) {
151        counter.FunctionEvaluations += rowEvaluationsCounter.FunctionEvaluations / n;
152        counter.GradientEvaluations += rowEvaluationsCounter.GradientEvaluations / n;
153      }
154
155      var newTree = (ISymbolicExpressionTree)tree.Clone();
156      if (applyLinearScaling) {
157        var tmp = new double[c.Length - 2];
158        Array.Copy(c, 2, tmp, 0, tmp.Length);
159        UpdateConstants(newTree, tmp, updateVariableWeights);
160      } else
161        UpdateConstants(newTree, c, updateVariableWeights);
162
163      return newTree;
164    }
165
166    private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants, bool updateVariableWeights) {
167      int i = 0;
168      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
169        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
170        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
171        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
172        if (constantTreeNode != null)
173          constantTreeNode.Value = constants[i++];
174        else if (updateVariableWeights && variableTreeNodeBase != null)
175          variableTreeNodeBase.Weight = constants[i++];
176        else if (factorVarTreeNode != null) {
177          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
178            factorVarTreeNode.Weights[j] = constants[i++];
179        }
180      }
181    }
182
183    private static alglib.ndimensional_pfunc CreatePFunc(TreeToAutoDiffTermConverter.ParametricFunction func) {
184      return (double[] c, double[] x, ref double fx, object o) => {
185        fx = func(c, x);
186        var counter = (EvaluationsCounter)o;
187        counter.FunctionEvaluations++;
188      };
189    }
190
191    private static alglib.ndimensional_pgrad CreatePGrad(TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad) {
192      return (double[] c, double[] x, ref double fx, double[] grad, object o) => {
193        var tuple = func_grad(c, x);
194        fx = tuple.Item2;
195        Array.Copy(tuple.Item1, grad, grad.Length);
196        var counter = (EvaluationsCounter)o;
197        counter.GradientEvaluations++;
198      };
199    }
200
201    public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
202      return TreeToAutoDiffTermConverter.IsCompatible(tree);
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.