Free cookie consent management tool by TermsFeed Policy Generator

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

#2359: Refactored pruning operators and analyzers.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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.