#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System.Collections.Generic; using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers { /// /// An operator that analyzes the population diversity with respect to the sets of used variables. /// [Item("VariablesUsagePopulationDiversityAnalysisOperator", "An operator that analyzes the population diversity with respect to the sets of used variables.")] [StorableClass] public sealed class VariablesUsagePopulationDiversityAnalyzer : SymbolicRegressionPopulationDiversityAnalyzer { [StorableConstructor] private VariablesUsagePopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { } private VariablesUsagePopulationDiversityAnalyzer(VariablesUsagePopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { } public VariablesUsagePopulationDiversityAnalyzer() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new VariablesUsagePopulationDiversityAnalyzer(this, cloner); } protected override double[,] CalculateSimilarities(SymbolicExpressionTree[] solutions) { int n = solutions.Length; List variableNames = new List() ; foreach (StringValue inputVariable in ProblemData.InputVariables) variableNames.Add(inputVariable.Value); List[] usedVariables = new List[n]; for (int i = 0; i < n; i++) { usedVariables[i] = collectUsedVariables(solutions[i], variableNames); } double[,] result = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) result[i, j] = 1; else result[i, j] = overlapRatio(usedVariables[i], usedVariables[j]); } } return result; } private List collectUsedVariables(SymbolicExpressionTree tree, List variableNames) { List variables = new List(); collectUsedVariables(tree.Root, variables, variableNames); return variables; } private void collectUsedVariables(SymbolicExpressionTreeNode node, List variables, List variableNames) { if (node is VariableTreeNode) { string varName = (node as VariableTreeNode).VariableName; int varIndex = variableNames.IndexOf(varName); if (!variables.Contains(varIndex)) variables.Add(varIndex); } foreach (SymbolicExpressionTreeNode subnode in node.SubTrees) collectUsedVariables(subnode, variables, variableNames); } private double overlapRatio(List list1, List list2) { if (list1.Count == 0) return 0; int found = 0; foreach (int i in list1) if (list2.Contains(i)) found++; return ((double)found) / list1.Count; } } }