Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/07/12 11:27:49 (12 years ago)
Author:
mkommend
Message:

#1940: Added support in symbolic classification for different methods to create the classification ModelCreator.

  • Added ModelCreators
  • Refactored SymbolicClassificationModel and SymbolicDiscriminantFunctionClassificationModel
  • Added ModelCreatorParameter to Analyzers and Evaluators if needed
  • Corrected wiring in symbolic classification problems (single- and multiobjective
  • Adapted simplifier
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs

    r8528 r8594  
    3232  [StorableClass]
    3333  [Item(Name = "SymbolicClassificationModel", Description = "Represents a symbolic classification model.")]
    34   public class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel {
     34  public abstract class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel {
     35    [Storable]
     36    private double lowerEstimationLimit;
     37    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
     38    [Storable]
     39    private double upperEstimationLimit;
     40    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
     41
    3542    [StorableConstructor]
    3643    protected SymbolicClassificationModel(bool deserializing) : base(deserializing) { }
    3744    protected SymbolicClassificationModel(SymbolicClassificationModel original, Cloner cloner)
    3845      : base(original, cloner) {
     46      lowerEstimationLimit = original.lowerEstimationLimit;
     47      upperEstimationLimit = original.upperEstimationLimit;
    3948    }
    40     public SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter)
     49    protected SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
    4150      : base(tree, interpreter) {
     51      this.lowerEstimationLimit = lowerEstimationLimit;
     52      this.upperEstimationLimit = upperEstimationLimit;
    4253    }
    4354
    44     public override IDeepCloneable Clone(Cloner cloner) {
    45       return new SymbolicClassificationModel(this, cloner);
    46     }
     55    public abstract IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows);
     56    public abstract void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows);
    4757
    48     public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
    49       return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows);
    50     }
     58    public abstract ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
    5159
    52     public ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    53       return new SymbolicClassificationSolution(this, new ClassificationProblemData(problemData));
    54     }
    5560    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
    5661      return CreateClassificationSolution(problemData);
    5762    }
    5863
     64    #region scaling
     65    public static void Scale(ISymbolicClassificationModel model, IClassificationProblemData problemData) {
     66      var dataset = problemData.Dataset;
     67      var targetVariable = problemData.TargetVariable;
     68      var rows = problemData.TrainingIndices;
     69      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
     70      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
     71      double alpha;
     72      double beta;
     73      OnlineCalculatorError errorState;
     74      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
     75      if (errorState != OnlineCalculatorError.None) return;
    5976
     77      ConstantTreeNode alphaTreeNode = null;
     78      ConstantTreeNode betaTreeNode = null;
     79      // check if model has been scaled previously by analyzing the structure of the tree
     80      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
     81      if (startNode.GetSubtree(0).Symbol is Addition) {
     82        var addNode = startNode.GetSubtree(0);
     83        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
     84          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
     85          var mulNode = addNode.GetSubtree(0);
     86          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
     87            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
     88          }
     89        }
     90      }
     91      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     92      if (alphaTreeNode != null && betaTreeNode != null) {
     93        betaTreeNode.Value *= beta;
     94        alphaTreeNode.Value *= beta;
     95        alphaTreeNode.Value += alpha;
     96      } else {
     97        var mainBranch = startNode.GetSubtree(0);
     98        startNode.RemoveSubtree(0);
     99        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
     100        startNode.AddSubtree(scaledMainBranch);
     101      }
     102    }
     103
     104    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     105      if (alpha.IsAlmost(0.0)) {
     106        return treeNode;
     107      } else {
     108        var addition = new Addition();
     109        var node = addition.CreateTreeNode();
     110        var alphaConst = MakeConstant(alpha);
     111        node.AddSubtree(treeNode);
     112        node.AddSubtree(alphaConst);
     113        return node;
     114      }
     115    }
     116
     117    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
     118      if (beta.IsAlmost(1.0)) {
     119        return treeNode;
     120      } else {
     121        var multipliciation = new Multiplication();
     122        var node = multipliciation.CreateTreeNode();
     123        var betaConst = MakeConstant(beta);
     124        node.AddSubtree(treeNode);
     125        node.AddSubtree(betaConst);
     126        return node;
     127      }
     128    }
     129
     130    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     131      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     132      node.Value = c;
     133      return node;
     134    }
     135    #endregion
    60136  }
    61137}
Note: See TracChangeset for help on using the changeset viewer.