- Timestamp:
- 06/18/14 15:27:38 (10 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs
r11013 r11025 1 using System; 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 24 using System; 2 25 using System.Linq; 3 26 using HeuristicLab.Analysis; … … 29 52 private const string PopulationSizeParameterName = "PopulationSize"; 30 53 #endregion 54 31 55 #region private members 32 56 private DataReducer prunedSubtreesReducer; … … 36 60 private EmptyOperator emptyOp; 37 61 #endregion 62 38 63 #region parameter properties 39 64 public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter { … … 49 74 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 50 75 } 51 private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {52 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }53 }54 76 public IValueParameter<IntValue> UpdateIntervalParameter { 55 77 get { return (IValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; } … … 68 90 } 69 91 #endregion 92 70 93 #region properties 71 94 protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } } 72 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }73 95 protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } } 74 96 protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } } 75 97 protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } } 76 98 protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } } 77 protected IRandom Random { get { return RandomParameter.ActualValue; } } 78 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 79 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 80 #endregion 99 100 protected bool PruneOnlyZeroImpactNodes { 101 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; } 102 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; } 103 } 104 protected double NodeImpactThreshold { 105 get { return NodeImpactThresholdParameter.Value.Value; } 106 set { NodeImpactThresholdParameter.Value.Value = value; } 107 } 108 #endregion 109 81 110 #region IStatefulItem members 82 111 public override void InitializeState() { … … 92 121 [StorableConstructor] 93 122 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { } 123 94 124 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner) 95 125 : base(original, cloner) { … … 125 155 } 126 156 127 private void InitializeOperators() {128 prunedSubtreesReducer = new DataReducer();129 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;130 prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values131 prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter132 prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;133 134 prunedTreesReducer = new DataReducer();135 prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;136 prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);137 prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);138 prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;139 140 valuesCollector = new DataTableValuesCollector();141 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));142 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));143 valuesCollector.DataTableParameter.ActualName = "Population pruning";144 145 resultsCollector = new ResultsCollector();146 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));147 resultsCollector.ResultsParameter.ActualName = ResultsParameterName;148 149 emptyOp = new EmptyOperator();150 }151 152 157 // 153 158 /// <summary> … … 176 181 177 182 var subscopes = ExecutionContext.Scope.SubScopes; 183 var random = RandomParameter.ActualValue; 178 184 179 185 for (int i = 0; i < subscopes.Count; ++i) { 180 186 IOperator op; 181 if (range.Start <= i && i < range.End && Random.NextDouble() <= PruningProbability.Value)187 if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability.Value) 182 188 op = PruningOperator; 183 189 else op = emptyOp; … … 204 210 return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() }; 205 211 } 212 213 private void InitializeOperators() { 214 prunedSubtreesReducer = new DataReducer(); 215 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName; 216 prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values 217 prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter 218 prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName; 219 220 prunedTreesReducer = new DataReducer(); 221 prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName; 222 prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); 223 prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); 224 prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName; 225 226 valuesCollector = new DataTableValuesCollector(); 227 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName)); 228 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName)); 229 valuesCollector.DataTableParameter.ActualName = "Population pruning"; 230 231 resultsCollector = new ResultsCollector(); 232 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning")); 233 resultsCollector.ResultsParameter.ActualName = ResultsParameterName; 234 235 emptyOp = new EmptyOperator(); 236 } 206 237 } 207 238 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs
r10469 r11025 85 85 } 86 86 #endregion 87 87 88 #region properties 88 89 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 89 90 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } } 90 91 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } } 91 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 92 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 92 protected bool PruneOnlyZeroImpactNodes { 93 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; } 94 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; } 95 } 96 protected double NodeImpactThreshold { 97 get { return NodeImpactThresholdParameter.Value.Value; } 98 set { NodeImpactThresholdParameter.Value.Value = value; } 99 } 93 100 protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } } 94 101 protected DoubleValue Quality { get { return QualityParameter.ActualValue; } } … … 117 124 #endregion 118 125 } 126 127 protected abstract ISymbolicDataAnalysisModel CreateModel(); 128 129 protected abstract double Evaluate(IDataAnalysisModel model); 130 119 131 public override IOperation Apply() { 120 132 var model = CreateModel(); 121 133 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 122 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToList(); 123 134 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 124 135 var prunedSubtrees = 0; 125 136 var prunedTrees = 0; … … 134 145 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality); 135 146 136 if (PruneOnlyZeroImpactNodes.Value && (!impactValue.IsAlmost(0.0))) continue; 137 else if (NodeImpactThreshold.Value < impactValue) continue; 147 if (PruneOnlyZeroImpactNodes) { 148 if (!impactValue.IsAlmost(0.0)) continue; 149 } else if (NodeImpactThreshold < impactValue) { 150 continue; 151 } 138 152 139 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 153 var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode(); 154 constantNode.Value = replacementValue; 155 140 156 ReplaceWithConstant(node, constantNode); 141 157 i += node.GetLength() - 1; // skip subtrees under the node that was folded … … 152 168 return base.Apply(); 153 169 } 170 154 171 private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) { 155 172 var parent = original.Parent; … … 158 175 parent.InsertSubtree(i, replacement); 159 176 } 160 protected abstract ISymbolicDataAnalysisModel CreateModel();161 protected abstract double Evaluate(IDataAnalysisModel model);162 177 } 163 178 }
Note: See TracChangeset
for help on using the changeset viewer.