[10368] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Analysis;
|
---|
| 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
[10469] | 7 | using HeuristicLab.Operators;
|
---|
| 8 | using HeuristicLab.Optimization.Operators;
|
---|
[10368] | 9 | using HeuristicLab.Parameters;
|
---|
| 10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 11 |
|
---|
| 12 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 13 | [StorableClass]
|
---|
| 14 | [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")]
|
---|
| 15 | public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
|
---|
[10469] | 16 | #region parameter names
|
---|
[10368] | 17 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 18 | private const string UpdateIntervalParameterName = "UpdateInverval";
|
---|
| 19 | private const string UpdateCounterParameterName = "UpdateCounter";
|
---|
| 20 | private const string PopulationSliceParameterName = "PopulationSlice";
|
---|
| 21 | private const string PruningProbabilityParameterName = "PruningProbability";
|
---|
[10469] | 22 | private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees";
|
---|
| 23 | private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees";
|
---|
[10368] | 24 | private const string RandomParameterName = "Random";
|
---|
| 25 | private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
|
---|
| 26 | private const string NodeImpactThresholdParameterName = "ImpactThreshold";
|
---|
[10469] | 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
|
---|
[10368] | 37 | #region parameter properties
|
---|
[10469] | 38 | public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {
|
---|
| 39 | get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; }
|
---|
| 40 | }
|
---|
[10368] | 41 | public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {
|
---|
| 42 | get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }
|
---|
| 43 | }
|
---|
| 44 | public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {
|
---|
| 45 | get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
|
---|
| 46 | }
|
---|
| 47 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 48 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
| 49 | }
|
---|
| 50 | private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
|
---|
| 51 | get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 52 | }
|
---|
| 53 | public IValueParameter<IntValue> UpdateIntervalParameter {
|
---|
| 54 | get { return (IValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
| 55 | }
|
---|
| 56 | public IValueParameter<IntValue> UpdateCounterParameter {
|
---|
| 57 | get { return (IValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
| 58 | }
|
---|
| 59 | public IValueParameter<DoubleRange> PopulationSliceParameter {
|
---|
| 60 | get { return (IValueParameter<DoubleRange>)Parameters[PopulationSliceParameterName]; }
|
---|
| 61 | }
|
---|
| 62 | public IValueParameter<DoubleValue> PruningProbabilityParameter {
|
---|
| 63 | get { return (IValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; }
|
---|
| 64 | }
|
---|
| 65 | #endregion
|
---|
| 66 | #region properties
|
---|
[10469] | 67 | protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } }
|
---|
[10368] | 68 | protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
|
---|
| 69 | protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
|
---|
| 70 | protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
|
---|
| 71 | protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } }
|
---|
| 72 | protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } }
|
---|
| 73 | protected IRandom Random { get { return RandomParameter.ActualValue; } }
|
---|
| 74 | protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } }
|
---|
| 75 | protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } }
|
---|
| 76 | #endregion
|
---|
[10414] | 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 | }
|
---|
| 86 | #endregion
|
---|
| 87 |
|
---|
[10378] | 88 | [StorableConstructor]
|
---|
| 89 | protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[10368] | 90 | protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner)
|
---|
| 91 | : base(original, cloner) {
|
---|
[10470] | 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();
|
---|
[10368] | 100 | }
|
---|
| 101 | protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() {
|
---|
[10469] | 102 | #region add parameters
|
---|
[10368] | 103 | Parameters.Add(new ValueParameter<DoubleRange>(PopulationSliceParameterName, new DoubleRange(0.75, 1)));
|
---|
| 104 | Parameters.Add(new ValueParameter<DoubleValue>(PruningProbabilityParameterName, new DoubleValue(0.5)));
|
---|
| 105 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
| 106 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0)));
|
---|
| 107 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName));
|
---|
| 108 | Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
|
---|
| 109 | Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, new DoubleValue(0.0)));
|
---|
| 110 | Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, new BoolValue(false)));
|
---|
[10469] | 111 | #endregion
|
---|
[10368] | 112 | }
|
---|
| 113 |
|
---|
[10469] | 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;
|
---|
[10368] | 120 |
|
---|
[10469] | 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;
|
---|
[10368] | 126 |
|
---|
[10469] | 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";
|
---|
[10368] | 131 |
|
---|
[10469] | 132 | resultsCollector = new ResultsCollector();
|
---|
| 133 | resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));
|
---|
| 134 | resultsCollector.ResultsParameter.ActualName = ResultsParameterName;
|
---|
[10368] | 135 |
|
---|
[10469] | 136 | emptyOp = new EmptyOperator();
|
---|
| 137 | }
|
---|
[10368] | 138 |
|
---|
[10469] | 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;
|
---|
[10368] | 149 |
|
---|
[10469] | 150 | if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds.");
|
---|
| 151 | return new IntRange(start, end);
|
---|
| 152 | }
|
---|
[10368] | 153 |
|
---|
[10469] | 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);
|
---|
[10368] | 161 |
|
---|
[10469] | 162 | var subscopes = ExecutionContext.Scope.SubScopes;
|
---|
[10368] | 163 |
|
---|
[10469] | 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));
|
---|
[10368] | 172 | }
|
---|
[10469] | 173 | return oc;
|
---|
| 174 | }
|
---|
[10368] | 175 |
|
---|
[10469] | 176 | public override IOperation Apply() {
|
---|
| 177 | UpdateCounter.Value++;
|
---|
| 178 | if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
|
---|
| 179 | UpdateCounter.Value = 0;
|
---|
[10368] | 180 |
|
---|
[10469] | 181 | if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }
|
---|
[10368] | 182 |
|
---|
[10469] | 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);
|
---|
[10368] | 188 |
|
---|
[10469] | 189 | return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };
|
---|
[10368] | 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|