Changeset 8531


Ignore:
Timestamp:
08/28/12 13:11:15 (10 years ago)
Author:
mkommend
Message:

#1919: Refactored calculation of thresholds for SymbolicDiscriminantFunctionClassficationModels and removed the automatic recalculation of thresholds during solution creation.

Location:
trunk/sources
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs

    r8139 r8531  
    111111      IClassificationProblemData problemData,
    112112      IEnumerable<int> rows) {
    113       return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
     113      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
     114      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, problemData);
     115      return model;
    114116    }
    115117  }
  • trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs

    r7259 r8531  
    105105
    106106    public static IEnumerable<double> LimitToRange(this IEnumerable<double> values, double min, double max) {
     107      if (min > max) throw new ArgumentException(string.Format("Minimum {0} is larger than maximum {1}.", min, max));
    107108      foreach (var x in values) {
    108109        if (double.IsNaN(x)) yield return (max + min) / 2.0;
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs

    r7662 r8531  
    7575      foreach (Type t in GetTypes(type)) {
    7676        object instance = null;
    77         try { instance = Activator.CreateInstance(t); } catch { }
     77        try { instance = Activator.CreateInstance(t); }
     78        catch { }
    7879        if (instance != null) instances.Add(instance);
    7980      }
     
    137138               where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
    138139               select t;
    139       } catch (TypeLoadException) {
     140      }
     141      catch (TypeLoadException) {
    140142        return Enumerable.Empty<Type>();
    141       } catch (ReflectionTypeLoadException) {
     143      }
     144      catch (ReflectionTypeLoadException) {
    142145        return Enumerable.Empty<Type>();
    143146      }
     
    163166        foreach (var genericArgument in typeGenericArguments) {
    164167          if (otherGenericArguments[i].IsGenericParameter) {
     168            //check class contraint on generic type parameter
     169            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
     170              if (!genericArgument.IsClass) return false;
     171
     172            //check default constructor constraint on generic type parameter
     173            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
     174              if (!genericArgument.IsValueType && genericArgument.GetConstructor(Type.EmptyTypes) == null) return false;
     175
    165176            foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints())
    166177              if (!constraint.IsAssignableFrom(genericArgument)) return false;
     
    173184          if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments)))
    174185            return true;
    175         } catch (Exception) { }
     186        }
     187        catch (Exception) { }
    176188      }
    177189      return false;
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs

    r7586 r8531  
    293293        foreach (var genericArgument in typeGenericArguments) {
    294294          if (otherGenericArguments[i].IsGenericParameter) {
     295            //check class contraint on generic type parameter
     296            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
     297              if (!genericArgument.IsClass) return false;
     298
     299            //check default constructor constraint on generic type parameter
     300            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
     301              if (!genericArgument.IsValueType && genericArgument.GetConstructor(Type.EmptyTypes) == null) return false;
     302
    295303            foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints())
    296304              if (!constraint.IsAssignableFrom(genericArgument)) return false;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs

    r8139 r8531  
    5050
    5151    protected override void UpdateModel(ISymbolicExpressionTree tree) {
    52       Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
     52      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
    5353      // the default policy for setting thresholds in classification models is the accuarcy maximizing policy.
    5454      // This is rather slow to calculate and can lead to a very laggy UI in the interactive solution simplifier.
    5555      // However, since we automatically prune sub-trees based on the threshold reaching the maximum accuracy we must
    5656      // also use maximum accuracy threshold calculation here in order to prevent incoherent behavior of the simplifier.
    57       Content.SetAccuracyMaximizingThresholds();
     57      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, Content.ProblemData);
     58      Content.Model = model;
    5859    }
    5960
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8531  
    7777    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    7878      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       }
     79      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     80
     81      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    8282      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    8383    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8531  
    6666    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQualities) {
    6767      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       }
     68      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     69
     70      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    7171      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    7272    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8531  
    7575    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    7676      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       }
     77      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     78
     79      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    8080      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    8181    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs

    r8169 r8531  
    6060    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    6161      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);
     62      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     63
     64      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    6465      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    6566    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8531  
    6666    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    6767      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       }
     68      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     69
     70      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    7171      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    7272    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r8169 r8531  
    6060    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    6161      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);
     62      if (ApplyLinearScaling.Value) SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     63
     64      SymbolicDiscriminantFunctionClassificationModel.SetAccuracyMaximizingThresholds(model, ProblemDataParameter.ActualValue);
    6465      return new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone());
    6566    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs

    r8528 r8531  
    6767      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
    6868      : base(tree, interpreter) {
    69       thresholds = new double[] { double.NegativeInfinity };
    70       classValues = new double[] { 0.0 };
     69      this.thresholds = new double[0];
     70      this.classValues = new double[0];
    7171      this.lowerEstimationLimit = lowerEstimationLimit;
    7272      this.upperEstimationLimit = upperEstimationLimit;
     
    8888
    8989    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
    90       return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
    91         .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     90      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    9291    }
    9392
    9493    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
     94      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
    9595      foreach (var x in GetEstimatedValues(dataset, rows)) {
    9696        int classIndex = 0;
     
    121121    }
    122122    #endregion
     123
     124    public static void SetAccuracyMaximizingThresholds(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
     125      double[] classValues;
     126      double[] thresholds;
     127      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
     128      var estimatedTrainingValues = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     129      AccuracyMaximizationThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
     130
     131      model.SetThresholdsAndClassValues(thresholds, classValues);
     132    }
     133
     134    public static void SetClassDistibutionCutPointThresholds(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
     135      double[] classValues;
     136      double[] thresholds;
     137      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
     138      var estimatedTrainingValues = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     139      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
     140
     141      model.SetThresholdsAndClassValues(thresholds, classValues);
     142    }
    123143
    124144    public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs

    r8139 r8531  
    5151      valueEvaluationCache = new Dictionary<int, double>();
    5252      classValueEvaluationCache = new Dictionary<int, double>();
    53 
    54       SetAccuracyMaximizingThresholds();
    5553    }
    5654
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolutionBase.cs

    r8139 r8531  
    9696    protected override void OnModelChanged() {
    9797      DeregisterEventHandler();
    98       SetAccuracyMaximizingThresholds();
    9998      RegisterEventHandler();
    10099      base.OnModelChanged();
     
    137136    }
    138137
    139     public void SetAccuracyMaximizingThresholds() {
    140       double[] classValues;
    141       double[] thresholds;
    142       var targetClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
    143       AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
    144 
    145       Model.SetThresholdsAndClassValues(thresholds, classValues);
    146     }
    147 
    148     public void SetClassDistibutionCutPointThresholds() {
    149       double[] classValues;
    150       double[] thresholds;
    151       var targetClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
    152       NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
    153 
    154       Model.SetThresholdsAndClassValues(thresholds, classValues);
    155     }
    156 
    157138    protected virtual void OnModelThresholdsChanged(EventArgs e) {
    158139      CalculateResults();
Note: See TracChangeset for help on using the changeset viewer.