Changeset 16507 for branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/ConstantsOptimization/Util.cs
- Timestamp:
- 01/06/19 18:03:15 (6 years ago)
- Location:
- branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/ConstantsOptimization
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/ConstantsOptimization/Util.cs
r16500 r16507 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter;28 27 29 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization { 30 29 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); 30 /// <summary> 31 /// Extracts all variable information in a symbolic expression tree. The variable information is necessary to convert a tree in an AutoDiff term. 32 /// </summary> 33 /// <param name="tree">The tree referencing the variables.</param> 34 /// <returns>The data for variables occuring in the tree.</returns> 35 public static List<VariableData> ExtractVariables(ISymbolicExpressionTree tree) { 36 if (tree == null) throw new ArgumentNullException("tree"); 37 38 var variables = new HashSet<VariableData>(); 39 foreach (var node in tree.IterateNodesPrefix().OfType<IVariableTreeNode>()) { 40 string variableName = node.VariableName; 41 int lag = 0; 42 var laggedNode = node as ILaggedTreeNode; 43 if (laggedNode != null) lag = laggedNode.Lag; 44 45 46 var factorNode = node as FactorVariableTreeNode; 47 if (factorNode != null) { 48 foreach (var factorValue in factorNode.Symbol.GetVariableValues(variableName)) { 49 var data = new VariableData(variableName, factorValue, lag); 50 variables.Add(data); 51 } 52 } else { 53 var data = new VariableData(variableName, string.Empty, lag); 61 54 variables.Add(data); 62 55 } 63 56 } 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 57 return variables.ToList(); 77 58 } 78 79 public static double[] ExtractConstants(ISymbolicExpressionTree tree) { 59 /// <summary> 60 /// Extract the necessary date for constants optimization with AutoDiff 61 /// </summary> 62 /// <param name="dataset">The dataset holding the data.</param> 63 /// <param name="variables">The variables for which the data from the dataset should be extracted.</param> 64 /// <param name="rows">The rows for which the data should be extracted.</param> 65 /// <returns>A two-dimensiona double array containing the input data.</returns> 66 public static double[,] ExtractData(IDataset dataset, IEnumerable<VariableData> variables, IEnumerable<int> rows) { 67 if (dataset == null) throw new ArgumentNullException("dataset"); 68 if (variables == null) throw new ArgumentNullException("variables"); 69 if (rows == null) throw new ArgumentNullException("rows"); 70 71 var x = new double[rows.Count(), variables.Count()]; 72 73 int col = 0; 74 foreach (var variable in variables) { 75 if (dataset.VariableHasType<double>(variable.variableName)) { 76 IEnumerable<double> values; 77 if (variable.lag == 0) 78 values = dataset.GetDoubleValues(variable.variableName, rows); 79 else 80 values = dataset.GetDoubleValues(variable.variableName, rows.Select(r => r + variable.lag)); 81 82 int row = 0; 83 foreach (var value in values) { 84 x[row, col] = value; 85 row++; 86 } 87 } else if (dataset.VariableHasType<string>(variable.variableName)) { 88 var values = dataset.GetStringValues(variable.variableName, rows); 89 90 int row = 0; 91 foreach (var value in values) { 92 x[row, col] = value == variable.variableValue ? 1 : 0; ; 93 row++; 94 } 95 } else throw new NotSupportedException("found a variable of unknown type"); 96 col++; 97 } 98 99 return x; 100 } 101 102 /// <summary> 103 /// Extracts all numeric nodes from a symbolic expression tree that can be optimized by the constants optimization 104 /// </summary> 105 /// <param name="tree">The tree from which the numeric nodes should be extracted.</param> 106 /// <returns>A list containing all nodes with numeric coefficients.</returns> 107 public static List<ISymbolicExpressionTreeNode> ExtractNumericNodes(ISymbolicExpressionTree tree) { 108 if (tree == null) throw new ArgumentNullException("tree"); 109 110 var nodes = new List<ISymbolicExpressionTreeNode>(); 111 foreach (var node in tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) { 112 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 113 VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase; 114 FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode; 115 if (constantTreeNode != null) nodes.Add(constantTreeNode); 116 else if (variableTreeNodeBase != null) nodes.Add(variableTreeNodeBase); 117 else if (factorVarTreeNode != null) nodes.Add(variableTreeNodeBase); 118 else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 119 } 120 return nodes; 121 } 122 123 /// <summary> 124 /// Extracts all numeric constants from a symbolic expression tree. 125 /// </summary> 126 /// <param name="tree">The tree from which the numeric constants should be extracted.</param> 127 /// <param name="addLinearScalingConstants">Flag to determine whether constants for linear scaling have to be added at the end. 128 /// α *f(x) + β, α = 1.0, β = 0.0 </param> 129 /// <returns> An array containing the numeric constants.</returns> 130 public static double[] ExtractConstants(ISymbolicExpressionTree tree, bool addLinearScalingConstants) { 131 if (tree == null) throw new ArgumentNullException("tree"); 132 return ExtractConstants(tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>(), addLinearScalingConstants); 133 } 134 135 /// <summary> 136 /// Extracts all numeric constants from a list of nodes. 137 /// </summary> 138 /// <param name="nodes">The list of nodes for which the numeric constants should be extracted.</param> 139 /// <param name="addLinearScalingConstants">Flag to determine whether constants for linear scaling have to be added at the end. 140 /// α *f(x) + β, α = 1.0, β = 0.0 </param> 141 /// <returns> An array containing the numeric constants.</returns> 142 public static double[] ExtractConstants(IEnumerable<ISymbolicExpressionTreeNode> nodes, bool addLinearScalingConstants) { 143 if (nodes == null) throw new ArgumentNullException("nodes"); 144 80 145 var constants = new List<double>(); 81 foreach (var node in tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {146 foreach (var node in nodes) { 82 147 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 83 148 VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase; … … 90 155 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 91 156 constants.Add(factorVarTreeNode.Weights[j]); 92 } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 93 } 157 } else throw new NotSupportedException(string.Format("Nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 158 } 159 constants.Add(1.0); 160 constants.Add(0.0); 94 161 return constants.ToArray(); 95 162 } 96 163 97 public static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) { 164 /// <summary> 165 /// Sets the numeric constants of the nodes to the provided values. 166 /// </summary> 167 /// <param name="nodes">The nodes whose constants should be updated.</param> 168 /// <param name="constants">The numeric constants which should be set. </param> 169 public static void UpdateConstants(IEnumerable<ISymbolicExpressionTreeNode> nodes, double[] constants) { 170 if (nodes == null) throw new ArgumentNullException("nodes"); 171 if (constants == null) throw new ArgumentNullException("constants"); 172 98 173 int i = 0; 99 foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {174 foreach (var node in nodes) { 100 175 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 101 176 VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase; … … 111 186 } 112 187 } 188 189 /// <summary> 190 /// Sets all numeric constants of the symbolic expression tree to the provided values. 191 /// </summary> 192 /// <param name="tree">The tree for which the numeric constants should be updated.</param> 193 /// <param name="constants">The numeric constants which should be set.</param> 194 public static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) { 195 if (tree == null) throw new ArgumentNullException("tree"); 196 if (constants == null) throw new ArgumentNullException("constants"); 197 UpdateConstants(tree.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>(), constants); 198 } 113 199 } 114 200 }
Note: See TracChangeset
for help on using the changeset viewer.