Changeset 8594
- Timestamp:
- 09/07/12 11:27:49 (12 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs
r8550 r8594 111 111 IClassificationProblemData problemData, 112 112 IEnumerable<int> rows) { 113 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter );114 model. SetAccuracyMaximizingThresholds(problemData);113 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, new AccuracyMaximizationThresholdCalculator()); 114 model.RecalculateModelParameters(problemData, rows); 115 115 return model; 116 116 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs
r8575 r8594 50 50 51 51 protected override void UpdateModel(ISymbolicExpressionTree tree) { 52 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit); 53 // the default policy for setting thresholds in classification models is the accuarcy maximizing policy. 54 // This is rather slow to calculate and can lead to a very laggy UI in the interactive solution simplifier. 55 // However, since we automatically prune sub-trees based on the threshold reaching the maximum accuracy we must 56 // also use maximum accuracy threshold calculation here in order to prevent incoherent behavior of the simplifier. 57 model.SetAccuracyMaximizingThresholds(Content.ProblemData); 52 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter, Content.Model.ThresholdCalculator, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit); 53 model.RecalculateModelParameters(Content.ProblemData, Content.ProblemData.TrainingIndices); 58 54 Content.Model = model; 59 55 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.csproj
r8548 r8594 112 112 </ItemGroup> 113 113 <ItemGroup> 114 <Compile Include="Interfaces\ISymbolicClassificationModelCreatorOperator.cs" /> 115 <Compile Include="Interfaces\ISymbolicDiscriminantFunctionClassificationModelCreator.cs" /> 116 <Compile Include="Interfaces\ISymbolicClassificationModelCreator.cs" /> 114 117 <Compile Include="Interfaces\ISymbolicDiscriminantFunctionClassificationModel.cs" /> 118 <Compile Include="ModelCreators\AccuracyMaximizingThresholdsModelCreator.cs" /> 119 <Compile Include="ModelCreators\NormalDistributedThresholdsModelCreator.cs" /> 115 120 <Compile Include="MultiObjective\SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs" /> 116 121 <Compile Include="Plugin.cs" /> … … 177 182 <Private>False</Private> 178 183 </ProjectReference> 184 <ProjectReference Include="..\..\HeuristicLab.Common.Resources\3.3\HeuristicLab.Common.Resources-3.3.csproj"> 185 <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project> 186 <Name>HeuristicLab.Common.Resources-3.3</Name> 187 </ProjectReference> 179 188 <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj"> 180 189 <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project> -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Interfaces/ISymbolicClassificationModel.cs
r7259 r8594 20 20 #endregion 21 21 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;22 using System.Collections.Generic; 23 23 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification { 24 24 public interface ISymbolicClassificationModel : IClassificationModel, ISymbolicDataAnalysisModel { 25 double LowerEstimationLimit { get; } 26 double UpperEstimationLimit { get; } 27 void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows); 28 new ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData); 25 29 } 26 30 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Interfaces/ISymbolicDiscriminantFunctionClassificationModel.cs
r7259 r8594 20 20 #endregion 21 21 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;23 22 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification { 24 23 public interface ISymbolicDiscriminantFunctionClassificationModel : IDiscriminantFunctionClassificationModel, ISymbolicClassificationModel { 25 double LowerEstimationLimit { get; } 26 double UpperEstimationLimit { get; } 24 IDiscriminantFunctionThresholdCalculator ThresholdCalculator { get; } 27 25 } 28 26 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveProblem.cs
r8175 r8594 36 36 private const string EstimationLimitsParameterName = "EstimationLimits"; 37 37 private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model."; 38 private const string ModelCreatorParameterName = "ModelCreator"; 39 38 40 39 41 #region parameter properties … … 41 43 get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 42 44 } 45 public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 46 get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 47 } 43 48 #endregion 44 49 #region properties 45 50 public DoubleLimit EstimationLimits { 46 51 get { return EstimationLimitsParameter.Value; } 52 } 53 public ISymbolicClassificationModelCreator ModelCreator { 54 get { return ModelCreatorParameter.Value; } 47 55 } 48 56 #endregion … … 58 66 : base(new ClassificationProblemData(), new SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) { 59 67 Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription)); 68 Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator())); 60 69 61 70 EstimationLimitsParameter.Hidden = true; … … 74 83 [StorableHook(HookType.AfterDeserialization)] 75 84 private void AfterDeserialization() { 85 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 86 Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator())); 76 87 RegisterEventHandlers(); 77 88 } … … 79 90 private void RegisterEventHandlers() { 80 91 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 92 ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators(); 81 93 } 82 94 … … 110 122 } 111 123 112 protected newvoid ParameterizeOperators() {124 protected override void ParameterizeOperators() { 113 125 base.ParameterizeOperators(); 114 126 if (Parameters.ContainsKey(EstimationLimitsParameterName)) { 115 127 var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators); 116 foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) { 117 op.EstimationLimitsParameter.ActualName = EstimationLimitsParameterName; 118 } 128 foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) 129 op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name; 130 foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>()) 131 op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name; 119 132 } 120 133 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs
r8550 r8594 34 34 [StorableClass] 35 35 public sealed class SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicClassificationSolution>, 36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator { 37 37 private const string ProblemDataParameterName = "ProblemData"; 38 private const string ModelCreatorParameterName = "ModelCreator"; 38 39 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 40 private const string EstimationLimitsParameterName = "EstimationLimits"; … … 43 44 public ILookupParameter<IClassificationProblemData> ProblemDataParameter { 44 45 get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; } 46 } 47 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 48 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 49 } 50 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 51 get { return ModelCreatorParameter; } 45 52 } 46 53 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter { … … 67 74 : base() { 68 75 Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data for the symbolic classification solution.")); 76 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 69 77 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 70 78 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model.")); … … 75 83 } 76 84 85 [StorableHook(HookType.AfterDeserialization)] 86 private void AfterDeserialization() { 87 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 88 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 89 } 90 77 91 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 78 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);79 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);92 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 93 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 80 94 81 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);82 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());95 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 96 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 83 97 } 84 98 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs
r8550 r8594 34 34 [StorableClass] 35 35 public sealed class SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationMultiObjectiveEvaluator, IClassificationProblemData>, 36 ISymbolicDataAnalysisBoundedOperator { 36 ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator { 37 private const string ModelCreatorParameterName = "ModelCreator"; 37 38 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 39 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; … … 44 45 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 45 46 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 47 } 48 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 49 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 50 } 51 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 52 get { return ModelCreatorParameter; } 46 53 } 47 54 #endregion … … 59 66 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The loewr and upper limit for the estimated values produced by the symbolic classification model.")); 60 67 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false))); 68 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 61 69 } 62 70 public override IDeepCloneable Clone(Cloner cloner) { … … 64 72 } 65 73 74 [StorableHook(HookType.AfterDeserialization)] 75 private void AfterDeserialization() { 76 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 77 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 78 } 79 66 80 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQualities) { 67 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);68 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);81 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 82 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 69 83 70 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);71 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());84 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 85 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 72 86 } 73 87 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs
r8551 r8594 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 30 31 [Item("Penalty Score Evaluator", "Calculates the penalty score of a symbolic classification solution.")] 31 32 [StorableClass] 32 public class SymbolicClassificationSingleObjectivePenaltyScoreEvaluator : SymbolicClassificationSingleObjectiveEvaluator { 33 public class SymbolicClassificationSingleObjectivePenaltyScoreEvaluator : SymbolicClassificationSingleObjectiveEvaluator, ISymbolicClassificationModelCreatorOperator { 34 private const string ModelCreatorParameterName = "ModelCreator"; 33 35 public override bool Maximization { get { return false; } } 36 37 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 38 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 39 } 40 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 41 get { return ModelCreatorParameter; } 42 } 34 43 35 44 [StorableConstructor] 36 45 protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(bool deserializing) : base(deserializing) { } 37 46 protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(SymbolicClassificationSingleObjectivePenaltyScoreEvaluator original, Cloner cloner) : base(original, cloner) { } 38 public SymbolicClassificationSingleObjectivePenaltyScoreEvaluator() : base() { } 47 public SymbolicClassificationSingleObjectivePenaltyScoreEvaluator() 48 : base() { 49 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 50 } 39 51 40 52 public override IDeepCloneable Clone(Cloner cloner) { 41 53 return new SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(this, cloner); 42 54 } 55 56 [StorableHook(HookType.AfterDeserialization)] 57 private void AfterDeserialization() { 58 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 59 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 60 } 61 43 62 44 63 public override IOperation Apply() { … … 66 85 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 67 86 EstimationLimitsParameter.ExecutionContext = context; 87 ModelCreatorParameter.ExecutionContext = context; 68 88 69 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);70 model. SetAccuracyMaximizingThresholds(problemData);89 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 90 model.RecalculateModelParameters(problemData, rows); 71 91 double penalty = Calculate(model, problemData, rows); 72 92 73 93 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 74 94 EstimationLimitsParameter.ExecutionContext = null; 95 ModelCreatorParameter.ExecutionContext = null; 75 96 76 97 return penalty; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs
r8175 r8594 35 35 private const string EstimationLimitsParameterName = "EstimationLimits"; 36 36 private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model."; 37 private const string ModelCreatorParameterName = "ModelCreator"; 37 38 38 39 #region parameter properties … … 40 41 get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 41 42 } 43 public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 44 get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 45 } 42 46 #endregion 43 47 #region properties 44 48 public DoubleLimit EstimationLimits { 45 49 get { return EstimationLimitsParameter.Value; } 50 } 51 public ISymbolicClassificationModelCreator ModelCreator { 52 get { return ModelCreatorParameter.Value; } 46 53 } 47 54 #endregion … … 57 64 : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) { 58 65 Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription)); 66 Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator())); 59 67 60 68 EstimationLimitsParameter.Hidden = true; … … 71 79 [StorableHook(HookType.AfterDeserialization)] 72 80 private void AfterDeserialization() { 73 RegisterEventHandlers(); 74 // compatibility 81 82 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 83 Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator())); 84 75 85 bool changed = false; 76 86 if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) { … … 83 93 } 84 94 if (changed) ParameterizeOperators(); 95 RegisterEventHandlers(); 85 96 } 86 97 87 98 private void RegisterEventHandlers() { 88 99 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 100 ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators(); 89 101 } 90 102 … … 125 137 if (Parameters.ContainsKey(EstimationLimitsParameterName)) { 126 138 var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators); 127 foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {139 foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) 128 140 op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name; 129 } 141 foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>()) 142 op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name; 130 143 } 131 144 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs
r8550 r8594 34 34 [StorableClass] 35 35 public sealed class SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<ISymbolicClassificationSolution>, 36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator { 37 37 private const string ProblemDataParameterName = "ProblemData"; 38 private const string ModelCreatorParameterName = "ModelCreator"; 38 39 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 40 private const string EstimationLimitsParameterName = "UpperEstimationLimit"; … … 42 43 public ILookupParameter<IClassificationProblemData> ProblemDataParameter { 43 44 get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; } 45 } 46 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 47 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 48 } 49 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 50 get { return ModelCreatorParameter; } 44 51 } 45 52 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter { … … 65 72 : base() { 66 73 Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data for the symbolic classification solution.")); 74 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 67 75 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 68 76 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model.")); 69 77 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false))); 70 78 } 79 71 80 public override IDeepCloneable Clone(Cloner cloner) { 72 81 return new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer(this, cloner); 73 82 } 83 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 86 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 87 } 74 88 75 89 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 76 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);77 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);90 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 91 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 78 92 79 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);80 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());93 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 94 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 81 95 } 82 96 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs
r8550 r8594 33 33 [Item("SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that collects the training Pareto-best symbolic classification solutions for single objective symbolic classification problems.")] 34 34 [StorableClass] 35 public sealed class SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IClassificationProblemData, ISymbolicClassificationSolution> {35 public sealed class SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IClassificationProblemData, ISymbolicClassificationSolution>, ISymbolicClassificationModelCreatorOperator { 36 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 37 private const string ModelCreatorParameterName = "ModelCreator"; 37 38 #region parameter properties 38 39 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 39 40 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 41 } 42 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 43 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 44 } 45 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 46 get { return ModelCreatorParameter; } 40 47 } 41 48 #endregion … … 53 60 : base() { 54 61 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false))); 62 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 55 63 } 56 64 public override IDeepCloneable Clone(Cloner cloner) { … … 58 66 } 59 67 68 [StorableHook(HookType.AfterDeserialization)] 69 private void AfterDeserialization() { 70 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 71 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 72 } 73 60 74 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);62 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);75 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 76 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 63 77 64 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);65 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());78 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 79 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 66 80 } 67 81 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs
r8550 r8594 34 34 [StorableClass] 35 35 public sealed class SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData>, 36 ISymbolicDataAnalysisBoundedOperator {36 ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator { 37 37 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 38 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 39 private const string ModelCreatorParameterName = "ModelCreator"; 39 40 40 41 #region parameter properties … … 44 45 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 45 46 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 47 } 48 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 49 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 50 } 51 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 52 get { return ModelCreatorParameter; } 46 53 } 47 54 #endregion … … 59 66 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model.")); 60 67 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false))); 68 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 61 69 } 62 70 public override IDeepCloneable Clone(Cloner cloner) { … … 64 72 } 65 73 74 [StorableHook(HookType.AfterDeserialization)] 75 private void AfterDeserialization() { 76 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 77 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 78 } 79 66 80 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 67 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);68 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);81 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 82 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 69 83 70 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);71 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());84 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 85 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 72 86 } 73 87 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer.cs
r8550 r8594 33 33 [Item("SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer", "An operator that collects the validation Pareto-best symbolic classification solutions for single objective symbolic classification problems.")] 34 34 [StorableClass] 35 public sealed class SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData> {35 public sealed class SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData>, ISymbolicClassificationModelCreatorOperator { 36 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 37 private const string ModelCreatorParameterName = "ModelCreator"; 37 38 #region parameter properties 38 39 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 39 40 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 41 } 42 public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 43 get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 44 } 45 ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter { 46 get { return ModelCreatorParameter; } 40 47 } 41 48 #endregion … … 53 60 : base() { 54 61 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false))); 62 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 55 63 } 56 64 public override IDeepCloneable Clone(Cloner cloner) { … … 58 66 } 59 67 68 [StorableHook(HookType.AfterDeserialization)] 69 private void AfterDeserialization() { 70 if (!Parameters.ContainsKey(ModelCreatorParameterName)) 71 Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "")); 72 } 73 60 74 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 var model = new SymbolicDiscriminantFunctionClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);62 if (ApplyLinearScaling.Value) Symbolic DiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);75 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 76 if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue); 63 77 64 model. SetAccuracyMaximizingThresholds(ProblemDataParameter.ActualValue);65 return new SymbolicDiscriminantFunctionClassificationSolution(model,(IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());78 model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices); 79 return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone()); 66 80 } 67 81 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs
r8528 r8594 32 32 [StorableClass] 33 33 [Item(Name = "SymbolicClassificationModel", Description = "Represents a symbolic classification model.")] 34 public class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel { 34 public abstract class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel { 35 [Storable] 36 private double lowerEstimationLimit; 37 public double LowerEstimationLimit { get { return lowerEstimationLimit; } } 38 [Storable] 39 private double upperEstimationLimit; 40 public double UpperEstimationLimit { get { return upperEstimationLimit; } } 41 35 42 [StorableConstructor] 36 43 protected SymbolicClassificationModel(bool deserializing) : base(deserializing) { } 37 44 protected SymbolicClassificationModel(SymbolicClassificationModel original, Cloner cloner) 38 45 : base(original, cloner) { 46 lowerEstimationLimit = original.lowerEstimationLimit; 47 upperEstimationLimit = original.upperEstimationLimit; 39 48 } 40 p ublic SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter)49 protected SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue) 41 50 : base(tree, interpreter) { 51 this.lowerEstimationLimit = lowerEstimationLimit; 52 this.upperEstimationLimit = upperEstimationLimit; 42 53 } 43 54 44 public override IDeepCloneable Clone(Cloner cloner) { 45 return new SymbolicClassificationModel(this, cloner); 46 } 55 public abstract IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows); 56 public abstract void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows); 47 57 48 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 49 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows); 50 } 58 public abstract ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData); 51 59 52 public ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {53 return new SymbolicClassificationSolution(this, new ClassificationProblemData(problemData));54 }55 60 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 56 61 return CreateClassificationSolution(problemData); 57 62 } 58 63 64 #region scaling 65 public static void Scale(ISymbolicClassificationModel model, IClassificationProblemData problemData) { 66 var dataset = problemData.Dataset; 67 var targetVariable = problemData.TargetVariable; 68 var rows = problemData.TrainingIndices; 69 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows); 70 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 71 double alpha; 72 double beta; 73 OnlineCalculatorError errorState; 74 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState); 75 if (errorState != OnlineCalculatorError.None) return; 59 76 77 ConstantTreeNode alphaTreeNode = null; 78 ConstantTreeNode betaTreeNode = null; 79 // check if model has been scaled previously by analyzing the structure of the tree 80 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0); 81 if (startNode.GetSubtree(0).Symbol is Addition) { 82 var addNode = startNode.GetSubtree(0); 83 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) { 84 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 85 var mulNode = addNode.GetSubtree(0); 86 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 87 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 88 } 89 } 90 } 91 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 92 if (alphaTreeNode != null && betaTreeNode != null) { 93 betaTreeNode.Value *= beta; 94 alphaTreeNode.Value *= beta; 95 alphaTreeNode.Value += alpha; 96 } else { 97 var mainBranch = startNode.GetSubtree(0); 98 startNode.RemoveSubtree(0); 99 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha); 100 startNode.AddSubtree(scaledMainBranch); 101 } 102 } 103 104 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) { 105 if (alpha.IsAlmost(0.0)) { 106 return treeNode; 107 } else { 108 var addition = new Addition(); 109 var node = addition.CreateTreeNode(); 110 var alphaConst = MakeConstant(alpha); 111 node.AddSubtree(treeNode); 112 node.AddSubtree(alphaConst); 113 return node; 114 } 115 } 116 117 private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) { 118 if (beta.IsAlmost(1.0)) { 119 return treeNode; 120 } else { 121 var multipliciation = new Multiplication(); 122 var node = multipliciation.CreateTreeNode(); 123 var betaConst = MakeConstant(beta); 124 node.AddSubtree(treeNode); 125 node.AddSubtree(betaConst); 126 return node; 127 } 128 } 129 130 private static ISymbolicExpressionTreeNode MakeConstant(double c) { 131 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode(); 132 node.Value = c; 133 return node; 134 } 135 #endregion 60 136 } 61 137 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs
r8550 r8594 34 34 [StorableClass] 35 35 [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")] 36 public class SymbolicDiscriminantFunctionClassificationModel : Symbolic DataAnalysisModel, ISymbolicDiscriminantFunctionClassificationModel {36 public class SymbolicDiscriminantFunctionClassificationModel : SymbolicClassificationModel, ISymbolicDiscriminantFunctionClassificationModel { 37 37 38 38 [Storable] … … 48 48 private set { classValues = value.ToArray(); } 49 49 } 50 51 private IDiscriminantFunctionThresholdCalculator thresholdCalculator; 50 52 [Storable] 51 p rivate double lowerEstimationLimit;52 public double LowerEstimationLimit { get { return lowerEstimationLimit; }}53 [Storable]54 private double upperEstimationLimit;55 public double UpperEstimationLimit { get { return upperEstimationLimit; } } 53 public IDiscriminantFunctionThresholdCalculator ThresholdCalculator { 54 get { return thresholdCalculator; } 55 private set { thresholdCalculator = value; } 56 } 57 56 58 57 59 [StorableConstructor] … … 61 63 classValues = (double[])original.classValues.Clone(); 62 64 thresholds = (double[])original.thresholds.Clone(); 63 lowerEstimationLimit = original.lowerEstimationLimit; 64 upperEstimationLimit = original.upperEstimationLimit; 65 thresholdCalculator = cloner.Clone(original.thresholdCalculator); 65 66 } 66 public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 67 public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDiscriminantFunctionThresholdCalculator thresholdCalculator, 67 68 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue) 68 : base(tree, interpreter ) {69 : base(tree, interpreter, lowerEstimationLimit, upperEstimationLimit) { 69 70 this.thresholds = new double[0]; 70 71 this.classValues = new double[0]; 71 this.lowerEstimationLimit = lowerEstimationLimit; 72 this.upperEstimationLimit = upperEstimationLimit; 72 this.ThresholdCalculator = thresholdCalculator; 73 } 74 75 [StorableHook(HookType.AfterDeserialization)] 76 private void AfterDeserialization() { 77 if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator(); 73 78 } 74 79 … … 87 92 } 88 93 89 public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) { 90 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit); 94 public override void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) { 95 double[] classValues; 96 double[] thresholds; 97 var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 98 var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows); 99 thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds); 100 SetThresholdsAndClassValues(thresholds, classValues); 91 101 } 92 102 93 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 103 public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) { 104 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(LowerEstimationLimit, UpperEstimationLimit); 105 } 106 107 public override IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 94 108 if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model."); 95 109 foreach (var x in GetEstimatedValues(dataset, rows)) { … … 104 118 } 105 119 106 public SymbolicDiscriminantFunctionClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 120 121 public override ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 122 return CreateDiscriminantClassificationSolution(problemData); 123 } 124 public SymbolicDiscriminantFunctionClassificationSolution CreateDiscriminantClassificationSolution(IClassificationProblemData problemData) { 107 125 return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData)); 108 126 } 109 127 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 110 return Create ClassificationSolution(problemData);128 return CreateDiscriminantClassificationSolution(problemData); 111 129 } 112 130 IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) { 113 return Create ClassificationSolution(problemData);131 return CreateDiscriminantClassificationSolution(problemData); 114 132 } 115 133 … … 121 139 } 122 140 #endregion 123 124 public void SetAccuracyMaximizingThresholds(IClassificationProblemData problemData) {125 double[] classValues;126 double[] thresholds;127 var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);128 var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);129 AccuracyMaximizationThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);130 131 SetThresholdsAndClassValues(thresholds, classValues);132 }133 134 public void SetClassDistributionCutPointThresholds(IClassificationProblemData problemData) {135 double[] classValues;136 double[] thresholds;137 var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);138 var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);139 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);140 141 SetThresholdsAndClassValues(thresholds, classValues);142 }143 144 public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {145 var dataset = problemData.Dataset;146 var targetVariable = problemData.TargetVariable;147 var rows = problemData.TrainingIndices;148 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);149 var targetValues = dataset.GetDoubleValues(targetVariable, rows);150 double alpha;151 double beta;152 OnlineCalculatorError errorState;153 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);154 if (errorState != OnlineCalculatorError.None) return;155 156 ConstantTreeNode alphaTreeNode = null;157 ConstantTreeNode betaTreeNode = null;158 // check if model has been scaled previously by analyzing the structure of the tree159 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);160 if (startNode.GetSubtree(0).Symbol is Addition) {161 var addNode = startNode.GetSubtree(0);162 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {163 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;164 var mulNode = addNode.GetSubtree(0);165 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {166 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;167 }168 }169 }170 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes171 if (alphaTreeNode != null && betaTreeNode != null) {172 betaTreeNode.Value *= beta;173 alphaTreeNode.Value *= beta;174 alphaTreeNode.Value += alpha;175 } else {176 var mainBranch = startNode.GetSubtree(0);177 startNode.RemoveSubtree(0);178 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);179 startNode.AddSubtree(scaledMainBranch);180 }181 }182 183 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {184 if (alpha.IsAlmost(0.0)) {185 return treeNode;186 } else {187 var addition = new Addition();188 var node = addition.CreateTreeNode();189 var alphaConst = MakeConstant(alpha);190 node.AddSubtree(treeNode);191 node.AddSubtree(alphaConst);192 return node;193 }194 }195 196 private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {197 if (beta.IsAlmost(1.0)) {198 return treeNode;199 } else {200 var multipliciation = new Multiplication();201 var node = multipliciation.CreateTreeNode();202 var betaConst = MakeConstant(beta);203 node.AddSubtree(treeNode);204 node.AddSubtree(betaConst);205 return node;206 }207 }208 209 private static ISymbolicExpressionTreeNode MakeConstant(double c) {210 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();211 node.Value = c;212 return node;213 }214 141 } 215 142 }
Note: See TracChangeset
for help on using the changeset viewer.