Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2635_HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/OSGAPredictionCountsAnalyzer.cs @ 16403

Last change on this file since 16403 was 14584, checked in by bburlacu, 8 years ago

#2635: Improve reporting of statistics about rejected individuals.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
31
32namespace HeuristicLab.OSGAEvaluator {
33  [Item("OSGAPredictionCountsAnalyzer", "An analyzer which records the counts of the rejected offspring predicted by the evaluator without evaluating all rows.")]
34  [StorableClass]
35
36  public class OSGAPredictionCountsAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
37    private const string EvaluatorParameterName = "Evaluator";
38    private const string ResultCollectionParameterName = "Results";
39
40    public ILookupParameter<SymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
41      get { return (ILookupParameter<SymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
42    }
43
44    public OSGAPredictionCountsAnalyzer() {
45      Parameters.Add(new LookupParameter<SymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
46    }
47
48    protected OSGAPredictionCountsAnalyzer(OSGAPredictionCountsAnalyzer original, Cloner cloner) : base(original, cloner) {
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new OSGAPredictionCountsAnalyzer(this, cloner);
53    }
54
55    public override IOperation Apply() {
56      var evaluator = EvaluatorParameter.ActualValue as SymbolicRegressionSingleObjectiveOsgaEvaluator;
57      if (evaluator == null)
58        return base.Apply();
59
60      var rejectedStats = evaluator.RejectedStats;
61      var totalStats = evaluator.TotalStats;
62
63      if (rejectedStats.Rows == 0 || totalStats.Rows == 0)
64        return base.Apply();
65
66      ResultCollection predictionResults;
67      DataTable rejectedStatsTable, totalStatsTable, rejectedStatsPerGenerationTable;
68      if (!ResultCollection.ContainsKey("OS Prediction")) {
69        predictionResults = new ResultCollection();
70        rejectedStatsTable = new DataTable("Rejected Stats");
71        foreach (var rowName in rejectedStats.RowNames) {
72          rejectedStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
73        }
74        predictionResults.Add(new Result("Rejected Stats", rejectedStatsTable));
75        totalStatsTable = new DataTable("Total Stats");
76        foreach (var rowName in totalStats.RowNames) {
77          totalStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
78        }
79        predictionResults.Add(new Result("Total Stats", totalStatsTable));
80        rejectedStatsPerGenerationTable = new DataTable("Rejected Per Generation");
81        foreach (var rowName in rejectedStats.RowNames) {
82          rejectedStatsPerGenerationTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Line, LineWidth = 1 } });
83          rejectedStatsPerGenerationTable.Rows[rowName].Values.Add(0d);
84        }
85        predictionResults.Add(new Result("Rejected Stats Per Generation", rejectedStatsPerGenerationTable));
86        ResultCollection.Add(new Result("OS Prediction", predictionResults));
87      } else {
88        predictionResults = (ResultCollection)ResultCollection["OS Prediction"].Value;
89        rejectedStatsTable = (DataTable)predictionResults["Rejected Stats"].Value;
90        totalStatsTable = (DataTable)predictionResults["Total Stats"].Value;
91        rejectedStatsPerGenerationTable = (DataTable)predictionResults["Rejected Stats Per Generation"].Value;
92      }
93
94      int i = 0;
95      foreach (var rowName in rejectedStats.RowNames) {
96        // pad values with a 0 on each side to prevent clipping
97        rejectedStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, rejectedStats.Columns).Select(j => (double)rejectedStats[i, j])).Concat(new[] { 0d }));
98        ++i;
99      }
100      i = 0;
101      foreach (var rowName in totalStats.RowNames) {
102        totalStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, totalStats.Columns).Select(j => (double)totalStats[i, j])).Concat(new[] { 0d }));
103        var row = rejectedStatsPerGenerationTable.Rows[rowName];
104        var sum = row.Values.Sum();
105        row.Values.Add(totalStats[i, 0] - sum);
106        ++i;
107      }
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.