Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorDistributionAnalyzer.cs @ 15945

Last change on this file since 15945 was 15289, checked in by pkimmesw, 7 years ago

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 7.8 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    private const string INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistributionHistory";
23
24    private const string HISTORY_TABLE_NAME = "Individual Zero Error Distribution History";
25    private const string RESULT_PARAMETER_NAME = "Zero Error Individual Per Case Distribution";
26    private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population.";
27    private const string Y_AXIS_TITLE = "Number of zero error cases";
28    private const string X_AXIS_TITLE = "Individuals Nr";
29    private const string ROW_NAME = "Cases";
30
31    public IndividualZeroErrorDistributionAnalyzer() {
32      Parameters.Add(new LookupParameter<DataTable>(
33        INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME,
34        "The data table to store the count of individuals with zero error."));
35
36      Parameters.Add(new ValueLookupParameter<DataTableHistory>(
37        INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME,
38        "The data table to store the history."));
39
40      Parameters.Add(new LookupParameter<ResultCollection>(
41        RESULTS_PARAMETER_NAME,
42        "The result collection where the symbol frequencies should be stored."));
43
44      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(
45       IntegerVectorPushProblem.CaseQualitiesScopeParameterName,
46       "The quality of every single training case for each individual."));
47
48      Parameters.Add(new ValueParameter<BoolValue>(
49        "StoreHistory",
50        "True if the history of the analysis should be stored.",
51        new BoolValue(false)));
52
53      Parameters.Add(new ValueParameter<IntValue>(
54        "UpdateInterval",
55        "The interval in which the analysis should be applied.",
56        new IntValue(1)));
57
58      Parameters.Add(new ValueParameter<IntValue>(
59        "UpdateCounter",
60        "The value which counts how many times the operator was called since the last update.",
61        new IntValue(0)));
62
63      IndividualZeroErrorDistributionParameter.Hidden = true;
64      IndividualZeroErrorDistributionHistoryParameter.Hidden = true;
65      ResultsParameter.Hidden = true;
66      UpdateCounterParameter.Hidden = true;
67    }
68
69    [StorableConstructor]
70    public IndividualZeroErrorDistributionAnalyzer(bool deserializing) : base(deserializing) { }
71
72    public IndividualZeroErrorDistributionAnalyzer(IndividualZeroErrorDistributionAnalyzer origin, Cloner cloner) : base(origin, cloner) { }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new IndividualZeroErrorDistributionAnalyzer(this, cloner);
76    }
77
78    public bool EnabledByDefault { get { return true; } }
79
80    public ValueParameter<BoolValue> StoreHistoryParameter
81    {
82      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
83    }
84    public ValueParameter<IntValue> UpdateIntervalParameter
85    {
86      get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
87    }
88    public ValueParameter<IntValue> UpdateCounterParameter
89    {
90      get { return (ValueParameter<IntValue>)Parameters["UpdateCounter"]; }
91    }
92
93    public ILookupParameter<DataTable> IndividualZeroErrorDistributionParameter
94    {
95      get { return (ILookupParameter<DataTable>)Parameters[INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME]; }
96    }
97
98    public ValueLookupParameter<DataTableHistory> IndividualZeroErrorDistributionHistoryParameter
99    {
100      get { return (ValueLookupParameter<DataTableHistory>)Parameters[INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME]; }
101    }
102
103    public ILookupParameter<ResultCollection> ResultsParameter
104    {
105      get { return (ILookupParameter<ResultCollection>)Parameters[RESULTS_PARAMETER_NAME]; }
106    }
107
108    public ILookupParameter<ItemArray<DoubleArray>> CaseQualitiesParameter
109    {
110      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters[IntegerVectorPushProblem.CaseQualitiesScopeParameterName]; }
111    }
112
113    public override IOperation Apply() {
114      UpdateCounterParameter.Value.Value++;
115      // the analyzer runs periodically, every 'updateInterval' times
116      if (UpdateCounterParameter.Value.Value != UpdateIntervalParameter.Value.Value)
117        return base.Apply();
118
119      UpdateCounterParameter.Value.Value = 0; // reset counter
120
121      var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue;
122      var individualCount = caseQualitiesPerIndividual.Length;
123      var caseCount = caseQualitiesPerIndividual[0].Length;
124
125      var results = ResultsParameter.ActualValue;
126      var individualZeroErrorDistribution = IndividualZeroErrorDistributionParameter.ActualValue;
127
128      if (individualZeroErrorDistribution == null) {
129        individualZeroErrorDistribution = new DataTable(
130         RESULT_PARAMETER_NAME,
131         RESULT_PARAMETER_DESCRIPTION) {
132          VisualProperties = {
133            YAxisTitle = Y_AXIS_TITLE,
134            XAxisTitle = X_AXIS_TITLE,
135            YAxisMaximumFixedValue = 1,
136            YAxisMinimumFixedValue = 0,
137            YAxisMaximumAuto = false,
138            YAxisMinimumAuto = false,
139            XAxisMinimumFixedValue = 1,
140            XAxisMaximumFixedValue = individualCount,
141            XAxisMaximumAuto = false,
142            XAxisMinimumAuto = false,
143          }
144        };
145
146        IndividualZeroErrorDistributionParameter.ActualValue = individualZeroErrorDistribution;
147      }
148
149      if (!results.ContainsKey(RESULT_PARAMETER_NAME)) {
150        results.Add(new Result(RESULT_PARAMETER_NAME, individualZeroErrorDistribution));
151      }
152
153      DataRow row;
154      if (!individualZeroErrorDistribution.Rows.TryGetValue(ROW_NAME, out row)) {
155        row = new DataRow(ROW_NAME) {
156          VisualProperties = {
157              StartIndexZero = true,
158              IsVisibleInLegend = false,
159              ChartType = DataRowVisualProperties.DataRowChartType.Columns,
160            }
161        };
162
163        individualZeroErrorDistribution.Rows.Add(row);
164      }
165
166      row.Values.Clear();
167
168      for (var i = 0; i < caseQualitiesPerIndividual.Length; i++) {
169        var count = caseQualitiesPerIndividual[i].Count(q => q == 0.0);
170        var ratio = count / (double)caseCount;
171
172        row.Values.Add(ratio);
173      }
174
175      var storeHistory = StoreHistoryParameter.Value.Value;
176
177      if (storeHistory) {
178        var history = IndividualZeroErrorDistributionHistoryParameter.ActualValue;
179
180        if (history == null) {
181          history = new DataTableHistory();
182          IndividualZeroErrorDistributionHistoryParameter.ActualValue = history;
183        }
184
185        history.Add((DataTable)individualZeroErrorDistribution.Clone());
186
187        if (!results.ContainsKey(HISTORY_TABLE_NAME)) {
188          results.Add(new Result(HISTORY_TABLE_NAME, history));
189        } else {
190          results[HISTORY_TABLE_NAME].Value = history;
191        }
192      }
193
194      return base.Apply();
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.