1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Parameters;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
9 | [StorableClass]
|
---|
10 | [Item("SymbolicClassificationPruningOperator", "An operator which prunes symbolic classificaton trees.")]
|
---|
11 | public class SymbolicClassificationPruningOperator : SymbolicDataAnalysisExpressionPruningOperator {
|
---|
12 | private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator";
|
---|
13 | private const string ModelCreatorParameterName = "ModelCreator";
|
---|
14 | private const string ApplyLinearScalingParmameterName = "ApplyLinearScaling";
|
---|
15 |
|
---|
16 | #region parameter properties
|
---|
17 | public ILookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
|
---|
18 | get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
|
---|
22 | get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParmameterName]; }
|
---|
23 | }
|
---|
24 | #endregion
|
---|
25 | #region properties
|
---|
26 | private ISymbolicClassificationModelCreator ModelCreator { get { return ModelCreatorParameter.ActualValue; } }
|
---|
27 | private BoolValue ApplyLinearScaling { get { return ApplyLinearScalingParameter.ActualValue; } }
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | protected SymbolicClassificationPruningOperator(SymbolicClassificationPruningOperator
|
---|
31 | original, Cloner cloner)
|
---|
32 | : base(original, cloner) {
|
---|
33 | }
|
---|
34 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
35 | return new SymbolicClassificationPruningOperator(this, cloner);
|
---|
36 | }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | protected SymbolicClassificationPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
40 |
|
---|
41 | public SymbolicClassificationPruningOperator() {
|
---|
42 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, new SymbolicClassificationSolutionImpactValuesCalculator()));
|
---|
43 | Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName));
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected override ISymbolicDataAnalysisModel CreateModel() {
|
---|
47 | var model = ModelCreator.CreateSymbolicClassificationModel(SymbolicExpressionTree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper);
|
---|
48 | var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
|
---|
49 | var problemData = (IClassificationProblemData)ProblemData;
|
---|
50 | model.RecalculateModelParameters(problemData, rows);
|
---|
51 | return model;
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected override double Evaluate(IDataAnalysisModel model) {
|
---|
55 | var classificationModel = (IClassificationModel)model;
|
---|
56 | var classificationProblemData = (IClassificationProblemData)ProblemData;
|
---|
57 | var trainingIndices = ProblemData.TrainingIndices.ToList();
|
---|
58 | var estimatedValues = classificationModel.GetEstimatedClassValues(ProblemData.Dataset, trainingIndices);
|
---|
59 | var targetValues = ProblemData.Dataset.GetDoubleValues(classificationProblemData.TargetVariable, trainingIndices);
|
---|
60 | OnlineCalculatorError errorState;
|
---|
61 | var quality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);
|
---|
62 | if (errorState != OnlineCalculatorError.None) return double.NaN;
|
---|
63 | return quality;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|