Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/24/12 11:19:07 (12 years ago)
Author:
mkommend
Message:

#1976: Added constant optimization analyzer.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/ConstantOptimizationAnalyzer.cs

    r8821 r8844  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
     24using HeuristicLab.Analysis;
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2928using HeuristicLab.Optimization;
    3029using HeuristicLab.Parameters;
    3130using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3231
    33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3433  /// <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.
    3635  /// </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.")]
    3837  [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";
    4546
    4647    #region parameter properties
    47     public ILookupParameter<S> ValidationBestSolutionParameter {
    48       get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
     48    public IFixedValueParameter<PercentValue> PercentageOfBestSolutionsParameter {
     49      get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageOfBestSolutionsParameterName]; }
    4950    }
    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]; }
    6254    }
    6355    #endregion
    6456
    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; }
    7263    }
    7364
     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;
    74101    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) {
    77104
    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();
    84121
    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) };
    94130      }
    95131
    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();
    98135
    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²";
    101140
    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;
    109143
    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;
    115149      }
    116       #endregion
    117150
    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());
    123154
    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;
    132157      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;
    140158    }
    141159  }
Note: See TracChangeset for help on using the changeset viewer.