Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/12/15 11:23:06 (9 years ago)
Author:
gkronber
Message:

#2359, #2398: merged r12189,r12358,r12359,r12361,r12461,r12674,r12720,r12744 from trunk to stable

Location:
stable
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs

    r12009 r12745  
    4545    private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
    4646    private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
     47    private const string TotalNumberOfPrunedNodesParameterName = "Number of pruned nodes";
    4748    private const string RandomParameterName = "Random";
    48     private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
    49     private const string NodeImpactThresholdParameterName = "ImpactThreshold";
    50     private const string PruningOperatorParameterName = "PruningOperator";
    5149    private const string ResultsParameterName = "Results";
    5250    private const string PopulationSizeParameterName = "PopulationSize";
     
    5452
    5553    #region private members
     54    private DataReducer prunedNodesReducer;
    5655    private DataReducer prunedSubtreesReducer;
    5756    private DataReducer prunedTreesReducer;
     
    6160
    6261    #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     }
    7262    public ILookupParameter<IRandom> RandomParameter {
    7363      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
     
    9181
    9282    #region properties
    93     protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
     83    protected abstract SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get; }
    9484    protected int UpdateInterval { get { return UpdateIntervalParameter.Value.Value; } }
    9585
     
    112102      get { return PruningProbabilityParameter.Value.Value; }
    113103      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; }
    123104    }
    124105    #endregion
     
    140121    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
    141122      : base(original, cloner) {
     123      if (original.prunedNodesReducer != null)
     124        this.prunedNodesReducer = (DataReducer)original.prunedNodesReducer.Clone();
    142125      if (original.prunedSubtreesReducer != null)
    143126        this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone();
     
    197180      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random number generator."));
    198181      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)));
    201182      Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
    202183      #endregion
     
    225206      var qualities = Quality.Select(x => x.Value).ToArray();
    226207      var indices = Enumerable.Range(0, qualities.Length).ToArray();
    227       Array.Sort(qualities, indices);
     208      indices.StableSort((a, b) => qualities[a].CompareTo(qualities[b]));
     209
    228210      if (!Maximization.Value) Array.Reverse(indices);
    229211
     
    233215      var empty = new EmptyOperator();
    234216
    235       for (int i = 0; i < subscopes.Count; ++i) {
     217      for (int i = 0; i < indices.Length; ++i) {
    236218        IOperator @operator;
    237219        if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability)
     
    250232      UpdateCounter = 0;
    251233
    252       if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
     234      if (prunedNodesReducer == null || prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
    253235
    254236      var prune = CreatePruningOperation();
     237      var reducePrunedNodes = ExecutionContext.CreateChildOperation(prunedNodesReducer);
    255238      var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer);
    256239      var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer);
     
    258241      var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
    259242
    260       return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
     243      return new OperationCollection { prune, reducePrunedNodes, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
    261244    }
    262245
    263246    private void InitializeOperators() {
     247      prunedNodesReducer = new DataReducer();
     248      prunedNodesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedNodesParameter.ActualName;
     249      prunedNodesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
     250      prunedNodesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
     251      prunedNodesReducer.TargetParameter.ActualName = TotalNumberOfPrunedNodesParameterName;
     252
    264253      prunedSubtreesReducer = new DataReducer();
    265254      prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
     
    275264
    276265      valuesCollector = new DataTableValuesCollector();
     266      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedNodesParameterName));
    277267      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
    278268      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisImpactValuesCalculator.cs

    r11145 r12745  
    66  public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator : IItem {
    77    double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
    8     double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN);
     8    double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN);
    99    void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData,
    10       IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN);
     10      IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN);
    1111  }
    1212}
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs

    r12009 r12745  
    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";
     
    4141    private const string PrunedSubtreesParameterName = "PrunedSubtrees";
    4242    private const string PrunedTreesParameterName = "PrunedTrees";
     43    private const string PrunedNodesParameterName = "PrunedNodes";
    4344    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
    4445    private const string NodeImpactThresholdParameterName = "ImpactThreshold";
     
    4849    private const string EstimationLimitsParameterName = "EstimationLimits";
    4950    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
     51    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    5052    #endregion
    5153
     
    7274      get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; }
    7375    }
     76    public ILookupParameter<IntValue> PrunedNodesParameter {
     77      get { return (ILookupParameter<IntValue>)Parameters[PrunedNodesParameterName]; }
     78    }
    7479    public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {
    7580      get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
     
    8489      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
    8590    }
     91    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
     92      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     93    }
    8694    #endregion
    8795
    8896    #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 {
     97    public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator {
     98      get { return ImpactValuesCalculatorParameter.Value; }
     99      set { ImpactValuesCalculatorParameter.Value = value; }
     100    }
     101    public bool PruneOnlyZeroImpactNodes {
    93102      get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
    94103      set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
    95104    }
    96     protected double NodeImpactThreshold {
     105    public double NodeImpactThreshold {
    97106      get { return NodeImpactThresholdParameter.Value.Value; }
    98107      set { NodeImpactThresholdParameter.Value.Value = value; }
    99108    }
    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; } }
    104109    #endregion
    105110
     
    109114      : base(original, cloner) { }
    110115
    111     protected SymbolicDataAnalysisExpressionPruningOperator() {
     116    protected SymbolicDataAnalysisExpressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator) {
    112117      #region add parameters
    113118      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
    114119      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName));
    115120      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName));
     121      Parameters.Add(new LookupParameter<IntValue>(PrunedNodesParameterName, "A counter of how many nodes were pruned."));
    116122      Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced."));
    117123      Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned."));
     
    122128      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
    123129      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName));
     130      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
     131      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, impactValuesCalculator));
    124132      #endregion
    125133    }
    126134
    127     protected abstract ISymbolicDataAnalysisModel CreateModel();
     135    [StorableHook(HookType.AfterDeserialization)]
     136    private void AfterDeserialization() {
     137      // BackwardsCompatibility3.3
     138      #region Backwards compatible code, remove with 3.4
     139      if (!Parameters.ContainsKey(PrunedNodesParameterName)) {
     140        Parameters.Add(new LookupParameter<IntValue>(PrunedNodesParameterName, "A counter of how many nodes were pruned."));
     141      }
     142      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
     143        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
     144      }
     145      if (!Parameters.ContainsKey(ImpactValuesCalculatorParameterName)) {
     146        // value must be set by derived operators (regression/classification)
     147        Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName));
     148      }
     149      #endregion
     150    }
     151
     152    protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits);
    128153
    129154    protected abstract double Evaluate(IDataAnalysisModel model);
    130155
    131156    public override IOperation Apply() {
    132       var model = CreateModel();
    133       var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
    134       var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     157      var tree = SymbolicExpressionTreeParameter.ActualValue;
     158      var problemData = ProblemDataParameter.ActualValue;
     159      var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue;
     160      var estimationLimits = EstimationLimitsParameter.ActualValue;
     161      var interpreter = InterpreterParameter.ActualValue;
     162
     163      var model = CreateModel(tree, interpreter, problemData, estimationLimits);
     164      var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
     165      var rows = Enumerable.Range(fitnessCalculationPartition.Start, fitnessCalculationPartition.Size).ToList();
    135166      var prunedSubtrees = 0;
    136167      var prunedTrees = 0;
    137 
    138       double quality = Evaluate(model);
     168      var prunedNodes = 0;
     169
     170      double qualityForImpactsCalculation = double.NaN;
    139171
    140172      for (int i = 0; i < nodes.Count; ++i) {
     
    143175
    144176        double impactValue, replacementValue;
    145         ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
    146 
    147         if (PruneOnlyZeroImpactNodes) {
    148           if (!impactValue.IsAlmost(0.0)) continue;
    149         } else if (NodeImpactThreshold < impactValue) {
    150           continue;
    151         }
     177        double newQualityForImpacts;
     178        ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpacts, qualityForImpactsCalculation);
     179
     180        if (PruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
     181        if (!PruneOnlyZeroImpactNodes && impactValue > NodeImpactThreshold) continue;
    152182
    153183        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
    154184        constantNode.Value = replacementValue;
    155185
     186        var length = node.GetLength();
    156187        ReplaceWithConstant(node, constantNode);
    157         i += node.GetLength() - 1; // skip subtrees under the node that was folded
    158 
    159         quality -= impactValue;
     188        i += length - 1; // skip subtrees under the node that was folded
    160189
    161190        prunedSubtrees++;
     191        prunedNodes += length;
     192
     193        qualityForImpactsCalculation = newQualityForImpacts;
    162194      }
    163195
     
    165197      PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees);
    166198      PrunedTreesParameter.ActualValue = new IntValue(prunedTrees);
     199      PrunedNodesParameter.ActualValue = new IntValue(prunedNodes);
     200
     201      if (prunedSubtrees > 0) // if nothing was pruned then there's no need to re-evaluate the tree
     202        QualityParameter.ActualValue.Value = Evaluate(model);
    167203
    168204      return base.Apply();
    169205    }
    170206
    171     private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
     207    protected static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
    172208      var parent = original.Parent;
    173209      var i = parent.IndexOfSubtree(original);
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs

    r12702 r12745  
    3737    protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
    3838    public abstract double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
    39     public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN);
    40     public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN);
     39    public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN);
     40    public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN);
    4141
    4242    protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
Note: See TracChangeset for help on using the changeset viewer.