- Timestamp:
- 04/03/14 16:21:07 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowBestSolutionsCollection.cs
r10720 r10721 35 35 [Storable] 36 36 private List<SlidingWindowRange> slidingWindowRanges; 37 37 38 public List<SlidingWindowRange> SlidingWindowRanges { 38 39 get { return slidingWindowRanges; } … … 55 56 [Storable] 56 57 private Dictionary<SlidingWindowRange, ISymbolicExpressionTree> slidingWindowBestSolutions; 58 57 59 public Dictionary<SlidingWindowRange, ISymbolicExpressionTree> SlidingWindowBestSolutions { 58 60 get { return slidingWindowBestSolutions; } … … 62 64 [Storable] 63 65 private IDataAnalysisProblemData problemData; 66 64 67 public IDataAnalysisProblemData ProblemData { 65 68 get { return problemData; } … … 69 72 [Storable] 70 73 private ISymbolicDataAnalysisExpressionTreeInterpreter interpreter; 74 71 75 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { 72 76 get { return interpreter; } … … 76 80 [Storable] 77 81 private bool applyLinearScaling; 82 78 83 public bool ApplyLinearScaling { 79 84 get { return applyLinearScaling; } … … 98 103 99 104 private QualityMeasures qualityMeasure; 105 100 106 public QualityMeasures QualityMeasure { 101 107 get { return qualityMeasure; } … … 122 128 } 123 129 130 public event EventHandler QualitiesCalculationStarted; 131 132 private void OnQualitiesCalculationStarted(object sender, EventArgs e) { 133 var started = QualitiesCalculationStarted; 134 if (started != null) { 135 started(sender, e); 136 } 137 } 138 124 139 public event EventHandler QualitiesUpdated; 125 140 private void OnQualitiesUpdated(object sender, EventArgs e) { 126 141 var updated = QualitiesUpdated; 127 if (updated != null) { updated(sender, e); } 142 if (updated != null) { 143 updated(sender, e); 144 } 128 145 } 129 146 130 147 [StorableConstructor] 131 protected SlidingWindowBestSolutionsCollection(bool deserializing) : base(deserializing) { } 148 protected SlidingWindowBestSolutionsCollection(bool deserializing) 149 : base(deserializing) { 150 } 151 132 152 protected SlidingWindowBestSolutionsCollection(SlidingWindowBestSolutionsCollection original, Cloner cloner) 133 153 : base(original, cloner) { … … 137 157 this.applyLinearScaling = original.ApplyLinearScaling; 138 158 } 159 139 160 protected SlidingWindowBestSolutionsCollection() { 140 161 slidingWindowBestSolutions = new Dictionary<SlidingWindowRange, ISymbolicExpressionTree>(); … … 154 175 155 176 public ISymbolicExpressionTree this[SlidingWindowRange key] { 156 get { 157 return slidingWindowBestSolutions[key]; 158 } 177 get { return slidingWindowBestSolutions[key]; } 159 178 set { 160 179 AddSolution(key, value); // this should be fast so there's no need for a background worker … … 177 196 } 178 197 179 public abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 180 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue); 181 182 public abstract ISymbolicDataAnalysisSolution CreateSolution(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData); 198 public abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, 199 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 200 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue); 201 202 public abstract ISymbolicDataAnalysisSolution CreateSolution(ISymbolicDataAnalysisModel model, 203 IDataAnalysisProblemData problemData); 183 204 184 205 private void AddSolution(SlidingWindowRange range, ISymbolicExpressionTree solution) { 185 // add a solution and update the qualities matrix186 206 Add(range, solution); 187 207 … … 193 213 var trainingIndices = problemData.TrainingIndices.ToList(); 194 214 var matrix = new double[nRows, nCols]; 195 List<int> rows; 215 196 216 // copy old qualities into the new matrix 197 217 for (int i = 0; i < nRows - 1; ++i) { … … 201 221 } 202 222 // copy qualities of new solution into the new matrix 223 var rows = Enumerable.Range(slidingWindowRanges.First().Start, slidingWindowRanges.Last().End - slidingWindowRanges.First().Start).ToList(); 224 var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).ToList(); 225 var originalValues = ProblemData.Dataset.GetDoubleValues(GetTargetVariable(ProblemData), rows).ToList(); 203 226 for (int i = 0; i < nCols; ++i) { 204 rows = i == nCols - 1 ? trainingIndices : Enumerable.Range(slidingWindowRanges[i].Start, slidingWindowRanges[i].Size).ToList(); 205 matrix[nRows - 1, i] = CalculateQuality(solution, rows); 227 if (i == nCols - 1) { 228 matrix[nRows - 1, i] = CalculateQuality(solution, trainingIndices); 229 } else { 230 var indices = Enumerable.Range(slidingWindowRanges[i].Start, slidingWindowRanges[i].Size).ToList(); 231 var estimated = indices.Select(x => estimatedValues[x]); 232 var original = indices.Select(x => originalValues[x]); 233 matrix[nRows - 1, i] = CalculateQuality(estimated, original); 234 } 206 235 } 207 236 // shift old training qualities one column to the right … … 223 252 } 224 253 254 OnQualitiesCalculationStarted(this, EventArgs.Empty); 255 225 256 var ranges = SlidingWindowRanges; 226 257 var solutions = ranges.Select(x => SlidingWindowBestSolutions[x]).ToList(); 227 258 228 int rows = solutions.Count; 229 int columns = ranges.Count + 1; 230 231 SlidingWindowQualities = new double[rows, columns]; 232 233 for (int i = 0; i < rows; ++i) { 259 int nRows = solutions.Count; 260 int nCols = ranges.Count + 1; 261 262 SlidingWindowQualities = new double[nRows, nCols]; 263 var rows = Enumerable.Range(ranges.First().Start, ranges.Last().End - ranges.First().Start).ToList(); 264 var originalValues = ProblemData.Dataset.GetDoubleValues(GetTargetVariable(ProblemData), rows).ToList(); 265 266 for (int i = 0; i < nRows; ++i) { 234 267 if (worker.CancellationPending) { 235 268 e.Cancel = true; … … 238 271 239 272 var solution = solutions[i]; 240 for (int j = 0; j < columns; ++j) { 241 var range = (j == columns - 1) ? ProblemData.TrainingIndices : Enumerable.Range(ranges[j].Start, ranges[j].Size); 242 var q = CalculateQuality(solution, range); 273 var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).ToList(); 274 275 for (int j = 0; j < nCols; ++j) { 276 double q; 277 if (j == nCols - 1) { 278 q = CalculateQuality(solution, problemData.TrainingIndices); 279 } else { 280 var range = ranges[j]; 281 var indices = Enumerable.Range(range.Start, range.Size).ToList(); 282 var estimated = indices.Select(x => estimatedValues[x]); 283 var original = indices.Select(x => originalValues[x]); 284 285 q = CalculateQuality(estimated, original); 286 } 243 287 244 288 SlidingWindowQualities[i, j] = q; 245 289 } 246 290 247 worker.ReportProgress((int)Math.Round(i * 100.0 / rows));291 worker.ReportProgress((int)Math.Round(i * 100.0 / nRows)); 248 292 } 249 293 } … … 259 303 if (classificationProblemData != null) return classificationProblemData.TargetVariable; 260 304 throw new NotSupportedException(); 305 } 306 307 private double CalculateQuality(IEnumerable<double> estimatedValues, IEnumerable<double> originalValues) { 308 var errorState = OnlineCalculatorError.None; 309 double quality = 0.0; 310 switch (QualityMeasure) { 311 case QualityMeasures.PEARSON: 312 quality = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState); 313 break; 314 case QualityMeasures.MSE: 315 quality = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, originalValues, out errorState); 316 break; 317 } 318 return errorState == OnlineCalculatorError.None ? quality : double.NaN; 261 319 } 262 320
Note: See TracChangeset
for help on using the changeset viewer.