Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/12 16:47:25 (12 years ago)
Author:
mkommend
Message:

#1763: merged changes from trunk into the tree simplifier branch.

Location:
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4
Files:
21 edited
5 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.csproj

    r8388 r8915  
    9393  </PropertyGroup>
    9494  <ItemGroup>
    95     <Reference Include="ALGLIB-3.5.0">
    96       <HintPath>..\..\..\..\Trunk\sources\bin\ALGLIB-3.5.0.dll</HintPath>
     95    <Reference Include="ALGLIB-3.6.0">
     96      <HintPath>..\..\..\..\Trunk\sources\bin\ALGLIB-3.6.0.dll</HintPath>
     97      <Private>False</Private>
    9798    </Reference>
    9899    <Reference Include="HeuristicLab.Analysis-3.3">
    99100      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
     101      <Private>False</Private>
    100102    </Reference>
    101103    <Reference Include="HeuristicLab.Collections-3.3">
    102104      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath>
     105      <Private>False</Private>
    103106    </Reference>
    104107    <Reference Include="HeuristicLab.Common-3.3">
    105108      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     109      <Private>False</Private>
     110    </Reference>
     111    <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     112      <SpecificVersion>False</SpecificVersion>
     113      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>
     114      <Private>False</Private>
    106115    </Reference>
    107116    <Reference Include="HeuristicLab.Core-3.3">
    108117      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     118      <Private>False</Private>
    109119    </Reference>
    110120    <Reference Include="HeuristicLab.Data-3.3">
    111121      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
     122      <Private>False</Private>
    112123    </Reference>
    113124    <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4">
    114125      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath>
     126      <Private>False</Private>
    115127    </Reference>
    116128    <Reference Include="HeuristicLab.Operators-3.3">
    117129      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath>
     130      <Private>False</Private>
    118131    </Reference>
    119132    <Reference Include="HeuristicLab.Optimization-3.3">
    120133      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
     134      <Private>False</Private>
    121135    </Reference>
    122136    <Reference Include="HeuristicLab.Parameters-3.3">
    123137      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
     138      <Private>False</Private>
    124139    </Reference>
    125140    <Reference Include="HeuristicLab.Persistence-3.3">
    126141      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
     142      <Private>False</Private>
    127143    </Reference>
    128144    <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    129145      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     146      <Private>False</Private>
    130147    </Reference>
    131148    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4">
    132149      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
     150      <Private>False</Private>
    133151    </Reference>
    134152    <Reference Include="HeuristicLab.Problems.Instances-3.3">
    135153      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
     154      <Private>False</Private>
    136155    </Reference>
    137156    <Reference Include="System" />
     
    150169  </ItemGroup>
    151170  <ItemGroup>
     171    <Compile Include="Interfaces\ISymbolicClassificationModelCreatorOperator.cs" />
     172    <Compile Include="Interfaces\ISymbolicDiscriminantFunctionClassificationModelCreator.cs" />
     173    <Compile Include="Interfaces\ISymbolicClassificationModelCreator.cs" />
    152174    <Compile Include="Interfaces\ISymbolicDiscriminantFunctionClassificationModel.cs" />
     175    <Compile Include="ModelCreators\AccuracyMaximizingThresholdsModelCreator.cs" />
     176    <Compile Include="ModelCreators\NearestNeighborModelCreator.cs" />
     177    <Compile Include="ModelCreators\NormalDistributedThresholdsModelCreator.cs" />
    153178    <Compile Include="MultiObjective\SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs" />
     179    <Compile Include="SymbolicNearestNeighbourClassificationModel.cs" />
    154180    <Compile Include="Plugin.cs" />
     181    <Compile Include="SingleObjective\SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs" />
    155182    <Compile Include="SingleObjective\SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator.cs" />
    156183    <Compile Include="SingleObjective\SymbolicClassificationSingleObjectiveOverfittingAnalyzer.cs" />
     
    178205      <SubType>Code</SubType>
    179206    </Compile>
    180     <Compile Include="SymbolicDiscriminantFunctionClassificationSolutionValuesCalculator.cs" />
    181207    <None Include="HeuristicLab.snk" />
    182208    <None Include="Plugin.cs.frame" />
     
    220246  -->
    221247  <PropertyGroup>
    222     <PreBuildEvent>set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     248    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    223249set ProjectDir=$(ProjectDir)
    224250set SolutionDir=$(SolutionDir)
     
    227253call PreBuildEvent.cmd
    228254</PreBuildEvent>
     255    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     256export ProjectDir=$(ProjectDir)
     257export SolutionDir=$(SolutionDir)
     258
     259$SolutionDir/PreBuildEvent.sh
     260</PreBuildEvent>
    229261  </PropertyGroup>
    230262</Project>
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Interfaces/ISymbolicClassificationModel.cs

    r7259 r8915  
    2020#endregion
    2121
    22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     22using System.Collections.Generic;
    2323namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
    2424  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);
    2529  }
    2630}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Interfaces/ISymbolicDiscriminantFunctionClassificationModel.cs

    r7259 r8915  
    2020#endregion
    2121
    22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2322namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
    2423  public interface ISymbolicDiscriminantFunctionClassificationModel : IDiscriminantFunctionClassificationModel, ISymbolicClassificationModel {
    25     double LowerEstimationLimit { get; }
    26     double UpperEstimationLimit { get; }
     24
    2725  }
    2826}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs

    r7259 r8915  
    6565      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6666      EstimationLimitsParameter.ExecutionContext = context;
     67      ApplyLinearScalingParameter.ExecutionContext = context;
    6768
    6869      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     
    7071      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7172      EstimationLimitsParameter.ExecutionContext = null;
     73      ApplyLinearScalingParameter.ExecutionContext = null;
    7274
    7375      return quality;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs

    r6740 r8915  
    2626      IEnumerable<int> rows = GenerateRowsToEvaluate();
    2727      var solution = SymbolicExpressionTreeParameter.ActualValue;
    28       double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     28      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    2929      QualitiesParameter.ActualValue = new DoubleArray(qualities);
    3030      return base.Apply();
    3131    }
    3232
    33     public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     33    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    3434      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    35       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     35      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    3636      OnlineCalculatorError errorState;
    37       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
    38       if (errorState != OnlineCalculatorError.None) r2 = 0.0;
    39       return new double[] { r2, solution.Length };
     37
     38      double r2;
     39      if (applyLinearScaling) {
     40        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
     41        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
     42        errorState = r2Calculator.ErrorState;
     43        r2 = r2Calculator.RSquared;
     44      } else {
     45        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     46        r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     47      }
     48
     49      if (errorState != OnlineCalculatorError.None) r2 = double.NaN;
     50      return new double[2] { r2, solution.Length };
    4051
    4152    }
     
    4556      EstimationLimitsParameter.ExecutionContext = context;
    4657
    47       double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     58      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    4859
    4960      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveProblem.cs

    r8175 r8915  
    3636    private const string EstimationLimitsParameterName = "EstimationLimits";
    3737    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
    3840
    3941    #region parameter properties
     
    4143      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4244    }
     45    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     46      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     47    }
    4348    #endregion
    4449    #region properties
    4550    public DoubleLimit EstimationLimits {
    4651      get { return EstimationLimitsParameter.Value; }
     52    }
     53    public ISymbolicClassificationModelCreator ModelCreator {
     54      get { return ModelCreatorParameter.Value; }
    4755    }
    4856    #endregion
     
    5866      : base(new ClassificationProblemData(), new SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
    5967      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
     68      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
    6069
     70      ApplyLinearScalingParameter.Value.Value = false;
    6171      EstimationLimitsParameter.Hidden = true;
    6272
     
    7484    [StorableHook(HookType.AfterDeserialization)]
    7585    private void AfterDeserialization() {
     86      // BackwardsCompatibility3.4
     87      #region Backwards compatible code, remove with 3.5
     88      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     89        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
     90      #endregion
    7691      RegisterEventHandlers();
    7792    }
     
    7994    private void RegisterEventHandlers() {
    8095      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
     96      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
    8197    }
    8298
     
    110126    }
    111127
    112     protected new void ParameterizeOperators() {
     128    protected override void ParameterizeOperators() {
    113129      base.ParameterizeOperators();
    114130      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
    115131        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         }
     132        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
     133          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
     134        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
     135          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
    119136      }
    120137    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicClassificationSolution>,
    36     ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
     35    ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator {
    3736    private const string ProblemDataParameterName = "ProblemData";
     37    private const string ModelCreatorParameterName = "ModelCreator";
    3838    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    3939    private const string EstimationLimitsParameterName = "EstimationLimits";
    40     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4140
    4241    #region parameter properties
    4342    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
    4443      get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
     44    }
     45    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     46      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     47    }
     48    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     49      get { return ModelCreatorParameter; }
    4550    }
    4651    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
     
    4954    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    5055      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    51     }
    52     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    53       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    54     }
    55     #endregion
    56 
    57     #region properties
    58     public BoolValue ApplyLinearScaling {
    59       get { return ApplyLinearScalingParameter.Value; }
    6056    }
    6157    #endregion
     
    6763      : base() {
    6864      Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data for the symbolic classification solution."));
     65      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6966      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    7067      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    71       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    7268    }
    7369    public override IDeepCloneable Clone(Cloner cloner) {
     
    7571    }
    7672
     73    [StorableHook(HookType.AfterDeserialization)]
     74    private void AfterDeserialization() {
     75      // BackwardsCompatibility3.4
     76      #region Backwards compatible code, remove with 3.5
     77      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     78        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     79      #endregion
     80    }
     81
    7782    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) {
    80         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    81       }
    82       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     83      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     84      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     85
     86      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     87      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    8388    }
    8489  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationMultiObjectiveEvaluator, IClassificationProblemData>,
    36   ISymbolicDataAnalysisBoundedOperator {
     35  ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator {
     36    private const string ModelCreatorParameterName = "ModelCreator";
    3737    private const string EstimationLimitsParameterName = "EstimationLimits";
    38     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3938
    4039    #region parameter properties
     
    4241      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4342    }
    44     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    45       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     43    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     44      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     45    }
     46    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     47      get { return ModelCreatorParameter; }
    4648    }
    4749    #endregion
    4850
    49     #region properties
    50     public BoolValue ApplyLinearScaling {
    51       get { return ApplyLinearScalingParameter.Value; }
    52     }
    53     #endregion
    5451    [StorableConstructor]
    5552    private SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    5855      : base() {
    5956      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The loewr and upper limit for the estimated values produced by the symbolic classification model."));
    60       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
     57      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6158    }
    6259    public override IDeepCloneable Clone(Cloner cloner) {
     
    6461    }
    6562
     63    [StorableHook(HookType.AfterDeserialization)]
     64    private void AfterDeserialization() {
     65      // BackwardsCompatibility3.4
     66      #region Backwards compatible code, remove with 3.5
     67      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     68        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     69      #endregion
     70    }
     71
    6672    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) {
    69         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    70       }
    71       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     73      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     74      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     75
     76      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     77      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    7278    }
    7379  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Plugin.cs.frame

    r8246 r8915  
    2828  [Plugin("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification","Provides classes to perform symbolic classification (single- or multiobjective).", "3.4.3.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.dll", PluginFileType.Assembly)]
    30   [PluginDependency("HeuristicLab.ALGLIB", "3.5")]
     30  [PluginDependency("HeuristicLab.ALGLIB", "3.6.0")]
    3131  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    3232  [PluginDependency("HeuristicLab.Common", "3.3")]
     33  [PluginDependency("HeuristicLab.Common.Resources", "3.3")]
    3334  [PluginDependency("HeuristicLab.Collections", "3.3")]
    3435  [PluginDependency("HeuristicLab.Core", "3.3")]
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator.cs

    r7259 r8915  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     49      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5050      QualityParameter.ActualValue = new DoubleValue(quality);
    5151      return base.Apply();
    5252    }
    5353
    54     public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     54    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5555      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    56       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    57       IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     56      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     57      OnlineCalculatorError errorState;
    5858
    59       double minClassValue = problemData.ClassValues.OrderBy(x => x).First();
    60       double maxClassValue = problemData.ClassValues.OrderBy(x => x).Last();
     59      double lowestClassValue = problemData.ClassValues.OrderBy(x => x).First();
     60      double upmostClassValue = problemData.ClassValues.OrderByDescending(x => x).First();
    6161
    62       IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
    63       IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
    64       double errorSum = 0.0;
    65       int n = 0;
    66 
    67       // always move forward both enumerators (do not use short-circuit evaluation!)
    68       while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
    69         double estimated = estimatedEnumerator.Current;
    70         double original = originalEnumerator.Current;
    71         double error = estimated - original;
    72 
    73         if (estimated < minClassValue || estimated > maxClassValue)
    74           errorSum += Math.Abs(error);
    75         else
    76           errorSum += Math.Pow(error, 2);
    77         n++;
     62      double boundedMse;
     63      if (applyLinearScaling) {
     64        var boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowestClassValue, upmostClassValue);
     65        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, boundedMseCalculator, problemData.Dataset.Rows);
     66        errorState = boundedMseCalculator.ErrorState;
     67        boundedMse = boundedMseCalculator.BoundedMeanSquaredError;
     68      } else {
     69        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     70        boundedMse = OnlineBoundedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, lowestClassValue, upmostClassValue, out errorState);
    7871      }
    79 
    80       // check if both enumerators are at the end to make sure both enumerations have the same length
    81       if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
    82         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    83       } else {
    84         return errorSum / n;
    85       }
     72      if (errorState != OnlineCalculatorError.None) return Double.NaN;
     73      return boundedMse;
    8674    }
    8775
     
    8977      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    9078      EstimationLimitsParameter.ExecutionContext = context;
     79      ApplyLinearScalingParameter.ExecutionContext = context;
    9180
    92       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     81      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    9382
    9483      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    9584      EstimationLimitsParameter.ExecutionContext = null;
     85      ApplyLinearScalingParameter.ExecutionContext = null;
    9686
    9787      return mse;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator.cs

    r7259 r8915  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
     
    4748      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4849      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     50      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5051      QualityParameter.ActualValue = new DoubleValue(quality);
    5152      return base.Apply();
    5253    }
    5354
    54     public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     55    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5556      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    56       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    57       IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     57      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5858      OnlineCalculatorError errorState;
    59       double mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState);
    60       if (errorState != OnlineCalculatorError.None) return double.NaN;
    61       else return mse;
     59
     60      double mse;
     61      if (applyLinearScaling) {
     62        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
     63        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
     64        errorState = mseCalculator.ErrorState;
     65        mse = mseCalculator.MeanSquaredError;
     66      } else {
     67        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     68        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     69      }
     70      if (errorState != OnlineCalculatorError.None) return Double.NaN;
     71      return mse;
    6272    }
    6373
     
    6575      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6676      EstimationLimitsParameter.ExecutionContext = context;
     77      ApplyLinearScalingParameter.ExecutionContext = context;
    6778
    68       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     79      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6980
    7081      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7182      EstimationLimitsParameter.ExecutionContext = null;
     83      ApplyLinearScalingParameter.ExecutionContext = null;
    7284
    7385      return mse;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePearsonRSquaredEvaluator.cs

    r7259 r8915  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     49      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5050      QualityParameter.ActualValue = new DoubleValue(quality);
    5151      return base.Apply();
    5252    }
    5353
    54     public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     54    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5555      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    56       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     56      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5757      OnlineCalculatorError errorState;
    58       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
    59       if (errorState != OnlineCalculatorError.None) return 0.0;
    60       else return r2;
     58
     59      double r2;
     60      if (applyLinearScaling) {
     61        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
     62        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
     63        errorState = r2Calculator.ErrorState;
     64        r2 = r2Calculator.RSquared;
     65      } else {
     66        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     67        r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     68      }
     69      if (errorState != OnlineCalculatorError.None) return double.NaN;
     70      return r2;
    6171    }
    6272
     
    6474      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6575      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    6677
    67       double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     78      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6879
    6980      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7081      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    7183
    7284      return r2;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs

    r8175 r8915  
    3535    private const string EstimationLimitsParameterName = "EstimationLimits";
    3636    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";
    3738
    3839    #region parameter properties
     
    4041      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4142    }
     43    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     44      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     45    }
    4246    #endregion
    4347    #region properties
    4448    public DoubleLimit EstimationLimits {
    4549      get { return EstimationLimitsParameter.Value; }
     50    }
     51    public ISymbolicClassificationModelCreator ModelCreator {
     52      get { return ModelCreatorParameter.Value; }
    4653    }
    4754    #endregion
     
    5764      : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
    5865      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
     66      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
    5967
     68      ApplyLinearScalingParameter.Value.Value = false;
    6069      EstimationLimitsParameter.Hidden = true;
    6170
     
    7180    [StorableHook(HookType.AfterDeserialization)]
    7281    private void AfterDeserialization() {
    73       RegisterEventHandlers();
    74       // compatibility
     82      // BackwardsCompatibility3.4
     83      #region Backwards compatible code, remove with 3.5
     84      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     85        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
     86
    7587      bool changed = false;
    7688      if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
     
    8395      }
    8496      if (changed) ParameterizeOperators();
     97      #endregion
     98      RegisterEventHandlers();
    8599    }
    86100
    87101    private void RegisterEventHandlers() {
    88102      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
     103      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
    89104    }
    90105
     
    125140      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
    126141        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
    127         foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
     142        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
    128143          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
    129         }
     144        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
     145          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
    130146      }
    131147    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<ISymbolicClassificationSolution>,
    36     ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
     35    ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator {
    3736    private const string ProblemDataParameterName = "ProblemData";
     37    private const string ModelCreatorParameterName = "ModelCreator";
    3838    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    3939    private const string EstimationLimitsParameterName = "UpperEstimationLimit";
    40     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4140    #region parameter properties
    4241    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
    4342      get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
     43    }
     44    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     45      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     46    }
     47    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     48      get { return ModelCreatorParameter; }
    4449    }
    4550    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
     
    4954      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    5055    }
    51     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    52       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    53     }
    5456    #endregion
    55     #region properties
    56     public BoolValue ApplyLinearScaling {
    57       get { return ApplyLinearScalingParameter.Value; }
    58     }
    59     #endregion
     57
    6058
    6159    [StorableConstructor]
     
    6563      : base() {
    6664      Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data for the symbolic classification solution."));
     65      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6766      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    6867      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    69       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    7068    }
     69
    7170    public override IDeepCloneable Clone(Cloner cloner) {
    7271      return new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer(this, cloner);
    7372    }
     73    [StorableHook(HookType.AfterDeserialization)]
     74    private void AfterDeserialization() {
     75      // BackwardsCompatibility3.4
     76      #region Backwards compatible code, remove with 3.5
     77      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     78        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     79      #endregion
     80    }
    7481
    7582    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) {
    78         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    79       }
    80       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     83      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     84      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     85
     86      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     87      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    8188    }
    8289  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs

    r8169 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3332  [Item("SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that collects the training Pareto-best symbolic classification solutions for single objective symbolic classification problems.")]
    3433  [StorableClass]
    35   public sealed class SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IClassificationProblemData, ISymbolicClassificationSolution> {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
     34  public sealed class SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IClassificationProblemData, ISymbolicClassificationSolution>, ISymbolicClassificationModelCreatorOperator {
     35    private const string ModelCreatorParameterName = "ModelCreator";
    3736    #region parameter properties
    38     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    39       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     37    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     38      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
    4039    }
    41     #endregion
    42 
    43     #region properties
    44     public BoolValue ApplyLinearScaling {
    45       get { return ApplyLinearScalingParameter.Value; }
     40    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     41      get { return ModelCreatorParameter; }
    4642    }
    4743    #endregion
     
    5248    public SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer()
    5349      : base() {
    54       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
     50      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    5551    }
    5652    public override IDeepCloneable Clone(Cloner cloner) {
     
    5854    }
    5955
     56    [StorableHook(HookType.AfterDeserialization)]
     57    private void AfterDeserialization() {
     58      // BackwardsCompatibility3.4
     59      #region Backwards compatible code, remove with 3.5
     60      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     61        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     62      #endregion
     63    }
     64
    6065    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)
    63         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    64       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     66      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     67      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     68
     69      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     70      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    6571    }
    6672  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData>,
    36   ISymbolicDataAnalysisBoundedOperator {
     35  ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator {
    3736    private const string EstimationLimitsParameterName = "EstimationLimits";
    38     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
     37    private const string ModelCreatorParameterName = "ModelCreator";
    3938
    4039    #region parameter properties
     
    4241      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4342    }
    44     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    45       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     43    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     44      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     45    }
     46    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     47      get { return ModelCreatorParameter; }
    4648    }
    4749    #endregion
    4850
    49     #region properties
    50     public BoolValue ApplyLinearScaling {
    51       get { return ApplyLinearScalingParameter.Value; }
    52     }
    53     #endregion
    5451    [StorableConstructor]
    5552    private SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    5855      : base() {
    5956      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    60       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
     57      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6158    }
    6259    public override IDeepCloneable Clone(Cloner cloner) {
     
    6461    }
    6562
     63    [StorableHook(HookType.AfterDeserialization)]
     64    private void AfterDeserialization() {
     65      // BackwardsCompatibility3.4
     66      #region Backwards compatible code, remove with 3.5
     67      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     68        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     69      #endregion
     70    }
     71
    6672    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) {
    69         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    70       }
    71       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     73      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     74      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     75
     76      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     77      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    7278    }
    7379  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r8169 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3332  [Item("SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer", "An operator that collects the validation Pareto-best symbolic classification solutions for single objective symbolic classification problems.")]
    3433  [StorableClass]
    35   public sealed class SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData> {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
     34  public sealed class SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData>, ISymbolicClassificationModelCreatorOperator {
     35    private const string ModelCreatorParameterName = "ModelCreator";
    3736    #region parameter properties
    38     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    39       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     37    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     38      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
    4039    }
    41     #endregion
    42 
    43     #region properties
    44     public BoolValue ApplyLinearScaling {
    45       get { return ApplyLinearScalingParameter.Value; }
     40    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
     41      get { return ModelCreatorParameter; }
    4642    }
    4743    #endregion
     
    5248    public SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer()
    5349      : base() {
    54       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
     50      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    5551    }
    5652    public override IDeepCloneable Clone(Cloner cloner) {
     
    5854    }
    5955
     56    [StorableHook(HookType.AfterDeserialization)]
     57    private void AfterDeserialization() {
     58      // BackwardsCompatibility3.4
     59      #region Backwards compatible code, remove with 3.5
     60      if (!Parameters.ContainsKey(ModelCreatorParameterName))
     61        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
     62      #endregion
     63    }
     64
    6065    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)
    63         SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    64       return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
     66      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     67      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
     68
     69      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     70      return model.CreateClassificationSolution((IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    6571    }
    6672  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs

    r7259 r8915  
    3232  [StorableClass]
    3333  [Item(Name = "SymbolicClassificationModel", Description = "Represents a symbolic classification model.")]
    34   public class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel {
     34  public abstract class
     35    SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel {
     36    [Storable]
     37    private double lowerEstimationLimit;
     38    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
     39    [Storable]
     40    private double upperEstimationLimit;
     41    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
     42
    3543    [StorableConstructor]
    3644    protected SymbolicClassificationModel(bool deserializing) : base(deserializing) { }
    3745    protected SymbolicClassificationModel(SymbolicClassificationModel original, Cloner cloner)
    3846      : base(original, cloner) {
     47      lowerEstimationLimit = original.lowerEstimationLimit;
     48      upperEstimationLimit = original.upperEstimationLimit;
    3949    }
    40     public SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter)
     50    protected SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
    4151      : base(tree, interpreter) {
     52      this.lowerEstimationLimit = lowerEstimationLimit;
     53      this.upperEstimationLimit = upperEstimationLimit;
    4254    }
    4355
    44     public override IDeepCloneable Clone(Cloner cloner) {
    45       return new SymbolicClassificationModel(this, cloner);
    46     }
     56    public abstract IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows);
     57    public abstract void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows);
    4758
    48     public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
    49       return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows);
    50     }
     59    public abstract ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
    5160
    52     public ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    53       return new SymbolicClassificationSolution(this, problemData);
    54     }
    5561    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
    5662      return CreateClassificationSolution(problemData);
    5763    }
    58 
    59 
    6064  }
    6165}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolution.cs

    r7259 r8915  
    7171
    7272    protected override void RecalculateResults() {
     73      base.RecalculateResults();
    7374      ModelLength = Model.SymbolicExpressionTree.Length;
    7475      ModelDepth = Model.SymbolicExpressionTree.Depth;
    75       CalculateResults();
    7676    }
    7777  }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs

    r8139 r8915  
    3434  [StorableClass]
    3535  [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
    36   public class SymbolicDiscriminantFunctionClassificationModel : SymbolicDataAnalysisModel, ISymbolicDiscriminantFunctionClassificationModel {
     36  public class SymbolicDiscriminantFunctionClassificationModel : SymbolicClassificationModel, ISymbolicDiscriminantFunctionClassificationModel {
    3737
    3838    [Storable]
     
    4848      private set { classValues = value.ToArray(); }
    4949    }
     50
     51    private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
    5052    [Storable]
    51     private 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
    5658
    5759    [StorableConstructor]
     
    6163      classValues = (double[])original.classValues.Clone();
    6264      thresholds = (double[])original.thresholds.Clone();
    63       lowerEstimationLimit = original.lowerEstimationLimit;
    64       upperEstimationLimit = original.upperEstimationLimit;
     65      thresholdCalculator = cloner.Clone(original.thresholdCalculator);
    6566    }
    66     public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     67    public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDiscriminantFunctionThresholdCalculator thresholdCalculator,
    6768      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
    68       : base(tree, interpreter) {
    69       thresholds = new double[] { double.NegativeInfinity };
    70       classValues = new double[] { 0.0 };
    71       this.lowerEstimationLimit = lowerEstimationLimit;
    72       this.upperEstimationLimit = upperEstimationLimit;
     69      : base(tree, interpreter, lowerEstimationLimit, upperEstimationLimit) {
     70      this.thresholds = new double[0];
     71      this.classValues = new double[0];
     72      this.ThresholdCalculator = thresholdCalculator;
     73    }
     74
     75    [StorableHook(HookType.AfterDeserialization)]
     76    private void AfterDeserialization() {
     77      // BackwardsCompatibility3.4
     78      #region Backwards compatible code, remove with 3.5
     79      if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
     80      #endregion
    7381    }
    7482
     
    8795    }
    8896
    89     public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
    90       return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
    91         .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     97    public override void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
     98      double[] classValues;
     99      double[] thresholds;
     100      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     101      var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
     102      thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
     103      SetThresholdsAndClassValues(thresholds, classValues);
    92104    }
    93105
    94     public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
     106    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
     107      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(LowerEstimationLimit, UpperEstimationLimit);
     108    }
     109
     110    public override IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
     111      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
    95112      foreach (var x in GetEstimatedValues(dataset, rows)) {
    96113        int classIndex = 0;
     
    104121    }
    105122
    106     public SymbolicDiscriminantFunctionClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    107       return new SymbolicDiscriminantFunctionClassificationSolution(this, problemData);
     123
     124    public override ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
     125      return CreateDiscriminantClassificationSolution(problemData);
     126    }
     127    public SymbolicDiscriminantFunctionClassificationSolution CreateDiscriminantClassificationSolution(IClassificationProblemData problemData) {
     128      return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
    108129    }
    109130    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
    110       return CreateClassificationSolution(problemData);
     131      return CreateDiscriminantClassificationSolution(problemData);
    111132    }
    112133    IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
    113       return CreateClassificationSolution(problemData);
     134      return CreateDiscriminantClassificationSolution(problemData);
    114135    }
    115136
     
    121142    }
    122143    #endregion
    123 
    124     public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
    125       var dataset = problemData.Dataset;
    126       var targetVariable = problemData.TargetVariable;
    127       var rows = problemData.TrainingIndices;
    128       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    129       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    130       double alpha;
    131       double beta;
    132       OnlineCalculatorError errorState;
    133       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
    134       if (errorState != OnlineCalculatorError.None) return;
    135 
    136       ConstantTreeNode alphaTreeNode = null;
    137       ConstantTreeNode betaTreeNode = null;
    138       // check if model has been scaled previously by analyzing the structure of the tree
    139       var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    140       if (startNode.GetSubtree(0).Symbol is Addition) {
    141         var addNode = startNode.GetSubtree(0);
    142         if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    143           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    144           var mulNode = addNode.GetSubtree(0);
    145           if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    146             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    147           }
    148         }
    149       }
    150       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    151       if (alphaTreeNode != null && betaTreeNode != null) {
    152         betaTreeNode.Value *= beta;
    153         alphaTreeNode.Value *= beta;
    154         alphaTreeNode.Value += alpha;
    155       } else {
    156         var mainBranch = startNode.GetSubtree(0);
    157         startNode.RemoveSubtree(0);
    158         var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    159         startNode.AddSubtree(scaledMainBranch);
    160       }
    161     }
    162 
    163     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    164       if (alpha.IsAlmost(0.0)) {
    165         return treeNode;
    166       } else {
    167         var addition = new Addition();
    168         var node = addition.CreateTreeNode();
    169         var alphaConst = MakeConstant(alpha);
    170         node.AddSubtree(treeNode);
    171         node.AddSubtree(alphaConst);
    172         return node;
    173       }
    174     }
    175 
    176     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
    177       if (beta.IsAlmost(1.0)) {
    178         return treeNode;
    179       } else {
    180         var multipliciation = new Multiplication();
    181         var node = multipliciation.CreateTreeNode();
    182         var betaConst = MakeConstant(beta);
    183         node.AddSubtree(treeNode);
    184         node.AddSubtree(betaConst);
    185         return node;
    186       }
    187     }
    188 
    189     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    190       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    191       node.Value = c;
    192       return node;
    193     }
    194144  }
    195145}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationSolution.cs

    r7259 r8915  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3536    private const string ModelLengthResultName = "Model Length";
    3637    private const string ModelDepthResultName = "Model Depth";
     38
     39    private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
     40    private const string EstimationLimitsResultName = "Estimation Limits";
     41    private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
     42    private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
     43    private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
     44    private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
     45    private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
     46    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
    3747
    3848    public new ISymbolicDiscriminantFunctionClassificationModel Model {
     
    5767      private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
    5868    }
     69
     70    private ResultCollection EstimationLimitsResultCollection {
     71      get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
     72    }
     73    public DoubleLimit EstimationLimits {
     74      get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
     75    }
     76
     77    public int TrainingUpperEstimationLimitHits {
     78      get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
     79      private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
     80    }
     81    public int TestUpperEstimationLimitHits {
     82      get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
     83      private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
     84    }
     85    public int TrainingLowerEstimationLimitHits {
     86      get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
     87      private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
     88    }
     89    public int TestLowerEstimationLimitHits {
     90      get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
     91      private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
     92    }
     93    public int TrainingNaNEvaluations {
     94      get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
     95      private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
     96    }
     97    public int TestNaNEvaluations {
     98      get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
     99      private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
     100    }
     101
    59102    [StorableConstructor]
    60103    private SymbolicDiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { }
     
    66109      Add(new Result(ModelLengthResultName, "Length of the symbolic classification model.", new IntValue()));
    67110      Add(new Result(ModelDepthResultName, "Depth of the symbolic classification model.", new IntValue()));
    68       RecalculateResults();
     111
     112
     113      ResultCollection estimationLimitResults = new ResultCollection();
     114      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
     115      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
     116      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
     117      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
     118      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
     119      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
     120      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
     121      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
     122
     123      CalculateResults();
    69124    }
    70125
     
    73128    }
    74129
    75     protected override void RecalculateResults() {
    76       CalculateResults();
    77       CalculateRegressionResults();
     130    [StorableHook(HookType.AfterDeserialization)]
     131    private void AfterDeserialization() {
     132      if (!ContainsKey(EstimationLimitsResultsResultName)) {
     133        ResultCollection estimationLimitResults = new ResultCollection();
     134        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
     135        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
     136        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
     137        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
     138        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
     139        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
     140        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
     141        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
     142        CalculateResults();
     143      }
     144    }
     145
     146
     147    private void CalculateResults() {
    78148      ModelLength = Model.SymbolicExpressionTree.Length;
    79149      ModelDepth = Model.SymbolicExpressionTree.Depth;
     150
     151      EstimationLimits.Lower = Model.LowerEstimationLimit;
     152      EstimationLimits.Upper = Model.UpperEstimationLimit;
     153
     154      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
     155      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
     156      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
     157      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
     158      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
     159      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
     160    }
     161
     162    protected override void RecalculateResults() {
     163      base.RecalculateResults();
     164      CalculateResults();
    80165    }
    81166  }
Note: See TracChangeset for help on using the changeset viewer.