Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorDistributionAnalyzer.cs @ 15287

Last change on this file since 15287 was 15275, checked in by pkimmesw, 7 years ago

#2665 Added PlushEncoding, ZeroErrorDistributionAnalzer

File size: 5.4 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer {
2  using System.Linq;
3
4  using HeuristicLab.Analysis;
5  using HeuristicLab.Common;
6  using HeuristicLab.Core;
7  using HeuristicLab.Data;
8  using HeuristicLab.Operators;
9  using HeuristicLab.Optimization;
10  using HeuristicLab.Parameters;
11  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem;
13
14  /// <summary>
15  /// An operater that tracks the count of individuals with zero error on the cases
16  /// </summary>
17  [Item("IndividualZeroErrorDistributionAnalyzer", "An operater that tracks the distribution of individuals with zero error on the cases")]
18  [StorableClass]
19  public class IndividualZeroErrorDistributionAnalyzer : SingleSuccessorOperator, IIndividualZeroErrorDistributionAnalyzer {
20    private const string RESULTS_PARAMETER_NAME = "Results";
21    private const string INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistribution";
22
23    private const string RESULT_PARAMETER_NAME = "Zero Error Individual Per Case Distribution";
24    private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population.";
25    private const string Y_AXIS_TITLE = "Number of zero error cases";
26    private const string X_AXIS_TITLE = "Number of individuals";
27    private const string ROW_NAME = "Cases";
28
29    public IndividualZeroErrorDistributionAnalyzer() {
30      Parameters.Add(new LookupParameter<DataTable>(
31        INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME,
32        "The data table to store the count of individuals with zero error."));
33
34      Parameters.Add(new LookupParameter<ResultCollection>(
35        RESULTS_PARAMETER_NAME,
36        "The result collection where the symbol frequencies should be stored."));
37
38      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(
39       IntegerVectorPushProblem.CaseQualitiesScopeParameterName,
40       "The quality of every single training case for each individual."));
41    }
42
43    [StorableConstructorAttribute]
44    public IndividualZeroErrorDistributionAnalyzer(bool deserializing) : base(deserializing) { }
45
46    public IndividualZeroErrorDistributionAnalyzer(IndividualZeroErrorDistributionAnalyzer origin, Cloner cloner) : base(origin, cloner) { }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new IndividualZeroErrorDistributionAnalyzer(this, cloner);
50    }
51
52    public bool EnabledByDefault { get { return true; } }
53
54    public ILookupParameter<DataTable> IndividualZeroErrorDistributionParameter
55    {
56      get { return (ILookupParameter<DataTable>)Parameters[INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME]; }
57    }
58
59    public ILookupParameter<ResultCollection> ResultsParameter
60    {
61      get { return (ILookupParameter<ResultCollection>)Parameters[RESULTS_PARAMETER_NAME]; }
62    }
63
64    public ILookupParameter<ItemArray<DoubleArray>> CaseQualitiesParameter
65    {
66      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters[IntegerVectorPushProblem.CaseQualitiesScopeParameterName]; }
67    }
68
69    public override IOperation Apply() {
70      var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue;
71      var individualCount = caseQualitiesPerIndividual.Length;
72      var caseCount = caseQualitiesPerIndividual[0].Length;
73
74      var results = ResultsParameter.ActualValue;
75      var individualZeroErrorDistribution = IndividualZeroErrorDistributionParameter.ActualValue;
76
77      if (individualZeroErrorDistribution == null) {
78        individualZeroErrorDistribution = new DataTable(
79         RESULT_PARAMETER_NAME,
80         RESULT_PARAMETER_DESCRIPTION) {
81          VisualProperties = {
82            YAxisTitle = Y_AXIS_TITLE,
83            XAxisTitle = X_AXIS_TITLE,
84            XAxisMinimumFixedValue = 0,
85            XAxisMaximumAuto = true,
86            XAxisMinimumAuto = false,
87          }
88        };
89
90        IndividualZeroErrorDistributionParameter.ActualValue = individualZeroErrorDistribution;
91      }
92
93      if (!results.ContainsKey(RESULT_PARAMETER_NAME)) {
94        results.Add(new Result(RESULT_PARAMETER_NAME, individualZeroErrorDistribution));
95      }
96
97      DataRow row;
98      if (!individualZeroErrorDistribution.Rows.TryGetValue(ROW_NAME, out row)) {
99        row = new DataRow(ROW_NAME) {
100          VisualProperties = {
101              StartIndexZero = true,
102              IsVisibleInLegend = false,
103              ChartType = DataRowVisualProperties.DataRowChartType.Histogram,
104            }
105        };
106
107        individualZeroErrorDistribution.Rows.Add(row);
108      }
109
110      var caseCountChanged = row.Values.Count != caseCount;
111
112      if (caseCountChanged) {
113        row.Values.Clear();
114      }
115
116      // key represents the number of cases with zero error
117      // value represents the number of individuals with key cases with zero error
118      var distribution = caseQualitiesPerIndividual
119              .Select(individual => individual.Count(x => x == 0.0))
120              .GroupBy(x => x)
121              .ToDictionary(x => x.Key, x => x.Count());
122
123      for (var i = 0; i < caseCount; i++) {
124        int count;
125        distribution.TryGetValue(i, out count);
126        var x = count / (double)individualCount;
127
128        if (caseCountChanged)
129          row.Values.Add(x);
130        else
131          row.Values[i] = x;
132      }
133
134      return base.Apply();
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.