Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1837_Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs @ 17687

Last change on this file since 17687 was 17687, checked in by fbaching, 4 years ago

#1837: merged changes from trunk

  • apply changes from Attic release to all SlidingWindow specific code files (replace StorableClass with StorableType)
File size: 14.4 KB
RevLine 
[17687]1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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;
[10681]30using HeuristicLab.Operators;
31using HeuristicLab.Optimization.Operators;
[10368]32using HeuristicLab.Parameters;
[17687]33using HEAL.Attic;
[10368]34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[17687]36  [StorableType("83603E95-3CBD-476C-B11F-B30C80ACD0D3")]
[10368]37  [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")]
38  public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
[10681]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";
[10681]45    private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
46    private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
[17687]47    private const string TotalNumberOfPrunedNodesParameterName = "Number of pruned nodes";
[10368]48    private const string RandomParameterName = "Random";
[10681]49    private const string ResultsParameterName = "Results";
[17687]50    private const string PopulationSizeParameterName = "PopulationSize";
[10681]51    #endregion
[17687]52
[10681]53    #region private members
[17687]54    private DataReducer prunedNodesReducer;
[10681]55    private DataReducer prunedSubtreesReducer;
56    private DataReducer prunedTreesReducer;
57    private DataTableValuesCollector valuesCollector;
58    private ResultsCollector resultsCollector;
59    #endregion
[17687]60
[10368]61    #region parameter properties
62    public ILookupParameter<IRandom> RandomParameter {
63      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
64    }
[17687]65    public IFixedValueParameter<IntValue> UpdateIntervalParameter {
66      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
[10368]67    }
[17687]68    public IFixedValueParameter<IntValue> UpdateCounterParameter {
69      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
[10368]70    }
[17687]71    public IFixedValueParameter<DoubleRange> PopulationSliceParameter {
72      get { return (IFixedValueParameter<DoubleRange>)Parameters[PopulationSliceParameterName]; }
[10368]73    }
[17687]74    public IFixedValueParameter<DoubleValue> PruningProbabilityParameter {
75      get { return (IFixedValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; }
[10368]76    }
[17687]77    public ILookupParameter<IntValue> PopulationSizeParameter {
78      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
[10368]79    }
80    #endregion
[17687]81
[10368]82    #region properties
[17687]83    protected abstract SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get; }
84    protected int UpdateInterval { get { return UpdateIntervalParameter.Value.Value; } }
85
86    protected int UpdateCounter {
87      get { return UpdateCounterParameter.Value.Value; }
88      set { UpdateCounterParameter.Value.Value = value; }
89    }
90
91    protected double PopulationSliceStart {
92      get { return PopulationSliceParameter.Value.Start; }
93      set { PopulationSliceParameter.Value.Start = value; }
94    }
95
96    protected double PopulationSliceEnd {
97      get { return PopulationSliceParameter.Value.End; }
98      set { PopulationSliceParameter.Value.End = value; }
99    }
100
101    protected double PruningProbability {
102      get { return PruningProbabilityParameter.Value.Value; }
103      set { PruningProbabilityParameter.Value.Value = value; }
104    }
[10368]105    #endregion
[17687]106
[10429]107    #region IStatefulItem members
108    public override void InitializeState() {
109      base.InitializeState();
[17687]110      UpdateCounter = 0;
[10429]111    }
112    public override void ClearState() {
113      base.ClearState();
[17687]114      UpdateCounter = 0;
[10429]115    }
116    #endregion
117
[10396]118    [StorableConstructor]
[17687]119    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(StorableConstructorFlag _) : base(_) { }
120
[10368]121    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
122      : base(original, cloner) {
[17687]123      if (original.prunedNodesReducer != null)
124        this.prunedNodesReducer = (DataReducer)original.prunedNodesReducer.Clone();
[10681]125      if (original.prunedSubtreesReducer != null)
126        this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone();
127      if (original.prunedTreesReducer != null)
128        this.prunedTreesReducer = (DataReducer)original.prunedTreesReducer.Clone();
129      if (original.valuesCollector != null)
130        this.valuesCollector = (DataTableValuesCollector)original.valuesCollector.Clone();
131      if (original.resultsCollector != null)
132        this.resultsCollector = (ResultsCollector)original.resultsCollector.Clone();
[10368]133    }
[17687]134
135    [StorableHook(HookType.AfterDeserialization)]
136    private void AfterDeserialization() {
137      if (!Parameters.ContainsKey(PopulationSizeParameterName)) {
138        Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
139      }
140      if (Parameters.ContainsKey(UpdateCounterParameterName)) {
141        var fixedValueParameter = Parameters[UpdateCounterParameterName] as FixedValueParameter<IntValue>;
142        if (fixedValueParameter == null) {
143          var valueParameter = (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName];
144          Parameters.Remove(UpdateCounterParameterName);
145          Parameters.Add(new FixedValueParameter<IntValue>(UpdateCounterParameterName, valueParameter.Value));
146        }
147      }
148      if (Parameters.ContainsKey(UpdateIntervalParameterName)) {
149        var fixedValueParameter = Parameters[UpdateIntervalParameterName] as FixedValueParameter<IntValue>;
150        if (fixedValueParameter == null) {
151          var valueParameter = (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName];
152          Parameters.Remove(UpdateIntervalParameterName);
153          Parameters.Add(new FixedValueParameter<IntValue>(UpdateIntervalParameterName, valueParameter.Value));
154        }
155      }
156      if (Parameters.ContainsKey(PopulationSliceParameterName)) {
157        var fixedValueParameter = Parameters[PopulationSliceParameterName] as FixedValueParameter<DoubleRange>;
158        if (fixedValueParameter == null) {
159          var valueParameter = (ValueParameter<DoubleRange>)Parameters[PopulationSliceParameterName];
160          Parameters.Remove(PopulationSliceParameterName);
161          Parameters.Add(new FixedValueParameter<DoubleRange>(PopulationSliceParameterName, valueParameter.Value));
162        }
163      }
164      if (Parameters.ContainsKey(PruningProbabilityParameterName)) {
165        var fixedValueParameter = Parameters[PruningProbabilityParameterName] as FixedValueParameter<DoubleValue>;
166        if (fixedValueParameter == null) {
167          var valueParameter = (ValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName];
168          Parameters.Remove(PruningProbabilityParameterName);
169          Parameters.Add(new FixedValueParameter<DoubleValue>(PruningProbabilityParameterName, valueParameter.Value));
170        }
171      }
172    }
173
[10368]174    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() {
[10681]175      #region add parameters
[17687]176      Parameters.Add(new FixedValueParameter<DoubleRange>(PopulationSliceParameterName, "The slice of the population where pruning should be applied.", new DoubleRange(0.75, 1)));
177      Parameters.Add(new FixedValueParameter<DoubleValue>(PruningProbabilityParameterName, "The probability for pruning an individual.", new DoubleValue(0.5)));
178      Parameters.Add(new FixedValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
179      Parameters.Add(new FixedValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0)));
180      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random number generator."));
181      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data."));
182      Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals."));
[10681]183      #endregion
[10368]184    }
185
[10681]186    //
187    /// <summary>
188    /// Computes the closed interval bounding the portion of the population that is to be pruned.
189    /// </summary>
190    /// <returns>Returns an int range [start, end]</returns>
191    private IntRange GetSliceBounds() {
[17687]192      if (PopulationSliceStart < 0 || PopulationSliceEnd < 0) throw new ArgumentOutOfRangeException("The slice bounds cannot be negative.");
193      if (PopulationSliceStart > 1 || PopulationSliceEnd > 1) throw new ArgumentOutOfRangeException("The slice bounds should be expressed as unit percentages.");
194      var count = PopulationSizeParameter.ActualValue.Value;
195      var start = (int)Math.Round(PopulationSliceStart * count);
196      var end = (int)Math.Round(PopulationSliceEnd * count);
[10681]197      if (end > count) end = count;
[10368]198
[10681]199      if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds.");
200      return new IntRange(start, end);
201    }
[10368]202
[10681]203    private IOperation CreatePruningOperation() {
[17687]204      var operations = new OperationCollection { Parallel = true };
[10681]205      var range = GetSliceBounds();
206      var qualities = Quality.Select(x => x.Value).ToArray();
207      var indices = Enumerable.Range(0, qualities.Length).ToArray();
[17687]208      indices.StableSort((a, b) => qualities[a].CompareTo(qualities[b]));
209
[10681]210      if (!Maximization.Value) Array.Reverse(indices);
[10368]211
[10681]212      var subscopes = ExecutionContext.Scope.SubScopes;
[17687]213      var random = RandomParameter.ActualValue;
[10368]214
[17687]215      var empty = new EmptyOperator();
216
217      for (int i = 0; i < indices.Length; ++i) {
218        IOperator @operator;
219        if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability)
220          @operator = PruningOperator;
221        else @operator = empty;
[10681]222        var index = indices[i];
223        var subscope = subscopes[index];
[17687]224        operations.Add(ExecutionContext.CreateChildOperation(@operator, subscope));
[10368]225      }
[17687]226      return operations;
[10681]227    }
[10368]228
[10681]229    public override IOperation Apply() {
[17687]230      UpdateCounter++;
231      if (UpdateCounter != UpdateInterval) return base.Apply();
232      UpdateCounter = 0;
[10368]233
[17687]234      if (prunedNodesReducer == null || prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
[10368]235
[10681]236      var prune = CreatePruningOperation();
[17687]237      var reducePrunedNodes = ExecutionContext.CreateChildOperation(prunedNodesReducer);
[10681]238      var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer);
239      var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer);
240      var collectValues = ExecutionContext.CreateChildOperation(valuesCollector);
241      var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
[10368]242
[17687]243      return new OperationCollection { prune, reducePrunedNodes, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
[10368]244    }
[17687]245
246    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
253      prunedSubtreesReducer = new DataReducer();
254      prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;
255      prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values
256      prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
257      prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;
258
259      prunedTreesReducer = new DataReducer();
260      prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;
261      prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
262      prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
263      prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;
264
265      valuesCollector = new DataTableValuesCollector();
266      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedNodesParameterName));
267      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
268      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
269      valuesCollector.DataTableParameter.ActualName = "Population pruning";
270
271      resultsCollector = new ResultsCollector();
272      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
273      resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
274    }
[10368]275  }
276}
Note: See TracBrowser for help on using the repository browser.