Index: /branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FineGrainedStructuralPopulationDiversityAnalyzer.cs
===================================================================
--- /branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FineGrainedStructuralPopulationDiversityAnalyzer.cs	(revision 4933)
+++ /branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FineGrainedStructuralPopulationDiversityAnalyzer.cs	(revision 4934)
@@ -62,5 +62,5 @@
       IList<GeneticInformationItem>[] geneticInformationItemsList = new List<GeneticInformationItem>[n];
       for (int i = 0; i < n; i++) {
-        geneticInformationItemsList[i] = GeneticInformationItem.getGeneticInformationItems(solutions[i].Root, variableNames);
+        geneticInformationItemsList[i] = GeneticInformationItem.getGeneticInformationItems(solutions[i].Root, variableNames, 1, 5);
       }
       double[,] result = new double[n, n];
@@ -96,7 +96,7 @@
       }
 
-      private int myLevelDelta;
-      public int LevelDelta {
-        get { return myLevelDelta; }
+      private int myAncestorLevel;
+      public int AncestorLevel {
+        get { return myAncestorLevel; }
       }
 
@@ -138,4 +138,18 @@
       public int DescendantTimeOffset {
         get { return myDescendantTimeOffset; }
+      }
+
+      private SymbolicExpressionTreeNode myDescendantTreeNode;
+      public SymbolicExpressionTreeNode DescendantTreeNode {
+        get { return myDescendantTreeNode; }
+      }
+
+      private int myDescendantLevel;
+      public int DescendantLevel {
+        get { return myDescendantLevel; }
+      }
+
+      public int LevelDelta {
+        get { return (myDescendantLevel - myAncestorLevel); }
       }
 
@@ -369,24 +383,45 @@
       }*/
 
-      public static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node, List<string> variableNames) {
+      public static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node, List<string> variableNames,
+          int MinimumLevelDelta, int MaximumLevelDelta) {
+        // first we have to collect all items, then we filter; it is not possible to filter while collecting because the items are
+        // collected recursively and used for collecting the parents' items.
+        if (MinimumLevelDelta > MaximumLevelDelta)
+          return new List<GeneticInformationItem>();
+        IList<GeneticInformationItem> list = getGeneticInformationItems(node, variableNames, 0);
+        List<GeneticInformationItem> resultList = new List<GeneticInformationItem>();
+        foreach (GeneticInformationItem item in list)
+          if (item.LevelDelta >= MinimumLevelDelta && item.LevelDelta <= MaximumLevelDelta)
+            resultList.Add(item);
+        return resultList;
+      }
+
+      private static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node, List<string> variableNames, int level) {
         // Idea: collect all descendants' lists and then add new items using the retrieved ones.
         // This should save lots of time and reduce complexity of the items retrieval process.
+        // Program roots are not considered, neither are start symbol nodes
         if (node.Symbol is ProgramRootSymbol)
-          return getGeneticInformationItems(node.SubTrees[0], variableNames);
+          return getGeneticInformationItems(node.SubTrees[0], variableNames, level + 1);
         List<GeneticInformationItem> list = new List<GeneticInformationItem>();
         // add item for this node:
-        list.Add(new GeneticInformationItem(node, variableNames));
-        // add items for the descendants:
+        if (!(node.Symbol is StartSymbol)) {
+          list.Add(new GeneticInformationItem(node, variableNames, level));
+        }
+        // add items for the descendants, but prevent multiple references to descendant nodes:
+        List<SymbolicExpressionTreeNode> descendantNodes = new List<SymbolicExpressionTreeNode>();
         for (int i = 0; i < node.SubTrees.Count; i++) {
-          IList<GeneticInformationItem> descendantItems = getGeneticInformationItems(node.SubTrees[i], variableNames);
+          IList<GeneticInformationItem> descendantItems = getGeneticInformationItems(node.SubTrees[i], variableNames, level + 1);
           list.AddRange(descendantItems);
-          foreach (GeneticInformationItem item in descendantItems) {
-            list.Add(new GeneticInformationItem(node, item, i));
-          }
+          if (!(node.Symbol is StartSymbol))
+            foreach (GeneticInformationItem item in descendantItems) {
+              if (!descendantNodes.Contains(item.DescendantTreeNode))
+                list.Add(new GeneticInformationItem(node, item, i, level));
+              descendantNodes.Add(item.DescendantTreeNode);
+            }
         }
         return list;
       }
 
-      private GeneticInformationItem (SymbolicExpressionTreeNode node, List<string> variableNames) {
+      private GeneticInformationItem (SymbolicExpressionTreeNode node, List<string> variableNames, int level) {
         myAncestorIndex = -1;
         VariableTreeNode variableTreeNode = node as VariableTreeNode;
@@ -409,10 +444,13 @@
         else
           myDescendantVariableIndex = -1;
-        myLevelDelta = 0;
-      }
-
-      private GeneticInformationItem(SymbolicExpressionTreeNode parentNode, GeneticInformationItem descendantGeneticInformationItem, int ancestorIndex) {
+        myAncestorLevel = level;
+        myDescendantLevel = level;
+        myDescendantTreeNode = node;
+      }
+
+      private GeneticInformationItem(SymbolicExpressionTreeNode parentNode, GeneticInformationItem descendantGeneticInformationItem,
+          int ancestorIndex, int parentNodeLevel) {
         myAncestorIndex = ancestorIndex;
-        myLevelDelta = descendantGeneticInformationItem.LevelDelta + 1;
+        myAncestorLevel = parentNodeLevel;
         myAncestorDefinition = parentNode.Symbol.GetType();
         myDescendantCoefficient = descendantGeneticInformationItem.DescendantCoefficient;
@@ -420,10 +458,13 @@
         myDescendantTimeOffset = descendantGeneticInformationItem.DescendantTimeOffset;
         myDescendantVariableIndex = descendantGeneticInformationItem.DescendantVariableIndex;
+        myDescendantLevel = descendantGeneticInformationItem.DescendantLevel;
+        myDescendantTreeNode = descendantGeneticInformationItem.DescendantTreeNode;
       }
 
       public override string ToString() {
-        return "ancestor: " + AncestorDefinition.Name.ToString() + ", [" + AncestorIndex + "], delta " + LevelDelta
-          + "; descendant: " + DescendantDefinition.Name.ToString() + " (varIndex " + DescendantVariableIndex + ", "
-          + DescendantCoefficient + ", t-" + DescendantTimeOffset + ")";
+        return "ancestor: " + AncestorDefinition.Name.ToString() + ", [" + AncestorIndex + "]; "
+          + "descendant: " + DescendantDefinition.Name.ToString() + " (varIndex " + DescendantVariableIndex + ", "
+          + DescendantCoefficient + ", t-" + DescendantTimeOffset + ");"
+          + " level delta = " + DescendantLevel + "-" + AncestorLevel + " = " + LevelDelta;
       }
 
