#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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.Linq; using HeuristicLab.Analysis; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers { [Item("SymbolicDataAnalysisPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic similarity between trees")] [StorableClass] public class SymbolicDataAnalysisPhenotypicDiversityAnalyzer : SingleObjectivePopulationDiversityAnalyzer { private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; private const string EvaluatedValuesParameterName = "EstimatedValues"; private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; private const string ProblemDataParameterName = "ProblemData"; private const string EstimationLimitsParameterName = "EstimationLimits"; public IScopeTreeLookupParameter SymbolicExpressionTreeParameter { get { return (IScopeTreeLookupParameter)Parameters[SymbolicExpressionTreeParameterName]; } } private IScopeTreeLookupParameter EvaluatedValuesParameter { get { return (IScopeTreeLookupParameter)Parameters[EvaluatedValuesParameterName]; } } public ILookupParameter SymbolicDataAnalysisTreeInterpreterParameter { get { return (ILookupParameter)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; } } public IValueLookupParameter ProblemDataParameter { get { return (IValueLookupParameter)Parameters[ProblemDataParameterName]; } } public IValueLookupParameter EstimationLimitsParameter { get { return (IValueLookupParameter)Parameters[EstimationLimitsParameterName]; } } public SymbolicDataAnalysisPhenotypicDiversityAnalyzer() { SimilarityCalculator = new PhenotypicSimilarityCalculator { SolutionVariableName = "SymbolicExpressionTree" }; Parameters.Add(new ScopeTreeLookupParameter(SymbolicExpressionTreeParameterName, "The symbolic expression trees.")); Parameters.Add(new ScopeTreeLookupParameter(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes.")); Parameters.Add(new LookupParameter(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree.")); Parameters.Add(new ValueLookupParameter(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated.")); Parameters.Add(new ValueLookupParameter(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees.")); } public override IOperation Apply() { var trees = SymbolicExpressionTreeParameter.ActualValue; var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; var ds = ProblemDataParameter.ActualValue.Dataset; var rows = ProblemDataParameter.ActualValue.TrainingIndices; var evaluatedValues = new ItemArray(trees.Select(t => new DoubleArray(interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray()))); EvaluatedValuesParameter.ActualValue = evaluatedValues; return base.Apply(); } } }