[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Analysis;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 34 | using System.Collections.Generic;
|
---|
| 35 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 36 |
|
---|
| 37 | namespace 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 | }
|
---|