- Timestamp:
- 11/03/17 17:07:58 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r15447 r15448 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Parameters; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 41 42 private const string UpdateVariableWeightsParameterName = "Update Variable Weights"; 42 43 44 private const string FunctionEvaluationsResultParameterName = "Constants Optimization Function Evaluations"; 45 private const string GradientEvaluationsResultParameterName = "Constants Optimization Gradient Evaluations"; 46 43 47 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 44 48 get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; } … … 58 62 public IFixedValueParameter<BoolValue> UpdateVariableWeightsParameter { 59 63 get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateVariableWeightsParameterName]; } 64 } 65 66 public IResultParameter<IntValue> FunctionEvaluationsResultParameter { 67 get { return (IResultParameter<IntValue>)Parameters[FunctionEvaluationsResultParameterName]; } 68 } 69 public IResultParameter<IntValue> GradientEvaluationsResultParameter { 70 get { return (IResultParameter<IntValue>)Parameters[GradientEvaluationsResultParameterName]; } 60 71 } 61 72 … … 100 111 Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true }); 101 112 Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized.", new BoolValue(true)) { Hidden = true }); 113 114 Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue())); 115 Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue())); 102 116 } 103 117 … … 112 126 if (!Parameters.ContainsKey(UpdateVariableWeightsParameterName)) 113 127 Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized.", new BoolValue(true))); 114 } 115 128 129 if (!Parameters.ContainsKey(FunctionEvaluationsResultParameterName)) 130 Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue())); 131 if (!Parameters.ContainsKey(GradientEvaluationsResultParameterName)) 132 Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue())); 133 } 134 135 private static readonly object locker = new object(); 116 136 public override IOperation InstrumentedApply() { 117 137 var solution = SymbolicExpressionTreeParameter.ActualValue; … … 119 139 if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) { 120 140 IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value); 141 var counter = new EvaluationsCounter(); 121 142 quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue, 122 constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree );143 constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter); 123 144 124 145 if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { … … 126 147 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value); 127 148 } 149 150 lock (locker) { 151 FunctionEvaluationsResultParameter.ActualValue.Value += counter.FunctionEvaluations; 152 GradientEvaluationsResultParameter.ActualValue.Value += counter.GradientEvaluations; 153 } 154 128 155 } else { 129 156 var evaluationRows = GenerateRowsToEvaluate(); … … 139 166 EstimationLimitsParameter.ExecutionContext = context; 140 167 ApplyLinearScalingParameter.ExecutionContext = context; 168 FunctionEvaluationsResultParameter.ExecutionContext = context; 169 GradientEvaluationsResultParameter.ExecutionContext = context; 141 170 142 171 // Pearson R² evaluator is used on purpose instead of the const-opt evaluator, … … 148 177 EstimationLimitsParameter.ExecutionContext = null; 149 178 ApplyLinearScalingParameter.ExecutionContext = null; 179 FunctionEvaluationsResultParameter.ExecutionContext = null; 180 GradientEvaluationsResultParameter.ExecutionContext = null; 150 181 151 182 return r2; 183 } 184 185 public class EvaluationsCounter { 186 public int FunctionEvaluations = 0; 187 public int GradientEvaluations = 0; 152 188 } 153 189 … … 156 192 int maxIterations, bool updateVariableWeights = true, 157 193 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue, 158 bool updateConstantsInTree = true, Action<double[], double, object> iterationCallback = null ) {194 bool updateConstantsInTree = true, Action<double[], double, object> iterationCallback = null, EvaluationsCounter counter = null) { 159 195 160 196 // numeric constants in the tree become variables for constant opt … … 171 207 throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree."); 172 208 if (parameters.Count == 0) return 0.0; // gkronber: constant expressions always have a R² of 0.0 173 174 209 var parameterEntries = parameters.ToArray(); // order of entries must be the same for x 175 210 … … 188 223 189 224 double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 225 226 if (counter == null) counter = new EvaluationsCounter(); 227 var rowEvaluationsCounter = new EvaluationsCounter(); 190 228 191 229 alglib.lsfitstate state; … … 222 260 alglib.lsfitsetxrep(state, iterationCallback != null); 223 261 //alglib.lsfitsetgradientcheck(state, 0.001); 224 alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, xrep, null);262 alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, xrep, rowEvaluationsCounter); 225 263 alglib.lsfitresults(state, out retVal, out c, out rep); 226 264 } catch (ArithmeticException) { … … 229 267 return originalQuality; 230 268 } 269 270 counter.FunctionEvaluations += rowEvaluationsCounter.FunctionEvaluations / n; 271 counter.GradientEvaluations += rowEvaluationsCounter.GradientEvaluations / n; 231 272 232 273 //retVal == -7 => constant optimization failed due to wrong gradient … … 266 307 return (double[] c, double[] x, ref double fx, object o) => { 267 308 fx = func(c, x); 309 var counter = (EvaluationsCounter)o; 310 counter.FunctionEvaluations++; 268 311 }; 269 312 } … … 274 317 fx = tupel.Item2; 275 318 Array.Copy(tupel.Item1, grad, grad.Length); 319 var counter = (EvaluationsCounter)o; 320 counter.GradientEvaluations++; 276 321 }; 277 322 }
Note: See TracChangeset
for help on using the changeset viewer.