Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4949 was 4885, checked in by swinkler, 14 years ago

Added abstract base class for population diversity analyzers for symbolic regression; simplified management of population diversity analyzers in symbolic regression problem base. (#1278)

File size: 4.2 KB
RevLine 
[4881]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]
[4885]43  public sealed class VariablesUsagePopulationDiversityAnalyzer : SymbolicRegressionPopulationDiversityAnalyzer {
[4881]44
45    [StorableConstructor]
46    private VariablesUsagePopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
47    private VariablesUsagePopulationDiversityAnalyzer(VariablesUsagePopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
[4885]48    public VariablesUsagePopulationDiversityAnalyzer() : base() { }
[4881]49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new VariablesUsagePopulationDiversityAnalyzer(this, cloner);
52    }
53
54    protected override double[,] CalculateSimilarities(SymbolicExpressionTree[] solutions) {
55      int n = solutions.Length;
56      List<string> variableNames = new List<string>() ;
57      foreach (StringValue inputVariable in ProblemData.InputVariables)
58        variableNames.Add(inputVariable.Value);
59      List<int>[] usedVariables = new List<int>[n];
60      for (int i = 0; i < n; i++) {
61        usedVariables[i] = collectUsedVariables(solutions[i], variableNames);
62      }
63      double[,] result = new double[n, n];
64      for (int i = 0; i < n; i++) {
65        for (int j = 0; j < n; j++) {
66          if (i == j)
67            result[i, j] = 1;
68          else
69            result[i, j] = overlapRatio(usedVariables[i], usedVariables[j]);
70        }
71      }
72      return result;
73    }
74
75    private List<int> collectUsedVariables(SymbolicExpressionTree tree, List<string> variableNames) {
76      List<int> variables = new List<int>();
77      collectUsedVariables(tree.Root, variables, variableNames);
78      return variables;
79    }
80
81    private void collectUsedVariables(SymbolicExpressionTreeNode node, List<int> variables, List<string> variableNames) {
82      if (node is VariableTreeNode) {
83        string varName = (node as VariableTreeNode).VariableName;
84        int varIndex = variableNames.IndexOf(varName);
85        if (!variables.Contains(varIndex))
86          variables.Add(varIndex);
87      }
88      foreach (SymbolicExpressionTreeNode subnode in node.SubTrees)
89        collectUsedVariables(subnode, variables, variableNames);
90    }
91
92    private double overlapRatio(List<int> list1, List<int> list2) {
[4882]93      if (list1.Count == 0)
94        return 0;
[4881]95      int found = 0;
96      foreach (int i in list1)
97        if (list2.Contains(i))
98          found++;
99      return ((double)found) / list1.Count;
100    }
101
102  }
103}
Note: See TracBrowser for help on using the repository browser.