- Timestamp:
- 09/17/12 11:18:40 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveAnalyzer.cs
r7259 r8664 20 20 #endregion 21 21 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Core; 26 24 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;28 using HeuristicLab.Operators;29 25 using HeuristicLab.Parameters; 30 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Optimization;32 27 33 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 39 34 private const string QualitiesParameterName = "Qualities"; 40 35 private const string MaximizationParameterName = "Maximization"; 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 41 37 #region parameter properties 42 38 public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter { … … 45 41 public ILookupParameter<BoolArray> MaximizationParameter { 46 42 get { return (ILookupParameter<BoolArray>)Parameters[MaximizationParameterName]; } 43 } 44 public ILookupParameter<BoolValue> ApplyLinearScalingParameter { 45 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 47 46 } 48 47 #endregion … … 64 63 Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(QualitiesParameterName, "The qualities of the trees that should be analyzed.")); 65 64 Parameters.Add(new LookupParameter<BoolArray>(MaximizationParameterName, "The directions of optimization for each dimension.")); 65 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 66 } 67 68 [StorableHook(HookType.AfterDeserialization)] 69 private void AfterDeserialization() { 70 if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>) 71 Parameters.Remove(ApplyLinearScalingParameterName); 72 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) 73 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 66 74 } 67 75 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveAnalyzer.cs
r7259 r8664 20 20 #endregion 21 21 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Core; 26 24 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;28 using HeuristicLab.Operators;29 25 using HeuristicLab.Parameters; 30 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Optimization;32 27 33 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 39 34 private const string QualityParameterName = "Quality"; 40 35 private const string MaximizationParameterName = "Maximization"; 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 37 41 38 #region parameter properties 42 39 public IScopeTreeLookupParameter<DoubleValue> QualityParameter { … … 45 42 public ILookupParameter<BoolValue> MaximizationParameter { 46 43 get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; } 44 } 45 public ILookupParameter<BoolValue> ApplyLinearScalingParameter { 46 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 47 47 } 48 48 #endregion … … 64 64 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the trees that should be analyzed.")); 65 65 Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization.")); 66 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 67 } 68 69 [StorableHook(HookType.AfterDeserialization)] 70 private void AfterDeserialization() { 71 if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>)) 72 Parameters.Remove(ApplyLinearScalingParameterName); 73 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) 74 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 66 75 } 67 76 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs
r7259 r8664 44 44 private const string EvaluationPartitionParameterName = "EvaluationPartition"; 45 45 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 46 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 46 47 47 48 public override bool CanChangeName { get { return false; } } … … 70 71 get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 71 72 } 73 public ILookupParameter<BoolValue> ApplyLinearScalingParameter { 74 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 75 } 72 76 #endregion 73 77 … … 87 91 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees.")); 88 92 Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.")); 93 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 94 } 95 96 [StorableHook(HookType.AfterDeserialization)] 97 private void AfterDeserialization() { 98 if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>)) 99 Parameters.Remove(ApplyLinearScalingParameterName); 100 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) 101 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.")); 89 102 } 90 103 … … 94 107 95 108 protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) { 96 97 98 109 IEnumerable<int> rows; 99 110 int samplesStart = EvaluationPartitionParameter.ActualValue.Start; … … 115 126 return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i); 116 127 } 128 129 [ThreadStatic] 130 private static double[] cache; 131 protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, 132 double lowerEstimationLimit, double upperEstimationLimit, 133 IOnlineCalculator calculator, int maxRows) { 134 if (cache == null || cache.GetLength(0) < maxRows) { 135 cache = new double[maxRows]; 136 } 137 138 //calculate linear scaling 139 //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements 140 //this is not true if the cache is used 141 int i = 0; 142 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 143 var targetValuesEnumerator = targetValues.GetEnumerator(); 144 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 145 while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) { 146 double target = targetValuesEnumerator.Current; 147 double estimated = estimatedValuesEnumerator.Current; 148 cache[i] = estimated; 149 if (!double.IsNaN(estimated) && !double.IsInfinity(estimated)) 150 linearScalingCalculator.Add(estimated, target); 151 i++; 152 } 153 if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext())) 154 throw new ArgumentException("Number of elements in target and estimated values enumeration do not match."); 155 156 double alpha = linearScalingCalculator.Alpha; 157 double beta = linearScalingCalculator.Beta; 158 if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) { 159 alpha = 0.0; 160 beta = 1.0; 161 } 162 163 //calculate the quality by using the passed online calculator 164 targetValuesEnumerator = targetValues.GetEnumerator(); 165 var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha) 166 .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator(); 167 168 while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) { 169 calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current); 170 } 171 } 117 172 } 118 173 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisEvaluator.cs
r7259 r8664 29 29 IValueLookupParameter<IntRange> EvaluationPartitionParameter { get; } 30 30 IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { get; } 31 ILookupParameter<BoolValue> ApplyLinearScalingParameter { get; } 31 32 32 33 IValueLookupParameter<T> ProblemDataParameter { get; } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisMultiObjectiveAnalyzer.cs
r7259 r8664 21 21 using HeuristicLab.Core; 22 22 using HeuristicLab.Data; 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;24 using HeuristicLab.Optimization;25 using HeuristicLab.Parameters;26 23 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 27 24 public interface ISymbolicDataAnalysisMultiObjectiveAnalyzer : ISymbolicDataAnalysisAnalyzer { 28 25 IScopeTreeLookupParameter<DoubleArray> QualitiesParameter { get; } 29 26 ILookupParameter<BoolArray> MaximizationParameter { get; } 27 ILookupParameter<BoolValue> ApplyLinearScalingParameter { get; } 30 28 31 29 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisSingleObjectiveAnalyzer.cs
r7259 r8664 28 28 IScopeTreeLookupParameter<DoubleValue> QualityParameter { get; } 29 29 ILookupParameter<BoolValue> MaximizationParameter { get; } 30 ILookupParameter<BoolValue> ApplyLinearScalingParameter { get; } 30 31 } 31 32 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs
r7259 r8664 20 20 #endregion 21 21 22 using System; 22 23 using System.Drawing; 23 24 using HeuristicLab.Common; … … 66 67 this.interpreter = interpreter; 67 68 } 69 70 #region Scaling 71 public static void Scale(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, string targetVariable) { 72 var dataset = problemData.Dataset; 73 var rows = problemData.TrainingIndices; 74 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows); 75 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 76 77 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 78 var targetValuesEnumerator = targetValues.GetEnumerator(); 79 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 80 while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) { 81 double target = targetValuesEnumerator.Current; 82 double estimated = estimatedValuesEnumerator.Current; 83 if (!double.IsNaN(estimated) && !double.IsInfinity(estimated)) 84 linearScalingCalculator.Add(estimated, target); 85 } 86 if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext())) 87 throw new ArgumentException("Number of elements in target and estimated values enumeration do not match."); 88 89 double alpha = linearScalingCalculator.Alpha; 90 double beta = linearScalingCalculator.Beta; 91 if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) return; 92 93 ConstantTreeNode alphaTreeNode = null; 94 ConstantTreeNode betaTreeNode = null; 95 // check if model has been scaled previously by analyzing the structure of the tree 96 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0); 97 if (startNode.GetSubtree(0).Symbol is Addition) { 98 var addNode = startNode.GetSubtree(0); 99 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) { 100 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 101 var mulNode = addNode.GetSubtree(0); 102 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 103 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 104 } 105 } 106 } 107 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 108 if (alphaTreeNode != null && betaTreeNode != null) { 109 betaTreeNode.Value *= beta; 110 alphaTreeNode.Value *= beta; 111 alphaTreeNode.Value += alpha; 112 } else { 113 var mainBranch = startNode.GetSubtree(0); 114 startNode.RemoveSubtree(0); 115 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha); 116 startNode.AddSubtree(scaledMainBranch); 117 } 118 } 119 120 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) { 121 if (alpha.IsAlmost(0.0)) { 122 return treeNode; 123 } else { 124 var addition = new Addition(); 125 var node = addition.CreateTreeNode(); 126 var alphaConst = MakeConstant(alpha); 127 node.AddSubtree(treeNode); 128 node.AddSubtree(alphaConst); 129 return node; 130 } 131 } 132 133 private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) { 134 if (beta.IsAlmost(1.0)) { 135 return treeNode; 136 } else { 137 var multipliciation = new Multiplication(); 138 var node = multipliciation.CreateTreeNode(); 139 var betaConst = MakeConstant(beta); 140 node.AddSubtree(treeNode); 141 node.AddSubtree(betaConst); 142 return node; 143 } 144 } 145 146 private static ISymbolicExpressionTreeNode MakeConstant(double c) { 147 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode(); 148 node.Value = c; 149 return node; 150 } 151 #endregion 68 152 } 69 153 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs
r8203 r8664 53 53 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 54 54 private const string ValidationPartitionParameterName = "ValidationPartition"; 55 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 55 56 56 57 private const string ProblemDataParameterDescription = ""; … … 64 65 private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual."; 65 66 private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional)."; 67 private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating."; 66 68 #endregion 67 69 … … 100 102 get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; } 101 103 } 104 public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter { 105 get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 106 } 102 107 #endregion 103 108 … … 144 149 public IntRange ValidationPartition { 145 150 get { return ValidationPartitionParameter.Value; } 151 } 152 public BoolValue ApplyLinearScaling { 153 get { return ApplyLinearScalingParameter.Value; } 146 154 } 147 155 #endregion … … 151 159 [StorableHook(HookType.AfterDeserialization)] 152 160 private void AfterDeserialization() { 161 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) { 162 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false))); 163 ApplyLinearScalingParameter.Hidden = true; 164 } 165 153 166 RegisterEventHandlers(); 154 167 } … … 170 183 Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription)); 171 184 Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1))); 185 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false))); 172 186 173 187 SymbolicExpressionTreeInterpreterParameter.Hidden = true; 174 188 MaximumFunctionArgumentsParameter.Hidden = true; 175 189 MaximumFunctionDefinitionsParameter.Hidden = true; 190 ApplyLinearScalingParameter.Hidden = true; 176 191 177 192 SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar(); … … 274 289 275 290 foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) { 276 op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter Name;291 op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name; 277 292 } 278 293 foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) { 279 op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter Name;280 op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter Name;294 op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name; 295 op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name; 281 296 } 282 297 foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) { 283 op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter Name;284 op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter Name;298 op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name; 299 op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name; 285 300 } 286 301 foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) { … … 289 304 op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name; 290 305 op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name; 306 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 291 307 } 292 308 foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) { … … 300 316 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 301 317 } 318 foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) { 319 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 320 } 321 foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) { 322 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 323 } 302 324 foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) { 303 325 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; … … 308 330 } 309 331 foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) { 310 op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter Name;332 op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name; 311 333 } 312 334 foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) { 313 op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter Name;335 op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name; 314 336 op.ProblemDataParameter.ActualName = ProblemDataParameter.Name; 315 337 op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
Note: See TracChangeset
for help on using the changeset viewer.