Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorAnalyzer.cs @ 17507

Last change on this file since 17507 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

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