Index: /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs
===================================================================
--- /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs	(revision 12567)
+++ /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs	(revision 12568)
@@ -87,5 +87,5 @@
             {
                 // TODO: Add code when content has been changed and is not null
-                viewHost2.Content = CalculateNodeImportance(CalculateAdjacencyMatrix());     
+                viewHost2.Content = CalculateNodeImportance(CalculateAdjacencyMatrix());
                 var adjMatrix = CalculateAdjacencyMatrix();
                 viewHost1.Content = adjMatrix;
@@ -110,5 +110,6 @@
         {
             var runCollection = Content;
-            var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
+            var inputVariables = ((IRegressionProblemData)runCollection.First().Parameters["ProblemData"]).InputVariables.Select(x => x.Value).ToList();
+            var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).OrderBy(x => inputVariables.IndexOf(x.Key)).ToList();
 
             var allVariableImpacts = runCollection.Select(run => (DoubleMatrix)run.Results[variableImpactResultName]);
@@ -116,25 +117,47 @@
                                  from variableName in variableImpact.RowNames
                                  select variableName).Distinct().ToArray();
+           
+            var vars = new List<Tuple<int, string>>();
+            
+            var allowedInputs = ((IRegressionProblemData)groupRunCollection[0].First().Parameters["ProblemData"]).AllowedInputVariables.ToList();
+
             var adjMatrix = new DoubleMatrix(variableNames.Length, variableNames.Length);
 
-            adjMatrix.RowNames = groupRunCollection.Select(x => x.Key);
+            for (int i = 0; i < adjMatrix.Rows; ++i)
+            {
+                int inputIndex = allowedInputs.FindIndex(x => x == variableNames[i]);
+                vars.Add(new Tuple<int, string>(inputIndex, variableNames[i]));
+            }
+
+            vars.Sort((a, b) => a.Item1.CompareTo(b.Item1));
+
+            for (int i = 0; i < adjMatrix.Rows; ++i)
+            {
+                variableNames[i] = vars[i].Item2;
+            }
+            adjMatrix.RowNames = variableNames;
             adjMatrix.ColumnNames = adjMatrix.RowNames;
+
             for (int j = 0; j < groupRunCollection.Count; ++j)
             {
                 var g = groupRunCollection[j];
+                
                 var matrix = CalculateAdjacencyRows(g);
-                var variables = new List<Tuple<string, double>>();
-                var columnNames = matrix.ColumnNames.ToList();
+                var variables = new List<double>();
 
                 for (int i = 0; i < matrix.Columns; ++i)
                 {
-                    variables.Add(new Tuple<string, double>(columnNames[i], matrix[0, i]));
-                }
-                variables.Add(new Tuple<string, double>(g.Key, 0));
-                variables.Sort((a, b) => a.Item1.CompareTo(b.Item1));
+                    variables.Add(matrix[0, i]);
+                }
+                
                 for (int i = 0; i < variables.Count; ++i)
                 {
-                    adjMatrix[j, i] = variables[i].Item2;
-
+                    if (i == j)
+                    {
+                        adjMatrix[i, i] = 0;
+                        variables.Insert(i, 0);
+                    }
+                    else
+                        adjMatrix[j, i] = variables[i];
                 }
             }
@@ -144,19 +167,9 @@
         private DoubleMatrix CalculateAdjacencyRows(IEnumerable<IRun> runs)
         {
-            IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs
-                                                            select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
-            var variableNames = (from variableImpact in allVariableImpacts
-                                 from variableName in variableImpact.RowNames
-                                 select variableName).Distinct().ToArray();
-
-            List<string> variableNamesList = (from variableName in variableNames
-                                              where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
-                                              select variableName)
-                                             .ToList();
-
             var runNames = runs.Select(x => x.Name).ToArray();
             var runsArray = runs.ToArray();
             DoubleMatrix varImpactMatrix = CalculateVariableImpactMatrix(runsArray, runNames);
-            var targetMatrix = new DoubleMatrix(1, variableNames.Length);
+          
+            var targetMatrix = new DoubleMatrix(1, varImpactMatrix.Rows);
 
             for (int i = 0; i < varImpactMatrix.Rows; ++i)
@@ -165,7 +178,6 @@
             }
 
-            targetMatrix.RowNames = new[] { "Target" };
-            targetMatrix.ColumnNames = variableNames;
-
+            targetMatrix.RowNames = new[] { "Impacts" };
+            targetMatrix.ColumnNames = varImpactMatrix.RowNames;
             return targetMatrix;
         }
@@ -262,5 +274,5 @@
         }
 
-        //adapted from RunCollectionVariableImpactView
+        //modified from RunCollectionVariableImpactView
         private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames)
         {
@@ -307,5 +319,44 @@
                 }
             }
