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 4937)
+++ /branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FineGrainedStructuralPopulationDiversityAnalyzer.cs	(revision 4938)
@@ -44,8 +44,22 @@
   public sealed class FineGrainedStructuralPopulationDiversityAnalyzer : SymbolicRegressionPopulationDiversityAnalyzer {
 
+    // properties: min level delts, max level delta, etc.
+
+    private const string FunctionTreeGrammarParameterName = "FunctionTreeGrammar";
+
+    public IValueLookupParameter<GlobalSymbolicExpressionGrammar> FunctionTreeGrammarParameter {
+      get { return (IValueLookupParameter<GlobalSymbolicExpressionGrammar>)Parameters[FunctionTreeGrammarParameterName]; }
+    }
+    public GlobalSymbolicExpressionGrammar FunctionTreeGrammar {
+      get { return FunctionTreeGrammarParameter.ActualValue; }
+    }
+
+
     [StorableConstructor]
     private FineGrainedStructuralPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
     private FineGrainedStructuralPopulationDiversityAnalyzer(FineGrainedStructuralPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
-    public FineGrainedStructuralPopulationDiversityAnalyzer() : base() { }
+    public FineGrainedStructuralPopulationDiversityAnalyzer() : base() {
+      Parameters.Add(new ValueLookupParameter<GlobalSymbolicExpressionGrammar>(FunctionTreeGrammarParameterName, "The grammar that is used for symbolic regression models."));
+    }
 
     public override IDeepCloneable Clone(Cloner cloner) {
@@ -54,4 +68,11 @@
 
     protected override double[,] CalculateSimilarities(SymbolicExpressionTree[] solutions) {
+      Constant c = null;
+      foreach (Symbol symbol in FunctionTreeGrammar.Symbols) {
+        if (symbol is Constant) {
+          if (c != null) throw new InvalidProgramException("There are more and one constnat definitions in the given function tree grammar.");
+          c = (Constant)symbol;
+        }
+      }
       int n = solutions.Length;
       List<string> variableNames = new List<string>();
@@ -62,5 +83,5 @@
       IList<GeneticInformationItem>[] geneticInformationItemsList = new List<GeneticInformationItem>[n];
       for (int i = 0; i < n; i++) {
-        geneticInformationItemsList[i] = GeneticInformationItem.getGeneticInformationItems(solutions[i].Root, variableNames, 1, 5);
+        geneticInformationItemsList[i] = GeneticInformationItem.getGeneticInformationItems(solutions[i].Root, variableNames, 0, int.MaxValue);
       }
       double[,] result = new double[n, n];
@@ -101,28 +122,4 @@
       }
 
-      // not used in HL 3.3
-      // private double myAncestorCoefficient;
-      // public double AncestorCoefficient {
-      //   get { return myAncestorCoefficient; }
-      // }
-
-      // not used in HL 3.3
-      // private double myAncestorVariableIndex;
-      // public double AncestorVariableIndex {
-      //   get { return myAncestorVariableIndex; }
-      // }
-
-      // not used in HL 3.3
-      // private int myAncestorTimeOffset;
-      // public int AncestorTimeOffset {
-      //   get { return myAncestorTimeOffset; }
-      // }
-
-      // not used in HL 3.3
-      // private int myAncestorVariant;
-      // public int AncestorVariant {
-      //   get { return myAncestorVariant; }
-      // }
-
       private double myDescendantCoefficient;
       public double DescendantCoefficient {
@@ -153,10 +150,4 @@
         get { return (myDescendantLevel - myAncestorLevel); }
       }
-
-      // not used in HL 3.3
-      //private int myDescendantVariant;
-      //public int DescendantVariant {
-      //  get { return myDescendantVariant; }
-      //}
 
       /*
@@ -181,136 +172,97 @@
       }*/
 
-      /*
       public static double Similarity(GeneticInformationItem Item1, GeneticInformationItem Item2,
-          StructuralSimilarityAnalysisParameters Parameters,
-          int MaxTreeHeight, int MaxTimeOffset) {
-
-        if (Item1.AncestorDefinition != Item2.AncestorDefinition ||
-          Item1.DescendantDefinition != Item2.DescendantDefinition)
+          GlobalSymbolicExpressionGrammar ExpressionGrammar, double ConstantMinimum, double ConstantMaximum, double VariableWeightSigma,
+          int MaximumTreeHeight, int MaximumTimeOffset,
+          // StructuralSimilarityAnalysisParameters Parameters,
+          double LevelDifferenceCoefficient,
+          double AncestorIndexCoefficient,
+          double ConstantValueCoefficient,
+          double VariableWeightCoefficient,
+          double TimeOffsetCoefficient,
+          double VariableIndexCoefficient,
+          bool AdditiveSimilarityCalculation
+        ) {
+
+        if (Item1.AncestorDefinition != Item2.AncestorDefinition || Item1.DescendantDefinition != Item2.DescendantDefinition)
           return double.NaN;
 
         // the two items for sure have the same behavior definitions
-        #region init
+
+        #region initialize punishments
+
         double punishmentContributionSum = 0;
         double punishmentCoefficientsProduct = 1;
-        double ancestorCoefficientDifferencePunishment = 0;
-        double ancestorTimeOffsetDifferencePunishment = 0;
-        double ancestorVariantDifferencePunishment = 0;
-        double ancestorVariableIndexDifferencePunishment = 0;
-        double descendantCoefficientDifferencePunishment = 0;
-        double descendantTimeOffsetDifferencePunishment = 0;
-        double descendantVariantDifferencePunishment = 0;
-        double descendantVariableIndexDifferencePunishment = 0;
+
         double ancestorIndexDifferencePunishment = 0;
         double levelDifferencePunishment = 0;
+
+        double descendantConstantValueDifferencePunishment = 0;
+        double descendantVariableWeightDifferencePunishment = 0;
+        double descendantTimeOffsetDifferencePunishment = 0;
+        double descendantVariableIndexDifferencePunishment = 0;
+
         #endregion
 
-        ITerminalDefinition ancestorTerminal = Item1.AncestorDefinition as ITerminalDefinition;
-        ITerminalDefinition descendantTerminal = Item1.DescendantDefinition as ITerminalDefinition;
-        IFunctionDefinition ancestorFunction = Item1.AncestorDefinition as IFunctionDefinition;
-        IFunctionDefinition descendantFunction = Item1.DescendantDefinition as IFunctionDefinition;
-
-        if (Parameters.ConsiderLevelDifference) {
+        if (LevelDifferenceCoefficient > 0) {
           levelDifferencePunishment = Item1.LevelDelta - Item2.LevelDelta;
           if (levelDifferencePunishment < 0)
             levelDifferencePunishment = -levelDifferencePunishment;
-          levelDifferencePunishment /= MaxTreeHeight;
+          levelDifferencePunishment /= MaximumTreeHeight;
           if (levelDifferencePunishment > 1)
             levelDifferencePunishment = 1;
-          levelDifferencePunishment *= Parameters.LevelDifferenceCoefficient;
-          punishmentContributionSum += Parameters.LevelDifferenceCoefficient;
-          punishmentCoefficientsProduct *= (1 - Parameters.LevelDifferenceCoefficient);
-        }
-        if (Parameters.ConsiderAncestorIndex) {
+          levelDifferencePunishment *= LevelDifferenceCoefficient;
+          punishmentContributionSum += LevelDifferenceCoefficient;
+          punishmentCoefficientsProduct *= (1 - LevelDifferenceCoefficient);
+        }
+        if (AncestorIndexCoefficient > 0) {
           if (Item1.AncestorIndex != Item2.AncestorIndex)
             ancestorIndexDifferencePunishment = 1;
           else
             ancestorIndexDifferencePunishment = 0;
-          ancestorIndexDifferencePunishment *= Parameters.AncestorIndexCoefficient;
-          punishmentContributionSum += Parameters.AncestorIndexCoefficient;
-          punishmentCoefficientsProduct *= (1 - Parameters.AncestorIndexCoefficient);
-        }
-
-        if (Item1.AncestorDefinition is ITerminalDefinition) {
-          if (Parameters.ConsiderCoefficient) {
-            double coefficientDifference = Math.Abs(Item1.myAncestorCoefficient - Item2.myAncestorCoefficient);
-            if (ancestorTerminal.Parametrization.CoefficientIsGaussian)
-              ancestorCoefficientDifferencePunishment = (coefficientDifference / (ancestorTerminal.Parametrization.CoefficientSigma * 4));
-            else
-              ancestorCoefficientDifferencePunishment = (coefficientDifference / (ancestorTerminal.Parametrization.CoefficientMaxValue - ancestorTerminal.Parametrization.CoefficientMinValue));
-            if (ancestorCoefficientDifferencePunishment > 1)
-              ancestorCoefficientDifferencePunishment = 1;
-            ancestorCoefficientDifferencePunishment *= Parameters.CoefficientCoefficient;
-            punishmentContributionSum += Parameters.CoefficientCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.CoefficientCoefficient);
-          }
-          if (Parameters.ConsiderTimeOffset) {
-            double timeOffsetDifference = Math.Abs(Item1.AncestorTimeOffset - Item2.AncestorTimeOffset);
-            if (MaxTimeOffset > 0)
-              ancestorTimeOffsetDifferencePunishment = timeOffsetDifference / MaxTimeOffset;
-            ancestorTimeOffsetDifferencePunishment *= Parameters.TimeOffsetCoefficient;
-            punishmentContributionSum += Parameters.TimeOffsetCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.TimeOffsetCoefficient);
-          }
-          if (Parameters.ConsiderVariableIndex) {
-            if (Item1.AncestorVariableIndex != Item2.AncestorVariableIndex)
-              ancestorVariableIndexDifferencePunishment = 1;
-            else
-              ancestorVariableIndexDifferencePunishment = 0;
-            ancestorVariableIndexDifferencePunishment *= Parameters.VariableIndexCoefficient;
-            punishmentContributionSum += Parameters.VariableIndexCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.VariableIndexCoefficient);
-          }
-        } else {
-          if (Parameters.ConsiderVariant) {
-            if (Item1.AncestorVariant != Item2.AncestorVariant)
-              ancestorVariantDifferencePunishment = 1;
-            else
-              ancestorVariantDifferencePunishment = 0;
-            ancestorVariantDifferencePunishment *= Parameters.VariantCoefficient;
-            punishmentContributionSum += Parameters.VariantCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.VariantCoefficient);
-          }
-        }
-
-        if (Item1.DescendantDefinition is ITerminalDefinition) {
-          if (Parameters.ConsiderCoefficient) {
-            double coefficientDifference = Math.Abs(Item1.myDescendantCoefficient - Item2.myDescendantCoefficient);
-            if (descendantTerminal.Parametrization.CoefficientIsGaussian)
-              descendantCoefficientDifferencePunishment = (coefficientDifference / (descendantTerminal.Parametrization.CoefficientSigma * 4));
-            else
-              descendantCoefficientDifferencePunishment = (coefficientDifference / (descendantTerminal.Parametrization.CoefficientMaxValue - descendantTerminal.Parametrization.CoefficientMinValue));
-            if (descendantCoefficientDifferencePunishment > 1)
-              descendantCoefficientDifferencePunishment = 1;
-            descendantCoefficientDifferencePunishment *= Parameters.CoefficientCoefficient;
-            punishmentContributionSum += Parameters.CoefficientCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.CoefficientCoefficient);
-          }
-          if (Parameters.ConsiderTimeOffset) {
+          ancestorIndexDifferencePunishment *= AncestorIndexCoefficient;
+          punishmentContributionSum += AncestorIndexCoefficient;
+          punishmentCoefficientsProduct *= (1 - AncestorIndexCoefficient);
+        }
+
+        if (Item1.DescendantTreeNode is ConstantTreeNode) {
+          if (ConstantValueCoefficient > 0) {
+            double constantValueCoefficientDifference = Math.Abs(Item1.DescendantCoefficient - Item2.DescendantCoefficient);
+            // assume uniform distribution within given minimum and maximum
+            descendantConstantValueDifferencePunishment = (constantValueCoefficientDifference / (ConstantMaximum - ConstantMinimum));
+            if (descendantConstantValueDifferencePunishment > 1)
+              descendantConstantValueDifferencePunishment = 1;
+            descendantConstantValueDifferencePunishment *= ConstantValueCoefficient;
+            punishmentContributionSum += ConstantValueCoefficient;
+            punishmentCoefficientsProduct *= (1 - ConstantValueCoefficient);
+          }
+        }
+        if(Item1.DescendantTreeNode is VariableTreeNode) {
+          if (VariableWeightCoefficient > 0) {
+            double variableWeightDifference = Math.Abs(Item1.DescendantCoefficient - Item2.DescendantCoefficient);
+            // assume normal distribution within given sigma
+            descendantVariableWeightDifferencePunishment = variableWeightDifference / (VariableWeightSigma * 4);
+            if (descendantVariableWeightDifferencePunishment > 1)
+              descendantVariableWeightDifferencePunishment = 1;
+            descendantVariableWeightDifferencePunishment *= VariableWeightCoefficient;
+            punishmentContributionSum += VariableWeightCoefficient;
+            punishmentCoefficientsProduct *= (1 - VariableWeightCoefficient);
+          }
+          if (TimeOffsetCoefficient > 0) {
             double timeOffsetDifference = Math.Abs(Item1.DescendantTimeOffset - Item2.DescendantTimeOffset);
-            if (MaxTimeOffset > 0)
-              descendantTimeOffsetDifferencePunishment = timeOffsetDifference / MaxTimeOffset;
-            descendantTimeOffsetDifferencePunishment *= Parameters.TimeOffsetCoefficient;
-            punishmentContributionSum += Parameters.TimeOffsetCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.TimeOffsetCoefficient);
-          }
-          if (Parameters.ConsiderVariableIndex) {
+            if (MaximumTimeOffset > 0)
+              descendantTimeOffsetDifferencePunishment = timeOffsetDifference / MaximumTimeOffset;
+            descendantTimeOffsetDifferencePunishment *= TimeOffsetCoefficient;
+            punishmentContributionSum += TimeOffsetCoefficient;
+            punishmentCoefficientsProduct *= (1 - TimeOffsetCoefficient);
+          }
+          if (VariableIndexCoefficient > 0) {
             if (Item1.DescendantVariableIndex != Item2.DescendantVariableIndex)
               descendantVariableIndexDifferencePunishment = 1;
             else
               descendantVariableIndexDifferencePunishment = 0;
-            descendantVariableIndexDifferencePunishment *= Parameters.VariableIndexCoefficient;
-            punishmentContributionSum += Parameters.VariableIndexCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.VariableIndexCoefficient);
-          }
-        } else {
-          if (Parameters.ConsiderVariant) {
-            if (Item1.DescendantVariant != Item2.DescendantVariant)
-              descendantVariantDifferencePunishment = 1;
-            else
-              descendantVariantDifferencePunishment = 0;
-            descendantVariantDifferencePunishment *= Parameters.VariantCoefficient;
-            punishmentContributionSum += Parameters.VariantCoefficient;
-            punishmentCoefficientsProduct *= (1 - Parameters.VariantCoefficient);
+            descendantVariableIndexDifferencePunishment *= VariableIndexCoefficient;
+            punishmentContributionSum += VariableIndexCoefficient;
+            punishmentCoefficientsProduct *= (1 - VariableIndexCoefficient);
           }
         }
@@ -318,24 +270,17 @@
         double result;
 
-        if (Parameters.AdditiveSimilarityCalculation) {
-          double punishmentsSum =
-            ancestorCoefficientDifferencePunishment + ancestorTimeOffsetDifferencePunishment +
-            ancestorVariantDifferencePunishment + ancestorVariableIndexDifferencePunishment +
-            descendantCoefficientDifferencePunishment + descendantTimeOffsetDifferencePunishment +
-            descendantVariantDifferencePunishment + descendantVariableIndexDifferencePunishment +
-            ancestorIndexDifferencePunishment + levelDifferencePunishment;
+        if (AdditiveSimilarityCalculation) {
+          double punishmentsSum = ancestorIndexDifferencePunishment + levelDifferencePunishment +
+            descendantConstantValueDifferencePunishment + descendantVariableWeightDifferencePunishment +
+            descendantTimeOffsetDifferencePunishment + descendantVariableIndexDifferencePunishment;
           result = (1 - punishmentsSum / punishmentContributionSum);
         } else {
           result =
-            (1 - ancestorCoefficientDifferencePunishment) *
-            (1 - ancestorTimeOffsetDifferencePunishment) *
-            (1 - ancestorVariantDifferencePunishment) *
-            (1 - ancestorVariableIndexDifferencePunishment) *
-            (1 - descendantCoefficientDifferencePunishment) *
+            (1 - ancestorIndexDifferencePunishment) *
+            (1 - levelDifferencePunishment) *
+            (1 - descendantConstantValueDifferencePunishment) *
+            (1 - descendantVariableWeightDifferencePunishment) *
             (1 - descendantTimeOffsetDifferencePunishment) *
-            (1 - descendantVariantDifferencePunishment) *
-            (1 - descendantVariableIndexDifferencePunishment) *
-            (1 - ancestorIndexDifferencePunishment) *
-            (1 - levelDifferencePunishment);
+            (1 - descendantVariableIndexDifferencePunishment);
           // worst possible result is (1-punishmentCoefficientsProduct), so scale linearly to [0;1]:
           result = (result - punishmentCoefficientsProduct) / (1 - punishmentCoefficientsProduct);
@@ -347,39 +292,5 @@
         return result;
 
-      }*/
-
-      /*
-      public static List<GeneticInformationItem> GetGeneticInformationItems(SymbolicExpressionTreeNode Formula, int MinLevelDifference, int MaxLevelDifference) {
-        List<GeneticInformationItem> result = new List<GeneticInformationItem>();
-        List<SymbolicExpressionTreeNode> allNodes = new List<SymbolicExpressionTreeNode>();
-        allNodes.Add(Formula);
-        allNodes.AddRange(getDescendants(Formula));
-        foreach (SymbolicExpressionTreeNode node in allNodes) {
-          List<SymbolicExpressionTreeNode> allDescendants = new List<SymbolicExpressionTreeNode>();
-          allDescendants.Add(node);
-          allDescendants.AddRange(getDescendants(node));
-          foreach (SymbolicExpressionTreeNode descendant in allDescendants) {
-            GeneticInformationItem item = new GeneticInformationItem(node, descendant);
-            if (item.LevelDelta >= MinLevelDifference && item.LevelDelta <= MaxLevelDifference)
-              result.Add(item);
-          }
-        }
-        return result;
-      }*/
-
-      /*
-      public static List<SymbolicExpressionTreeNode> GetDescendants(SymbolicExpressionTreeNode node) {
-        List<SymbolicExpressionTreeNode> descendants = new List<SymbolicExpressionTreeNode>();
-        foreach (SymbolicExpressionTreeNode subTreeNode in node.SubTrees) {
-          AddDescendants(descendants, subTreeNode);
-        }
-        return descendants;
-      }
-      private static void AddDescendants(List<SymbolicExpressionTreeNode> list, SymbolicExpressionTreeNode node) {
-        list.Add(node);
-        foreach (SymbolicExpressionTreeNode subTreeNode in node.SubTrees) {
-          AddDescendants(list, subTreeNode);
-        }
-      }*/
+      }
 
       public static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node, List<string> variableNames,
@@ -415,7 +326,8 @@
           if (!(node.Symbol is StartSymbol))
             foreach (GeneticInformationItem item in descendantItems) {
-              if (!descendantNodes.Contains(item.DescendantTreeNode))
+              if (!descendantNodes.Contains(item.DescendantTreeNode)) {
                 list.Add(new GeneticInformationItem(node, item, i, level));
-              descendantNodes.Add(item.DescendantTreeNode);
+                descendantNodes.Add(item.DescendantTreeNode);
+              }
             }
         }
