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 | using HeuristicLab.Parameters;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
15 | [StorableType("4B69A82A-265B-46DA-9055-B6E0EB6C3EC8")]
|
---|
16 | public class SymbolicRegressionSingleObjectiveMetaModelAnalyzer
|
---|
17 | : SymbolicRegressionMetaModelAnalyzer<SymbolicRegressionSingleObjectiveProblem>, ISymbolicExpressionTreeAnalyzer {
|
---|
18 |
|
---|
19 | #region constants
|
---|
20 | private const string BestMetaModelParameterName = "Best Meta Model";
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region parameter properties
|
---|
24 | public IResultParameter<ItemList<ISymbolicRegressionSolution>> BestMetaModelParameter =>
|
---|
25 | (IResultParameter<ItemList<ISymbolicRegressionSolution>>)Parameters[BestMetaModelParameterName];
|
---|
26 | #endregion
|
---|
27 |
|
---|
28 | #region constructors and cloning
|
---|
29 | [StorableConstructor]
|
---|
30 | protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
31 |
|
---|
32 | protected SymbolicRegressionSingleObjectiveMetaModelAnalyzer(SymbolicRegressionSingleObjectiveMetaModelAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
33 |
|
---|
34 | public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() {
|
---|
35 | Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName,
|
---|
36 | "A list with the meta model for all problems."));
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableHook(HookType.AfterDeserialization)]
|
---|
40 | private void AfterDeserialization() {
|
---|
41 | Parameters.Remove(BestMetaModelParameterName);
|
---|
42 |
|
---|
43 | if (!Parameters.ContainsKey(BestMetaModelParameterName))
|
---|
44 | Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName,
|
---|
45 | "A list with all meta models for the problems."));
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) => new SymbolicRegressionSingleObjectiveMetaModelAnalyzer(this, cloner);
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | protected override void PerformApply(
|
---|
52 | SymbolicRegressionSingleObjectiveProblem baseProblem,
|
---|
53 | IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems,
|
---|
54 | string targetVariable) {
|
---|
55 | // init
|
---|
56 | var solutions = this.SymbolicExpressionTree.ToArray();
|
---|
57 | var bestQualityWithConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
|
---|
58 | var bestQualityWithoutConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
|
---|
59 | var evaluator = baseProblem.Evaluator;
|
---|
60 | var interpreter = baseProblem.SymbolicExpressionTreeInterpreter;
|
---|
61 | ISymbolicExpressionTree bestSolutionWithConstantOpt = null;
|
---|
62 | ISymbolicExpressionTree bestSolutionWithoutConstantOpt = null;
|
---|
63 | var metaModels = new ItemList<ISymbolicRegressionSolution>();
|
---|
64 | // iterate solutions
|
---|
65 | foreach (var solution in solutions) {
|
---|
66 | // calculate with constant optimization
|
---|
67 | var tmpSolution = (ISymbolicExpressionTree) solution.Clone();
|
---|
68 | double qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, true);
|
---|
69 | // check if this solution is the best
|
---|
70 | bool isBest = baseProblem.Maximization.Value ? (bestQualityWithConstantOpt < qualityAvg) : (bestQualityWithConstantOpt > qualityAvg);
|
---|
71 | if (isBest) {
|
---|
72 | bestQualityWithConstantOpt = qualityAvg;
|
---|
73 | bestSolutionWithConstantOpt = tmpSolution;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // calculate it again without constant optimization to have a comparison
|
---|
77 | tmpSolution = (ISymbolicExpressionTree) solution.Clone();
|
---|
78 | qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, false);
|
---|
79 | // check if this solution is the best
|
---|
80 | isBest = baseProblem.Maximization.Value ? (bestQualityWithoutConstantOpt < qualityAvg) : (bestQualityWithoutConstantOpt > qualityAvg);
|
---|
81 | if (isBest) {
|
---|
82 | bestQualityWithoutConstantOpt = qualityAvg;
|
---|
83 | bestSolutionWithoutConstantOpt = tmpSolution;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | foreach(var problem in problems) {
|
---|
88 | metaModels.Add(BuildSolution(bestSolutionWithConstantOpt, targetVariable, problem, "withConstantOpt"));
|
---|
89 | metaModels.Add(BuildSolution(bestSolutionWithoutConstantOpt, targetVariable, problem, "withoutConstantOpt"));
|
---|
90 | }
|
---|
91 |
|
---|
92 | BestMetaModelParameter.ActualValue = metaModels;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private SymbolicRegressionSolution BuildSolution(
|
---|
96 | ISymbolicExpressionTree solution,
|
---|
97 | string targetVariable,
|
---|
98 | SymbolicRegressionSingleObjectiveProblem problem,
|
---|
99 | string suffix) {
|
---|
100 | var model = new SymbolicRegressionModel(
|
---|
101 | targetVariable,
|
---|
102 | (ISymbolicExpressionTree)solution.Clone(),
|
---|
103 | new SymbolicDataAnalysisExpressionTreeInterpreter());
|
---|
104 | return new SymbolicRegressionSolution(model, problem.ProblemData) { Name = $"{problem.Name}_solution_{suffix}" };
|
---|
105 | }
|
---|
106 |
|
---|
107 | private double CalculateAverageQuality(
|
---|
108 | ISymbolicExpressionTree solution,
|
---|
109 | ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator,
|
---|
110 | IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems,
|
---|
111 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
112 | bool useConstantOptimization) {
|
---|
113 | double qualitySum = 0.0;
|
---|
114 | // iterate problems
|
---|
115 | foreach (var problem in problems) {
|
---|
116 | var problemData = problem.ProblemData;
|
---|
117 |
|
---|
118 | if (useConstantOptimization) {
|
---|
119 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
|
---|
120 | interpreter,
|
---|
121 | solution,
|
---|
122 | problemData,
|
---|
123 | problemData.TrainingIndices,
|
---|
124 | false, 10, true);
|
---|
125 | }
|
---|
126 |
|
---|
127 | qualitySum += evaluator.Evaluate(
|
---|
128 | ExecutionContext,
|
---|
129 | solution,
|
---|
130 | problemData,
|
---|
131 | problemData.TrainingIndices);
|
---|
132 | }
|
---|
133 |
|
---|
134 | // calculate the average quality
|
---|
135 | return qualitySum / problems.Count();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|