Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Constants Optimization/Util.cs @ 16461

Last change on this file since 16461 was 16461, checked in by mkommend, 5 years ago

#2974: Added unit tests and refactoring.

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization {
29  public static class Util {
30
31    public static double[,] ExtractData(IDataset dataset, IEnumerable<DataForVariable> variables, IEnumerable<int> rows) {
32      var x = new double[rows.Count(), variables.Count()];
33
34      int row = 0;
35      foreach (var r in rows) {
36        int col = 0;
37        foreach (var variable in variables) {
38          if (dataset.VariableHasType<double>(variable.variableName)) {
39            x[row, col] = dataset.GetDoubleValue(variable.variableName, r + variable.lag);
40          } else if (dataset.VariableHasType<string>(variable.variableName)) {
41            x[row, col] = dataset.GetStringValue(variable.variableName, r) == variable.variableValue ? 1 : 0;
42          } else throw new InvalidProgramException("found a variable of unknown type");
43          col++;
44        }
45        row++;
46      }
47      return x;
48    }
49
50    public static Dictionary<DataForVariable, AutoDiff.Variable> ExtractParameters(IDataset dataset) {
51      var parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
52      foreach (var doubleVariable in dataset.DoubleVariables) {
53        var data = new DataForVariable(doubleVariable, string.Empty, 0);
54        var param = new AutoDiff.Variable();
55        parameters.Add(data, param);
56      }
57
58      foreach (var stringVariable in dataset.StringVariables) {
59        foreach (var stringValue in dataset.GetStringValues(stringVariable).Distinct()) {
60          var data = new DataForVariable(stringVariable, stringValue, 0);
61          var param = new AutoDiff.Variable();
62          parameters.Add(data, param);
63        }
64      }
65      return parameters;
66    }
67
68    public static double[] ExtractConstants(ISymbolicExpressionTree tree) {
69      var constants = new List<double>();
70      foreach (var node in tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
71        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
72        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
73        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
74        if (constantTreeNode != null)
75          constants.Add(constantTreeNode.Value);
76        else if (variableTreeNodeBase != null)
77          constants.Add(variableTreeNodeBase.Weight);
78        else if (factorVarTreeNode != null) {
79          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
80            constants.Add(factorVarTreeNode.Weights[j]);
81        }       
82      }
83      return constants.ToArray();
84    }
85
86    public static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) {
87      int i = 0;
88      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
89        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
90        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
91        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
92        if (constantTreeNode != null)
93          constantTreeNode.Value = constants[i++];
94        else if (variableTreeNodeBase != null)
95          variableTreeNodeBase.Weight = constants[i++];
96        else if (factorVarTreeNode != null) {
97          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
98            factorVarTreeNode.Weights[j] = constants[i++];
99        }
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.