Changeset 16508
- Timestamp:
- 01/06/19 18:03:51 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16500 r16508 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 );100 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, addLinearScalingTerms); 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 vector107 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 115 104 var parameterEntries = transformator.parameters.ToArray(); // guarantee same order for keys and values 116 105 var compiledTerm = term.Compile(transformator.variables.ToArray(), 117 106 parameterEntries.Select(kvp => kvp.Value).ToArray()); 118 119 107 parameters = new List<DataForVariable>(parameterEntries.Select(kvp => kvp.Key)); 120 108 initialConstants = transformator.initialConstants.ToArray(); … … 123 111 return true; 124 112 } catch (ConversionException) { 125 parameters = null;126 initialConstants = null;127 113 func = null; 128 114 func_grad = null; 129 } 130 return false; 131 } 132 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool addLinearScalingTerms, IEnumerable<DataForVariable> variables, 134 out IParametricCompiledTerm autoDiffTerm, out double[] initialConstants) { 135 // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree 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 148 AutoDiff.Term term; 149 try { 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 151 if (addLinearScalingTerms) { 152 // scaling variables α, β are given at the end of the parameter vector 153 var alpha = new AutoDiff.Variable(); 154 var beta = new AutoDiff.Variable(); 155 156 term = term * alpha + beta; 157 158 transformator.variables.Add(alpha); 159 transformator.variables.Add(beta); 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; 167 initialConstants = transformator.initialConstants.ToArray(); 168 169 return true; 170 } catch (ConversionException) { 171 autoDiffTerm = null; 115 parameters = null; 172 116 initialConstants = null; 173 117 } … … 180 124 private readonly List<AutoDiff.Variable> variables; 181 125 private readonly bool makeVariableWeightsVariable; 182 183 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) { 126 private readonly bool addLinearScalingTerms; 127 128 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms) { 184 129 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 130 this.addLinearScalingTerms = addLinearScalingTerms; 185 131 this.initialConstants = new List<double>(); 186 132 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); … … 325 271 } 326 272 if (node.Symbol is StartSymbol) { 327 return ConvertToAutoDiff(node.GetSubtree(0)); 273 if (addLinearScalingTerms) { 274 // scaling variables α, β are given at the beginning of the parameter vector 275 var alpha = new AutoDiff.Variable(); 276 var beta = new AutoDiff.Variable(); 277 variables.Add(beta); 278 variables.Add(alpha); 279 var t = ConvertToAutoDiff(node.GetSubtree(0)); 280 return t * alpha + beta; 281 } else return ConvertToAutoDiff(node.GetSubtree(0)); 328 282 } 329 283 throw new ConversionException();
Note: See TracChangeset
for help on using the changeset viewer.