1 |
|
---|
2 |
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HEAL.Attic;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.Parameters;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
14 | [Item("TestAnalyzer",
|
---|
15 | "An operator that analyzes the training best symbolic regression solution for multi objective symbolic regression problems.")]
|
---|
16 | [StorableType("D802BF87-6137-47D6-BC09-A905BBB3534D")]
|
---|
17 | public class SymbolicRegressionMultiObjectiveConstraintAnalyzer :
|
---|
18 | SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>,
|
---|
19 | ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
|
---|
20 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
21 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
22 | private const string ProblemDataParameterName = "ProblemData";
|
---|
23 |
|
---|
24 | private const string SolutionObjectivesName = "Solution Objectives";
|
---|
25 | private const string SolutionConstraintsName = "Solutions & Constraints";
|
---|
26 |
|
---|
27 | #region Constructors
|
---|
28 |
|
---|
29 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>
|
---|
30 | SymbolicDataAnalysisTreeInterpreterParameter =>
|
---|
31 | (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters
|
---|
32 | [SymbolicDataAnalysisTreeInterpreterParameterName];
|
---|
33 |
|
---|
34 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter =>
|
---|
35 | (IValueLookupParameter<DoubleLimit>) Parameters[EstimationLimitsParameterName];
|
---|
36 |
|
---|
37 | public IValueLookupParameter<IRegressionProblemData> ProblemDataParameter =>
|
---|
38 | (IValueLookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName];
|
---|
39 |
|
---|
40 | public IResultParameter<DoubleMatrix> SolutionObjectivesParameter =>
|
---|
41 | (IResultParameter<DoubleMatrix>) Parameters[SolutionObjectivesName];
|
---|
42 |
|
---|
43 | public IResultParameter<ItemList<ISymbolicRegressionSolution>> SolutionConstraintsParameter =>
|
---|
44 | (IResultParameter<ItemList<ISymbolicRegressionSolution>>) Parameters[SolutionConstraintsName];
|
---|
45 |
|
---|
46 | public SymbolicRegressionMultiObjectiveConstraintAnalyzer() {
|
---|
47 | Parameters.Add(new
|
---|
48 | LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName,
|
---|
49 | "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
|
---|
50 | Parameters.Add(new ValueLookupParameter<IRegressionProblemData>(ProblemDataParameterName,
|
---|
51 | "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
52 | Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName,
|
---|
53 | "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
|
---|
54 | Parameters.Add(new ResultParameter<DoubleMatrix>(SolutionObjectivesName,
|
---|
55 | "Shows the constraints violation for each solution"));
|
---|
56 | SolutionObjectivesParameter.DefaultValue = new DoubleMatrix();
|
---|
57 | Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(SolutionConstraintsName,
|
---|
58 | "Holds the symbolic regression solutions"));
|
---|
59 | SolutionConstraintsParameter.DefaultValue = new ItemList<ISymbolicRegressionSolution>();
|
---|
60 | }
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected SymbolicRegressionMultiObjectiveConstraintAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
64 |
|
---|
65 | protected SymbolicRegressionMultiObjectiveConstraintAnalyzer(
|
---|
66 | SymbolicRegressionMultiObjectiveConstraintAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
67 |
|
---|
68 | #endregion
|
---|
69 |
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new SymbolicRegressionMultiObjectiveConstraintAnalyzer(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IOperation Apply() {
|
---|
75 | var results = ResultCollection;
|
---|
76 | var dataset = ProblemDataParameter.ActualValue.Dataset;
|
---|
77 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
78 | var qualities = QualitiesParameter.ActualValue;
|
---|
79 | var constraints = ProblemDataParameter.ActualValue.IntervalConstraints.Constraints;
|
---|
80 |
|
---|
81 | if (!(qualities.First().Length < constraints.Count())) {
|
---|
82 | //DoubleMatrix
|
---|
83 | var dm = new DoubleMatrix(trees.Length, qualities[0].Length);
|
---|
84 | for (var i = 0; i < qualities.Length; ++i) {
|
---|
85 | for (var j = 0; j < qualities[i].Length; ++j) dm[i, j] = qualities[i][j];
|
---|
86 | }
|
---|
87 |
|
---|
88 | dm.RowNames = GetRowNames(dm);
|
---|
89 | dm.ColumnNames = GetColumnNames(constraints);
|
---|
90 | SolutionObjectivesParameter.ActualValue = dm;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //SolutionsList
|
---|
94 | var treeQualities = new[] {new {Tree = default(ISymbolicExpressionTree), Qualities = default(double[])}}.ToList();
|
---|
95 | treeQualities.Clear();
|
---|
96 |
|
---|
97 | var q = qualities.Select(x => x.ToArray()).ToList();
|
---|
98 | for (var i = 0; i < trees.Length; ++i) treeQualities.Add(new {Tree = trees[i], Qualities = q[i]});
|
---|
99 |
|
---|
100 |
|
---|
101 | var count = 0;
|
---|
102 | var resSol = new ItemList<ISymbolicRegressionSolution>();
|
---|
103 | foreach (var tq in treeQualities) {
|
---|
104 | var model = new SymbolicRegressionModel(ProblemDataParameter.ActualValue.TargetVariable,
|
---|
105 | (ISymbolicExpressionTree) tq.Tree.Clone(),
|
---|
106 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
107 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
108 | EstimationLimitsParameter.ActualValue.Upper);
|
---|
109 | var sol =
|
---|
110 | new SymbolicRegressionSolution(model, (IRegressionProblemData) ProblemDataParameter.ActualValue.Clone());
|
---|
111 | //var sol = CreateSolution(tq.Tree, tq.Qualities);
|
---|
112 | sol.Name = string.Join(", ", "Solution" + count, tq.Qualities[0], tq.Qualities.Skip(1).Count(x => x > 0));
|
---|
113 | resSol.Add(sol);
|
---|
114 | count++;
|
---|
115 | }
|
---|
116 |
|
---|
117 | SolutionConstraintsParameter.ActualValue = resSol;
|
---|
118 | return base.Apply();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree,
|
---|
122 | double[] bestQuality) {
|
---|
123 | var model = new SymbolicRegressionModel(ProblemDataParameter.ActualValue.TargetVariable,
|
---|
124 | (ISymbolicExpressionTree) bestTree.Clone(),
|
---|
125 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
126 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
127 | EstimationLimitsParameter.ActualValue.Upper);
|
---|
128 | if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
|
---|
129 | return new SymbolicRegressionSolution(model, (IRegressionProblemData) ProblemDataParameter.ActualValue.Clone());
|
---|
130 | }
|
---|
131 |
|
---|
132 | private static IEnumerable<string> GetColumnNames(IEnumerable<IntervalConstraint> constraints) {
|
---|
133 | yield return "NMSE";
|
---|
134 | foreach (var constraint in constraints) {
|
---|
135 | if(constraint.Enabled)
|
---|
136 | yield return constraint.Expression;
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static IEnumerable<string> GetRowNames(DoubleMatrix dm) {
|
---|
142 | for (var i = 0; i < dm.Rows; ++i) yield return "Solution " + i;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | } |
---|