- Timestamp:
- 08/13/18 08:58:58 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/RSquaredEvaluator.cs
r16053 r16073 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 29 30 using HeuristicLab.Problems.DataAnalysis.Symbolic; 30 31 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 32 using HeuristicLab.Random; 31 33 32 34 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { … … 37 39 private readonly string ApplyLinearScalingParameterName = "Apply Linear Scaling"; 38 40 private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations"; 41 private readonly string RestartsParameterName = "Restarts"; 42 private readonly string SeedParameterName = "Seed"; // seed for the random number generator 43 44 private readonly MersenneTwister random = new MersenneTwister(); 39 45 40 46 #region parameter properties … … 49 55 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 50 56 get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; } 57 } 58 59 private IFixedValueParameter<IntValue> RestartsParameter { 60 get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; } 61 } 62 63 private int Restarts { 64 get { return RestartsParameter.Value.Value; } 65 set { RestartsParameter.Value.Value = value; } 51 66 } 52 67 … … 72 87 Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false))); 73 88 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Apply linear scaling on the tree model during evaluation.", new BoolValue(false))); 74 Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10))); 89 Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Number of gradient descent iterations.", new IntValue(10))); 90 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "Number of restarts for gradient descent.", new IntValue(10))); 91 92 var seedParameter = new FixedValueParameter<IntValue>(SeedParameterName, "Seed value for random restarts.", new IntValue(0)); 93 seedParameter.Value.ValueChanged += (sender, args) => random.Seed((uint)seedParameter.Value.Value); 94 random.Seed(0u); 95 96 Parameters.Add(seedParameter); 75 97 } 76 98 … … 87 109 public double Evaluate(IRegressionProblemData problemData, Grammar grammar, SymbolList sentence) { 88 110 var tree = grammar.ParseSymbolicExpressionTree(sentence); 89 return Evaluate(problemData, tree , OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);111 return Evaluate(problemData, tree); 90 112 } 91 113 92 114 public double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree) { 93 return Evaluate(problemData, tree, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);115 return Evaluate(problemData, tree, random, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling, Restarts); 94 116 } 95 117 96 public static double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree, bool optimizeConstants = true, int maxIterations = 10, bool applyLinearScaling = false) { 97 double r2; 98 99 // TODO: Initialize constant values randomly 100 // TODO: Restarts 101 if (optimizeConstants) { 102 r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter, 103 tree, 104 problemData, 105 problemData.TrainingIndices, 106 applyLinearScaling: applyLinearScaling, 107 maxIterations: maxIterations, 108 updateVariableWeights: false, 109 updateConstantsInTree: true); 110 111 foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) { 112 ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode; 113 if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) { 114 constTreeNode.Value = 0.0; 115 } 116 } 117 } else { 118 r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(expressionTreeLinearInterpreter, 118 public static double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree, IRandom random, bool optimizeConstants = true, int maxIterations = 10, bool applyLinearScaling = false, int restarts = 1) { 119 // we begin with an evaluation without constant optimization (relatively small speed penalty compared to const opt) 120 // this value will be used as a baseline to decide if an improvement was achieved via const opt 121 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(expressionTreeLinearInterpreter, 119 122 tree, 120 123 double.MinValue, … … 123 126 problemData.TrainingIndices, 124 127 applyLinearScaling: applyLinearScaling); 128 129 // restart const opt and try to obtain an improved r2 value 130 if (optimizeConstants) { 131 int count = 0; 132 double optimized = r2; 133 do { 134 foreach (var constantNode in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) { 135 constantNode.ResetLocalParameters(random); 136 } 137 138 optimized = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants( 139 expressionTreeLinearInterpreter, 140 tree, 141 problemData, 142 problemData.TrainingIndices, 143 applyLinearScaling, 144 maxIterations, 145 false, 146 double.MinValue, 147 double.MaxValue, 148 true); 149 } while (optimized <= r2 && ++count < restarts); 150 151 // do not update constants if quality is not improved 152 if (optimized > r2) { 153 r2 = optimized; 154 155 // is this code really necessary ? 156 foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) { 157 ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode; 158 if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) { 159 constTreeNode.Value = 0.0; 160 } 161 } 162 } 125 163 } 126 return double.IsNaN(r2) ? 0.0 : r2;164 return double.IsNaN(r2) || double.IsInfinity(r2) ? 0.0 : r2; 127 165 } 128 166 }
Note: See TracChangeset
for help on using the changeset viewer.