Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/25/10 00:22:15 (14 years ago)
Author:
swinkler
Message:

Worked on structural population diversity analysis (#1278):

  • Improved definition of genetic items.
  • Implemented recursive collection of all genetic items of symbolic expression trees.
  • Added FineGrainedStructuralPopulationDiversityAnalyzer to SymbolicRegressionProblemBase.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FineGrainedStructuralPopulationDiversityAnalyzer.cs

    r4917 r4928  
    5555    protected override double[,] CalculateSimilarities(SymbolicExpressionTree[] solutions) {
    5656      int n = solutions.Length;
     57      List<string> variableNames = new List<string>();
     58      foreach (StringValue variableName in ProblemData.InputVariables) {
     59        variableNames.Add(variableName.Value);
     60      }
     61      variableNames.Add(ProblemData.TargetVariable.Value);
     62      IList<GeneticInformationItem>[] geneticInformationItemsList = new List<GeneticInformationItem>[n];
     63      for (int i = 0; i < n; i++) {
     64        geneticInformationItemsList[i] = GeneticInformationItem.getGeneticInformationItems(solutions[i].Root, variableNames);
     65      }
    5766      double[,] result = new double[n, n];
    5867      for (int i = 0; i < n; i++) {
     
    6675      return result;
    6776    }
    68     /*
    69     #region private class GeneticInformationItem, ported from HL 2
     77
     78
     79    #region private class GeneticInformationItem
    7080
    7181    private class GeneticInformationItem {
     
    91101      }
    92102
    93       private double myAncestorCoefficient;
    94       public double AncestorCoefficient {
    95         get { return myAncestorCoefficient; }
    96       }
    97 
    98       private double myAncestorVariableIndex;
    99       public double AncestorVariableIndex {
    100         get { return myAncestorVariableIndex; }
    101       }
    102 
    103       private int myAncestorTimeOffset;
    104       public int AncestorTimeOffset {
    105         get { return myAncestorTimeOffset; }
    106       }
     103      // not used in HL 3.3
     104      // private double myAncestorCoefficient;
     105      // public double AncestorCoefficient {
     106      //   get { return myAncestorCoefficient; }
     107      // }
     108
     109      // not used in HL 3.3
     110      // private double myAncestorVariableIndex;
     111      // public double AncestorVariableIndex {
     112      //   get { return myAncestorVariableIndex; }
     113      // }
     114
     115      // not used in HL 3.3
     116      // private int myAncestorTimeOffset;
     117      // public int AncestorTimeOffset {
     118      //   get { return myAncestorTimeOffset; }
     119      // }
    107120
    108121      // not used in HL 3.3
     
    127140      }
    128141
    129       private int myDescendantVariant;
    130       public int DescendantVariant {
    131         get { return myDescendantVariant; }
    132       }
    133 
    134       public GeneticInformationItem(SymbolicExpressionTreeNode Ancestor, SymbolicExpressionTreeNode Descendant) {
    135 
    136         myAncestorDefinition = Ancestor.Symbol.GetType();
    137         myAncestorCoefficient = Ancestor.Coefficient;
    138         myAncestorTimeOffset = Ancestor.TimeOffset;
    139         myAncestorVariableIndex = Ancestor.VariableIndex;
    140         //myAncestorVariant = Ancestor.Variant;
    141 
    142         myDescendantDefinition = Descendant.Symbol.GetType();
    143         myDescendantCoefficient = Descendant.Coefficient;
    144         myDescendantTimeOffset = Descendant.TimeOffset;
    145         myDescendantVariableIndex = Descendant.VariableIndex;
    146         //myDescendantVariant = Descendant.Variant;
    147 
    148         myLevelDelta = Descendant.Level - Ancestor.Level;
    149         if (myLevelDelta < 0)
    150           throw new InvalidOperationException("ERROR in GeneticInformationItem: The Given ancestor is not an ancestor of the given descendant node.");
    151         myAncestorIndex = CalculateAncestorIndex(Ancestor, Descendant);
    152 
    153       }
    154 
    155       private GeneticInformationItem(GeneticInformationItem item) {
    156         myAncestorDefinition = item.myAncestorDefinition;
    157         myAncestorCoefficient = item.myAncestorCoefficient;
    158         myAncestorTimeOffset = item.myAncestorTimeOffset;
    159         myAncestorVariableIndex = item.myAncestorVariableIndex;
    160         // myAncestorVariant = item.myAncestorVariant;
    161         myDescendantDefinition = item.myDescendantDefinition;
    162         myDescendantCoefficient = item.myDescendantCoefficient;
    163         myDescendantTimeOffset = item.myDescendantTimeOffset;
    164         myDescendantVariableIndex = item.myDescendantVariableIndex;
    165         myDescendantVariant = item.myDescendantVariant;
    166         myLevelDelta = item.myLevelDelta;
    167         myAncestorIndex = item.myAncestorIndex;
    168       }
    169 
    170       public GeneticInformationItem Clone() {
    171         return new GeneticInformationItem(this);
    172       }
    173 
    174       public static List<GeneticInformationItem> CopyList(List<GeneticInformationItem> list) {
    175         List<GeneticInformationItem> result = new List<GeneticInformationItem>();
    176         for (int i = 0; i < list.Count; i++)
    177           result.Add(list[i]);
    178         return result;
    179       }
    180 
    181       private static int CalculateAncestorIndex(SymbolicExpressionTreeNode Ancestor, SymbolicExpressionTreeNode Descendant) {
    182         if (Ancestor == Descendant)
    183           return 0;
    184         // Node lastParent = Descendant, parent = Descendant.Parent;
    185         // while (parent != Ancestor) {
    186         //   lastParent = parent;
    187         //   parent = parent.Parent;
    188         //   if (parent == null)
    189         //     throw new InvalidOperationException("Error in GeneticInformationItem: The given ancestor node does not seem to be an ancestor of the given descendant.");
    190         // }
    191         // // parent == Ancestor, lastParent is child of Ancestor
    192         // int childIndex = 0;
    193         // while (Ancestor.GetChild(childIndex) != lastParent)
    194         //   childIndex++;
    195         // return childIndex;
    196         //
    197         for (int i = 0; i < Ancestor.SubTrees.Count; i++)
    198           if (treeContainsNode(Ancestor.SubTrees[i], Descendant))
    199             return i;
    200         return -1;
    201       }
    202 
    203       private static bool treeContainsNode(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode node) {
    204         if (node == root)
    205           return true;
    206         foreach (SymbolicExpressionTreeNode subTree in root.SubTrees)
    207           if(treeContainsNode(subTree, node))
    208             return true;
    209         return false;
    210       }
    211 
     142      // not used in HL 3.3
     143      //private int myDescendantVariant;
     144      //public int DescendantVariant {
     145      //  get { return myDescendantVariant; }
     146      //}
     147
     148      /*
    212149      public static GeneticInformationItem FindBestPendant(GeneticInformationItem Item, List<GeneticInformationItem> ComparisonItems,
    213150          StructuralSimilarityAnalysisParameters Parameters,
     
    228165        else
    229166          return null;
    230       }
    231 
     167      }*/
     168
     169      /*
    232170      public static double Similarity(GeneticInformationItem Item1, GeneticInformationItem Item2,
    233171          StructuralSimilarityAnalysisParameters Parameters,
     
    395333        return result;
    396334
    397       }
    398 
     335      }*/
     336
     337      /*
    399338      public static List<GeneticInformationItem> GetGeneticInformationItems(SymbolicExpressionTreeNode Formula, int MinLevelDifference, int MaxLevelDifference) {
    400339        List<GeneticInformationItem> result = new List<GeneticInformationItem>();
     
    413352        }
    414353        return result;
    415       }
    416 
    417       public static List<SymbolicExpressionTreeNode> getDescendants(SymbolicExpressionTreeNode node) {
     354      }*/
     355
     356      /*
     357      public static List<SymbolicExpressionTreeNode> GetDescendants(SymbolicExpressionTreeNode node) {
    418358        List<SymbolicExpressionTreeNode> descendants = new List<SymbolicExpressionTreeNode>();
    419359        foreach (SymbolicExpressionTreeNode subTreeNode in node.SubTrees) {
    420           addDescendants(descendants, subTreeNode);
     360          AddDescendants(descendants, subTreeNode);
    421361        }
    422362        return descendants;
    423363      }
    424       private static void addDescendants(List<SymbolicExpressionTreeNode> list, SymbolicExpressionTreeNode node) {
     364      private static void AddDescendants(List<SymbolicExpressionTreeNode> list, SymbolicExpressionTreeNode node) {
    425365        list.Add(node);
    426366        foreach (SymbolicExpressionTreeNode subTreeNode in node.SubTrees) {
    427           addDescendants(list, subTreeNode);
    428         }
    429       }
    430 
    431       private static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node) {
     367          AddDescendants(list, subTreeNode);
     368        }
     369      }*/
     370
     371      public static IList<GeneticInformationItem> getGeneticInformationItems(SymbolicExpressionTreeNode node, List<string> variableNames) {
    432372        // Idea: collect all descendants' lists and then add new items using the retrieved ones.
    433373        // This should save lots of time and reduce complexity of the items retrieval process.
     374        if (node.Symbol is ProgramRootSymbol)
     375          return getGeneticInformationItems(node.SubTrees[0], variableNames);
    434376        List<GeneticInformationItem> list = new List<GeneticInformationItem>();
     377        // add item for this node:
     378        list.Add(new GeneticInformationItem(node, variableNames));
     379        // add items for the descendants:
    435380        for (int i = 0; i < node.SubTrees.Count; i++) {
    436           IList<GeneticInformationItem> descendantItems = getGeneticInformationItems(node.SubTrees[i]);
     381          IList<GeneticInformationItem> descendantItems = getGeneticInformationItems(node.SubTrees[i], variableNames);
    437382          list.AddRange(descendantItems);
    438383          foreach (GeneticInformationItem item in descendantItems) {
    439             GeneticInformationItem newItem = new GeneticInformationItem();
    440             newItem.AncestorIndex = i;
    441             newItem.AncestorDefinition = node.Symbol.GetType();
    442             // ... TODO!
     384            list.Add(new GeneticInformationItem(node, item, i));
    443385          }
    444386        }
     
    446388      }
    447389
     390      private GeneticInformationItem (SymbolicExpressionTreeNode node, List<string> variableNames) {
     391        myAncestorIndex = -1;
     392        VariableTreeNode variableTreeNode = node as VariableTreeNode;
     393        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
     394        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     395        myAncestorDefinition = node.Symbol.GetType();
     396        myDescendantDefinition = myAncestorDefinition;
     397        if (variableTreeNode != null)
     398          myDescendantCoefficient = variableTreeNode.Weight;
     399        else if (constantTreeNode != null)
     400          myDescendantCoefficient = constantTreeNode.Value;
     401        else
     402          myDescendantCoefficient = double.NaN;
     403        if (laggedVariableTreeNode != null)
     404          myDescendantTimeOffset = laggedVariableTreeNode.Lag;
     405        else
     406          myDescendantTimeOffset = 0;
     407        if (variableTreeNode != null)
     408          myDescendantVariableIndex = variableNames.IndexOf(variableTreeNode.VariableName);
     409        else
     410          myDescendantVariableIndex = -1;
     411        myLevelDelta = 0;
     412      }
     413
     414      private GeneticInformationItem(SymbolicExpressionTreeNode parentNode, GeneticInformationItem descendantGeneticInformationItem, int ancestorIndex) {
     415        myAncestorIndex = ancestorIndex;
     416        myLevelDelta = descendantGeneticInformationItem.LevelDelta + 1;
     417        myAncestorDefinition = parentNode.Symbol.GetType();
     418        myDescendantCoefficient = descendantGeneticInformationItem.DescendantCoefficient;
     419        myDescendantDefinition = descendantGeneticInformationItem.DescendantDefinition;
     420        myDescendantTimeOffset = descendantGeneticInformationItem.DescendantTimeOffset;
     421        myDescendantVariableIndex = descendantGeneticInformationItem.DescendantVariableIndex;
     422      }
     423
     424      public override string ToString() {
     425        return "ancestor: " + AncestorDefinition.Name.ToString() + ", [" + AncestorIndex + "], delta " + LevelDelta
     426          + "; descendant: " + DescendantDefinition.Name.ToString() + " (varIndex " + DescendantVariableIndex + ", "
     427          + DescendantCoefficient + ", t-" + DescendantTimeOffset + ")";
     428      }
     429
    448430    }
    449431
    450432    #endregion
    451     */
     433
    452434  }
    453435}
Note: See TracChangeset for help on using the changeset viewer.