Changeset 16457 for branches/2974_Constants_Optimization
- Timestamp:
- 12/28/18 10:18:39 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16360 r16457 98 98 99 99 // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree 100 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable , addLinearScalingTerms);100 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable); 101 101 AutoDiff.Term term; 102 102 try { 103 103 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 104 105 if (addLinearScalingTerms) { 106 // scaling variables α, β are given at the beginning of the parameter vector 107 var alpha = new AutoDiff.Variable(); 108 var beta = new AutoDiff.Variable(); 109 transformator.variables.Insert(0, alpha); 110 transformator.variables.Insert(0, beta); 111 112 term = term * alpha + beta; 113 } 114 104 115 var parameterEntries = transformator.parameters.ToArray(); // guarantee same order for keys and values 105 116 var compiledTerm = term.Compile(transformator.variables.ToArray(), 106 117 parameterEntries.Select(kvp => kvp.Value).ToArray()); 118 107 119 parameters = new List<DataForVariable>(parameterEntries.Select(kvp => kvp.Key)); 108 120 initialConstants = transformator.initialConstants.ToArray(); … … 111 123 return true; 112 124 } catch (ConversionException) { 125 parameters = null; 126 initialConstants = null; 113 127 func = null; 114 128 func_grad = null; 115 parameters = null;116 initialConstants = null;117 129 } 118 130 return false; … … 122 134 private readonly 123 135 List<double> initialConstants; 124 private readonlyDictionary<DataForVariable, AutoDiff.Variable> parameters;136 private Dictionary<DataForVariable, AutoDiff.Variable> parameters; 125 137 private readonly List<AutoDiff.Variable> variables; 126 138 private readonly bool makeVariableWeightsVariable; 127 private readonly bool addLinearScalingTerms; 128 129 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms) { 139 140 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) { 130 141 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 131 this.addLinearScalingTerms = addLinearScalingTerms;132 142 this.initialConstants = new List<double>(); 133 143 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); … … 249 259 if (node.Symbol is CubeRoot) { 250 260 return AutoDiff.TermBuilder.Power( 251 ConvertToAutoDiff(node.GetSubtree(0)), 1.0 /3.0);261 ConvertToAutoDiff(node.GetSubtree(0)), 1.0 / 3.0); 252 262 } 253 263 if (node.Symbol is Sine) { … … 272 282 } 273 283 if (node.Symbol is StartSymbol) { 274 if (addLinearScalingTerms) { 275 // scaling variables α, β are given at the beginning of the parameter vector 276 var alpha = new AutoDiff.Variable(); 277 var beta = new AutoDiff.Variable(); 278 variables.Add(beta); 279 variables.Add(alpha); 280 var t = ConvertToAutoDiff(node.GetSubtree(0)); 281 return t * alpha + beta; 282 } else return ConvertToAutoDiff(node.GetSubtree(0)); 284 return ConvertToAutoDiff(node.GetSubtree(0)); 283 285 } 284 286 throw new ConversionException();
Note: See TracChangeset
for help on using the changeset viewer.