-            return matrix;
+
+            // sort by median
+            var sortedMatrix = (DoubleMatrix)matrix.Clone();
+            var sortedIndexes = from i in Enumerable.Range(0, sortedMatrix.Rows)
+                                orderby matrix[i, numberOfRuns]
+                                select i;
+
+            int targetIndex = 0;
+            foreach (var sourceIndex in sortedIndexes)
+            {
+                for (int c = 0; c < matrix.Columns; c++)
+                    sortedMatrix[targetIndex, c] = matrix[sourceIndex, c];
+                targetIndex++;
+            }
+            sortedMatrix.RowNames = sortedIndexes.Select(i => variableNamesList[i]);
+
+            var vars = new List<Tuple<int, string, double>>();
+            var rowNames = sortedMatrix.RowNames.ToList();
+
+            var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
+            var inputs = ((IRegressionProblemData)groupRunCollection[0].First().Parameters["ProblemData"]).InputVariables.ToList();
+            List<string> inp = (from input in inputs 
+                                select (input.ToString())).ToList();
+
+            for (int i = 0; i < sortedMatrix.Rows; ++i)
+            {
+                int inputIndex = inp.FindIndex(x => x == rowNames[i]);
+                vars.Add(new Tuple<int, string, double>(inputIndex, rowNames[i], sortedMatrix[i, runNames.Length]));
+            }
+
+                vars.Sort((a, b) => a.Item1.CompareTo(b.Item1));
+               
+            for (int i = 0; i < sortedMatrix.Rows; ++i)
+            {
+                sortedMatrix[i, runNames.Length] = vars[i].Item3;
+                rowNames[i] = vars[i].Item2;
+            }
+            sortedMatrix.RowNames = rowNames;
+
+            return sortedMatrix;
         }
 
Index: /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/Plugin.cs.frame
===================================================================
--- /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/Plugin.cs.frame	(revision 12567)
+++ /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/Plugin.cs.frame	(revision 12568)
@@ -47,5 +47,5 @@
   [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic", "3.4")]
   [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")]
-  [PluginDependency("HeuristicLab.Problems.Instances", "3.4")]
+  [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]
   [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")]  
   public class Plugin : PluginBase {
Index: /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs
===================================================================
--- /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs	(revision 12567)
+++ /branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs	(revision 12568)
@@ -11,4 +11,5 @@
 using HeuristicLab.Problems.DataAnalysis.Symbolic;
 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
 
 namespace HeuristicLab.VariableInteractionNetworks
@@ -33,8 +34,9 @@
         private const string LastGenerationsParameterName = "LastGenerations";
         private const string MaximumGenerationsParameterName = "MaximumGenerations";
-        private const string OptimizedConstantsParameterName = "OptimizedConstants";
-        
-        private readonly SymbolicDataAnalysisExpressionTreeSimplifier simplifier;
-        private readonly SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator;
+        private const string OptimizeConstantsParameterName = "OptimizeConstants";
+        private const string PruneTreesParameterName = "PruneTrees";
+
+        private SymbolicDataAnalysisExpressionTreeSimplifier simplifier;
+        private SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator;
 
         #region parameters
@@ -87,7 +89,11 @@
             get { return (IFixedValueParameter<IntValue>)Parameters[LastGenerationsParameterName]; }
         }
-        public IFixedValueParameter<BoolValue> OptimizedParameter
-        {
-            get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizedConstantsParameterName]; }
+        public IFixedValueParameter<BoolValue> OptimizeConstantsParameter
+        {
+            get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
+        }
+        public IFixedValueParameter<BoolValue> PruneTreesParameter
+        {
+            get { return (IFixedValueParameter<BoolValue>)Parameters[PruneTreesParameterName]; }
         }
         private ILookupParameter<IntValue> MaximumGenerationsParameter
@@ -119,13 +125,14 @@
             Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
             Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
-            Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName));         
+            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>(OptimizedConstantsParameterName));
+            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();
-            impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
         }
 
@@ -133,7 +140,16 @@
         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();
         }
 
@@ -158,7 +174,5 @@
             int lastGen = LastGenerationsParameter.Value.Value;
 
-            if (lastGen > maxGen)
-                lastGen = maxGen;
-            if (maxGen - gen < lastGen)
+            if (gen < maxGen - lastGen)
                 return base.Apply();
 
@@ -176,5 +190,6 @@
             var estimationLimits = EstimationLimitsParameter.ActualValue; // lookup parameter => ActualValue
             var percentageBest = PercentageBestParameter.Value.Value;
-            var optimizedConstants = OptimizedParameter.Value.Value;
+            var optimizeConstants = OptimizeConstantsParameter.Value.Value;
+            var pruneTrees = PruneTreesParameter.Value.Value;
 
             var allowedInputVariables = problemData.AllowedInputVariables.ToList();
@@ -188,5 +203,5 @@
                 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)
                 {
@@ -197,14 +212,19 @@
             datatable = VariableImpactsParameter.ActualValue;
             int nTrees = (int)Math.Round(trees.Length * percentageBest);
-
-            // simplify trees
-            var simplifiedTrees = trees.Take(nTrees).Select(x => simplifier.Simplify(x));
+            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)));
-            foreach (var simplifiedTree in simplifiedTrees)
-            {
-                if (optimizedConstants == true)
-                {
-                    SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTree, problemData, problemData.TrainingIndices, applyLinearScaling, maxCOIterations, estimationLimits.Upper, estimationLimits.Lower);
-                }
+            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);
@@ -216,4 +236,6 @@
                 {
                     var avgImpact = g.Average(x => impactsCalculator.CalculateImpactValue(model, x, problemData, problemData.TrainingIndices, quality));
+                    if (double.IsNaN(avgImpact))
+                        avgImpact = 0;
                     variableImpacts[g.Key] += avgImpact;
                 }
@@ -222,5 +244,6 @@
             foreach (var pair in variableImpacts)
             {
-                datatable.Rows[pair.Key].Values.Add(pair.Value / variableCounts[pair.Key]);
+                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;
