Changeset 16463
- Timestamp:
- 12/28/18 17:57:13 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16461 r16463 133 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms, Dictionary<DataForVariable, AutoDiff.Variable> parameters, 134 134 out ParametricFunction func, 135 out ParametricFunctionGradient func_grad 135 out ParametricFunctionGradient func_grad, 136 out double[] initialConstants 136 137 ) { 137 138 138 // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree 139 139 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, parameters); 140 140 AutoDiff.Term term; 141 141 try { 142 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 142 143 143 144 144 if (addLinearScalingTerms) { … … 146 146 var alpha = new AutoDiff.Variable(); 147 147 var beta = new AutoDiff.Variable(); 148 transformator.variables. Insert(0, alpha);149 transformator.variables. Insert(0, beta);150 148 transformator.variables.Add(beta); 149 transformator.variables.Add(alpha); 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 151 151 term = term * alpha + beta; 152 } 153 154 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Select(kvp => kvp.Value).ToArray());155 156 152 } else { 153 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 154 } 155 156 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Values.ToArray()); 157 157 func = (vars, @params) => compiledTerm.Evaluate(vars, @params); 158 158 func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params); 159 initialConstants = transformator.initialConstants.ToArray(); 160 159 161 return true; 160 162 } catch (ConversionException) { 161 163 func = null; 162 164 func_grad = null; 165 initialConstants = null; 163 166 } 164 167 return false;
Note: See TracChangeset
for help on using the changeset viewer.