namespace HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer { using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Problem; /// /// An operater that tracks the count of individuals with zero error on the cases /// [Item("IndividualZeroErrorAnalyzer", "An operater that tracks the count of individuals with zero error on the cases")] [StorableClass] public class IndividualZeroErrorAnalyzer : SingleSuccessorOperator, IIndividualZeroErrorAnalyzer { private const string RESULTS_PARAMETER_NAME = "Results"; private const string INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME = "ZeroErrorIndividualsPerCase"; private const string RESULT_PARAMETER_NAME = "Zero Error Individuals Per Case"; private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population."; private const string Y_AXIS_TITLE = "Count of zero error individuals"; private const string X_AXIS_TITLE = "Case Index"; private const string ROW_NAME = "Cases"; public IndividualZeroErrorAnalyzer() { Parameters.Add(new LookupParameter( INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME, "The data table to store the count of individuals with zero error.")); Parameters.Add(new LookupParameter( RESULTS_PARAMETER_NAME, "The result collection where the symbol frequencies should be stored.")); Parameters.Add(new ScopeTreeLookupParameter( IntegerVectorPushProblem.CaseQualitiesScopeParameterName, "The quality of every single training case for each individual.")); } [StorableConstructor] public IndividualZeroErrorAnalyzer(bool deserializing) : base(deserializing) { } public IndividualZeroErrorAnalyzer(IndividualZeroErrorAnalyzer origin, Cloner cloner) : base(origin, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new IndividualZeroErrorAnalyzer(this, cloner); } public bool EnabledByDefault { get { return true; } } public ILookupParameter IndividualZeroErrorParameter { get { return (ILookupParameter)Parameters[INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME]; } } public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters[RESULTS_PARAMETER_NAME]; } } public ILookupParameter> CaseQualitiesParameter { get { return (ILookupParameter>)Parameters[IntegerVectorPushProblem.CaseQualitiesScopeParameterName]; } } public override IOperation Apply() { var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue; var caseCount = caseQualitiesPerIndividual[0].Length; var results = ResultsParameter.ActualValue; var individualZeroErrorCounts = IndividualZeroErrorParameter.ActualValue; if (individualZeroErrorCounts == null) { individualZeroErrorCounts = new DataTable( RESULT_PARAMETER_NAME, RESULT_PARAMETER_DESCRIPTION) { VisualProperties = { YAxisTitle = Y_AXIS_TITLE, XAxisTitle = X_AXIS_TITLE, XAxisMinimumFixedValue = 0, XAxisMaximumFixedValue = caseCount, XAxisMaximumAuto = false, XAxisMinimumAuto = false, } }; IndividualZeroErrorParameter.ActualValue = individualZeroErrorCounts; } if (!results.ContainsKey(RESULT_PARAMETER_NAME)) { results.Add(new Result(RESULT_PARAMETER_NAME, individualZeroErrorCounts)); } DataRow row; if (!individualZeroErrorCounts.Rows.TryGetValue(ROW_NAME, out row)) { row = new DataRow(ROW_NAME) { VisualProperties = { StartIndexZero = true, IsVisibleInLegend = false, ChartType = DataRowVisualProperties.DataRowChartType.Bars, } }; individualZeroErrorCounts.Rows.Add(row); } var caseCountChanged = row.Values.Count != caseCount; if (caseCountChanged) { individualZeroErrorCounts.VisualProperties.XAxisMaximumFixedValue = caseCount; row.Values.Clear(); } for (var i = 0; i < caseCount; i++) { var count = 0; for (var j = 0; j < caseQualitiesPerIndividual.Length; j++) { var caseQuality = caseQualitiesPerIndividual[j][i]; if (caseQuality == 0.0) count++; } if (caseCountChanged) row.Values.Add(count); else row.Values[i] = count; } return base.Apply(); } } }