1 | using HEAL.Attic;
|
---|
2 | using HeuristicLab.Analysis;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using System;
|
---|
10 | using System.Collections.Generic;
|
---|
11 | using System.Linq;
|
---|
12 | using System.Text;
|
---|
13 | using System.Threading.Tasks;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
16 | [StorableType("E5B7E058-E63F-4059-A87F-EA9247592254")]
|
---|
17 | public abstract class SymbolicRegressionMetaModelAnalyzer<T>
|
---|
18 | : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer where T: class, IRegressionProblem {
|
---|
19 |
|
---|
20 | #region constants
|
---|
21 | private const string ProblemsToAnalyzeParameterName = "Problems to Analyze";
|
---|
22 | private const string BaseProblemParameterName = "Base Problem";
|
---|
23 | private const string AnalyzeXIterationParameterName = "Analyze X Iteration";
|
---|
24 | private const string BestMetaModelParameterName = "Best Meta Model";
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | #region parameter properties
|
---|
28 | //IRegressionProblem
|
---|
29 | public IFixedValueParameter<ItemList<T>> ProblemsToAnalyzeParameter =>
|
---|
30 | (IFixedValueParameter<ItemList<T>>)Parameters[ProblemsToAnalyzeParameterName];
|
---|
31 |
|
---|
32 | public IValueParameter<SymbolicRegressionSingleObjectiveProblem> BaseProblemParameter =>
|
---|
33 | (IValueParameter<SymbolicRegressionSingleObjectiveProblem>)Parameters[BaseProblemParameterName];
|
---|
34 |
|
---|
35 | public IFixedValueParameter<IntValue> AnalyzeXIterationParameter =>
|
---|
36 | (IFixedValueParameter<IntValue>)Parameters[AnalyzeXIterationParameterName];
|
---|
37 |
|
---|
38 | public IResultParameter<ISymbolicRegressionSolution> BestMetaModelParameter =>
|
---|
39 | (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestMetaModelParameterName];
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | public static int Iterations { get; set; } = 1;
|
---|
43 |
|
---|
44 | protected SymbolicRegressionMetaModelAnalyzer(SymbolicRegressionMetaModelAnalyzer<T> original, Cloner cloner) :
|
---|
45 | base(original, cloner) { }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected SymbolicRegressionMetaModelAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
49 |
|
---|
50 | public SymbolicRegressionMetaModelAnalyzer() {
|
---|
51 | Parameters.Add(new FixedValueParameter<ItemList<T>>(ProblemsToAnalyzeParameterName,
|
---|
52 | "List of datasets, which are used to find a meta model.", new ItemList<T>()));
|
---|
53 | Parameters.Add(new ValueParameter<T>(BaseProblemParameterName,
|
---|
54 | "The problem which uses the algorithm (just drag&drop it)."));
|
---|
55 | Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
|
---|
56 | "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."));
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
62 | private void AfterDeserialization() {
|
---|
63 | if (!Parameters.ContainsKey(ProblemsToAnalyzeParameterName))
|
---|
64 | Parameters.Add(new FixedValueParameter<ItemList<T>>(ProblemsToAnalyzeParameterName,
|
---|
65 | "List of datasets, which are used to find a meta model.", new ItemList<T>()));
|
---|
66 |
|
---|
67 | if (!Parameters.ContainsKey(BaseProblemParameterName))
|
---|
68 | Parameters.Add(new ValueParameter<T>(BaseProblemParameterName,
|
---|
69 | "The problem which uses the algorithm (just drag&drop it)."));
|
---|
70 |
|
---|
71 | if (!Parameters.ContainsKey(AnalyzeXIterationParameterName))
|
---|
72 | Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
|
---|
73 | "After every X iteration, the analyzer will perform its step.", new IntValue(1)));
|
---|
74 |
|
---|
75 | if (!Parameters.ContainsKey(BestMetaModelParameterName))
|
---|
76 | Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
|
---|
77 | "The best meta model found."));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override void InitializeState() {
|
---|
81 | Iterations = 1;
|
---|
82 | base.InitializeState();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override void ClearState() {
|
---|
86 | Iterations = 1;
|
---|
87 | base.ClearState();
|
---|
88 | }
|
---|
89 |
|
---|
90 | //protected abstract bool TryFittingSolution(ISymbolicExpressionTree tree, T problem, out SymbolicRegressionSolution solution);
|
---|
91 | protected abstract void PerformApply(T problem, string targetVariable);
|
---|
92 |
|
---|
93 | public override IOperation Apply() {
|
---|
94 | if (BaseProblemParameter.Value == null)
|
---|
95 | throw new ArgumentNullException("BaseProblemParameter",
|
---|
96 | "Base Problem Parameter is null! Please drag&drop the used problem into the parameter slot.");
|
---|
97 |
|
---|
98 | if (Iterations >= AnalyzeXIterationParameter.Value.Value) {
|
---|
99 | Iterations = 1;
|
---|
100 | string targetVariable = BaseProblemParameter.Value.ProblemData.TargetVariable;
|
---|
101 |
|
---|
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 | */
|
---|
114 | } else {
|
---|
115 | Iterations++;
|
---|
116 | }
|
---|
117 | return base.Apply();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|