Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2635: Update solution to .net framework version 4.6.1, switch to new persistence, add plugin frame file and prebuild event.

File size: 5.5 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 HEAL.Attic;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
30using System.Linq;
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  [StorableType("7730D977-A422-4C64-83C2-8989EA4B3922")]
35  public class OSGAPredictionCountsAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
36    private const string EvaluatorParameterName = "Evaluator";
37    private const string ResultCollectionParameterName = "Results";
38
39    public ILookupParameter<SymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
40      get { return (ILookupParameter<SymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
41    }
42
43    [StorableConstructor]
44    protected OSGAPredictionCountsAnalyzer(StorableConstructorFlag _) : base(_) {
45    }
46
47    public OSGAPredictionCountsAnalyzer() {
48      Parameters.Add(new LookupParameter<SymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
49    }
50
51    protected OSGAPredictionCountsAnalyzer(OSGAPredictionCountsAnalyzer original, Cloner cloner) : base(original, cloner) {
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new OSGAPredictionCountsAnalyzer(this, cloner);
56    }
57
58    public override IOperation Apply() {
59      var evaluator = EvaluatorParameter.ActualValue as SymbolicRegressionSingleObjectiveOsgaEvaluator;
60      if (evaluator == null)
61        return base.Apply();
62
63      var rejectedStats = evaluator.RejectedStats;
64      var totalStats = evaluator.TotalStats;
65
66      if (rejectedStats.Rows == 0 || totalStats.Rows == 0)
67        return base.Apply();
68
69      ResultCollection predictionResults;
70      DataTable rejectedStatsTable, totalStatsTable, rejectedStatsPerGenerationTable;
71      if (!ResultCollection.ContainsKey("OS Prediction")) {
72        predictionResults = new ResultCollection();
73        rejectedStatsTable = new DataTable("Rejected Stats");
74        foreach (var rowName in rejectedStats.RowNames) {
75          rejectedStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
76        }
77        predictionResults.Add(new Result("Rejected Stats", rejectedStatsTable));
78        totalStatsTable = new DataTable("Total Stats");
79        foreach (var rowName in totalStats.RowNames) {
80          totalStatsTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns, LineWidth = 1 } });
81        }
82        predictionResults.Add(new Result("Total Stats", totalStatsTable));
83        rejectedStatsPerGenerationTable = new DataTable("Rejected Per Generation");
84        foreach (var rowName in rejectedStats.RowNames) {
85          rejectedStatsPerGenerationTable.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Line, LineWidth = 1 } });
86          rejectedStatsPerGenerationTable.Rows[rowName].Values.Add(0d);
87        }
88        predictionResults.Add(new Result("Rejected Stats Per Generation", rejectedStatsPerGenerationTable));
89        ResultCollection.Add(new Result("OS Prediction", predictionResults));
90      } else {
91        predictionResults = (ResultCollection)ResultCollection["OS Prediction"].Value;
92        rejectedStatsTable = (DataTable)predictionResults["Rejected Stats"].Value;
93        totalStatsTable = (DataTable)predictionResults["Total Stats"].Value;
94        rejectedStatsPerGenerationTable = (DataTable)predictionResults["Rejected Stats Per Generation"].Value;
95      }
96
97      int i = 0;
98      foreach (var rowName in rejectedStats.RowNames) {
99        // pad values with a 0 on each side to prevent clipping
100        rejectedStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, rejectedStats.Columns).Select(j => (double)rejectedStats[i, j])).Concat(new[] { 0d }));
101        ++i;
102      }
103      i = 0;
104      foreach (var rowName in totalStats.RowNames) {
105        totalStatsTable.Rows[rowName].Values.Replace(new[] { 0d }.Concat(Enumerable.Range(0, totalStats.Columns).Select(j => (double)totalStats[i, j])).Concat(new[] { 0d }));
106        var row = rejectedStatsPerGenerationTable.Rows[rowName];
107        var sum = row.Values.Sum();
108        row.Values.Add(totalStats[i, 0] - sum);
109        ++i;
110      }
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.