Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs @ 11026

Last change on this file since 11026 was 11026, checked in by bburlacu, 10 years ago

#2143:

  • Removed ToArray() calls from SymbolicClassificationPruningOperator.cs and SymbolicRegressionPruningOperator.cs.
  • Changed UpdateCounter, UpdateInterval, PruningProbability, PopulationSliceParameter to FixedValueParameters in SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs.
  • Removed empty operator data member
  • Added parameter descriptions
  • Improved source organization
File size: 12.9 KB
RevLine 
[11025]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;
[10368]25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
[10469]30using HeuristicLab.Operators;
31using HeuristicLab.Optimization.Operators;
[10368]32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableClass]
37  [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")]
38  public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
[10469]39    #region parameter names
[10368]40    private const string ProblemDataParameterName = "ProblemData";
41    private const string UpdateIntervalParameterName = "UpdateInverval";
42    private const string UpdateCounterParameterName = "UpdateCounter";
43    private const string PopulationSliceParameterName = "PopulationSlice";
44    private const string PruningProbabilityParameterName = "PruningProbability";
[10469]45    private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
46    private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
[10368]47    private const string RandomParameterName = "Random";
48    private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
49    private const string NodeImpactThresholdParameterName = "ImpactThreshold";
[10469]50    private const string PruningOperatorParameterName = "PruningOperator";
51    private const string ResultsParameterName = "Results";
[11013]52    private const string PopulationSizeParameterName = "PopulationSize";
[10469]53    #endregion
[11025]54
[10469]55    #region private members
56    private DataReducer prunedSubtreesReducer;
57    private DataReducer prunedTreesReducer;
58    private DataTableValuesCollector valuesCollector;
59    private ResultsCollector resultsCollector;
60    #endregion
[11025]61
[10368]62    #region parameter properties
[10469]63    public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {
64      get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; }
65    }
[10368]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    }
72    public ILookupParameter<IRandom> RandomParameter {
73      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
74    }
[11026]75    public IFixedValueParameter<IntValue> UpdateIntervalParameter {
76      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
[10368]77    }
[11026]78    public IFixedValueParameter<IntValue> UpdateCounterParameter {
79      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
[10368]80    }
[11026]81    public IFixedValueParameter<DoubleRange> PopulationSliceParameter {
82      get { return (IFixedValueParameter<DoubleRange>)Parameters[PopulationSliceParameterName]; }
[10368]83    }
[11026]84    public IFixedValueParameter<DoubleValue> PruningProbabilityParameter {
85      get { return (IFixedValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; }
[10368]86    }
[11013]87    public ILookupParameter<IntValue> PopulationSizeParameter {
88      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
89    }
[10368]90    #endregion
[11025]91
[10368]92    #region properties
[10469]93    protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
[11026]94    protected int UpdateInterval { get { return UpdateIntervalParameter.Value.Value; } }
[11025]95
[11026]96    protected int UpdateCounter {
97      get { return UpdateCounterParameter.Value.Value; }
98      set { UpdateCounterParameter.Value.Value = value; }
99    }
100
101    protected double PopulationSliceStart {
102      get { return PopulationSliceParameter.Value.Start; }
103      set { PopulationSliceParameter.Value.Start = value; }
104    }
105
106    protected double PopulationSliceEnd {
107      get { return PopulationSliceParameter.Value.End; }
108      set { PopulationSliceParameter.Value.End = value; }
109    }
110
111    protected double PruningProbability {
112      get { return PruningProbabilityParameter.Value.Value; }
113      set { PruningProbabilityParameter.Value.Value = value; }
114    }
115
[11025]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; }
123    }
[10368]124    #endregion
[11025]125
[10414]126    #region IStatefulItem members
127    public override void InitializeState() {
128      base.InitializeState();
[11026]129      UpdateCounter = 0;
[10414]130    }
131    public override void ClearState() {
132      base.ClearState();
[11026]133      UpdateCounter = 0;
[10414]134    }
135    #endregion
136
[10378]137    [StorableConstructor]
138    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { }
[11025]139
[10368]140    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
141      : base(original, cloner) {
[10470]142      if (original.prunedSubtreesReducer != null)
143        this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone();
144      if (original.prunedTreesReducer != null)
145        this.prunedTreesReducer = (DataReducer)original.prunedTreesReducer.Clone();
146      if (original.valuesCollector != null)
147        this.valuesCollector = (DataTableValuesCollector)original.valuesCollector.Clone();
148      if (original.resultsCollector != null)
149        this.resultsCollector = (ResultsCollector)original.resultsCollector.Clone();
[10368]150    }
[11013]151
152    [StorableHook(HookType.AfterDeserialization)]
153    private void AfterDeserialization() {
154      if (!Parameters.ContainsKey(PopulationSizeParameterName)) {
155        Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
156      }
157    }
158
[10368]159    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() {
[10469]160      #region add parameters
[11026]161      Parameters.Add(new FixedValueParameter<DoubleRange>(PopulationSliceParameterName, "The slice of the population where pruning should be applied.", new DoubleRange(0.75, 1)));
162      Parameters.Add(new FixedValueParameter<DoubleValue>(PruningProbabilityParameterName, "The probability for pruning an individual.", new DoubleValue(0.5)));
163      Parameters.Add(new FixedValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
164      Parameters.Add(new FixedValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0)));
165      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random number generator."));
166      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data."));
167      Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "The impact threshold  below which an individual should be pruned.", new DoubleValue(0.0)));
168      Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Switch to determine of only zero impact individuals should be pruned.", new BoolValue(false)));
[11013]169      Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
[10469]170      #endregion
[10368]171    }
172
[10469]173    //
174    /// <summary>
175    /// Computes the closed interval bounding the portion of the population that is to be pruned.
176    /// </summary>
177    /// <returns>Returns an int range [start, end]</returns>
178    private IntRange GetSliceBounds() {
[11026]179      if (PopulationSliceStart < 0 || PopulationSliceEnd < 0) throw new ArgumentOutOfRangeException("The slice bounds cannot be negative.");
180      if (PopulationSliceStart > 1 || PopulationSliceEnd > 1) throw new ArgumentOutOfRangeException("The slice bounds should be expressed as unit percentages.");
[11013]181      var count = PopulationSizeParameter.ActualValue.Value;
[11026]182      var start = (int)Math.Round(PopulationSliceStart * count);
183      var end = (int)Math.Round(PopulationSliceEnd * count);
[10469]184      if (end > count) end = count;
[10368]185
[10469]186      if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds.");
187      return new IntRange(start, end);
188    }
[10368]189
[10469]190    private IOperation CreatePruningOperation() {
[11026]191      var operations = new OperationCollection { Parallel = true };
[10469]192      var range = GetSliceBounds();
193      var qualities = Quality.Select(x => x.Value).ToArray();
194      var indices = Enumerable.Range(0, qualities.Length).ToArray();
195      Array.Sort(qualities, indices);
196      if (!Maximization.Value) Array.Reverse(indices);
[10368]197
[10469]198      var subscopes = ExecutionContext.Scope.SubScopes;
[11025]199      var random = RandomParameter.ActualValue;
[10368]200
[11026]201      var empty = new EmptyOperator();
202
[10469]203      for (int i = 0; i < subscopes.Count; ++i) {
[11026]204        IOperator @operator;
205        if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability)
206          @operator = PruningOperator;
207        else @operator = empty;
[10469]208        var index = indices[i];
209        var subscope = subscopes[index];
[11026]210        operations.Add(ExecutionContext.CreateChildOperation(@operator, subscope));
[10368]211      }
[11026]212      return operations;
[10469]213    }
[10368]214
[10469]215    public override IOperation Apply() {
[11026]216      UpdateCounter++;
217      if (UpdateCounter != UpdateInterval) return base.Apply();
218      UpdateCounter = 0;
[10368]219
[10469]220      if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
[10368]221
[10469]222      var prune = CreatePruningOperation();
223      var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer);
224      var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer);
225      var collectValues = ExecutionContext.CreateChildOperation(valuesCollector);
226      var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
[10368]227
[10469]228      return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
[10368]229    }
[11025]230
231    private void InitializeOperators() {
232      prunedSubtreesReducer = new DataReducer();
233      prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
234      prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
235      prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
236      prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;
237
238      prunedTreesReducer = new DataReducer();
239      prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;
240      prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
241      prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
242      prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;
243
244      valuesCollector = new DataTableValuesCollector();
245      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
246      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
247      valuesCollector.DataTableParameter.ActualName = "Population pruning";
248
249      resultsCollector = new ResultsCollector();
250      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
251      resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
252    }
[10368]253  }
254}
Note: See TracBrowser for help on using the repository browser.