Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/OSGAPredictionCountsAnalyzer.cs @ 14104

Last change on this file since 14104 was 14104, checked in by bburlacu, 6 years ago

#2635: Improve performance and accuracy of evaluator and analyzer.

File size: 3.8 KB
Line 
1using System.Linq;
2using HeuristicLab.Analysis;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Problems.DataAnalysis.Symbolic;
9using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
10
11namespace HeuristicLab.OSGAEvaluator {
12  [Item("OSGAPredictionCountsAnalyzer", "An analyzer which records the counts of the rejected offspring predicted by the evaluator without evaluating all rows.")]
13  [StorableClass]
14
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    }
26
27    protected OSGAPredictionCountsAnalyzer(OSGAPredictionCountsAnalyzer original, Cloner cloner) : base(original, cloner) {
28    }
29
30    public override IDeepCloneable Clone(Cloner cloner) {
31      return new OSGAPredictionCountsAnalyzer(this, cloner);
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    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.