Index: /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs
===================================================================
--- /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs	(revision 13664)
+++ /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs	(revision 13665)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Linq;
 using HeuristicLab.Analysis;
@@ -5,4 +26,5 @@
 using HeuristicLab.Core;
 using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
 using HeuristicLab.Optimization;
 using HeuristicLab.Parameters;
@@ -11,243 +33,235 @@
 using HeuristicLab.Problems.DataAnalysis.Symbolic;
 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
-
-namespace HeuristicLab.VariableInteractionNetworks
-{
-    [Item("SymbolicRegressionVariableImpactsAnalyzer", "An analyzer which calculates variable impacts based on the average node impacts from the tree")]
-    [StorableClass]
-    public class SymbolicRegressionVariableImpactsAnalyzer : SymbolicDataAnalysisAnalyzer
-    {
-        private const string UpdateCounterParameterName = "UpdateCounter";
-        private const string UpdateIntervalParameterName = "UpdateInterval";
-        public const string QualityParameterName = "Quality";
-        private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
-        private const string ProblemDataParameterName = "ProblemData";
-        private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
-        private const string MaxCOIterationsParameterName = "MaxCOIterations";
-        private const string EstimationLimitsParameterName = "EstimationLimits";
-        private const string EvaluatorParameterName = "Evaluator";
-
-        private const string VariableFrequenciesParameterName = "VariableFrequencies";
-        private const string VariableImpactsParameterName = "AverageVariableImpacts";
-        private const string PercentageBestParameterName = "PercentageBest";
-        private const string LastGenerationsParameterName = "LastGenerations";
-        private const string MaximumGenerationsParameterName = "MaximumGenerations";
-        private const string OptimizeConstantsParameterName = "OptimizeConstants";
-        private const string PruneTreesParameterName = "PruneTrees";
-
-        private SymbolicDataAnalysisExpressionTreeSimplifier simplifier;
-        private SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator;
-
-        #region parameters
-        public ValueParameter<IntValue> UpdateCounterParameter
-        {
-            get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
-        }
-        public ValueParameter<IntValue> UpdateIntervalParameter
-        {
-            get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
-        }
-        public IScopeTreeLookupParameter<DoubleValue> QualityParameter
-        {
-            get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
-        }
-        public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter
-        {
-            get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
-        }
-        public ILookupParameter<IRegressionProblemData> ProblemDataParameter
-        {
-            get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
-        }
-        public ILookupParameter<BoolValue> ApplyLinearScalingParameter
-        {
-            get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
-        }
-        public IFixedValueParameter<IntValue> MaxCOIterationsParameter
-        {
-            get { return (IFixedValueParameter<IntValue>)Parameters[MaxCOIterationsParameterName]; }
-        }
-        public ILookupParameter<DoubleLimit> EstimationLimitsParameter
-        {
-            get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
-        }
-        public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter
-        {
-            get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
-        }
-        public ILookupParameter<DataTable> VariableImpactsParameter
-        {
-            get { return (ILookupParameter<DataTable>)Parameters[VariableImpactsParameterName]; }
-        }
-        public IFixedValueParameter<PercentValue> PercentageBestParameter
-        {
-            get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageBestParameterName]; }
-        }
-        public IFixedValueParameter<IntValue> LastGenerationsParameter
-        {
-            get { return (IFixedValueParameter<IntValue>)Parameters[LastGenerationsParameterName]; }
-        }
-        public IFixedValueParameter<BoolValue> OptimizeConstantsParameter
-        {
-            get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
-        }
-        public IFixedValueParameter<BoolValue> PruneTreesParameter
-        {
-            get { return (IFixedValueParameter<BoolValue>)Parameters[PruneTreesParameterName]; }
-        }
-        private ILookupParameter<IntValue> MaximumGenerationsParameter
-        {
-            get { return (ILookupParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
-        }
-        #endregion
-
-        #region parameter properties
-        public int UpdateCounter
-        {
-            get { return UpdateCounterParameter.Value.Value; }
-            set { UpdateCounterParameter.Value.Value = value; }
-        }
-        public int UpdateInterval
-        {
-            get { return UpdateIntervalParameter.Value.Value; }
-            set { UpdateIntervalParameter.Value.Value = value; }
-        }
-        #endregion
-
-        public SymbolicRegressionVariableImpactsAnalyzer()
-        {
-            Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, new IntValue(0)));
-            Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, new IntValue(1)));
-            Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
-            Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName));
-            Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
-            Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
-            Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
-            Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName));
-            Parameters.Add(new LookupParameter<DataTable>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run."));
-            Parameters.Add(new FixedValueParameter<PercentValue>(PercentageBestParameterName));
-            Parameters.Add(new FixedValueParameter<IntValue>(LastGenerationsParameterName));
-            Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, new BoolValue(false)));
-            Parameters.Add(new FixedValueParameter<BoolValue>(PruneTreesParameterName, new BoolValue(false)));
-            Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName, "The maximum number of generations which should be processed."));
-
-            impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
-            simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
-        }
-
-        [StorableConstructor]
-        protected SymbolicRegressionVariableImpactsAnalyzer(bool deserializing) : base(deserializing) { }
-
-        [StorableHook(HookType.AfterDeserialization)]
-        private void AfterDeserialization()
-        {
-            impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
-            simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
-        }
-
-        protected SymbolicRegressionVariableImpactsAnalyzer(SymbolicRegressionVariableImpactsAnalyzer original, Cloner cloner)
-            : base(original, cloner)
-        {
-            impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
-            simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
-        }
-
-        public override IDeepCloneable Clone(Cloner cloner)
-        {
-            return new SymbolicRegressionVariableImpactsAnalyzer(this, cloner);
-        }
-
-        public override IOperation Apply()
-        {
-            #region Update counter & update interval
-            UpdateCounter++;
-            if (UpdateCounter != UpdateInterval)
-            {
-                return base.Apply();
-            }
-            UpdateCounter = 0;
-            #endregion
-            var results = ResultCollectionParameter.ActualValue;
-            int maxGen = MaximumGenerationsParameter.ActualValue.Value;
-            int gen = ((IntValue)results["Generations"].Value).Value;
-            int lastGen = LastGenerationsParameter.Value.Value;
-
-            if (gen < maxGen - lastGen)
-                return base.Apply();
-
-            var trees = SymbolicExpressionTree.ToArray();
-            var qualities = QualityParameter.ActualValue.ToArray();
-
-            Array.Sort(qualities, trees);
-            Array.Reverse(qualities);
-            Array.Reverse(trees);
-
-            var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
-            var problemData = ProblemDataParameter.ActualValue;
-            var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
-            var maxCOIterations = MaxCOIterationsParameter.Value.Value; // fixed value parameter => Value
-            var estimationLimits = EstimationLimitsParameter.ActualValue; // lookup parameter => ActualValue
-            var percentageBest = PercentageBestParameter.Value.Value;
-            var optimizeConstants = OptimizeConstantsParameter.Value.Value;
-            var pruneTrees = PruneTreesParameter.Value.Value;
-
-            var allowedInputVariables = problemData.AllowedInputVariables.ToList();
-            var variableImpacts = allowedInputVariables.ToDictionary(x => x, x => 0.0);
-            DataTable datatable;
-            if (VariableImpactsParameter.ActualValue == null)
-            {
-                datatable = new DataTable("Variable impacts", "Average impact of variables over the population");
-                datatable.VisualProperties.XAxisTitle = "Generation";
-                datatable.VisualProperties.YAxisTitle = "Average variable impact";
-                VariableImpactsParameter.ActualValue = datatable;
-                results.Add(new Result("Average variable impacts", "The relative variable relevance calculated as the average relative variable frequency over the whole run.", new DataTable()));
-
-                foreach (var v in allowedInputVariables)
-                {
-                    datatable.Rows.Add(new DataRow(v) { VisualProperties = { StartIndexZero = true } });
-                }
-                VariableImpactsParameter.ActualValue = datatable;
-            }
-            datatable = VariableImpactsParameter.ActualValue;
-            int nTrees = (int)Math.Round(trees.Length * percentageBest);
-            var bestTrees = trees.Take(nTrees).Select(x => (ISymbolicExpressionTree)x.Clone()).ToList();
-
-            if (optimizeConstants)
-            {
-                foreach (var tree in bestTrees)
-                    SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, maxCOIterations, estimationLimits.Upper, estimationLimits.Lower);
-            }
-            var simplifiedTrees = bestTrees.Select(x => simplifier.Simplify(x)).ToList();
-            var variableCounts = problemData.AllowedInputVariables.ToDictionary(x => x, x => simplifiedTrees.Count(t => t.IterateNodesPrefix().Any(n => n is VariableTreeNode && ((VariableTreeNode)n).VariableName == x)));
-            for (int i = 0; i < simplifiedTrees.Count; ++i)
-            {
-                var simplifiedTree = simplifiedTrees[i];
-
-                if (pruneTrees)
-                    simplifiedTree = SymbolicRegressionPruningOperator.Prune(simplifiedTree, impactsCalculator, interpreter, problemData, estimationLimits, problemData.TrainingIndices);
-
-                var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, simplifiedTree, estimationLimits.Lower, estimationLimits.Upper, problemData, problemData.TrainingIndices, applyLinearScaling);
-
-                var model = new SymbolicRegressionModel(simplifiedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
-                var variables = simplifiedTree.IterateNodesPrefix().Where(x => x is VariableTreeNode).GroupBy(x => ((VariableTreeNode)x).VariableName);
-
-                foreach (var g in variables)
-                {
-                    var avgImpact = g.Average(x => impactsCalculator.CalculateImpactValue(model, x, problemData, problemData.TrainingIndices, quality));
-                    if (double.IsNaN(avgImpact))
-                        avgImpact = 0;
-                    variableImpacts[g.Key] += avgImpact;
-                }
-            }
-
-            foreach (var pair in variableImpacts)
-            {
-                var v = variableCounts[pair.Key] > 0 ? pair.Value / variableCounts[pair.Key] : 0;
-                datatable.Rows[pair.Key].Values.Add(v);
-            }
-            results["Average variable impacts"].Value = datatable;
-            return base.Apply();
-        }
-    }
+
+namespace HeuristicLab.VariableInteractionNetworks {
+  [Item("SymbolicRegressionVariableImpactsAnalyzer", "An analyzer which calculates variable impacts based on the average node impacts from the tree")]
+  [StorableClass]
+  public class SymbolicRegressionVariableImpactsAnalyzer : SymbolicDataAnalysisAnalyzer {
+    private const string UpdateCounterParameterName = "UpdateCounter";
+    private const string UpdateIntervalParameterName = "UpdateInterval";
+    public const string QualityParameterName = "Quality";
+    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
+    private const string ProblemDataParameterName = "ProblemData";
+    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
+    private const string MaxCOIterationsParameterName = "MaxCOIterations";
+    private const string EstimationLimitsParameterName = "EstimationLimits";
+    private const string EvaluatorParameterName = "Evaluator";
+    private const string VariableImpactsParameterName = "AverageVariableImpacts";
+    private const string PercentageBestParameterName = "PercentageBest";
+    private const string LastGenerationsParameterName = "LastGenerations";
+    private const string MaximumGenerationsParameterName = "MaximumGenerations";
+    private const string OptimizeConstantsParameterName = "OptimizeConstants";
+    private const string PruneTreesParameterName = "PruneTrees";
+
+    private SymbolicDataAnalysisExpressionTreeSimplifier simplifier;
+    private SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator;
+
+    #region parameters
+    public ValueParameter<IntValue> UpdateCounterParameter {
+      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
+    }
+    public ValueParameter<IntValue> UpdateIntervalParameter {
+      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
+    }
+    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
+      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
+    }
+    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
+      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
+    }
+    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
+      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
+    }
+    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
+      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
+    }
+    public IFixedValueParameter<IntValue> MaxCOIterationsParameter {
+      get { return (IFixedValueParameter<IntValue>)Parameters[MaxCOIterationsParameterName]; }
+    }
+    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
+      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
+    }
+    public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
+      get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
+    }
+    public ILookupParameter<DataTable> VariableImpactsParameter {
+      get { return (ILookupParameter<DataTable>)Parameters[VariableImpactsParameterName]; }
+    }
+    public IFixedValueParameter<PercentValue> PercentageBestParameter {
+      get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageBestParameterName]; }
+    }
+    public IFixedValueParameter<IntValue> LastGenerationsParameter {
+      get { return (IFixedValueParameter<IntValue>)Parameters[LastGenerationsParameterName]; }
+    }
+    public IFixedValueParameter<BoolValue> OptimizeConstantsParameter {
+      get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
+    }
+    public IFixedValueParameter<BoolValue> PruneTreesParameter {
+      get { return (IFixedValueParameter<BoolValue>)Parameters[PruneTreesParameterName]; }
+    }
+    private ILookupParameter<IntValue> MaximumGenerationsParameter {
+      get { return (ILookupParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
+    }
+    #endregion
+
+    #region parameter properties
+    public int UpdateCounter {
+      get { return UpdateCounterParameter.Value.Value; }
+      set { UpdateCounterParameter.Value.Value = value; }
+    }
+    public int UpdateInterval {
+      get { return UpdateIntervalParameter.Value.Value; }
+      set { UpdateIntervalParameter.Value.Value = value; }
+    }
+    #endregion
+
+    public SymbolicRegressionVariableImpactsAnalyzer() {
+      #region add parameters
+      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, new IntValue(0)));
+      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, new IntValue(1)));
+      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
+      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName));
+      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
+      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
+      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
+      Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName, new IntValue(3)));
+      Parameters.Add(new LookupParameter<DataTable>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run."));
+      Parameters.Add(new FixedValueParameter<PercentValue>(PercentageBestParameterName, new PercentValue(100)));
+      Parameters.Add(new FixedValueParameter<IntValue>(LastGenerationsParameterName, new IntValue(10)));
+      Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, new BoolValue(false)));
+      Parameters.Add(new FixedValueParameter<BoolValue>(PruneTreesParameterName, new BoolValue(false)));
+      Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName, "The maximum number of generations which should be processed."));
+      Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
+      #endregion
+
+      impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
+      simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
+    }
+
+    [StorableConstructor]
+    protected SymbolicRegressionVariableImpactsAnalyzer(bool deserializing) : base(deserializing) { }
+
+    [StorableHook(HookType.AfterDeserialization)]
+    private void AfterDeserialization() {
+      impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
+      simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
+
+      if (!Parameters.ContainsKey(EvaluatorParameterName))
+        Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
+    }
+
+    protected SymbolicRegressionVariableImpactsAnalyzer(SymbolicRegressionVariableImpactsAnalyzer original, Cloner cloner)
+        : base(original, cloner) {
+      impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
+      simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
+    }
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new SymbolicRegressionVariableImpactsAnalyzer(this, cloner);
+    }
+
+    public override IOperation Apply() {
+      #region Update counter & update interval
+      UpdateCounter++;
+      if (UpdateCounter != UpdateInterval) {
+        return base.Apply();
+      }
+      UpdateCounter = 0;
+      #endregion
+      var results = ResultCollectionParameter.ActualValue;
+      int maxGen = MaximumGenerationsParameter.ActualValue.Value;
+      int gen = ((IntValue)results["Generations"].Value).Value;
+      int lastGen = LastGenerationsParameter.Value.Value;
+
+      if (lastGen > 0 && gen < maxGen - lastGen)
+        return base.Apply();
+
+      var trees = SymbolicExpressionTree.ToArray();
+      var qualities = QualityParameter.ActualValue.ToArray();
+
+      Array.Sort(qualities, trees);
+      Array.Reverse(qualities);
+      Array.Reverse(trees);
+
+      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
+      var problemData = ProblemDataParameter.ActualValue;
+      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
+      var constantOptimizationIterations = MaxCOIterationsParameter.Value.Value; // fixed value parameter => Value
+      var estimationLimits = EstimationLimitsParameter.ActualValue; // lookup parameter => ActualValue
+      var percentageBest = PercentageBestParameter.Value.Value;
+      var optimizeConstants = OptimizeConstantsParameter.Value.Value;
+      var pruneTrees = PruneTreesParameter.Value.Value;
+
+      var allowedInputVariables = problemData.AllowedInputVariables.ToList();
+      DataTable dataTable;
+      if (VariableImpactsParameter.ActualValue == null) {
+        dataTable = new DataTable("Variable impacts", "Average impact of variables over the population");
+        dataTable.VisualProperties.XAxisTitle = "Generation";
+        dataTable.VisualProperties.YAxisTitle = "Average variable impact";
+        VariableImpactsParameter.ActualValue = dataTable;
+        results.Add(new Result("Average variable impacts", "The relative variable relevance calculated as the average relative variable frequency over the whole run.", new DataTable()));
+
+        foreach (var v in allowedInputVariables) {
+          dataTable.Rows.Add(new DataRow(v) { VisualProperties = { StartIndexZero = true } });
+        }
+        VariableImpactsParameter.ActualValue = dataTable;
+      }
+      dataTable = VariableImpactsParameter.ActualValue;
+      int nTrees = (int)Math.Round(trees.Length * percentageBest);
+      var bestTrees = trees.Take(nTrees).Select(x => (ISymbolicExpressionTree)x.Clone()).ToList();
+      // simplify trees before doing anything else
+      var simplifiedTrees = bestTrees.Select(x => simplifier.Simplify(x)).ToList();
+
+      if (optimizeConstants) {
+        for (int i = 0; i < simplifiedTrees.Count; ++i) {
+          qualities[i].Value = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTrees[i], problemData, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, estimationLimits.Upper, estimationLimits.Lower);
+        }
+      }
+
+      if (pruneTrees) {
+        for (int i = 0; i < simplifiedTrees.Count; ++i) {
+          simplifiedTrees[i] = SymbolicRegressionPruningOperator.Prune(simplifiedTrees[i], impactsCalculator, interpreter, problemData, estimationLimits, problemData.TrainingIndices);
+        }
+      }
+      // map each variable to a list of indices of the trees that contain it
+      var variablesToTreeIndices = allowedInputVariables.ToDictionary(x => x, x => Enumerable.Range(0, simplifiedTrees.Count).Where(i => ContainsVariable(simplifiedTrees[i], x)).ToList());
+
+      foreach (var mapping in variablesToTreeIndices) {
+        var variableName = mapping.Key;
+        var median = problemData.Dataset.GetDoubleValues(variableName, problemData.TrainingIndices).Median();
+        var ds = new ModifiableDataset(problemData.Dataset.DoubleVariables, problemData.Dataset.DoubleVariables.Select(x => problemData.Dataset.GetReadOnlyDoubleValues(x).ToList()));
+        foreach (var i in problemData.TrainingIndices) {
+          ds.SetVariableValue(median, variableName, i);
+        }
+        var pd = new RegressionProblemData(ds, allowedInputVariables, problemData.TargetVariable);
+        pd.TrainingPartition.Start = problemData.TrainingPartition.Start;
+        pd.TrainingPartition.End = problemData.TrainingPartition.End;
+        pd.TestPartition.Start = problemData.TestPartition.Start;
+        pd.TestPartition.End = problemData.TestPartition.End;
+
+        var indices = mapping.Value;
+        var averageImpact = 0d;
+        for (int i = 0; i < indices.Count; ++i) {
+          var originalQuality = qualities[i].Value;
+          double newQuality;
+          if (optimizeConstants) {
+            newQuality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTrees[i], pd, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, estimationLimits.Upper, estimationLimits.Lower);
+          } else {
+            var evaluator = EvaluatorParameter.ActualValue;
+            newQuality = evaluator.Evaluate(this.ExecutionContext, simplifiedTrees[i], pd, pd.TrainingIndices);
+          }
+          averageImpact += originalQuality - newQuality; // impact calculated this way may be negative
+        }
+        averageImpact /= indices.Count;
+        dataTable.Rows[variableName].Values.Add(averageImpact);
+      }
+
+      results["Average variable impacts"].Value = dataTable;
+      return base.Apply();
+    }
+
+    private static bool ContainsVariable(ISymbolicExpressionTree tree, string variableName) {
+      return tree.IterateNodesPrefix().OfType<VariableTreeNode>().Any(x => x.VariableName == variableName);
+    }
+  }
 }
