private SymbolicDataAnalysisVariableFrequencyAnalyzer(SymbolicDataAnalysisVariableFrequencyAnalyzer original, Cloner cloner)
     : base(original, cloner)
 {
 }
        public override IOperation Apply()
        {
            ItemArray <ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
            ResultCollection results = ResultCollection;
            DataTable        datatable;

            if (VariableFrequenciesParameter.ActualValue == null)
            {
                datatable = new DataTable("Variable frequencies", "Relative frequency of variable references aggregated over the whole population.");
                datatable.VisualProperties.XAxisTitle    = "Generation";
                datatable.VisualProperties.YAxisTitle    = "Relative Variable Frequency";
                VariableFrequenciesParameter.ActualValue = datatable;
                results.Add(new Result("Variable frequencies", "Relative frequency of variable references aggregated over the whole population.", datatable));
                results.Add(new Result("Variable impacts", "The relative variable relevance calculated as the average relative variable frequency over the whole run.", new DoubleMatrix()));
            }

            datatable = VariableFrequenciesParameter.ActualValue;
            // all rows must have the same number of values so we can just take the first
            int numberOfValues = datatable.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();

            foreach (var pair in SymbolicDataAnalysisVariableFrequencyAnalyzer.CalculateVariableFrequencies(expressions, AggregateLaggedVariables.Value))
            {
                if (!datatable.Rows.ContainsKey(pair.Key))
                {
                    // initialize a new row for the variable and pad with zeros
                    DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues));
                    row.VisualProperties.StartIndexZero = true;
                    datatable.Rows.Add(row);
                }
                datatable.Rows[pair.Key].Values.Add(Math.Round(pair.Value, 3));
            }

            // add a zero for each data row that was not modified in the previous loop
            foreach (var row in datatable.Rows.Where(r => r.Values.Count != numberOfValues + 1))
            {
                row.Values.Add(0.0);
            }

            // update variable impacts matrix
            var orderedImpacts = (from row in datatable.Rows
                                  select new { Name = row.Name, Impact = Math.Round(datatable.Rows[row.Name].Values.Average(), 3) })
                                 .OrderByDescending(p => p.Impact)
                                 .ToList();
            var impacts = new DoubleMatrix();
            var matrix  = impacts as IStringConvertibleMatrix;

            matrix.Rows        = orderedImpacts.Count;
            matrix.RowNames    = orderedImpacts.Select(x => x.Name);
            matrix.Columns     = 1;
            matrix.ColumnNames = new string[] { "Relative variable relevance" };
            int i = 0;

            foreach (var p in orderedImpacts)
            {
                matrix.SetValue(p.Impact.ToString(), i++, 0);
            }

            VariableImpactsParameter.ActualValue = impacts;
            results["Variable impacts"].Value    = impacts;
            return(base.Apply());
        }
 private SymbolicDataAnalysisVariableFrequencyAnalyzer(SymbolicDataAnalysisVariableFrequencyAnalyzer original, Cloner cloner)
   : base(original, cloner) {
 }