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

Last change on this file since 16500 was 16500, checked in by mkommend, 14 months ago

#2974: Added intermediate version of new constants optimization for profiling.

File size: 5.2 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.Common;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization {
30  public static class Util {
31
32    public static double[,] ExtractData(IDataset dataset, IEnumerable<DataForVariable> variables, IEnumerable<int> rows) {
33      var x = new double[rows.Count(), variables.Count()];
34
35      int row = 0;
36      foreach (var r in rows) {
37        int col = 0;
38        foreach (var variable in variables) {
39          if (dataset.VariableHasType<double>(variable.variableName)) {
40            x[row, col] = dataset.GetDoubleValue(variable.variableName, r + variable.lag);
41          } else if (dataset.VariableHasType<string>(variable.variableName)) {
42            x[row, col] = dataset.GetStringValue(variable.variableName, r) == variable.variableValue ? 1 : 0;
43          } else throw new InvalidProgramException("found a variable of unknown type");
44          col++;
45        }
46        row++;
47      }
48      return x;
49    }
50
51    public static List<DataForVariable> GenerateVariables(IDataset dataset) {
52      var variables = new List<DataForVariable>();
53      foreach (var doubleVariable in dataset.DoubleVariables) {
54        var data = new DataForVariable(doubleVariable, string.Empty, 0);
55        variables.Add(data);
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          variables.Add(data);
62        }
63      }
64      return variables;
65    }
66
67    public static List<DataForVariable> ExtractLaggedVariables(ISymbolicExpressionTree tree) {
68      var variables = new HashSet<DataForVariable>();
69      foreach (var laggedNode in tree.IterateNodesPrefix().OfType<ILaggedTreeNode>()) {
70        var laggedVariableTreeNode = laggedNode as LaggedVariableTreeNode;
71        if (laggedVariableTreeNode != null) {
72          var data = new DataForVariable(laggedVariableTreeNode.VariableName, string.Empty, laggedVariableTreeNode.Lag);
73          if (!variables.Contains(data)) variables.Add(data);
74        }
75      }
76      return variables.ToList();
77    }
78
79    public static double[] ExtractConstants(ISymbolicExpressionTree tree) {
80      var constants = new List<double>();
81      foreach (var node in tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
82        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
83        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
84        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
85        if (constantTreeNode != null)
86          constants.Add(constantTreeNode.Value);
87        else if (variableTreeNodeBase != null)
88          constants.Add(variableTreeNodeBase.Weight);
89        else if (factorVarTreeNode != null) {
90          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
91            constants.Add(factorVarTreeNode.Weights[j]);
92        } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName()));
93      }
94      return constants.ToArray();
95    }
96
97    public static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) {
98      int i = 0;
99      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
100        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
101        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
102        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
103        if (constantTreeNode != null)
104          constantTreeNode.Value = constants[i++];
105        else if (variableTreeNodeBase != null)
106          variableTreeNodeBase.Weight = constants[i++];
107        else if (factorVarTreeNode != null) {
108          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
109            factorVarTreeNode.Weights[j] = constants[i++];
110        } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName()));
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.