1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HEAL.Attic;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
11 | using HeuristicLab.Optimization;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
14 | [StorableType("4B69A82A-265B-46DA-9055-B6E0EB6C3EC8")]
|
---|
15 | public class SymbolicRegressionSingleObjectiveMetaModelAnalyzer
|
---|
16 | : SymbolicRegressionMetaModelAnalyzer<SymbolicRegressionSingleObjectiveProblem>, ISymbolicExpressionTreeAnalyzer {
|
---|
17 |
|
---|
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
|
---|
28 | [StorableConstructor]
|
---|
29 | protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
30 |
|
---|
31 | protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(SymbolicRegressionSingleObjectiveMetaModelAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
32 |
|
---|
33 | public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() {
|
---|
34 | Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
|
---|
35 | "The best meta model found."));
|
---|
36 | }
|
---|
37 |
|
---|
38 | [StorableHook(HookType.AfterDeserialization)]
|
---|
39 | private void AfterDeserialization() {
|
---|
40 | if (!Parameters.ContainsKey(BestMetaModelParameterName))
|
---|
41 | Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
|
---|
42 | "The best meta model found."));
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) => new SymbolicRegressionSingleObjectiveMetaModelAnalyzer(this, cloner);
|
---|
46 | #endregion
|
---|
47 |
|
---|
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;
|
---|
56 | SymbolicRegressionSolution bestMetaModel = null;
|
---|
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);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | BestMetaModelParameter.ActualValue = bestMetaModel;
|
---|
70 | }
|
---|
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 | }
|
---|
98 | }
|
---|
99 | }
|
---|