Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12358


Ignore:
Timestamp:
04/29/15 11:56:50 (9 years ago)
Author:
mkommend
Message:

#2359: Refactored pruning operators and analyzers.

Location:
trunk/sources
Files:
6 edited

Legend:

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

    r12189 r12358  
    2929  [StorableClass]
    3030  public sealed class SymbolicClassificationPruningAnalyzer : SymbolicDataAnalysisSingleObjectivePruningAnalyzer {
    31     private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator";
    3231    private const string PruningOperatorParameterName = "PruningOperator";
    33     private SymbolicClassificationPruningAnalyzer(SymbolicClassificationPruningAnalyzer original, Cloner cloner)
    34       : base(original, cloner) {
     32    public IValueParameter<SymbolicClassificationPruningOperator> PruningOperatorParameter {
     33      get { return (IValueParameter<SymbolicClassificationPruningOperator>)Parameters[PruningOperatorParameterName]; }
    3534    }
    36     public override IDeepCloneable Clone(Cloner cloner) {
    37       return new SymbolicClassificationPruningAnalyzer(this, cloner);
     35
     36    protected override SymbolicDataAnalysisExpressionPruningOperator PruningOperator {
     37      get { return PruningOperatorParameter.Value; }
    3838    }
     39
     40    private SymbolicClassificationPruningAnalyzer(SymbolicClassificationPruningAnalyzer original, Cloner cloner) : base(original, cloner) { }
     41    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationPruningAnalyzer(this, cloner); }
    3942
    4043    [StorableConstructor]
     
    4245
    4346    public SymbolicClassificationPruningAnalyzer() {
    44       Parameters.Add(new ValueParameter<SymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator", new SymbolicClassificationSolutionImpactValuesCalculator()));
    4547      Parameters.Add(new ValueParameter<SymbolicDataAnalysisExpressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicClassificationPruningOperator(new SymbolicClassificationSolutionImpactValuesCalculator())));
    4648    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationPruningOperator.cs

    r12189 r12358  
    4242    #endregion
    4343
    44     protected SymbolicClassificationPruningOperator(SymbolicClassificationPruningOperator original, Cloner cloner)
    45       : base(original, cloner) {
    46     }
    47 
    48     public override IDeepCloneable Clone(Cloner cloner) {
    49       return new SymbolicClassificationPruningOperator(this, cloner);
    50     }
     44    protected SymbolicClassificationPruningOperator(SymbolicClassificationPruningOperator original, Cloner cloner) : base(original, cloner) { }
     45    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationPruningOperator(this, cloner); }
    5146
    5247    [StorableConstructor]
     
    6863    protected override double Evaluate(IDataAnalysisModel model) {
    6964      var classificationModel = (IClassificationModel)model;
    70       var classificationProblemData = (IClassificationProblemData)ProblemData;
    71       var trainingIndices = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     65      var classificationProblemData = (IClassificationProblemData)ProblemDataParameter.ActualValue;
     66      var rows = Enumerable.Range(FitnessCalculationPartitionParameter.ActualValue.Start, FitnessCalculationPartitionParameter.ActualValue.Size);
    7267
    73       return Evaluate(classificationModel, classificationProblemData, trainingIndices);
     68      return Evaluate(classificationModel, classificationProblemData, rows);
    7469    }
    7570
     
    10095        impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
    10196
    102         if (pruneOnlyZeroImpactNodes) {
    103           if (!impactValue.IsAlmost(0.0)) continue;
    104         } else if (nodeImpactThreshold < impactValue) {
    105           continue;
    106         }
     97        if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
     98        if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
    10799
    108100        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningAnalyzer.cs

    r12189 r12358  
    3131  [StorableClass]
    3232  public sealed class SymbolicRegressionPruningAnalyzer : SymbolicDataAnalysisSingleObjectivePruningAnalyzer {
    33     private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator";
    3433    private const string PruningOperatorParameterName = "PruningOperator";
    35     private SymbolicRegressionPruningAnalyzer(SymbolicRegressionPruningAnalyzer original, Cloner cloner)
    36       : base(original, cloner) {
     34    public IValueParameter<SymbolicRegressionPruningOperator> PruningOperatorParameter {
     35      get { return (IValueParameter<SymbolicRegressionPruningOperator>)Parameters[PruningOperatorParameterName]; }
    3736    }
    38     public override IDeepCloneable Clone(Cloner cloner) {
    39       return new SymbolicRegressionPruningAnalyzer(this, cloner);
     37
     38    protected override SymbolicDataAnalysisExpressionPruningOperator PruningOperator {
     39      get { return PruningOperatorParameter.Value; }
    4040    }
     41
     42    private SymbolicRegressionPruningAnalyzer(SymbolicRegressionPruningAnalyzer original, Cloner cloner) : base(original, cloner) { }
     43    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionPruningAnalyzer(this, cloner); }
    4144
    4245    [StorableConstructor]
     
    4447
    4548    public SymbolicRegressionPruningAnalyzer() {
    46       Parameters.Add(new ValueParameter<SymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator", new SymbolicRegressionSolutionImpactValuesCalculator()));
    47       Parameters.Add(new ValueParameter<SymbolicDataAnalysisExpressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicRegressionPruningOperator(new SymbolicRegressionSolutionImpactValuesCalculator())));
     49      Parameters.Add(new ValueParameter<SymbolicRegressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicRegressionPruningOperator(new SymbolicRegressionSolutionImpactValuesCalculator())));
    4850    }
    4951  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningOperator.cs

    r12189 r12358  
    5353    protected override double Evaluate(IDataAnalysisModel model) {
    5454      var regressionModel = (IRegressionModel)model;
    55       var regressionProblemData = (IRegressionProblemData)ProblemData;
    56       var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     55      var regressionProblemData = (IRegressionProblemData)ProblemDataParameter.ActualValue;
     56      var rows = Enumerable.Range(FitnessCalculationPartitionParameter.ActualValue.Start, FitnessCalculationPartitionParameter.ActualValue.Size);
    5757      return Evaluate(regressionModel, regressionProblemData, rows);
    5858    }
     
    8181        impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
    8282
    83         if (pruneOnlyZeroImpactNodes) {
    84           if (!impactValue.IsAlmost(0.0)) continue;
    85         } else if (nodeImpactThreshold < impactValue) {
    86           continue;
    87         }
     83        if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
     84        if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
    8885
    8986        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs

    r12012 r12358  
    6161
    6262    #region parameter properties
    63     public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {
    64       get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; }
    65     }
    66     public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {
    67       get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }
    68     }
    69     public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {
    70       get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
    71     }
    7263    public ILookupParameter<IRandom> RandomParameter {
    7364      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
     
    9182
    9283    #region properties
    93     protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
     84    protected abstract SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get; }
    9485    protected int UpdateInterval { get { return UpdateIntervalParameter.Value.Value; } }
    9586
     
    112103      get { return PruningProbabilityParameter.Value.Value; }
    113104      set { PruningProbabilityParameter.Value.Value = value; }
    114     }
    115 
    116     protected bool PruneOnlyZeroImpactNodes {
    117       get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
    118       set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
    119     }
    120     protected double NodeImpactThreshold {
    121       get { return NodeImpactThresholdParameter.Value.Value; }
    122       set { NodeImpactThresholdParameter.Value.Value = value; }
    123105    }
    124106    #endregion
     
    197179      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random number generator."));
    198180      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data."));
    199       Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "The impact threshold  below which an individual should be pruned.", new DoubleValue(0.0)));
    200       Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Switch to determine of only zero impact individuals should be pruned.", new BoolValue(false)));
    201181      Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
    202182      #endregion
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs

    r12189 r12358  
    3434  [StorableClass]
    3535  [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")]
    36   public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator {
     36  public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator, ISymbolicExpressionTreeOperator {
    3737    #region parameter names
    3838    private const string ProblemDataParameterName = "ProblemData";
     
    8787
    8888    #region properties
    89     protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
    90     protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } }
    91     protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } }
    92     protected bool PruneOnlyZeroImpactNodes {
     89    public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator {
     90      get { return ImpactValuesCalculatorParameter.Value; }
     91      set { ImpactValuesCalculatorParameter.Value = value; }
     92    }
     93    public bool PruneOnlyZeroImpactNodes {
    9394      get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
    9495      set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
    9596    }
    96     protected double NodeImpactThreshold {
     97    public double NodeImpactThreshold {
    9798      get { return NodeImpactThresholdParameter.Value.Value; }
    9899      set { NodeImpactThresholdParameter.Value.Value = value; }
    99100    }
    100     protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }
    101     protected DoubleValue Quality { get { return QualityParameter.ActualValue; } }
    102     protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }
    103     protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }
    104101    #endregion
    105102
     
    131128
    132129    public override IOperation Apply() {
    133       var model = CreateModel(SymbolicExpressionTree, Interpreter, ProblemData, EstimationLimits);
    134       var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
    135       var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     130      var tree = SymbolicExpressionTreeParameter.ActualValue;
     131      var problemData = ProblemDataParameter.ActualValue;
     132      var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue;
     133      var estimationLimits = EstimationLimitsParameter.ActualValue;
     134      var interpreter = InterpreterParameter.ActualValue;
     135
     136      var model = CreateModel(tree, interpreter, problemData, estimationLimits);
     137      var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
     138      var rows = Enumerable.Range(fitnessCalculationPartition.Start, fitnessCalculationPartition.Size);
    136139      var prunedSubtrees = 0;
    137140      var prunedTrees = 0;
     
    144147
    145148        double impactValue, replacementValue;
    146         ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
    147 
    148         if (PruneOnlyZeroImpactNodes) {
    149           if (!impactValue.IsAlmost(0.0)) continue;
    150         } else if (NodeImpactThreshold < impactValue) {
    151           continue;
    152         }
     149        ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
     150
     151        if (PruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
     152        if (!PruneOnlyZeroImpactNodes && impactValue > NodeImpactThreshold) continue;
    153153
    154154        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
     
    159159
    160160        quality -= impactValue;
    161 
    162161        prunedSubtrees++;
    163162      }
     
    166165      PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees);
    167166      PrunedTreesParameter.ActualValue = new IntValue(prunedTrees);
     167      QualityParameter.ActualValue.Value = quality;
    168168
    169169      return base.Apply();
    170170    }
    171171
    172     public ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
    173       var model = CreateModel((ISymbolicExpressionTree)tree.Clone(), Interpreter, ProblemData, EstimationLimits);
    174       var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
    175       var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
    176 
    177       double quality = Evaluate(model);
    178 
    179       for (int i = 0; i < nodes.Count; ++i) {
    180         var node = nodes[i];
    181         if (node is ConstantTreeNode) continue;
    182 
    183         double impactValue, replacementValue;
    184         ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
    185 
    186         if (PruneOnlyZeroImpactNodes) {
    187           if (!impactValue.IsAlmost(0.0)) continue;
    188         } else if (NodeImpactThreshold < impactValue) {
    189           continue;
    190         }
    191 
    192         var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
    193         constantNode.Value = replacementValue;
    194 
    195         ReplaceWithConstant(node, constantNode);
    196         i += node.GetLength() - 1; // skip subtrees under the node that was folded
    197 
    198         quality -= impactValue;
    199       }
    200       return model.SymbolicExpressionTree;
    201     }
     172    //public ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
     173    //  var model = CreateModel((ISymbolicExpressionTree)tree.Clone(), interpreter, problemData, estimationLimits);
     174    //  var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
     175    //  var rows = Enumerable.Range(fitnessCalculationPartition.Start, fitnessCalculationPartition.Size);
     176
     177    //  double quality = Evaluate(model);
     178
     179    //  for (int i = 0; i < nodes.Count; ++i) {
     180    //    var node = nodes[i];
     181    //    if (node is ConstantTreeNode) continue;
     182
     183    //    double impactValue, replacementValue;
     184    //    ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
     185
     186    //    if (PruneOnlyZeroImpactNodes) {
     187    //      if (!impactValue.IsAlmost(0.0)) continue;
     188    //    } else if (NodeImpactThreshold < impactValue) {
     189    //      continue;
     190    //    }
     191
     192    //    var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
     193    //    constantNode.Value = replacementValue;
     194
     195    //    ReplaceWithConstant(node, constantNode);
     196    //    i += node.GetLength() - 1; // skip subtrees under the node that was folded
     197
     198    //    quality -= impactValue;
     199    //  }
     200    //  return model.SymbolicExpressionTree;
     201    //}
    202202
    203203    protected static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
Note: See TracChangeset for help on using the changeset viewer.