Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/18/14 15:27:38 (11 years ago)
Author:
bburlacu
Message:

#2143: Attempted to fix the problems described above.

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

    r11013 r11025  
    1 using System;
     1#region License Information
     2
     3/* HeuristicLab
     4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     5 *
     6 * This file is part of HeuristicLab.
     7 *
     8 * HeuristicLab is free software: you can redistribute it and/or modify
     9 * it under the terms of the GNU General Public License as published by
     10 * the Free Software Foundation, either version 3 of the License, or
     11 * (at your option) any later version.
     12 *
     13 * HeuristicLab is distributed in the hope that it will be useful,
     14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 * GNU General Public License for more details.
     17 *
     18 * You should have received a copy of the GNU General Public License
     19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     20 */
     21
     22#endregion
     23
     24using System;
    225using System.Linq;
    326using HeuristicLab.Analysis;
     
    2952    private const string PopulationSizeParameterName = "PopulationSize";
    3053    #endregion
     54
    3155    #region private members
    3256    private DataReducer prunedSubtreesReducer;
     
    3660    private EmptyOperator emptyOp;
    3761    #endregion
     62
    3863    #region parameter properties
    3964    public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {
     
    4974      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
    5075    }
    51     private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
    52       get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
    53     }
    5476    public IValueParameter<IntValue> UpdateIntervalParameter {
    5577      get { return (IValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
     
    6890    }
    6991    #endregion
     92
    7093    #region properties
    7194    protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
    72     protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
    7395    protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
    7496    protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
    7597    protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } }
    7698    protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } }
    77     protected IRandom Random { get { return RandomParameter.ActualValue; } }
    78     protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } }
    79     protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } }
    80     #endregion
     99
     100    protected bool PruneOnlyZeroImpactNodes {
     101      get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
     102      set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
     103    }
     104    protected double NodeImpactThreshold {
     105      get { return NodeImpactThresholdParameter.Value.Value; }
     106      set { NodeImpactThresholdParameter.Value.Value = value; }
     107    }
     108    #endregion
     109
    81110    #region IStatefulItem members
    82111    public override void InitializeState() {
     
    92121    [StorableConstructor]
    93122    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { }
     123
    94124    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
    95125      : base(original, cloner) {
     
    125155    }
    126156
    127     private void InitializeOperators() {
    128       prunedSubtreesReducer = new DataReducer();
    129       prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
    130       prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
    131       prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
    132       prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;
    133 
    134       prunedTreesReducer = new DataReducer();
    135       prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;
    136       prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
    137       prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
    138       prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;
    139 
    140       valuesCollector = new DataTableValuesCollector();
    141       valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
    142       valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
    143       valuesCollector.DataTableParameter.ActualName = "Population pruning";
    144 
    145       resultsCollector = new ResultsCollector();
    146       resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
    147       resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
    148 
    149       emptyOp = new EmptyOperator();
    150     }
    151 
    152157    //
    153158    /// <summary>
     
    176181
    177182      var subscopes = ExecutionContext.Scope.SubScopes;
     183      var random = RandomParameter.ActualValue;
    178184
    179185      for (int i = 0; i < subscopes.Count; ++i) {
    180186        IOperator op;
    181         if (range.Start <= i && i < range.End && Random.NextDouble() <= PruningProbability.Value)
     187        if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability.Value)
    182188          op = PruningOperator;
    183189        else op = emptyOp;
     
    204210      return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
    205211    }
     212
     213    private void InitializeOperators() {
     214      prunedSubtreesReducer = new DataReducer();
     215      prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
     216      prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
     217      prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
     218      prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;
     219
     220      prunedTreesReducer = new DataReducer();
     221      prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;
     222      prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
     223      prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
     224      prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;
     225
     226      valuesCollector = new DataTableValuesCollector();
     227      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
     228      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
     229      valuesCollector.DataTableParameter.ActualName = "Population pruning";
     230
     231      resultsCollector = new ResultsCollector();
     232      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
     233      resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
     234
     235      emptyOp = new EmptyOperator();
     236    }
    206237  }
    207238}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs

    r10469 r11025  
    8585    }
    8686    #endregion
     87
    8788    #region properties
    8889    protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
    8990    protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } }
    9091    protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } }
    91     protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } }
    92     protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } }
     92    protected bool PruneOnlyZeroImpactNodes {
     93      get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
     94      set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
     95    }
     96    protected double NodeImpactThreshold {
     97      get { return NodeImpactThresholdParameter.Value.Value; }
     98      set { NodeImpactThresholdParameter.Value.Value = value; }
     99    }
    93100    protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }
    94101    protected DoubleValue Quality { get { return QualityParameter.ActualValue; } }
     
    117124      #endregion
    118125    }
     126
     127    protected abstract ISymbolicDataAnalysisModel CreateModel();
     128
     129    protected abstract double Evaluate(IDataAnalysisModel model);
     130
    119131    public override IOperation Apply() {
    120132      var model = CreateModel();
    121133      var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
    122       var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToList();
    123 
     134      var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
    124135      var prunedSubtrees = 0;
    125136      var prunedTrees = 0;
     
    134145        ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
    135146
    136         if (PruneOnlyZeroImpactNodes.Value && (!impactValue.IsAlmost(0.0))) continue;
    137         else if (NodeImpactThreshold.Value < impactValue) continue;
     147        if (PruneOnlyZeroImpactNodes) {
     148          if (!impactValue.IsAlmost(0.0)) continue;
     149        } else if (NodeImpactThreshold < impactValue) {
     150          continue;
     151        }
    138152
    139         var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
     153        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
     154        constantNode.Value = replacementValue;
     155
    140156        ReplaceWithConstant(node, constantNode);
    141157        i += node.GetLength() - 1; // skip subtrees under the node that was folded
     
    152168      return base.Apply();
    153169    }
     170
    154171    private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
    155172      var parent = original.Parent;
     
    158175      parent.InsertSubtree(i, replacement);
    159176    }
    160     protected abstract ISymbolicDataAnalysisModel CreateModel();
    161     protected abstract double Evaluate(IDataAnalysisModel model);
    162177  }
    163178}
Note: See TracChangeset for help on using the changeset viewer.