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

Last change on this file since 14428 was 14428, checked in by bburlacu, 5 years ago

#2635: Add analyzer for counting the AdjustedEvaluatedSolutions (according to the actual number of evaluated rows). Add option to preserve compatibility with the standard evaluator. Optimize performance.

File size: 5.4 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<SymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
20      get { return (ILookupParameter<SymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
21    }
22
23    public OSGAPredictionCountsAnalyzer() {
24      Parameters.Add(new LookupParameter<SymbolicRegressionSingleObjectiveEvaluator>(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 as SymbolicRegressionSingleObjectiveOsgaEvaluator;
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, rejectedStatsPerGenerationTable, nonRejectedStatsPerGenerationTable;
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        rejectedStatsPerGenerationTable = new DataTable("Rejected Per Generation");
60        nonRejectedStatsPerGenerationTable = new DataTable("Non Rejected Stats per Generation");
61        foreach (var rowName in rejectedStats.RowNames) {
62          rejectedStatsPerGenerationTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Line, LineWidth = 1 } });
63          rejectedStatsPerGenerationTable.Rows[rowName].Values.Add(0d);
64          nonRejectedStatsPerGenerationTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Line, LineWidth = 1 } });
65          nonRejectedStatsPerGenerationTable.Rows[rowName].Values.Add(0d);
66        }
67        predictionResults.Add(new Result("Rejected Stats Per Generation", rejectedStatsPerGenerationTable));
68        predictionResults.Add(new Result("Non Rejected Stats Per Generation", nonRejectedStatsPerGenerationTable));
69        ResultCollection.Add(new Result("OS Prediction", predictionResults));
70      } else {
71        predictionResults = (ResultCollection)ResultCollection["OS Prediction"].Value;
72        rejectedStatsTable = (DataTable)predictionResults["Rejected Stats"].Value;
73        totalStatsTable = (DataTable)predictionResults["Total Stats"].Value;
74        rejectedStatsPerGenerationTable = (DataTable)predictionResults["Rejected Stats Per Generation"].Value;
75        nonRejectedStatsPerGenerationTable = (DataTable)predictionResults["Non Rejected Stats Per Generation"].Value;
76      }
77
78      int i = 0;
79      foreach (var rowName in rejectedStats.RowNames) {
80        // pad values with a 0 on each side to prevent clipping
81        rejectedStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, rejectedStats.Columns).Select(j => (double)rejectedStats[i, j])).Concat(new[] { 0d }));
82        ++i;
83      }
84      i = 0;
85      foreach (var rowName in totalStats.RowNames) {
86        totalStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, totalStats.Columns).Select(j => (double)totalStats[i, j])).Concat(new[] { 0d }));
87        var row = rejectedStatsPerGenerationTable.Rows[rowName];
88        var sum = row.Values.Sum();
89        row.Values.Add(totalStats[i, 0] - sum);
90        row = nonRejectedStatsPerGenerationTable.Rows[rowName];
91        sum = row.Values.Sum();
92        row.Values.Add(totalStats[i, 1] - sum);
93        ++i;
94      }
95      return base.Apply();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.