Changeset 16500 for branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 01/04/19 17:37:13 (6 years ago)
- Location:
- branches/2974_Constants_Optimization
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization
- Property svn:ignore
-
old new 1 1 packages 2 TestResults
-
- Property svn:ignore
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Constants Optimization/Util.cs
r16461 r16500 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 27 using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter; … … 48 49 } 49 50 50 public static Dictionary<DataForVariable, AutoDiff.Variable> ExtractParameters(IDataset dataset) {51 var parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();51 public static List<DataForVariable> GenerateVariables(IDataset dataset) { 52 var variables = new List<DataForVariable>(); 52 53 foreach (var doubleVariable in dataset.DoubleVariables) { 53 54 var data = new DataForVariable(doubleVariable, string.Empty, 0); 54 var param = new AutoDiff.Variable(); 55 parameters.Add(data, param); 55 variables.Add(data); 56 56 } 57 57 … … 59 59 foreach (var stringValue in dataset.GetStringValues(stringVariable).Distinct()) { 60 60 var data = new DataForVariable(stringVariable, stringValue, 0); 61 var param = new AutoDiff.Variable(); 62 parameters.Add(data, param); 61 variables.Add(data); 63 62 } 64 63 } 65 return parameters; 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(); 66 77 } 67 78 … … 79 90 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 80 91 constants.Add(factorVarTreeNode.Weights[j]); 81 } 92 } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 82 93 } 83 94 return constants.ToArray(); … … 97 108 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 98 109 factorVarTreeNode.Weights[j] = constants[i++]; 99 } 110 } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 100 111 } 101 112 } -
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16463 r16500 131 131 } 132 132 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms, Dictionary<DataForVariable, AutoDiff.Variable> parameters, 134 out ParametricFunction func, 135 out ParametricFunctionGradient func_grad, 136 out double[] initialConstants 137 ) { 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool addLinearScalingTerms, IEnumerable<DataForVariable> variables, 134 out IParametricCompiledTerm autoDiffTerm, out double[] initialConstants) { 138 135 // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree 139 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, parameters); 136 //TODO change ctor 137 var transformator = new TreeToAutoDiffTermConverter(true); 138 var parameters = new AutoDiff.Variable[variables.Count()]; 139 140 int i = 0; 141 foreach(var variable in variables) { 142 var autoDiffVar = new AutoDiff.Variable(); 143 transformator.parameters.Add(variable, autoDiffVar); 144 parameters[i] = autoDiffVar; 145 i++; 146 } 147 140 148 AutoDiff.Term term; 141 149 try { 142 143 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 144 151 if (addLinearScalingTerms) { 145 // scaling variables α, β are given at the beginningof the parameter vector152 // scaling variables α, β are given at the end of the parameter vector 146 153 var alpha = new AutoDiff.Variable(); 147 154 var beta = new AutoDiff.Variable(); 155 156 term = term * alpha + beta; 157 158 transformator.variables.Add(alpha); 148 159 transformator.variables.Add(beta); 149 transformator.variables.Add(alpha); 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 151 term = term * alpha + beta; 152 } else { 153 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 154 } 155 156 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Values.ToArray()); 157 func = (vars, @params) => compiledTerm.Evaluate(vars, @params); 158 func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params); 160 161 transformator.initialConstants.Add(1.0); 162 transformator.initialConstants.Add(0.0); 163 } 164 165 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters); 166 autoDiffTerm = compiledTerm; 159 167 initialConstants = transformator.initialConstants.ToArray(); 160 168 161 169 return true; 162 170 } catch (ConversionException) { 163 func = null; 164 func_grad = null; 171 autoDiffTerm = null; 165 172 initialConstants = null; 166 173 } … … 174 181 private readonly bool makeVariableWeightsVariable; 175 182 176 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable , Dictionary<DataForVariable, AutoDiff.Variable> parameters = null) {183 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) { 177 184 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 178 185 this.initialConstants = new List<double>(); 179 if (parameters == null) 180 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); 181 else 182 this.parameters = parameters; 186 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); 183 187 this.variables = new List<AutoDiff.Variable>(); 184 188 } -
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16460 r16500 213 213 <SubType>Code</SubType> 214 214 </Compile> 215 <Compile Include="Constants Optimization\IConstantsOptimizer.cs" /> 216 <Compile Include="Constants Optimization\LMConstantsOptimizer.cs" /> 215 217 <Compile Include="Constants Optimization\Util.cs" /> 216 218 <Compile Include="Converters\LinearModelToTreeConverter.cs" />
Note: See TracChangeset
for help on using the changeset viewer.