Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/VariablesUsagePopulationDiversityAnalyzer.cs @ 4881

Last change on this file since 4881 was 4881, checked in by swinkler, 13 years ago

Added first version of variables usage population diversity analyzer. (#1278)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using System.Collections.Generic;
35using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
36
37namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
38  /// <summary>
39  /// An operator that analyzes the population diversity with respect to the sets of used variables.
40  /// </summary>
41  [Item("VariablesUsagePopulationDiversityAnalysisOperator", "An operator that analyzes the population diversity with respect to the sets of used variables.")]
42  [StorableClass]
43  public sealed class VariablesUsagePopulationDiversityAnalyzer : PopulationDiversityAnalyzer<SymbolicExpressionTree>, ISymbolicRegressionAnalyzer {
44
45    private const string ProblemDataParameterName = "ProblemData";
46
47    public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
48      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
49    }
50    public DataAnalysisProblemData ProblemData {
51      get { return ProblemDataParameter.ActualValue; }
52    }
53
54    #region ISymbolicRegressionAnalyzer Members
55
56    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
57      get { return base.SolutionParameter; }
58    }
59
60    public new ILookupParameter<ResultCollection> ResultsParameter {
61      get { return base.ResultsParameter; }
62    }
63
64    #endregion
65
66    [StorableConstructor]
67    private VariablesUsagePopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
68    private VariablesUsagePopulationDiversityAnalyzer(VariablesUsagePopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
69    public VariablesUsagePopulationDiversityAnalyzer() : base() {
70      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new VariablesUsagePopulationDiversityAnalyzer(this, cloner);
75    }
76
77    protected override double[,] CalculateSimilarities(SymbolicExpressionTree[] solutions) {
78      int n = solutions.Length;
79      List<string> variableNames = new List<string>() ;
80      foreach (StringValue inputVariable in ProblemData.InputVariables)
81        variableNames.Add(inputVariable.Value);
82      List<int>[] usedVariables = new List<int>[n];
83      for (int i = 0; i < n; i++) {
84        usedVariables[i] = collectUsedVariables(solutions[i], variableNames);
85      }
86      double[,] result = new double[n, n];
87      for (int i = 0; i < n; i++) {
88        for (int j = 0; j < n; j++) {
89          if (i == j)
90            result[i, j] = 1;
91          else
92            result[i, j] = overlapRatio(usedVariables[i], usedVariables[j]);
93        }
94      }
95      return result;
96    }
97
98    private List<int> collectUsedVariables(SymbolicExpressionTree tree, List<string> variableNames) {
99      List<int> variables = new List<int>();
100      collectUsedVariables(tree.Root, variables, variableNames);
101      return variables;
102    }
103
104    private void collectUsedVariables(SymbolicExpressionTreeNode node, List<int> variables, List<string> variableNames) {
105      if (node is VariableTreeNode) {
106        string varName = (node as VariableTreeNode).VariableName;
107        int varIndex = variableNames.IndexOf(varName);
108        if (!variables.Contains(varIndex))
109          variables.Add(varIndex);
110      }
111      foreach (SymbolicExpressionTreeNode subnode in node.SubTrees)
112        collectUsedVariables(subnode, variables, variableNames);
113    }
114
115    private double overlapRatio(List<int> list1, List<int> list2) {
116      int found = 0;
117      foreach (int i in list1)
118        if (list2.Contains(i))
119          found++;
120      return ((double)found) / list1.Count;
121    }
122
123  }
124}
Note: See TracBrowser for help on using the repository browser.