Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs @ 16752

Last change on this file since 16752 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 14.4 KB
RevLine 
[11145]1#region License Information
2
3/* HeuristicLab
[15584]4 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11145]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;
[11145]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 {
[11145]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";
[11145]45    private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
46    private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
[12745]47    private const string TotalNumberOfPrunedNodesParameterName = "Number of pruned nodes";
[10368]48    private const string RandomParameterName = "Random";
[11145]49    private const string ResultsParameterName = "Results";
50    private const string PopulationSizeParameterName = "PopulationSize";
51    #endregion
[10368]52
[11145]53    #region private members
[12745]54    private DataReducer prunedNodesReducer;
[11145]55    private DataReducer prunedSubtreesReducer;
56    private DataReducer prunedTreesReducer;
57    private DataTableValuesCollector valuesCollector;
58    private ResultsCollector resultsCollector;
59    #endregion
[10368]60
61    #region parameter properties
62    public ILookupParameter<IRandom> RandomParameter {
63      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
64    }
[11145]65    public IFixedValueParameter<IntValue> UpdateIntervalParameter {
66      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
[10368]67    }
[11145]68    public IFixedValueParameter<IntValue> UpdateCounterParameter {
69      get { return (IFixedValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
[10368]70    }
[11145]71    public IFixedValueParameter<DoubleRange> PopulationSliceParameter {
72      get { return (IFixedValueParameter<DoubleRange>)Parameters[PopulationSliceParameterName]; }
[10368]73    }
[11145]74    public IFixedValueParameter<DoubleValue> PruningProbabilityParameter {
75      get { return (IFixedValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; }
[10368]76    }
[11145]77    public ILookupParameter<IntValue> PopulationSizeParameter {
78      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
[10368]79    }
[11145]80    #endregion
81
82    #region properties
[12745]83    protected abstract SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get; }
[11145]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; }
[10368]89    }
[11145]90
91    protected double PopulationSliceStart {
92      get { return PopulationSliceParameter.Value.Start; }
93      set { PopulationSliceParameter.Value.Start = value; }
[10368]94    }
[11145]95
96    protected double PopulationSliceEnd {
97      get { return PopulationSliceParameter.Value.End; }
98      set { PopulationSliceParameter.Value.End = value; }
[10368]99    }
[11145]100
101    protected double PruningProbability {
102      get { return PruningProbabilityParameter.Value.Value; }
103      set { PruningProbabilityParameter.Value.Value = value; }
104    }
[10368]105    #endregion
[11145]106
107    #region IStatefulItem members
108    public override void InitializeState() {
109      base.InitializeState();
110      UpdateCounter = 0;
111    }
112    public override void ClearState() {
113      base.ClearState();
114      UpdateCounter = 0;
115    }
[10368]116    #endregion
[11145]117
118    [StorableConstructor]
119    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { }
120
[10368]121    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
122      : base(original, cloner) {
[12745]123      if (original.prunedNodesReducer != null)
124        this.prunedNodesReducer = (DataReducer)original.prunedNodesReducer.Clone();
[11145]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    }
134
[11145]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      }
[10368]172    }
173
[11145]174    protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() {
175      #region add parameters
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."));
183      #endregion
184    }
[10368]185
[11145]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() {
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);
197      if (end > count) end = count;
[10368]198
[11145]199      if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds.");
200      return new IntRange(start, end);
201    }
[10368]202
[11145]203    private IOperation CreatePruningOperation() {
204      var operations = new OperationCollection { Parallel = true };
205      var range = GetSliceBounds();
206      var qualities = Quality.Select(x => x.Value).ToArray();
207      var indices = Enumerable.Range(0, qualities.Length).ToArray();
[12745]208      indices.StableSort((a, b) => qualities[a].CompareTo(qualities[b]));
209
[11145]210      if (!Maximization.Value) Array.Reverse(indices);
[10368]211
[11145]212      var subscopes = ExecutionContext.Scope.SubScopes;
213      var random = RandomParameter.ActualValue;
[10368]214
[11145]215      var empty = new EmptyOperator();
[10368]216
[12745]217      for (int i = 0; i < indices.Length; ++i) {
[11145]218        IOperator @operator;
219        if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability)
220          @operator = PruningOperator;
221        else @operator = empty;
222        var index = indices[i];
223        var subscope = subscopes[index];
224        operations.Add(ExecutionContext.CreateChildOperation(@operator, subscope));
225      }
226      return operations;
227    }
[10368]228
[11145]229    public override IOperation Apply() {
230      UpdateCounter++;
231      if (UpdateCounter != UpdateInterval) return base.Apply();
232      UpdateCounter = 0;
[10368]233
[12745]234      if (prunedNodesReducer == null || prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
[10368]235
[11145]236      var prune = CreatePruningOperation();
[12745]237      var reducePrunedNodes = ExecutionContext.CreateChildOperation(prunedNodesReducer);
[11145]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
[12745]243      return new OperationCollection { prune, reducePrunedNodes, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
[11145]244    }
[10368]245
[11145]246    private void InitializeOperators() {
[12745]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
[11145]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;
[10368]258
[11145]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;
[10368]264
[11145]265      valuesCollector = new DataTableValuesCollector();
[12745]266      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedNodesParameterName));
[11145]267      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));
268      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));
269      valuesCollector.DataTableParameter.ActualName = "Population pruning";
[10368]270
[11145]271      resultsCollector = new ResultsCollector();
272      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
273      resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
[10368]274    }
275  }
276}
Note: See TracBrowser for help on using the repository browser.