- Timestamp:
- 03/12/14 15:23:47 (10 years ago)
- Location:
- branches/DataAnalysis.IslandAlgorithms
- Files:
-
- 16 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/ConsecutiveSamplesEvaluator.cs
r10421 r10579 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 131 132 var generation = generationValue == null ? 0 : generationValue.Value; 132 133 133 //calculat new rows for evaluation 134 if (dataMigrationInterval != 0 && generation % dataMigrationInterval == 0) { 135 //create fixed rows enumerable 136 var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size); 137 //create consecutive rows enumerable 138 if (ConsecutiveSamples > 0) { 139 var islandIndex = IslandIndexParameter.ActualValue.Value; 140 var iteration = islandIndex + (generation / dataMigrationInterval); 141 var consecutiveSamples = (int)(ConsecutiveSamples * samples.Size); 142 var overlap = (int)Overlap * consecutiveSamples; 143 var consecutiveRows = GenerateRows(samples, fixedSamples, consecutiveSamples, overlap, iteration); 144 rows = rows.Concat(consecutiveRows); 145 } 146 //filter out test rows 147 rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End); 134 if (ConsecutiveSamples > 0 && dataMigrationInterval == 0) 135 throw new ArgumentException("The data migration interval must not be 0 if consecutive samples are used."); 148 136 149 //TODO change to lookup parameter 150 ExecutionContext.Scope.Variables.Remove("Rows"); 151 ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows))); 137 //create fixed rows enumerable 138 var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size); 139 //create consecutive rows enumerable 140 if (ConsecutiveSamples > 0) { 141 var islandIndex = IslandIndexParameter.ActualValue.Value; 142 var iteration = islandIndex + (generation / dataMigrationInterval); 143 var consecutiveSamples = (int)(ConsecutiveSamples * samples.Size); 144 var overlap = (int)Overlap * consecutiveSamples; 145 var consecutiveRows = GenerateRows(samples, fixedSamples, consecutiveSamples, overlap, iteration); 146 rows = rows.Concat(consecutiveRows); 152 147 } 148 //filter out test rows 149 rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End); 150 151 //TODO change to lookup parameter 152 ExecutionContext.Scope.Variables.Remove("Rows"); 153 ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows))); 153 154 154 155 var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/RandomSamplesEvaluator .cs
r10421 r10579 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 25 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization;28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 34 namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic { 35 35 [StorableClass] 36 public sealed class RandomSamplesEvaluator : SingleSuccessorOperator, IStochasticOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator { 37 private const string RandomParameterName = "Random"; 36 public sealed class RandomSamplesEvaluator : SingleSuccessorOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator { 38 37 private const string ProblemDataParameterName = "ProblemData"; 39 38 private const string EvaluatorParameterName = "ProblemEvaluator"; … … 43 42 private const string DataMigrationIntervalParameterName = "DataMigrationInterval"; 44 43 private const string RandomSamplesParameterName = "RandomSamples"; 44 private const string IslandIndexParameterName = "IslandIndex"; 45 45 private const string IterationsParameterName = "Iterations"; 46 46 private const string MaximumIterationsParameterName = "Maximum Iterations"; 47 47 48 48 #region parameter properties 49 public ILookupParameter<IRandom> RandomParameter {50 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }51 }52 49 public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter { 53 50 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } … … 70 67 public IFixedValueParameter<PercentValue> RandomSamplesParameter { 71 68 get { return (IFixedValueParameter<PercentValue>)Parameters[RandomSamplesParameterName]; } 69 } 70 public ILookupParameter<IntValue> IslandIndexParameter { 71 get { return (ILookupParameter<IntValue>)Parameters[IslandIndexParameterName]; } 72 72 } 73 73 public ILookupParameter<IntValue> IterationsParameter { … … 98 98 public RandomSamplesEvaluator() 99 99 : base() { 100 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));101 100 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated.")); 102 101 Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis problem.")); … … 106 105 Parameters.Add(new FixedValueParameter<PercentValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new PercentValue())); 107 106 Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases.")); 107 Parameters.Add(new LookupParameter<IntValue>(IslandIndexParameterName, "The index of the current island.")); 108 108 Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations.")); 109 109 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true }); … … 122 122 var generation = generationValue == null ? 0 : generationValue.Value; 123 123 124 //calculat new rows for evaluation125 if (dataMigrationInterval != 0 && generation % dataMigrationInterval == 0) {126 //create fixed rows enumerable127 var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size);128 //create randomly chosen rows enumerable129 if (randomSamples > 0) {130 if (randomSamples > samples.Size - fixedSamples.Size) {131 var error = string.Format("Could not select {0} random samples, because there are {1} total samples present from which {2} where used in the fixed partition. Please lower the number of random samples in the algorithm configuration.", randomSamples, samples.Size, fixedSamples.Size);132 throw new OperatorExecutionException(this, error);133 }134 var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End);135 randomRows = randomRows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, samples.Size - fixedSamples.Size);136 124 137 rows = rows.Concat(randomRows); 125 if (randomSamples > 0 && dataMigrationInterval == 0) 126 throw new ArgumentException("The data migration interval must not be 0 if random samples are used."); 127 128 //create fixed rows enumerable 129 var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size); 130 //create randomly chosen rows enumerable 131 if (randomSamples > 0) { 132 var islandIndex = IslandIndexParameter.ActualValue.Value; 133 var random = new FastRandom(islandIndex + (generation / dataMigrationInterval)); 134 135 if (randomSamples > samples.Size - fixedSamples.Size) { 136 var error = string.Format("Could not select {0} random samples, because there are {1} total samples present from which {2} where used in the fixed partition. Please lower the number of random samples in the algorithm configuration.", randomSamples, samples.Size, fixedSamples.Size); 137 throw new OperatorExecutionException(this, error); 138 138 } 139 //filter out test rows 140 rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End); 141 ExecutionContext.Scope.Variables.Remove("Rows"); 142 ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows))); 139 var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End); 140 randomRows = randomRows.SampleRandomWithoutRepetition(random, randomSamples, samples.Size - fixedSamples.Size); 141 rows = rows.Concat(randomRows); 143 142 } 143 144 //filter out test rows 145 rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End); 146 //TODO change to lookup parameter 147 ExecutionContext.Scope.Variables.Remove("Rows"); 148 ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows))); 144 149 145 150 var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.GeneticAlgorithm
- Property svn:mergeinfo changed (with no actual effect on merging)
-
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm
- Property svn:mergeinfo changed (with no actual effect on merging)
-
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs
r9456 r10579 75 75 public static Allele[] CalculateAlleles(ISymbolicExpressionTree solution, int alleleTreedepth) { 76 76 return GetAllSubtreesOfDepth(solution, alleleTreedepth) 77 .AsParallel()78 77 .Select(t => GetAlleleFromSubtreeOfDepth(t, alleleTreedepth)) 79 78 .ToArray(); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs
r9456 r10579 121 121 122 122 var qualities = tree 123 .AsParallel()124 123 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 125 124 .ToArray(); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs
r10390 r10579 5 5 using HeuristicLab.Core; 6 6 using HeuristicLab.Data; 7 using HeuristicLab. Encodings.SymbolicExpressionTreeEncoding;8 using HeuristicLab.Optimization ;7 using HeuristicLab.Operators; 8 using HeuristicLab.Optimization.Operators; 9 9 using HeuristicLab.Parameters; 10 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 14 14 [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")] 15 15 public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer { 16 #region parameter names 16 17 private const string ProblemDataParameterName = "ProblemData"; 17 private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";18 19 18 private const string UpdateIntervalParameterName = "UpdateInverval"; 20 19 private const string UpdateCounterParameterName = "UpdateCounter"; 21 22 20 private const string PopulationSliceParameterName = "PopulationSlice"; 23 21 private const string PruningProbabilityParameterName = "PruningProbability"; 24 25 private const string NumberOfPrunedSubtreesParameterName = "PrunedSubtrees"; 26 private const string NumberOfPrunedTreesParameterName = "PrunedTrees"; 27 22 private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees"; 23 private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees"; 28 24 private const string RandomParameterName = "Random"; 29 private const string EstimationLimitsParameterName = "EstimationLimits";30 31 25 private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes"; 32 26 private const string NodeImpactThresholdParameterName = "ImpactThreshold"; 33 34 private bool reentry; 35 [Storable] 36 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator; 37 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 38 37 #region parameter properties 38 public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter { 39 get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; } 40 } 39 41 public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter { 40 42 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; } … … 43 45 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; } 44 46 } 45 public ILookupParameter<DoubleLimit> EstimationLimitsParameter {46 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }47 }48 47 public ILookupParameter<IRandom> RandomParameter { 49 48 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } … … 51 50 private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter { 52 51 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 53 }54 private ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {55 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }56 52 } 57 53 public IValueParameter<IntValue> UpdateIntervalParameter { … … 67 63 get { return (IValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; } 68 64 } 69 public IFixedValueParameter<DoubleValue> NumberOfPrunedSubtreesParameter {70 get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedSubtreesParameterName]; }71 }72 public IFixedValueParameter<DoubleValue> NumberOfPrunedTreesParameter {73 get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedTreesParameterName]; }74 }75 65 #endregion 76 66 #region properties 67 protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } } 77 68 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 78 protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }79 69 protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } } 80 70 protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } } 81 71 protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } } 82 72 protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } } 83 protected DoubleValue PrunedSubtrees { get { return NumberOfPrunedSubtreesParameter.Value; } }84 protected DoubleValue PrunedTrees { get { return NumberOfPrunedTreesParameter.Value; } }85 protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }86 73 protected IRandom Random { get { return RandomParameter.ActualValue; } } 87 74 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 88 75 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 76 #endregion 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 } 89 86 #endregion 90 87 … … 93 90 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner) 94 91 : base(original, cloner) { 95 impactValuesCalculator = original.impactValuesCalculator; 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(); 96 100 } 97 101 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() { 102 #region add parameters 98 103 Parameters.Add(new ValueParameter<DoubleRange>(PopulationSliceParameterName, new DoubleRange(0.75, 1))); 99 104 Parameters.Add(new ValueParameter<DoubleValue>(PruningProbabilityParameterName, new DoubleValue(0.5))); 100 // analyzer parameters101 105 Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1))); 102 106 Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0))); 103 107 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName)); 104 108 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName)); 105 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));106 107 Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedSubtreesParameterName, new DoubleValue(0)));108 Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedTreesParameterName, new DoubleValue(0)));109 Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));110 109 Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, new DoubleValue(0.0))); 111 110 Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, new BoolValue(false))); 111 #endregion 112 } 113 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; 120 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; 126 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"; 131 132 resultsCollector = new ResultsCollector(); 133 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning")); 134 resultsCollector.ResultsParameter.ActualName = ResultsParameterName; 135 136 emptyOp = new EmptyOperator(); 137 } 138 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; 149 150 if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds."); 151 return new IntRange(start, end); 152 } 153 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); 161 162 var subscopes = ExecutionContext.Scope.SubScopes; 163 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)); 172 } 173 return oc; 112 174 } 113 175 114 176 public override IOperation Apply() { 115 if (reentry) { 116 UpdateCounter.Value++; 177 UpdateCounter.Value++; 178 if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply(); 179 UpdateCounter.Value = 0; 117 180 118 if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply(); 119 UpdateCounter.Value = 0; 181 if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); } 120 182 121 var trees = SymbolicExpressionTreeParameter.ActualValue.ToList(); 122 var qualities = QualityParameter.ActualValue.ToList(); 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); 123 188 124 var population = trees.Zip(qualities, (tree, quality) => new { Tree = tree, Quality = quality }).ToList(); 125 Func<double, double, int> compare = (a, b) => Maximization.Value ? a.CompareTo(b) : b.CompareTo(a); 126 population.Sort((a, b) => compare(a.Quality.Value, b.Quality.Value)); 127 128 var start = (int)Math.Round(PopulationSlice.Start * trees.Count); 129 var end = (int)Math.Round(PopulationSlice.End * trees.Count); 130 131 if (end == population.Count) end--; 132 133 if (start >= end || end >= population.Count) throw new Exception("Invalid PopulationSlice bounds."); 134 135 PrunedSubtrees.Value = 0; 136 PrunedTrees.Value = 0; 137 138 reentry = false; 139 140 var operations = new OperationCollection { Parallel = true }; 141 foreach (var p in population.Skip(start).Take(end)) { 142 if (Random.NextDouble() > PruningProbability.Value) continue; 143 var op = new SymbolicDataAnalysisExpressionPruningOperator { 144 Model = CreateModel(p.Tree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper), 145 ImpactsCalculator = impactValuesCalculator, 146 ProblemData = ProblemData, 147 Random = Random, 148 PruneOnlyZeroImpactNodes = PruneOnlyZeroImpactNodes.Value, 149 NodeImpactThreshold = NodeImpactThreshold.Value 150 }; 151 operations.Add(ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope)); 152 } 153 return new OperationCollection { operations, ExecutionContext.CreateOperation(this) }; 154 } 155 156 DataTable table; 157 158 if (ResultCollection.ContainsKey("Population Pruning")) { 159 table = (DataTable)ResultCollection["Population Pruning"].Value; 160 } else { 161 table = new DataTable("Population Pruning"); 162 table.Rows.Add(new DataRow("Pruned Trees") { VisualProperties = { StartIndexZero = true } }); 163 table.Rows.Add(new DataRow("Pruned Subtrees") { VisualProperties = { StartIndexZero = true } }); 164 ResultCollection.Add(new Result("Population Pruning", table)); 165 } 166 167 table.Rows["Pruned Trees"].Values.Add(PrunedTrees.Value); 168 table.Rows["Pruned Subtrees"].Values.Add(PrunedSubtrees.Value); 169 170 reentry = true; 171 172 return base.Apply(); 189 return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() }; 173 190 } 174 175 protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,176 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue);177 191 } 178 192 } -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs
r9456 r10579 121 121 var quality = tree 122 122 .Take(topN) 123 .AsParallel()124 123 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 125 124 .ToArray(); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs
r9456 r10579 120 120 .Select(i => tree[i]) 121 121 .Take(topN) 122 .AsParallel()123 122 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 124 123 .ToArray(); -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs
r9456 r10579 131 131 132 132 var variableFrequencies = trees 133 .AsParallel()134 133 .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables)) 135 134 .GroupBy(pair => pair.Key, pair => pair.Value) -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r10390 r10579 139 139 <Private>False</Private> 140 140 </Reference> 141 <Reference Include="HeuristicLab.Optimization.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 142 <SpecificVersion>False</SpecificVersion> 143 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Optimization.Operators-3.3.dll</HintPath> 144 <Private>False</Private> 145 </Reference> 141 146 <Reference Include="HeuristicLab.Parameters-3.3"> 142 147 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> … … 192 197 <SubType>Code</SubType> 193 198 </Compile> 199 <Compile Include="Matching\SymbolicExpressionTreeCanonicalSorter.cs" /> 200 <Compile Include="Matching\SymbolicExpressionTreeEqualityComparer.cs" /> 201 <Compile Include="Matching\SymbolicExpressionTreeMatching.cs" /> 202 <Compile Include="Matching\SymbolicExpressionTreeMaxCommonSequenceCalculator.cs" /> 203 <Compile Include="Matching\SymbolicExpressionTreeNodeComparer.cs" /> 204 <Compile Include="Matching\SymbolicExpressionTreeNodeSimilarityComparer.cs" /> 194 205 <Compile Include="SymbolicDataAnalysisExpressionPruningOperator.cs" /> 195 206 <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" /> -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisImpactValuesCalculator.cs
r8946 r10579 1 1 using System.Collections.Generic; 2 using HeuristicLab.Core; 2 3 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 3 4 4 5 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 5 public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator {6 public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator : IItem { 6 7 double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows); 7 8 double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN); 9 void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, 10 IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN); 8 11 } 9 12 } -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Plugin.cs.frame
r10037 r10579 38 38 [PluginDependency("HeuristicLab.Operators", "3.3")] 39 39 [PluginDependency("HeuristicLab.Optimization", "3.3")] 40 [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")] 40 41 [PluginDependency("HeuristicLab.Parameters", "3.3")] 41 42 [PluginDependency("HeuristicLab.Persistence", "3.3")] -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs
r10390 r10579 34 34 [StorableClass] 35 35 [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")] 36 public class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator { 37 private const string NumberOfPrunedSubtreesParameterName = "PrunedSubtrees"; 38 private const string NumberOfPrunedTreesParameterName = "PrunedTrees"; 36 public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator { 37 #region parameter names 38 private const string ProblemDataParameterName = "ProblemData"; 39 private const string SymbolicDataAnalysisModelParameterName = "SymbolicDataAnalysisModel"; 40 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 41 private const string PrunedSubtreesParameterName = "PrunedSubtrees"; 42 private const string PrunedTreesParameterName = "PrunedTrees"; 43 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 44 private const string NodeImpactThresholdParameterName = "ImpactThreshold"; 45 private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes"; 46 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; // the tree to be pruned 47 private const string QualityParameterName = "Quality"; // the quality 48 private const string EstimationLimitsParameterName = "EstimationLimits"; 49 private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 50 #endregion 51 39 52 #region parameter properties 40 public ILookupParameter< DoubleValue> NumberOfPrunedSubtreesParameter {41 get { return (ILookupParameter< DoubleValue>)Parameters[NumberOfPrunedSubtreesParameterName]; }53 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 54 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 42 55 } 43 public ILookupParameter<DoubleValue> NumberOfPrunedTreesParameter { 44 get { return (ILookupParameter<DoubleValue>)Parameters[NumberOfPrunedTreesParameterName]; } 56 public ILookupParameter<DoubleValue> QualityParameter { 57 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 58 } 59 public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter { 60 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 61 } 62 public IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator> ImpactValuesCalculatorParameter { 63 get { return (IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>)Parameters[ImpactValuesCalculatorParameterName]; } 64 } 65 public ILookupParameter<IntRange> FitnessCalculationPartitionParameter { 66 get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; } 67 } 68 public ILookupParameter<IntValue> PrunedSubtreesParameter { 69 get { return (ILookupParameter<IntValue>)Parameters[PrunedSubtreesParameterName]; } 70 } 71 public ILookupParameter<IntValue> PrunedTreesParameter { 72 get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; } 73 } 74 public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter { 75 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; } 76 } 77 public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter { 78 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; } 79 } 80 public ILookupParameter<DoubleLimit> EstimationLimitsParameter { 81 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 82 } 83 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter { 84 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; } 45 85 } 46 86 #endregion 47 87 #region properties 48 private DoubleValue PrunedSubtrees { get { return NumberOfPrunedSubtreesParameter.ActualValue; } } 49 private DoubleValue PrunedTrees { get { return NumberOfPrunedTreesParameter.ActualValue; } } 88 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 89 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } } 90 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } } 91 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 92 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 93 protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } } 94 protected DoubleValue Quality { get { return QualityParameter.ActualValue; } } 95 protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } } 96 protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } } 50 97 #endregion 51 98 52 99 [StorableConstructor] 53 100 protected SymbolicDataAnalysisExpressionPruningOperator(bool deserializing) : base(deserializing) { } 54 public override IDeepCloneable Clone(Cloner cloner) { 55 return new SymbolicDataAnalysisExpressionPruningOperator(this, cloner); 101 protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner) 102 : base(original, cloner) { } 103 104 protected SymbolicDataAnalysisExpressionPruningOperator() { 105 #region add parameters 106 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName)); 107 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName)); 108 Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName)); 109 Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced.")); 110 Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned.")); 111 Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Specify whether or not only zero impact nodes should be pruned.")); 112 Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "Specifies an impact value threshold below which nodes should be pruned.")); 113 Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName)); 114 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName)); 115 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName)); 116 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName)); 117 #endregion 56 118 } 57 protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner) 58 : base(original, cloner) { 59 } 119 public override IOperation Apply() { 120 var model = CreateModel(); 121 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 122 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToList(); 60 123 61 public SymbolicDataAnalysisExpressionPruningOperator() { 62 Parameters.Add(new LookupParameter<DoubleValue>(NumberOfPrunedSubtreesParameterName)); 63 Parameters.Add(new LookupParameter<DoubleValue>(NumberOfPrunedTreesParameterName)); 64 } 124 var prunedSubtrees = 0; 125 var prunedTrees = 0; 65 126 66 public ISymbolicDataAnalysisModel Model { get; set; } 67 public IDataAnalysisProblemData ProblemData { get; set; } 68 public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactsCalculator { get; set; } 69 public IRandom Random { get; set; } 127 double quality = Evaluate(model); 70 128 71 public bool PruneOnlyZeroImpactNodes { get; set; } 72 public double NodeImpactThreshold { get; set; } 73 74 public override IOperation Apply() { 75 int prunedSubtrees = 0; 76 77 var nodes = Model.SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 78 79 for (int j = 0; j < nodes.Count; ++j) { 80 var node = nodes[j]; 129 for (int i = 0; i < nodes.Count; ++i) { 130 var node = nodes[i]; 81 131 if (node is ConstantTreeNode) continue; 82 132 83 var impact = ImpactsCalculator.CalculateImpactValue(Model, node, ProblemData, ProblemData.TrainingIndices); 133 double impactValue, replacementValue; 134 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality); 84 135 85 if (PruneOnlyZeroImpactNodes) { 86 if (!impact.IsAlmost(0.0)) continue; 87 } else { 88 if (NodeImpactThreshold < impact) continue; 89 } 136 if (PruneOnlyZeroImpactNodes.Value && (!impactValue.IsAlmost(0.0))) continue; 137 else if (NodeImpactThreshold.Value < impactValue) continue; 90 138 91 var replacementValue = ImpactsCalculator.CalculateReplacementValue(Model, node, ProblemData, ProblemData.TrainingIndices);92 139 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 93 140 ReplaceWithConstant(node, constantNode); 94 j += node.GetLength() - 1; // skip subtrees under the node that was folded 141 i += node.GetLength() - 1; // skip subtrees under the node that was folded 142 143 quality -= impactValue; 95 144 96 145 prunedSubtrees++; 97 146 } 98 147 99 if (prunedSubtrees > 0) {100 lock (PrunedSubtrees) { PrunedSubtrees.Value += prunedSubtrees; }101 lock (PrunedTrees) { PrunedTrees.Value += 1; }102 } 148 if (prunedSubtrees > 0) prunedTrees = 1; 149 PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees); 150 PrunedTreesParameter.ActualValue = new IntValue(prunedTrees); 151 103 152 return base.Apply(); 104 153 } … … 109 158 parent.InsertSubtree(i, replacement); 110 159 } 160 protected abstract ISymbolicDataAnalysisModel CreateModel(); 161 protected abstract double Evaluate(IDataAnalysisModel model); 111 162 } 112 163 } -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs
r9456 r10579 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 27 26 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 27 public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : ISymbolicDataAnalysisSolutionImpactValuesCalculator { 29 [StorableClass] 30 [Item("SymbolicDataAnalysisSolutionImpactValuesCalculator", "Calculates the impact values and replacements values for symbolic expression tree nodes.")] 31 public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : Item, ISymbolicDataAnalysisSolutionImpactValuesCalculator { 32 protected SymbolicDataAnalysisSolutionImpactValuesCalculator() { } 33 34 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(SymbolicDataAnalysisSolutionImpactValuesCalculator original, Cloner cloner) 35 : base(original, cloner) { } 36 [StorableConstructor] 37 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 28 38 public abstract double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows); 29 39 public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN); 40 public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN); 30 41 31 42 protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
Note: See TracChangeset
for help on using the changeset viewer.