Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/12/14 15:23:47 (11 years ago)
Author:
mkommend
Message:

#1997: Merged trunk changes into data analysis island algorithms branch and fixed bugs in the evaluators.

Location:
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs

    r9456 r10579  
    7575    public static Allele[] CalculateAlleles(ISymbolicExpressionTree solution, int alleleTreedepth) {
    7676      return GetAllSubtreesOfDepth(solution, alleleTreedepth)
    77         .AsParallel()
    7877        .Select(t => GetAlleleFromSubtreeOfDepth(t, alleleTreedepth))
    7978        .ToArray();
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs

    r9456 r10579  
    121121
    122122      var qualities = tree
    123         .AsParallel()
    124123        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
    125124        .ToArray();
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs

    r10390 r10579  
    55using HeuristicLab.Core;
    66using HeuristicLab.Data;
    7 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    8 using HeuristicLab.Optimization;
     7using HeuristicLab.Operators;
     8using HeuristicLab.Optimization.Operators;
    99using HeuristicLab.Parameters;
    1010using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    1414  [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")]
    1515  public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
     16    #region parameter names
    1617    private const string ProblemDataParameterName = "ProblemData";
    17     private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
    18 
    1918    private const string UpdateIntervalParameterName = "UpdateInverval";
    2019    private const string UpdateCounterParameterName = "UpdateCounter";
    21 
    2220    private const string PopulationSliceParameterName = "PopulationSlice";
    2321    private const string PruningProbabilityParameterName = "PruningProbability";
    24 
    25     private const string NumberOfPrunedSubtreesParameterName = "PrunedSubtrees";
    26     private const string NumberOfPrunedTreesParameterName = "PrunedTrees";
    27 
     22    private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
     23    private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
    2824    private const string RandomParameterName = "Random";
    29     private const string EstimationLimitsParameterName = "EstimationLimits";
    30 
    3125    private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
    3226    private const string NodeImpactThresholdParameterName = "ImpactThreshold";
    33 
    34     private bool reentry;
    35     [Storable]
    36     protected ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator;
    37 
     27    private const string PruningOperatorParameterName = "PruningOperator";
     28    private const string ResultsParameterName = "Results";
     29    #endregion
     30    #region private members
     31    private DataReducer prunedSubtreesReducer;
     32    private DataReducer prunedTreesReducer;
     33    private DataTableValuesCollector valuesCollector;
     34    private ResultsCollector resultsCollector;
     35    private EmptyOperator emptyOp;
     36    #endregion
    3837    #region parameter properties
     38    public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {
     39      get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; }
     40    }
    3941    public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {
    4042      get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }
     
    4345      get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
    4446    }
    45     public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
    46       get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    47     }
    4847    public ILookupParameter<IRandom> RandomParameter {
    4948      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
     
    5150    private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
    5251      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
    53     }
    54     private ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
    55       get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
    5652    }
    5753    public IValueParameter<IntValue> UpdateIntervalParameter {
     
    6763      get { return (IValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; }
    6864    }
    69     public IFixedValueParameter<DoubleValue> NumberOfPrunedSubtreesParameter {
    70       get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedSubtreesParameterName]; }
    71     }
    72     public IFixedValueParameter<DoubleValue> NumberOfPrunedTreesParameter {
    73       get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedTreesParameterName]; }
    74     }
    7565    #endregion
    7666    #region properties
     67    protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
    7768    protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
    78     protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }
    7969    protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
    8070    protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
    8171    protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } }
    8272    protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } }
    83     protected DoubleValue PrunedSubtrees { get { return NumberOfPrunedSubtreesParameter.Value; } }
    84     protected DoubleValue PrunedTrees { get { return NumberOfPrunedTreesParameter.Value; } }
    85     protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }
    8673    protected IRandom Random { get { return RandomParameter.ActualValue; } }
    8774    protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } }
    8875    protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } }
     76    #endregion
     77    #region IStatefulItem members
     78    public override void InitializeState() {
     79      base.InitializeState();
     80      UpdateCounter.Value = 0;
     81    }
     82    public override void ClearState() {
     83      base.ClearState();
     84      UpdateCounter.Value = 0;
     85    }
    8986    #endregion
    9087
     
    9390    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
    9491      : base(original, cloner) {
    95       impactValuesCalculator = original.impactValuesCalculator;
     92      if (original.prunedSubtreesReducer != null)
     93        this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone();
     94      if (original.prunedTreesReducer != null)
     95        this.prunedTreesReducer = (DataReducer)original.prunedTreesReducer.Clone();
     96      if (original.valuesCollector != null)
     97        this.valuesCollector = (DataTableValuesCollector)original.valuesCollector.Clone();
     98      if (original.resultsCollector != null)
     99        this.resultsCollector = (ResultsCollector)original.resultsCollector.Clone();
    96100    }
    97101    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() {
     102      #region add parameters
    98103      Parameters.Add(new ValueParameter<DoubleRange>(PopulationSliceParameterName, new DoubleRange(0.75, 1)));
    99104      Parameters.Add(new ValueParameter<DoubleValue>(PruningProbabilityParameterName, new DoubleValue(0.5)));
    100       // analyzer parameters
    101105      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
    102106      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0)));
    103107      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName));
    104108      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
    105       Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
    106 
    107       Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedSubtreesParameterName, new DoubleValue(0)));
    108       Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedTreesParameterName, new DoubleValue(0)));
    109       Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
    110109      Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, new DoubleValue(0.0)));
    111110      Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, new BoolValue(false)));
     111      #endregion
     112    }
     113
     114    private void InitializeOperators() {
     115      prunedSubtreesReducer = new DataReducer();
     116      prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
     117      prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
     118      prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
     119      prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;
     120
     121      prunedTreesReducer = new DataReducer();
     122      prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;
     123      prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
     124      prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
     125      prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;
     126
     127      valuesCollector = new DataTableValuesCollector();
     128      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
     129      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
     130      valuesCollector.DataTableParameter.ActualName = "Population pruning";
     131
     132      resultsCollector = new ResultsCollector();
     133      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
     134      resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
     135
     136      emptyOp = new EmptyOperator();
     137    }
     138
     139    //
     140    /// <summary>
     141    /// Computes the closed interval bounding the portion of the population that is to be pruned.
     142    /// </summary>
     143    /// <returns>Returns an int range [start, end]</returns>
     144    private IntRange GetSliceBounds() {
     145      var count = ExecutionContext.Scope.SubScopes.Count;
     146      var start = (int)Math.Round(PopulationSlice.Start * count);
     147      var end = (int)Math.Round(PopulationSlice.End * count);
     148      if (end > count) end = count;
     149
     150      if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds.");
     151      return new IntRange(start, end);
     152    }
     153
     154    private IOperation CreatePruningOperation() {
     155      var oc = new OperationCollection { Parallel = true };
     156      var range = GetSliceBounds();
     157      var qualities = Quality.Select(x => x.Value).ToArray();
     158      var indices = Enumerable.Range(0, qualities.Length).ToArray();
     159      Array.Sort(qualities, indices);
     160      if (!Maximization.Value) Array.Reverse(indices);
     161
     162      var subscopes = ExecutionContext.Scope.SubScopes;
     163
     164      for (int i = 0; i < subscopes.Count; ++i) {
     165        IOperator op;
     166        if (range.Start <= i && i < range.End && Random.NextDouble() <= PruningProbability.Value)
     167          op = PruningOperator;
     168        else op = emptyOp;
     169        var index = indices[i];
     170        var subscope = subscopes[index];
     171        oc.Add(ExecutionContext.CreateChildOperation(op, subscope));
     172      }
     173      return oc;
    112174    }
    113175
    114176    public override IOperation Apply() {
    115       if (reentry) {
    116         UpdateCounter.Value++;
     177      UpdateCounter.Value++;
     178      if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
     179      UpdateCounter.Value = 0;
    117180
    118         if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
    119         UpdateCounter.Value = 0;
     181      if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
    120182
    121         var trees = SymbolicExpressionTreeParameter.ActualValue.ToList();
    122         var qualities = QualityParameter.ActualValue.ToList();
     183      var prune = CreatePruningOperation();
     184      var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer);
     185      var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer);
     186      var collectValues = ExecutionContext.CreateChildOperation(valuesCollector);
     187      var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
    123188
    124         var population = trees.Zip(qualities, (tree, quality) => new { Tree = tree, Quality = quality }).ToList();
    125         Func<double, double, int> compare = (a, b) => Maximization.Value ? a.CompareTo(b) : b.CompareTo(a);
    126         population.Sort((a, b) => compare(a.Quality.Value, b.Quality.Value));
    127 
    128         var start = (int)Math.Round(PopulationSlice.Start * trees.Count);
    129         var end = (int)Math.Round(PopulationSlice.End * trees.Count);
    130 
    131         if (end == population.Count) end--;
    132 
    133         if (start >= end || end >= population.Count) throw new Exception("Invalid PopulationSlice bounds.");
    134 
    135         PrunedSubtrees.Value = 0;
    136         PrunedTrees.Value = 0;
    137 
    138         reentry = false;
    139 
    140         var operations = new OperationCollection { Parallel = true };
    141         foreach (var p in population.Skip(start).Take(end)) {
    142           if (Random.NextDouble() > PruningProbability.Value) continue;
    143           var op = new SymbolicDataAnalysisExpressionPruningOperator {
    144             Model = CreateModel(p.Tree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper),
    145             ImpactsCalculator = impactValuesCalculator,
    146             ProblemData = ProblemData,
    147             Random = Random,
    148             PruneOnlyZeroImpactNodes = PruneOnlyZeroImpactNodes.Value,
    149             NodeImpactThreshold = NodeImpactThreshold.Value
    150           };
    151           operations.Add(ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope));
    152         }
    153         return new OperationCollection { operations, ExecutionContext.CreateOperation(this) };
    154       }
    155 
    156       DataTable table;
    157 
    158       if (ResultCollection.ContainsKey("Population Pruning")) {
    159         table = (DataTable)ResultCollection["Population Pruning"].Value;
    160       } else {
    161         table = new DataTable("Population Pruning");
    162         table.Rows.Add(new DataRow("Pruned Trees") { VisualProperties = { StartIndexZero = true } });
    163         table.Rows.Add(new DataRow("Pruned Subtrees") { VisualProperties = { StartIndexZero = true } });
    164         ResultCollection.Add(new Result("Population Pruning", table));
    165       }
    166 
    167       table.Rows["Pruned Trees"].Values.Add(PrunedTrees.Value);
    168       table.Rows["Pruned Subtrees"].Values.Add(PrunedSubtrees.Value);
    169 
    170       reentry = true;
    171 
    172       return base.Apply();
     189      return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
    173190    }
    174 
    175     protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    176       double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue);
    177191  }
    178192}
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs

    r9456 r10579  
    121121      var quality = tree
    122122        .Take(topN)
    123         .AsParallel()
    124123        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
    125124        .ToArray();
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r9456 r10579  
    120120        .Select(i => tree[i])
    121121        .Take(topN)
    122         .AsParallel()
    123122        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
    124123        .ToArray();
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs

    r9456 r10579  
    131131
    132132      var variableFrequencies = trees
    133         .AsParallel()
    134133        .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables))
    135134        .GroupBy(pair => pair.Key, pair => pair.Value)
Note: See TracChangeset for help on using the changeset viewer.