Changeset 8844
 Timestamp:
 10/24/12 11:19:07 (11 years ago)
 Location:
 trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
 Files:

 1 edited
 1 copied
Legend:
 Unmodified
 Added
 Removed

trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression3.4.csproj
r8793 r8844 118 118 <Compile Include="MultiObjective\SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs" /> 119 119 <Compile Include="Plugin.cs" /> 120 <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" /> 120 121 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" /> 121 122 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs" /> 
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/ConstantOptimizationAnalyzer.cs
r8821 r8844 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 24 using HeuristicLab.Analysis; 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 28 using HeuristicLab.Optimization; 30 29 using HeuristicLab.Parameters; 31 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 31 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic {32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 34 33 /// <summary> 35 /// An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.34 /// An operator that optimizes the constants for the best symbolic expression tress in the current generation. 36 35 /// </summary> 37 [Item(" SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]36 [Item("ConstantOptimizationAnalyzer", "An operator that performs a constant optimization on the best symbolic expression trees.")] 38 37 [StorableClass] 39 public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U> 40 where S : class, ISymbolicDataAnalysisSolution 41 where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U> 42 where U : class, IDataAnalysisProblemData { 43 private const string ValidationBestSolutionParameterName = "Best validation solution"; 44 private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality"; 38 public sealed class ConstantOptimizationAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer { 39 private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions"; 40 private const string ConstantOptimizationEvaluatorParameterName = "ConstantOptimizationOperator"; 41 42 private const string DataTableNameConstantOptimizationImprovement = "Constant Optimization Improvement"; 43 private const string DataRowNameMinimumImprovement = "Minimum improvement"; 44 private const string DataRowNameAverageImprovement = "Average improvement"; 45 private const string DataRowNameMaximumImprovement = "Maximum improvement"; 45 46 46 47 #region parameter properties 47 public I LookupParameter<S> ValidationBestSolutionParameter {48 get { return (I LookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }48 public IFixedValueParameter<PercentValue> PercentageOfBestSolutionsParameter { 49 get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageOfBestSolutionsParameterName]; } 49 50 } 50 public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter { 51 get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; } 52 } 53 #endregion 54 #region properties 55 public S ValidationBestSolution { 56 get { return ValidationBestSolutionParameter.ActualValue; } 57 set { ValidationBestSolutionParameter.ActualValue = value; } 58 } 59 public DoubleValue ValidationBestSolutionQuality { 60 get { return ValidationBestSolutionQualityParameter.ActualValue; } 61 set { ValidationBestSolutionQualityParameter.ActualValue = value; } 51 52 public IFixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator> ConstantOptimizationEvaluatorParameter { 53 get { return (IFixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator>)Parameters[ConstantOptimizationEvaluatorParameterName]; } 62 54 } 63 55 #endregion 64 56 65 [StorableConstructor] 66 protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 67 protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { } 68 public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer() 69 : base() { 70 Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution.")); 71 Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution.")); 57 #region properties 58 public SymbolicRegressionConstantOptimizationEvaluator ConstantOptimizationEvaluator { 59 get { return ConstantOptimizationEvaluatorParameter.Value; } 60 } 61 public double PercentageOfBestSolutions { 62 get { return PercentageOfBestSolutionsParameter.Value.Value; } 72 63 } 73 64 65 private DataTable ConstantOptimizationImprovementDataTable { 66 get { 67 IResult result; 68 ResultCollection.TryGetValue("Constant Optimization Improvement", out result); 69 if (result == null) return null; 70 return (DataTable)result.Value; 71 } 72 } 73 private DataRow MinimumImprovement { 74 get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameMinimumImprovement]; } 75 } 76 private DataRow AverageImprovement { 77 get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameAverageImprovement]; } 78 } 79 private DataRow MaximumImprovement { 80 get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameMaximumImprovement]; } 81 } 82 83 #endregion 84 85 [StorableConstructor] 86 protected ConstantOptimizationAnalyzer(bool deserializing) : base(deserializing) { } 87 protected ConstantOptimizationAnalyzer(ConstantOptimizationAnalyzer original, Cloner cloner) : base(original, cloner) { } 88 public override IDeepCloneable Clone(Cloner cloner) { return new ConstantOptimizationAnalyzer(this, cloner); } 89 public ConstantOptimizationAnalyzer() 90 : base() { 91 Parameters.Add(new FixedValueParameter<PercentValue>(PercentageOfBestSolutionsParameterName, "The percentage of the top solutions which should be analyzed.", new PercentValue(0.1))); 92 Parameters.Add(new FixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator>(ConstantOptimizationEvaluatorParameterName, "The operator used to perform the constant optimization")); 93 94 //Changed the ActualName of the EvaluationPartitionParameter so that it matches the parameter name of symbolic regression problems. 95 ConstantOptimizationEvaluator.EvaluationPartitionParameter.ActualName = "FitnessCalculationPartition"; 96 } 97 98 99 private double[] qualitiesBeforeCoOp = null; 100 private int[] scopeIndexes = null; 74 101 public override IOperation Apply() { 75 IEnumerable<int> rows = GenerateRowsToEvaluate();76 if ( !rows.Any()) return base.Apply();102 //code executed for first call of analyzer 103 if (qualitiesBeforeCoOp == null) { 77 104 78 #region find best tree 79 var evaluator = EvaluatorParameter.ActualValue; 80 var problemData = ProblemDataParameter.ActualValue; 81 double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity; 82 ISymbolicExpressionTree bestTree = null; 83 ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); 105 double[] trainingQuality; 106 // sort is ascending and we take the first n% => order so that best solutions are smallest 107 // sort order is determined by maximization parameter 108 if (Maximization.Value) { 109 // largest values must be sorted first 110 trainingQuality = Quality.Select(x => x.Value).ToArray(); 111 } else { 112 // smallest values must be sorted first 113 trainingQuality = Quality.Select(x => x.Value).ToArray(); 114 } 115 // sort trees by training qualities 116 int topN = (int)Math.Max(trainingQuality.Length * PercentageOfBestSolutions, 1); 117 scopeIndexes = Enumerable.Range(0, trainingQuality.Length).ToArray(); 118 Array.Sort(trainingQuality, scopeIndexes); 119 scopeIndexes = scopeIndexes.Take(topN).ToArray(); 120 qualitiesBeforeCoOp = scopeIndexes.Select(x => Quality[x].Value).ToArray(); 84 121 85 // sort is ascending and we take the first n% => order so that best solutions are smallest 86 // sort order is determined by maximization parameter 87 double[] trainingQuality; 88 if (Maximization.Value) { 89 // largest values must be sorted first 90 trainingQuality = Quality.Select(x => x.Value).ToArray(); 91 } else { 92 // smallest values must be sorted first 93 trainingQuality = Quality.Select(x => x.Value).ToArray(); 122 OperationCollection operationCollection = new OperationCollection(); 123 operationCollection.Parallel = true; 124 foreach (var scopeIndex in scopeIndexes) { 125 var childOperation = ExecutionContext.CreateChildOperation(ConstantOptimizationEvaluator, ExecutionContext.Scope.SubScopes[scopeIndex]); 126 operationCollection.Add(childOperation); 127 } 128 129 return new OperationCollection { operationCollection, ExecutionContext.CreateOperation(this) }; 94 130 } 95 131 96 // sort trees by training qualities 97 Array.Sort(trainingQuality, tree); 132 //code executed to analyze results of constant optimization 133 double[] qualitiesAfterCoOp = scopeIndexes.Select(x => Quality[x].Value).ToArray(); 134 var qualityImprovement = qualitiesBeforeCoOp.Zip(qualitiesAfterCoOp, (b, a) => a  b).ToArray(); 98 135 99 // number of best training solutions to validate (at least 1) 100 int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1); 136 if (!ResultCollection.ContainsKey(DataTableNameConstantOptimizationImprovement)) { 137 var dataTable = new DataTable(DataTableNameConstantOptimizationImprovement); 138 ResultCollection.Add(new Result(DataTableNameConstantOptimizationImprovement, dataTable)); 139 dataTable.VisualProperties.YAxisTitle = "R²"; 101 140 102 IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator); 103 // evaluate best n training trees on validiation set 104 var quality = tree 105 .Take(topN) 106 .AsParallel() 107 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 108 .ToArray(); 141 dataTable.Rows.Add(new DataRow(DataRowNameMinimumImprovement)); 142 MinimumImprovement.VisualProperties.StartIndexZero = true; 109 143 110 for (int i = 0; i < quality.Length; i++) {111 if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {112 bestValidationQuality = quality[i]; 113 bestTree = tree[i];114 }144 dataTable.Rows.Add(new DataRow(DataRowNameAverageImprovement)); 145 AverageImprovement.VisualProperties.StartIndexZero = true; 146 147 dataTable.Rows.Add(new DataRow(DataRowNameMaximumImprovement)); 148 MaximumImprovement.VisualProperties.StartIndexZero = true; 115 149 } 116 #endregion117 150 118 var results = ResultCollection; 119 if (ValidationBestSolutionQuality == null  120 IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) { 121 ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality); 122 ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality); 151 MinimumImprovement.Values.Add(qualityImprovement.Min()); 152 AverageImprovement.Values.Add(qualityImprovement.Average()); 153 MaximumImprovement.Values.Add(qualityImprovement.Max()); 123 154 124 if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) { 125 results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution)); 126 results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality)); 127 } else { 128 results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution; 129 results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality; 130 } 131 } 155 qualitiesBeforeCoOp = null; 156 scopeIndexes = null; 132 157 return base.Apply(); 133 }134 135 protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);136 137 private bool IsBetter(double lhs, double rhs, bool maximization) {138 if (maximization) return lhs > rhs;139 else return lhs < rhs;140 158 } 141 159 }
Note: See TracChangeset
for help on using the changeset viewer.