Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorDistributionAnalyzer.cs @ 16752

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

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

File size: 7.7 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis {
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
13  /// <summary>
14  /// An operater that tracks the count of individuals with zero error on the cases
15  /// </summary>
16  [Item("IndividualZeroErrorDistributionAnalyzer", "An operater that tracks the distribution of individuals with zero error on the cases")]
17  [StorableClass]
18  public class IndividualZeroErrorDistributionAnalyzer : SingleSuccessorOperator, IIndividualZeroErrorDistributionAnalyzer {
19    private const string RESULTS_PARAMETER_NAME = "Results";
20    private const string INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistribution";
21    private const string INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistributionHistory";
22
23    private const string HISTORY_TABLE_NAME = "Individual Zero Error Distribution History";
24    private const string RESULT_PARAMETER_NAME = "Zero Error Individual Per Case Distribution";
25    private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population.";
26    private const string Y_AXIS_TITLE = "Number of zero error cases";
27    private const string X_AXIS_TITLE = "Individuals Nr";
28    private const string ROW_NAME = "Cases";
29
30    public IndividualZeroErrorDistributionAnalyzer() {
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      IndividualZeroErrorDistributionParameter.Hidden = true;
63      IndividualZeroErrorDistributionHistoryParameter.Hidden = true;
64      ResultsParameter.Hidden = true;
65      UpdateCounterParameter.Hidden = true;
66    }
67
68    [StorableConstructor]
69    public IndividualZeroErrorDistributionAnalyzer(bool deserializing) : base(deserializing) { }
70
71    public IndividualZeroErrorDistributionAnalyzer(IndividualZeroErrorDistributionAnalyzer origin, Cloner cloner) : base(origin, cloner) { }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new IndividualZeroErrorDistributionAnalyzer(this, cloner);
75    }
76
77    public bool EnabledByDefault { get { return true; } }
78
79    public ValueParameter<BoolValue> StoreHistoryParameter {
80      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
81    }
82    public ValueParameter<IntValue> UpdateIntervalParameter {
83      get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
84    }
85    public ValueParameter<IntValue> UpdateCounterParameter {
86      get { return (ValueParameter<IntValue>)Parameters["UpdateCounter"]; }
87    }
88
89    public ILookupParameter<DataTable> IndividualZeroErrorDistributionParameter {
90      get { return (ILookupParameter<DataTable>)Parameters[INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME]; }
91    }
92
93    public ValueLookupParameter<DataTableHistory> IndividualZeroErrorDistributionHistoryParameter {
94      get { return (ValueLookupParameter<DataTableHistory>)Parameters[INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME]; }
95    }
96
97    public ILookupParameter<ResultCollection> ResultsParameter {
98      get { return (ILookupParameter<ResultCollection>)Parameters[RESULTS_PARAMETER_NAME]; }
99    }
100
101    public ILookupParameter<ItemArray<DoubleArray>> CaseQualitiesParameter {
102      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters[IntegerVectorPushProblem.CaseQualitiesScopeParameterName]; }
103    }
104
105    public override IOperation Apply() {
106      UpdateCounterParameter.Value.Value++;
107      // the analyzer runs periodically, every 'updateInterval' times
108      if (UpdateCounterParameter.Value.Value != UpdateIntervalParameter.Value.Value)
109        return base.Apply();
110
111      UpdateCounterParameter.Value.Value = 0; // reset counter
112
113      var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue;
114      var individualCount = caseQualitiesPerIndividual.Length;
115      var caseCount = caseQualitiesPerIndividual[0].Length;
116
117      var results = ResultsParameter.ActualValue;
118      var individualZeroErrorDistribution = IndividualZeroErrorDistributionParameter.ActualValue;
119
120      if (individualZeroErrorDistribution == null) {
121        individualZeroErrorDistribution = new DataTable(
122         RESULT_PARAMETER_NAME,
123         RESULT_PARAMETER_DESCRIPTION) {
124          VisualProperties = {
125            YAxisTitle = Y_AXIS_TITLE,
126            XAxisTitle = X_AXIS_TITLE,
127            YAxisMaximumFixedValue = 1,
128            YAxisMinimumFixedValue = 0,
129            YAxisMaximumAuto = false,
130            YAxisMinimumAuto = false,
131            XAxisMinimumFixedValue = 1,
132            XAxisMaximumFixedValue = individualCount,
133            XAxisMaximumAuto = false,
134            XAxisMinimumAuto = false,
135          }
136        };
137
138        IndividualZeroErrorDistributionParameter.ActualValue = individualZeroErrorDistribution;
139      }
140
141      if (!results.ContainsKey(RESULT_PARAMETER_NAME)) {
142        results.Add(new Result(RESULT_PARAMETER_NAME, individualZeroErrorDistribution));
143      }
144
145      DataRow row;
146      if (!individualZeroErrorDistribution.Rows.TryGetValue(ROW_NAME, out row)) {
147        row = new DataRow(ROW_NAME) {
148          VisualProperties = {
149              StartIndexZero = true,
150              IsVisibleInLegend = false,
151              ChartType = DataRowVisualProperties.DataRowChartType.Columns,
152            }
153        };
154
155        individualZeroErrorDistribution.Rows.Add(row);
156      }
157
158      row.Values.Clear();
159
160      for (var i = 0; i < caseQualitiesPerIndividual.Length; i++) {
161        var count = caseQualitiesPerIndividual[i].Count(q => q == 0.0);
162        var ratio = count / (double)caseCount;
163
164        row.Values.Add(ratio);
165      }
166
167      var storeHistory = StoreHistoryParameter.Value.Value;
168
169      if (storeHistory) {
170        var history = IndividualZeroErrorDistributionHistoryParameter.ActualValue;
171
172        if (history == null) {
173          history = new DataTableHistory();
174          IndividualZeroErrorDistributionHistoryParameter.ActualValue = history;
175        }
176
177        history.Add((DataTable)individualZeroErrorDistribution.Clone());
178
179        if (!results.ContainsKey(HISTORY_TABLE_NAME)) {
180          results.Add(new Result(HISTORY_TABLE_NAME, history));
181        } else {
182          results[HISTORY_TABLE_NAME].Value = history;
183        }
184      }
185
186      return base.Apply();
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.