- Timestamp:
- 07/18/16 17:14:33 (8 years ago)
- Location:
- branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/OSGAPredictionCountsAnalyzer.cs
r14072 r14104 1 using HeuristicLab.Common; 1 using System.Linq; 2 using HeuristicLab.Analysis; 3 using HeuristicLab.Common; 2 4 using HeuristicLab.Core; 5 using HeuristicLab.Optimization; 6 using HeuristicLab.Parameters; 3 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 4 8 using HeuristicLab.Problems.DataAnalysis.Symbolic; 9 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 5 10 6 11 namespace HeuristicLab.OSGAEvaluator { … … 9 14 10 15 public class OSGAPredictionCountsAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer { 16 private const string EvaluatorParameterName = "Evaluator"; 17 private const string ResultCollectionParameterName = "Results"; 18 19 public ILookupParameter<SymbolicRegressionSingleObjectiveOsgaEvaluator> EvaluatorParameter { 20 get { return (ILookupParameter<SymbolicRegressionSingleObjectiveOsgaEvaluator>)Parameters[EvaluatorParameterName]; } 21 } 22 23 public OSGAPredictionCountsAnalyzer() { 24 Parameters.Add(new LookupParameter<SymbolicRegressionSingleObjectiveOsgaEvaluator>(EvaluatorParameterName)); 25 } 11 26 12 27 protected OSGAPredictionCountsAnalyzer(OSGAPredictionCountsAnalyzer original, Cloner cloner) : base(original, cloner) { … … 16 31 return new OSGAPredictionCountsAnalyzer(this, cloner); 17 32 } 33 34 public override IOperation Apply() { 35 var evaluator = EvaluatorParameter.ActualValue; 36 if (evaluator == null) 37 return base.Apply(); 38 39 var rejectedStats = evaluator.RejectedStats; 40 var totalStats = evaluator.TotalStats; 41 42 if (rejectedStats.Rows == 0 || totalStats.Rows == 0) 43 return base.Apply(); 44 45 ResultCollection predictionResults; 46 DataTable rejectedStatsTable, totalStatsTable; 47 if (!ResultCollection.ContainsKey("OS Prediction")) { 48 predictionResults = new ResultCollection(); 49 rejectedStatsTable = new DataTable("Rejected Stats"); 50 foreach (var rowName in rejectedStats.RowNames) { 51 rejectedStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } }); 52 } 53 predictionResults.Add(new Result("Rejected Stats", rejectedStatsTable)); 54 totalStatsTable = new DataTable("Total Stats"); 55 foreach (var rowName in totalStats.RowNames) { 56 totalStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } }); 57 } 58 predictionResults.Add(new Result("Total Stats", totalStatsTable)); 59 60 ResultCollection.Add(new Result("OS Prediction", predictionResults)); 61 } else { 62 predictionResults = (ResultCollection)ResultCollection["OS Prediction"].Value; 63 rejectedStatsTable = (DataTable)predictionResults["Rejected Stats"].Value; 64 totalStatsTable = (DataTable)predictionResults["Total Stats"].Value; 65 } 66 67 int i = 0; 68 foreach (var rowName in rejectedStats.RowNames) { 69 // add a padding zero below to prevent columns from being cut off to the left 70 rejectedStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, rejectedStats.Columns).Select(j => (double)rejectedStats[i, j])).Concat(new[] { 0d })); 71 ++i; 72 } 73 i = 0; 74 foreach (var rowName in totalStats.RowNames) { 75 totalStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, totalStats.Columns).Select(j => (double)totalStats[i, j])).Concat(new[] { 0d })); 76 ++i; 77 } 78 return base.Apply(); 79 } 18 80 } 19 81 } -
branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/SymbolicRegressionSingleObjectiveOSGAEvaluator.cs
r14072 r14104 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Analysis;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 44 43 get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; } 45 44 } 46 public IFixedValueParameter<IntValue> CorrectlyRejectedParameter { 47 get { return (IFixedValueParameter<IntValue>)Parameters["CorrectlyRejected"]; } 48 } 49 public IFixedValueParameter<IntValue> IncorrectlyRejectedParameter { 50 get { return (IFixedValueParameter<IntValue>)Parameters["IncorrectlyRejected"]; } 51 } 52 public IFixedValueParameter<IntValue> CorrectlyNotRejectedParameter { 53 get { return (IFixedValueParameter<IntValue>)Parameters["CorrectlyNotRejected"]; } 54 } 55 public IFixedValueParameter<IntValue> IncorrectlyNotRejectedParameter { 56 get { return (IFixedValueParameter<IntValue>)Parameters["IncorrectlyNotRejected"]; } 45 46 public IValueParameter<IntMatrix> RejectedStatsParameter { 47 get { return (IValueParameter<IntMatrix>)Parameters["RejectedStats"]; } 48 } 49 public IValueParameter<IntMatrix> NotRejectedStatsParameter { 50 get { return (IValueParameter<IntMatrix>)Parameters["TotalStats"]; } 57 51 } 58 52 public IValueLookupParameter<DoubleValue> ComparisonFactorParameter { … … 79 73 } 80 74 81 public int CorrectlyRejected { 82 get { return CorrectlyRejectedParameter.Value.Value; } 83 set { CorrectlyRejectedParameter.Value.Value = value; } 84 } 85 86 public int CorrectlyNotRejected { 87 get { return CorrectlyNotRejectedParameter.Value.Value; } 88 set { CorrectlyNotRejectedParameter.Value.Value = value; } 89 } 90 91 public int IncorrectlyRejected { 92 get { return IncorrectlyRejectedParameter.Value.Value; } 93 set { IncorrectlyRejectedParameter.Value.Value = value; } 94 } 95 96 public int IncorrectlyNotRejected { 97 get { return IncorrectlyNotRejectedParameter.Value.Value; } 98 set { IncorrectlyNotRejectedParameter.Value.Value = value; } 75 public IntMatrix RejectedStats { 76 get { return RejectedStatsParameter.Value; } 77 set { RejectedStatsParameter.Value = value; } 78 } 79 80 public IntMatrix TotalStats { 81 get { return NotRejectedStatsParameter.Value; } 82 set { NotRejectedStatsParameter.Value = value; } 99 83 } 100 84 #endregion … … 106 90 public SymbolicRegressionSingleObjectiveOsgaEvaluator() { 107 91 Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them.")); 108 Parameters.Add(new FixedValueParameter<PercentValue>(RelativeParentChildQualityThresholdParameterName, new PercentValue(0. 1)));92 Parameters.Add(new FixedValueParameter<PercentValue>(RelativeParentChildQualityThresholdParameterName, new PercentValue(0.9))); 109 93 Parameters.Add(new FixedValueParameter<PercentValue>(RelativeFitnessEvaluationIntervalSizeParameterName, new PercentValue(0.1))); 110 Parameters.Add(new FixedValueParameter<IntValue>("CorrectlyRejected", new IntValue(0)));111 Parameters.Add(new FixedValueParameter<IntValue>("IncorrectlyRejected", new IntValue(0)));112 Parameters.Add(new FixedValueParameter<IntValue>("CorrectlyNotRejected", new IntValue(0)));113 Parameters.Add(new FixedValueParameter<IntValue>("IncorrectlyNotRejected", new IntValue(0)));114 94 Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName)); 115 95 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentQualities") { ActualName = "Quality" }); 96 Parameters.Add(new ValueParameter<IntMatrix>("RejectedStats", new IntMatrix())); 97 Parameters.Add(new ValueParameter<IntMatrix>("TotalStats", new IntMatrix())); 116 98 } 117 99 … … 123 105 if (!Parameters.ContainsKey("ParentQualities")) 124 106 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentQualities") { ActualName = "Quality" }); 107 108 if (!Parameters.ContainsKey("RejectedStats")) 109 Parameters.Add(new ValueParameter<IntMatrix>("RejectedStats", new IntMatrix())); 110 111 if (!Parameters.ContainsKey("TotalStats")) 112 Parameters.Add(new ValueParameter<IntMatrix>("TotalStats", new IntMatrix())); 125 113 } 126 114 … … 136 124 public override void ClearState() { 137 125 base.ClearState(); 138 CorrectlyNotRejected = 0; 139 CorrectlyRejected = 0; 140 IncorrectlyNotRejected = 0; 141 IncorrectlyRejected = 0; 126 RejectedStats = new IntMatrix(); 127 TotalStats = new IntMatrix(); 142 128 } 143 129 … … 216 202 217 203 var interval = (int)Math.Floor(problemData.TrainingPartition.Size * RelativeFitnessEvaluationIntervalSize); 218 var i = 0; 204 var i = problemData.TrainingPartition.Start; 205 var trainingEnd = problemData.TrainingPartition.End; 219 206 var qualityPerInterval = new List<double>(); 220 207 while (targetValuesEnumerator.MoveNext() && scaledEstimatedValuesEnumerator.MoveNext()) { 221 208 pearsonRCalculator.Add(targetValuesEnumerator.Current, scaledEstimatedValuesEnumerator.Current); 222 209 ++i; 223 if (i % interval == 0 ) {210 if (i % interval == 0 || i == trainingEnd) { 224 211 var q = pearsonRCalculator.ErrorState != OnlineCalculatorError.None ? double.NaN : pearsonRCalculator.R; 225 212 qualityPerInterval.Add(q * q); … … 235 222 var threshold = parentQuality * RelativeParentChildQualityThreshold; 236 223 237 //var predictedRejected = qualityPerInterval.Any(x => double.IsNaN(x) || !(x > threshold));238 239 224 bool predictedRejected = false; 240 241 DataTable table;242 var results = ResultCollectionParameter.ActualValue;243 if (!results.ContainsKey("RejectionCounts")) {244 table = new DataTable("RejectionCounts");245 results.Add(new Result("RejectionCounts", table));246 247 var row = new DataRow("Predicted Rejected") { VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };248 table.Rows.Add(row);249 250 row = new DataRow("Actually Rejected") { VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };251 table.Rows.Add(row);252 253 // row = new DataRow("Actually Not Rejected") { VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Columns, StartIndexZero = true } };254 // row.Values.AddRange(qualityPerInterval.Select(x => 0.0));255 // table.Rows.Add(row);256 //257 // row = new DataRow("Predicted Not Rejected") { VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Columns, StartIndexZero = true } };258 // row.Values.AddRange(qualityPerInterval.Select(x => 0.0));259 // table.Rows.Add(row);260 } else {261 table = (DataTable)results["RejectionCounts"].Value;262 }263 225 264 226 i = 0; … … 272 234 273 235 var actuallyRejected = !(actualQuality > parentQuality); 236 237 if (RejectedStats.Rows == 0 || TotalStats.Rows == 0) { 238 RejectedStats = new IntMatrix(2, qualityPerInterval.Count); 239 RejectedStats.RowNames = new[] { "Predicted", "Actual" }; 240 RejectedStats.ColumnNames = Enumerable.Range(1, RejectedStats.Columns).Select(x => string.Format("0-{0}", Math.Min(trainingEnd, x * interval))); 241 TotalStats = new IntMatrix(2, 2); 242 TotalStats.RowNames = new[] { "Predicted", "Actual" }; 243 TotalStats.ColumnNames = new[] { "Rejected", "Not Rejected" }; 244 } 245 // gather some statistics 274 246 if (predictedRejected) { 275 table.Rows["Predicted Rejected"].Values.Add(i); 276 if (actuallyRejected) 277 table.Rows["Actually Rejected"].Values.Add(i); 278 } 279 // else { 280 // table.Rows["Predicted Not Rejected"].Values[i]++; 281 // if (!actuallyRejected) 282 // table.Rows["Actually Not Rejected"].Values[i]++; 283 // } 284 285 if (predictedRejected) { 286 if (actuallyRejected) { 287 CorrectlyRejected++; 288 } else { 289 IncorrectlyRejected++; 290 } 291 } else { 292 if (actuallyRejected) { 293 IncorrectlyNotRejected++; 294 } else { 295 CorrectlyNotRejected++; 296 } 247 RejectedStats[0, i]++; 248 TotalStats[0, 0]++; 249 } else { 250 TotalStats[0, 1]++; 251 } 252 if (actuallyRejected) { 253 TotalStats[1, 0]++; 254 } else { 255 TotalStats[1, 1]++; 256 } 257 if (predictedRejected && actuallyRejected) { 258 RejectedStats[1, i]++; 297 259 } 298 260 return r * r;
Note: See TracChangeset
for help on using the changeset viewer.