Changeset 8938 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective
- Timestamp:
- 11/22/12 16:26:15 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r8828 r8938 41 41 private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree"; 42 42 43 private const string EvaluatedTreesResultName = "EvaluatedTrees";44 private const string EvaluatedTreeNodesResultName = "EvaluatedTreeNodes";45 46 public ILookupParameter<IntValue> EvaluatedTreesParameter {47 get { return (ILookupParameter<IntValue>)Parameters[EvaluatedTreesResultName]; }48 }49 public ILookupParameter<IntValue> EvaluatedTreeNodesParameter {50 get { return (ILookupParameter<IntValue>)Parameters[EvaluatedTreeNodesResultName]; }51 }52 53 43 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 54 44 get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; } … … 95 85 public SymbolicRegressionConstantOptimizationEvaluator() 96 86 : base() { 97 Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue( 3), true));87 Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10), true)); 98 88 Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0), true)); 99 89 Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true)); 100 90 Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true)); 101 91 Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true))); 102 103 Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreesResultName));104 Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreeNodesResultName));105 92 } 106 93 … … 116 103 117 104 public override IOperation Apply() { 118 AddResults();119 105 var solution = SymbolicExpressionTreeParameter.ActualValue; 120 106 double quality; … … 123 109 quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue, 124 110 constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, 125 EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, UpdateConstantsInTree ,126 EvaluatedTreesParameter.ActualValue, EvaluatedTreeNodesParameter.ActualValue); 111 EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, UpdateConstantsInTree); 112 127 113 if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { 128 114 var evaluationRows = GenerateRowsToEvaluate(); … … 134 120 } 135 121 QualityParameter.ActualValue = new DoubleValue(quality); 136 lock (locker) {137 EvaluatedTreesParameter.ActualValue.Value += 1;138 EvaluatedTreeNodesParameter.ActualValue.Value += solution.Length;139 }140 122 141 123 if (Successor != null) … … 143 125 else 144 126 return null; 145 }146 147 private object locker = new object();148 private void AddResults() {149 lock (locker) {150 if (EvaluatedTreesParameter.ActualValue == null) {151 var scope = ExecutionContext.Scope;152 while (scope.Parent != null)153 scope = scope.Parent;154 scope.Variables.Add(new Core.Variable(EvaluatedTreesResultName, new IntValue()));155 }156 if (EvaluatedTreeNodesParameter.ActualValue == null) {157 var scope = ExecutionContext.Scope;158 while (scope.Parent != null)159 scope = scope.Parent;160 scope.Variables.Add(new Core.Variable(EvaluatedTreeNodesResultName, new IntValue()));161 }162 }163 127 } 164 128 … … 204 168 205 169 public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, 206 IEnumerable<int> rows, bool applyLinearScaling, int maxIterations, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, bool updateConstantsInTree = true , IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {170 IEnumerable<int> rows, bool applyLinearScaling, int maxIterations, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, bool updateConstantsInTree = true) { 207 171 208 172 List<AutoDiff.Variable> variables = new List<AutoDiff.Variable>(); … … 234 198 } 235 199 } 200 double[] originalConstants = (double[])c.Clone(); 201 double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 236 202 237 203 alglib.lsfitstate state; … … 258 224 try { 259 225 alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state); 260 alglib.lsfitsetcond(state, 0, 0, maxIterations); 226 alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations); 227 //alglib.lsfitsetgradientcheck(state, 0.001); 261 228 alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null); 262 229 alglib.lsfitresults(state, out info, out c, out rep); 263 264 230 } 265 231 catch (ArithmeticException) { 266 return 0.0;232 return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 267 233 } 268 234 catch (alglib.alglibexception) { 269 return 0.0; 270 } 271 var newTree = tree; 272 if (!updateConstantsInTree) newTree = (ISymbolicExpressionTree)tree.Clone(); 273 { 274 // only when no error occurred 275 // set constants in tree 276 int i = 2; 277 foreach (var node in newTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) { 278 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 279 VariableTreeNode variableTreeNode = node as VariableTreeNode; 280 if (constantTreeNode != null) 281 constantTreeNode.Value = c[i++]; 282 else if (variableTreeNode != null) 283 variableTreeNode.Weight = c[i++]; 284 } 285 286 } 287 return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, newTree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 235 return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 236 } 237 238 //info == -7 => constant optimization failed due to wrong gradient 239 if (info != -7) UpdateConstants(tree, c.Skip(2).ToArray()); 240 var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 241 242 if (!updateConstantsInTree) UpdateConstants(tree, originalConstants.Skip(2).ToArray()); 243 if (originalQuality - quality > 0.001 || double.IsNaN(quality)) { 244 UpdateConstants(tree, originalConstants.Skip(2).ToArray()); 245 return originalQuality; 246 } 247 return quality; 248 } 249 250 private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) { 251 int i = 0; 252 foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) { 253 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 254 VariableTreeNode variableTreeNode = node as VariableTreeNode; 255 if (constantTreeNode != null) 256 constantTreeNode.Value = constants[i++]; 257 else if (variableTreeNode != null) 258 variableTreeNode.Weight = constants[i++]; 259 } 288 260 } 289 261
Note: See TracChangeset
for help on using the changeset viewer.