[17771] | 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 | #endregion
|
---|
| 25 |
|
---|
| 26 | #region parameter properties
|
---|
| 27 | //IRegressionProblem
|
---|
| 28 | public IFixedValueParameter<ItemList<T>> ProblemsToAnalyzeParameter =>
|
---|
| 29 | (IFixedValueParameter<ItemList<T>>)Parameters[ProblemsToAnalyzeParameterName];
|
---|
| 30 |
|
---|
[17776] | 31 | public IValueParameter<T> BaseProblemParameter =>
|
---|
| 32 | (IValueParameter<T>)Parameters[BaseProblemParameterName];
|
---|
[17771] | 33 |
|
---|
| 34 | public IFixedValueParameter<IntValue> AnalyzeXIterationParameter =>
|
---|
| 35 | (IFixedValueParameter<IntValue>)Parameters[AnalyzeXIterationParameterName];
|
---|
| 36 | #endregion
|
---|
| 37 |
|
---|
| 38 | public static int Iterations { get; set; } = 1;
|
---|
| 39 |
|
---|
[17776] | 40 | #region constructors and cloning
|
---|
[17771] | 41 | protected SymbolicRegressionMetaModelAnalyzer(SymbolicRegressionMetaModelAnalyzer<T> original, Cloner cloner) :
|
---|
| 42 | base(original, cloner) { }
|
---|
| 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
| 45 | protected SymbolicRegressionMetaModelAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
| 46 |
|
---|
| 47 | public SymbolicRegressionMetaModelAnalyzer() {
|
---|
| 48 | Parameters.Add(new FixedValueParameter<ItemList<T>>(ProblemsToAnalyzeParameterName,
|
---|
| 49 | "List of datasets, which are used to find a meta model.", new ItemList<T>()));
|
---|
| 50 | Parameters.Add(new ValueParameter<T>(BaseProblemParameterName,
|
---|
| 51 | "The problem which uses the algorithm (just drag&drop it)."));
|
---|
| 52 | Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
|
---|
| 53 | "After every X iteration, the analyzer will perform its step.", new IntValue(1)));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 57 | private void AfterDeserialization() {
|
---|
| 58 | if (!Parameters.ContainsKey(ProblemsToAnalyzeParameterName))
|
---|
| 59 | Parameters.Add(new FixedValueParameter<ItemList<T>>(ProblemsToAnalyzeParameterName,
|
---|
| 60 | "List of datasets, which are used to find a meta model.", new ItemList<T>()));
|
---|
| 61 |
|
---|
| 62 | if (!Parameters.ContainsKey(BaseProblemParameterName))
|
---|
| 63 | Parameters.Add(new ValueParameter<T>(BaseProblemParameterName,
|
---|
| 64 | "The problem which uses the algorithm (just drag&drop it)."));
|
---|
| 65 |
|
---|
| 66 | if (!Parameters.ContainsKey(AnalyzeXIterationParameterName))
|
---|
| 67 | Parameters.Add(new FixedValueParameter<IntValue>(AnalyzeXIterationParameterName,
|
---|
| 68 | "After every X iteration, the analyzer will perform its step.", new IntValue(1)));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[17776] | 71 | #endregion
|
---|
| 72 |
|
---|
[17771] | 73 | public override void InitializeState() {
|
---|
| 74 | Iterations = 1;
|
---|
| 75 | base.InitializeState();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public override void ClearState() {
|
---|
| 79 | Iterations = 1;
|
---|
| 80 | base.ClearState();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //protected abstract bool TryFittingSolution(ISymbolicExpressionTree tree, T problem, out SymbolicRegressionSolution solution);
|
---|
[17776] | 84 | protected abstract void PerformApply(T baseProblem, IEnumerable<T> problems, string targetVariable);
|
---|
[17771] | 85 |
|
---|
| 86 | public override IOperation Apply() {
|
---|
| 87 | if (BaseProblemParameter.Value == null)
|
---|
| 88 | throw new ArgumentNullException("BaseProblemParameter",
|
---|
| 89 | "Base Problem Parameter is null! Please drag&drop the used problem into the parameter slot.");
|
---|
| 90 |
|
---|
| 91 | if (Iterations >= AnalyzeXIterationParameter.Value.Value) {
|
---|
| 92 | Iterations = 1;
|
---|
[17776] | 93 | var baseProblem = BaseProblemParameter.Value;
|
---|
| 94 | var problems = ProblemsToAnalyzeParameter.Value;
|
---|
| 95 | var targetVariable = BaseProblemParameter.Value.ProblemData.TargetVariable;
|
---|
[17771] | 96 |
|
---|
[17776] | 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);
|
---|
[17771] | 107 | } else {
|
---|
| 108 | Iterations++;
|
---|
| 109 | }
|
---|
| 110 | return base.Apply();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|