Changeset 16500 for branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters
- Timestamp:
- 01/04/19 17:37:13 (6 years ago)
- Location:
- branches/2974_Constants_Optimization
- Files:
-
- 2 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/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 }
Note: See TracChangeset
for help on using the changeset viewer.