Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorAnalyzer.cs

Last change on this file was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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