Changeset 17776


Ignore:
Timestamp:
10/19/20 14:14:11 (5 weeks ago)
Author:
dpiringe
Message:

#3076:

  • changed SymbolicRegressionSingleObjectiveMetaModelAnalyzer to calculate the average quality of an solution for all problems
    • average: because we need to include outlier
  • some architectural changes
  • added new (but empty) meta model analyzer for multi objective -> SymbolicRegressionMultiObjectiveMetaModelAnalyzer
Location:
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r17771 r17776  
    183183    <Compile Include="SymbolicRegressionConstraintAnalyzer.cs" />
    184184    <Compile Include="SymbolicRegressionMetaModelAnalyzer.cs" />
     185    <Compile Include="SymbolicRegressionMultiObjectiveMetaModelAnalyzer.cs" />
    185186    <Compile Include="SymbolicRegressionPhenotypicDiversityAnalyzer.cs" />
    186187    <Compile Include="SymbolicRegressionPruningAnalyzer.cs" />
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionMetaModelAnalyzer.cs

    r17771 r17776  
    2222    private const string BaseProblemParameterName = "Base Problem";
    2323    private const string AnalyzeXIterationParameterName = "Analyze X Iteration";
    24     private const string BestMetaModelParameterName = "Best Meta Model";
    2524    #endregion
    2625
     
    3029      (IFixedValueParameter<ItemList<T>>)Parameters[ProblemsToAnalyzeParameterName];
    3130
    32     public IValueParameter<SymbolicRegressionSingleObjectiveProblem> BaseProblemParameter =>
    33       (IValueParameter<SymbolicRegressionSingleObjectiveProblem>)Parameters[BaseProblemParameterName];
     31    public IValueParameter<T> BaseProblemParameter =>
     32      (IValueParameter<T>)Parameters[BaseProblemParameterName];
    3433
    3534    public IFixedValueParameter<IntValue> AnalyzeXIterationParameter =>
    3635      (IFixedValueParameter<IntValue>)Parameters[AnalyzeXIterationParameterName];
    37 
    38     public IResultParameter<ISymbolicRegressionSolution> BestMetaModelParameter =>
    39       (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestMetaModelParameterName];
    4036    #endregion
    4137
    4238    public static int Iterations { get; set; } = 1;
    4339
     40    #region constructors and cloning
    4441    protected SymbolicRegressionMetaModelAnalyzer(SymbolicRegressionMetaModelAnalyzer<T> original, Cloner cloner) :
    4542      base(original, cloner) { }
     
    5552      Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
    5653        "After every X iteration, the analyzer will perform its step.", new IntValue(1)));
    57       Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
    58         "The best meta model found."));
    5954    }
    6055
     
    7267        Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
    7368          "After every X iteration, the analyzer will perform its step.", new IntValue(1)));
     69    }
    7470
    75       if (!Parameters.ContainsKey(BestMetaModelParameterName))
    76         Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
    77           "The best meta model found."));
    78     }
     71    #endregion
    7972
    8073    public override void InitializeState() {
     
    8982
    9083    //protected abstract bool TryFittingSolution(ISymbolicExpressionTree tree, T problem, out SymbolicRegressionSolution solution);
    91     protected abstract void PerformApply(T problem, string targetVariable);
     84    protected abstract void PerformApply(T baseProblem, IEnumerable<T> problems, string targetVariable);
    9285
    9386    public override IOperation Apply() {
     
    9891      if (Iterations >= AnalyzeXIterationParameter.Value.Value) {
    9992        Iterations = 1;
    100         string targetVariable = BaseProblemParameter.Value.ProblemData.TargetVariable;
     93        var baseProblem = BaseProblemParameter.Value;
     94        var problems = ProblemsToAnalyzeParameter.Value;
     95        var targetVariable = BaseProblemParameter.Value.ProblemData.TargetVariable;
    10196
    102         foreach (var problem in ProblemsToAnalyzeParameter.Value) {
    103           if (problem.ProblemData.TargetVariable == targetVariable)
    104             PerformApply(problem, targetVariable);
    105           else
    106             throw new ArgumentException($"The target variable of the problem '{problem.Name}' does not match with the base problem.");
    107         }
    108        
    109         /*
    110           * besser als eigenes Problem (abgeleitet von SymbolicRegressionProblem?), vlt auch als "CompoundProblem" (mehrere SymReg Problems in einen)?
    111           * eigenes ProblemData?
    112           * oder im Analyzer einfach anstatt Dataset -> ProblemData?
    113           */
     97        // error handling
     98        var badProblems = problems.Where(x => x.ProblemData.TargetVariable != targetVariable);
     99        IList<Exception> errors = new List<Exception>();
     100        foreach (var problem in badProblems)
     101          errors.Add(new ArgumentException($"The target variable of the problem '{problem.Name}' does not match with the base problem."));
     102        if (badProblems.Any())
     103          throw new AggregateException(errors);
     104
     105        // apply
     106        PerformApply(baseProblem, problems, targetVariable);
    114107      } else {
    115108        Iterations++;
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSingleObjectiveMetaModelAnalyzer.cs

    r17771 r17776  
    99using HeuristicLab.Data;
    1010using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     11using HeuristicLab.Optimization;
    1112
    1213namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    1516    : SymbolicRegressionMetaModelAnalyzer<SymbolicRegressionSingleObjectiveProblem>, ISymbolicExpressionTreeAnalyzer {
    1617
     18    #region constants
     19    private const string BestMetaModelParameterName = "Best Meta Model";
     20    #endregion
     21
     22    #region parameter properties
     23    public IResultParameter<ISymbolicRegressionSolution> BestMetaModelParameter =>
     24      (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestMetaModelParameterName];
     25    #endregion
     26
     27    #region constructors and cloning
    1728    [StorableConstructor]
    1829    protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(StorableConstructorFlag _) : base(_) { }
     
    2031    protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(SymbolicRegressionSingleObjectiveMetaModelAnalyzer original, Cloner cloner) : base(original, cloner) { }
    2132
    22     public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() { }
     33    public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() {
     34      Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
     35        "The best meta model found."));
     36    }
    2337
    2438    [StorableHook(HookType.AfterDeserialization)]
    25     private void AfterDeserialization() { }
     39    private void AfterDeserialization() {
     40      if (!Parameters.ContainsKey(BestMetaModelParameterName))
     41        Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
     42          "The best meta model found."));
     43    }
    2644
    2745    public override IDeepCloneable Clone(Cloner cloner) => new SymbolicRegressionSingleObjectiveMetaModelAnalyzer(this, cloner);
     46    #endregion
    2847
    29     protected override void PerformApply(SymbolicRegressionSingleObjectiveProblem problem, string targetVariable) {
    30       double bestQuality = problem.Maximization.Value ? double.MinValue : double.MaxValue;
     48    protected override void PerformApply(
     49      SymbolicRegressionSingleObjectiveProblem baseProblem,
     50      IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems,
     51      string targetVariable) {
     52      // init
     53      var solutions = this.SymbolicExpressionTree.ToArray();
     54      var evaluator = baseProblem.Evaluator;
     55      var bestQuality = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
    3156      SymbolicRegressionSolution bestMetaModel = null;
    32       foreach (var tree in this.SymbolicExpressionTree.ToArray()) {
    33         IDataset dataset = problem.ProblemData.Dataset;
    34         IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows);
    35         var quality = problem.Evaluator.Evaluate(ExecutionContext, tree, problem.ProblemData, rows);
    36        
    37         bool isBetter = problem.Maximization.Value ? (bestQuality < quality) : (bestQuality > quality);
    38         if(isBetter) {
    39           bestQuality = quality;
    40           var model = new SymbolicRegressionModel(
    41             targetVariable,
    42             (ISymbolicExpressionTree)tree.Clone(),
    43             new SymbolicDataAnalysisExpressionTreeInterpreter());
    44           bestMetaModel = new SymbolicRegressionSolution(model, problem.ProblemData);
     57
     58      // iterate solutions
     59      foreach (var solution in solutions) {
     60        double qualityAvg = CalculateAverageQuality(solution, evaluator, problems);
     61
     62        // check if this solution is the best
     63        bool isBest = baseProblem.Maximization.Value ? (bestQuality < qualityAvg) : (bestQuality > qualityAvg);
     64        if (isBest) {
     65          bestQuality = qualityAvg;
     66          bestMetaModel = BuildSolution(solution, targetVariable, baseProblem);
    4567        }
    4668      }
    4769      BestMetaModelParameter.ActualValue = bestMetaModel;
    4870    }
     71
     72    private SymbolicRegressionSolution BuildSolution(
     73      ISymbolicExpressionTree solution,
     74      string targetVariable,
     75      SymbolicRegressionSingleObjectiveProblem baseProblem) {
     76      var model = new SymbolicRegressionModel(
     77            targetVariable,
     78            (ISymbolicExpressionTree)solution.Clone(),
     79            new SymbolicDataAnalysisExpressionTreeInterpreter());
     80      return new SymbolicRegressionSolution(model, baseProblem.ProblemData);
     81    }
     82
     83    private double CalculateAverageQuality(
     84      ISymbolicExpressionTree solution,
     85      ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator,
     86      IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems) {
     87      double qualitySum = 0.0;
     88      // iterate problems
     89      foreach (var problem in problems) {
     90        IDataset dataset = problem.ProblemData.Dataset;
     91        IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows);
     92        // evalute problem with the evaluator of the base problem
     93        qualitySum += evaluator.Evaluate(ExecutionContext, solution, problem.ProblemData, rows);
     94      }
     95      // calculate the average quality
     96      return qualitySum / problems.Count();
     97    }
    4998  }
    5099}
Note: See TracChangeset for help on using the changeset viewer.