#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HEAL.Attic;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
[Item("SymbolicClassificationPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
[StorableType("D09C1CC5-2BFB-4B5C-A496-F8EA98741C37")]
public class SymbolicClassificationPhenotypicDiversityAnalyzer : PopulationSimilarityAnalyzer,
ISymbolicDataAnalysisBoundedOperator, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
#region parameter names
private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
private const string EvaluatedValuesParameterName = "EstimatedValues";
private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
private const string ProblemDataParameterName = "ProblemData";
private const string ModelCreatorParameterName = "ModelCreator";
private const string EstimationLimitsParameterName = "EstimationLimits";
private const string UseClassValuesParameterName = "UseClassValues";
#endregion
#region parameter properties
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 ILookupParameter ModelCreatorParameter {
get { return (ILookupParameter)Parameters[ModelCreatorParameterName]; }
}
public IValueLookupParameter ProblemDataParameter {
get { return (IValueLookupParameter)Parameters[ProblemDataParameterName]; }
}
public IValueLookupParameter EstimationLimitsParameter {
get { return (IValueLookupParameter)Parameters[EstimationLimitsParameterName]; }
}
public IFixedValueParameter UseClassValuesParameter {
get { return (IFixedValueParameter)Parameters[UseClassValuesParameterName]; }
}
#endregion
#region properties
public bool UseClassValues {
get { return UseClassValuesParameter.Value.Value; }
set { UseClassValuesParameter.Value.Value = value; }
}
#endregion
public SymbolicClassificationPhenotypicDiversityAnalyzer(IEnumerable validSimilarityCalculators)
: base(validSimilarityCalculators) {
#region add parameters
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 LookupParameter(ModelCreatorParameterName, "The model creator for creating discriminant function classification models."));
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."));
Parameters.Add(new FixedValueParameter(UseClassValuesParameterName, "Specifies whether the raw estimated values of the tree or the corresponding class values should be used for similarity calculation.", new BoolValue(false)));
#endregion
UpdateCounterParameter.ActualName = "PhenotypicDiversityAnalyzerUpdateCounter";
DiversityResultName = "Phenotypic Similarity";
}
[StorableConstructor]
protected SymbolicClassificationPhenotypicDiversityAnalyzer(StorableConstructorFlag _) : base(_) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicClassificationPhenotypicDiversityAnalyzer(this, cloner);
}
protected SymbolicClassificationPhenotypicDiversityAnalyzer(SymbolicClassificationPhenotypicDiversityAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IOperation Apply() {
int updateInterval = UpdateIntervalParameter.Value.Value;
IntValue updateCounter = UpdateCounterParameter.ActualValue;
if (updateCounter == null) {
updateCounter = new IntValue(updateInterval);
UpdateCounterParameter.ActualValue = updateCounter;
}
if (updateCounter.Value == updateInterval) {
var trees = SymbolicExpressionTreeParameter.ActualValue;
var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
var problemData = ProblemDataParameter.ActualValue;
var ds = ProblemDataParameter.ActualValue.Dataset;
var rows = ProblemDataParameter.ActualValue.TrainingIndices;
var modelCreator = ModelCreatorParameter.ActualValue;
var estimationLimits = EstimationLimitsParameter.ActualValue;
var evaluatedValues = new ItemArray(trees.Length);
for (int i = 0; i < trees.Length; ++i) {
var model = (IDiscriminantFunctionClassificationModel)modelCreator.CreateSymbolicDiscriminantFunctionClassificationModel(problemData.TargetVariable, trees[i], interpreter, estimationLimits.Lower, estimationLimits.Upper);
model.RecalculateModelParameters(problemData, rows);
var values = UseClassValues ? model.GetEstimatedClassValues(ds, rows) : model.GetEstimatedValues(ds, rows);
evaluatedValues[i] = new DoubleArray(values.ToArray());
}
EvaluatedValuesParameter.ActualValue = evaluatedValues;
}
return base.Apply();
}
}
}