1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Runtime.CompilerServices;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HEAL.Attic;
|
---|
8 | using HeuristicLab.Analysis;
|
---|
9 | using HeuristicLab.Common;
|
---|
10 | using HeuristicLab.Core;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
13 | using HeuristicLab.Optimization;
|
---|
14 | using HeuristicLab.Parameters;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
17 | [StorableType("7CAE2D30-EDEC-44B6-B772-A2E63E64288F")]
|
---|
18 | public class SymbolicRegressionSplittingAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
|
---|
19 | private const string ProblemDataParameterName = "ProblemData";
|
---|
20 | private const string BetterWithSplittingParameterName = "Tighter bounds after splitting";
|
---|
21 | private const string NoSplitAcceptedParameterName = "Accepted without splitting";
|
---|
22 | private const string NoSplitDiscardedParameterName = "Dicarded without splitting";
|
---|
23 | private const string SplitAcceptedParameterName = "Accepted with splitting";
|
---|
24 | private const string SplitDiscardedParameterName = "Discarded with splitting";
|
---|
25 |
|
---|
26 |
|
---|
27 | #region parameter properties
|
---|
28 |
|
---|
29 | public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter =>
|
---|
30 | (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
|
---|
31 |
|
---|
32 | public IResultParameter<IntValue> BetterWithSplittingParameter =>
|
---|
33 | (IResultParameter<IntValue>) Parameters[BetterWithSplittingParameterName];
|
---|
34 |
|
---|
35 | public IResultParameter<IntValue> NoSplitAcceptedParameter =>
|
---|
36 | (IResultParameter<IntValue>) Parameters[NoSplitAcceptedParameterName];
|
---|
37 |
|
---|
38 | public IResultParameter<IntValue> NoSplitDiscardedParameter =>
|
---|
39 | (IResultParameter<IntValue>)Parameters[NoSplitDiscardedParameterName];
|
---|
40 |
|
---|
41 | public IResultParameter<IntValue> SplitAcceptedParameter =>
|
---|
42 | (IResultParameter<IntValue>)Parameters[SplitAcceptedParameterName];
|
---|
43 |
|
---|
44 | public IResultParameter<IntValue> SplitDiscardedParameter =>
|
---|
45 | (IResultParameter<IntValue>)Parameters[SplitDiscardedParameterName];
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | #endregion
|
---|
50 | public override bool EnabledByDefault => false;
|
---|
51 | public static int Iterations { get; set; } = 0;
|
---|
52 |
|
---|
53 | public static int BetterWithSplitting { get; set; } = 0;
|
---|
54 |
|
---|
55 | public static int AccpetedWithoutSplitting { get; set; } = 0;
|
---|
56 | public static int DiscardedWithoutSplitting { get; set; } = 0;
|
---|
57 | public static int AccptedWithSplitting { get; set; } = 0;
|
---|
58 | public static int DiscardedWithSplitting { get; set; } = 0;
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | protected SymbolicRegressionSplittingAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
62 |
|
---|
63 | private SymbolicRegressionSplittingAnalyzer(SymbolicRegressionSplittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
64 |
|
---|
65 | public SymbolicRegressionSplittingAnalyzer() {
|
---|
66 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName,
|
---|
67 | "The problem data of the symbolic data analysis problem."));
|
---|
68 |
|
---|
69 | if (!Parameters.ContainsKey(BetterWithSplittingParameterName)) {
|
---|
70 | Parameters.Add(new ResultParameter<IntValue>(BetterWithSplittingParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
71 | }
|
---|
72 | BetterWithSplittingParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
73 |
|
---|
74 | if (!Parameters.ContainsKey(NoSplitAcceptedParameterName)) {
|
---|
75 | Parameters.Add(new ResultParameter<IntValue>(NoSplitAcceptedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
76 | }
|
---|
77 | NoSplitAcceptedParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
78 |
|
---|
79 | if (!Parameters.ContainsKey(NoSplitDiscardedParameterName)) {
|
---|
80 | Parameters.Add(new ResultParameter<IntValue>(NoSplitDiscardedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
81 | }
|
---|
82 | BetterWithSplittingParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
83 |
|
---|
84 | if (!Parameters.ContainsKey(SplitAcceptedParameterName)) {
|
---|
85 | Parameters.Add(new ResultParameter<IntValue>(SplitAcceptedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
86 | }
|
---|
87 | BetterWithSplittingParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
88 |
|
---|
89 | if (!Parameters.ContainsKey(SplitDiscardedParameterName)) {
|
---|
90 | Parameters.Add(new ResultParameter<IntValue>(SplitDiscardedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
91 | }
|
---|
92 | BetterWithSplittingParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 | [StorableHook(HookType.AfterDeserialization)]
|
---|
98 | private void AfterDeserialization() {
|
---|
99 | if (!Parameters.ContainsKey(BetterWithSplittingParameterName)) {
|
---|
100 | Parameters.Add(new ResultParameter<IntValue>(BetterWithSplittingParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
101 | }
|
---|
102 |
|
---|
103 | BetterWithSplittingParameter.DefaultValue = new IntValue(BetterWithSplitting);
|
---|
104 |
|
---|
105 | if (!Parameters.ContainsKey(NoSplitAcceptedParameterName))
|
---|
106 | {
|
---|
107 | Parameters.Add(new ResultParameter<IntValue>(NoSplitAcceptedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
108 | }
|
---|
109 | NoSplitAcceptedParameter.DefaultValue = new IntValue(AccpetedWithoutSplitting);
|
---|
110 |
|
---|
111 | if (!Parameters.ContainsKey(NoSplitDiscardedParameterName))
|
---|
112 | {
|
---|
113 | Parameters.Add(new ResultParameter<IntValue>(NoSplitDiscardedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
114 | }
|
---|
115 | NoSplitDiscardedParameter.DefaultValue = new IntValue(DiscardedWithoutSplitting);
|
---|
116 |
|
---|
117 | if (!Parameters.ContainsKey(SplitAcceptedParameterName))
|
---|
118 | {
|
---|
119 | Parameters.Add(new ResultParameter<IntValue>(SplitAcceptedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
120 | }
|
---|
121 | SplitAcceptedParameter.DefaultValue = new IntValue(AccptedWithSplitting);
|
---|
122 |
|
---|
123 | if (!Parameters.ContainsKey(SplitDiscardedParameterName))
|
---|
124 | {
|
---|
125 | Parameters.Add(new ResultParameter<IntValue>(SplitDiscardedParameterName, "Shows the number constraints which gets accepted after splitting"));
|
---|
126 | }
|
---|
127 | SplitDiscardedParameter.DefaultValue = new IntValue(DiscardedWithSplitting);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
131 | return new SymbolicRegressionSplittingAnalyzer(this, cloner);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public override void InitializeState() {
|
---|
135 | Iterations = 0;
|
---|
136 | BetterWithSplitting = 0;
|
---|
137 | AccptedWithSplitting = 0;
|
---|
138 | AccpetedWithoutSplitting = 0;
|
---|
139 | DiscardedWithSplitting = 0;
|
---|
140 | DiscardedWithoutSplitting = 0;
|
---|
141 | base.InitializeState();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override void ClearState() {
|
---|
145 | Iterations = 0;
|
---|
146 | BetterWithSplitting = 0;
|
---|
147 | AccptedWithSplitting = 0;
|
---|
148 | AccpetedWithoutSplitting = 0;
|
---|
149 | DiscardedWithSplitting = 0;
|
---|
150 | DiscardedWithoutSplitting = 0;
|
---|
151 | base.ClearState();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public override IOperation Apply() {
|
---|
155 | var problemData = RegressionProblemDataParameter.ActualValue;
|
---|
156 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
157 | var constraints = problemData.IntervalConstraints.EnabledConstraints;
|
---|
158 | var intervalCollection = problemData.VariableRanges;
|
---|
159 | var estimator = new IABoundsEstimator();
|
---|
160 | var splittingEstimator = new IABoundsEstimator {UseIntervalSplitting = true, SplittingIterations = 12};
|
---|
161 |
|
---|
162 | foreach (var solution in trees) {
|
---|
163 | var noSplitting = IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution);
|
---|
164 | var splitting = IntervalUtil.IntervalConstraintsViolation(constraints, splittingEstimator, intervalCollection, solution);
|
---|
165 |
|
---|
166 | if (noSplitting.Any(x => x != 0)) DiscardedWithoutSplitting++;
|
---|
167 | if (noSplitting.All(x => x == 0)) AccpetedWithoutSplitting++;
|
---|
168 |
|
---|
169 | if (splitting.Any(x => x != 0))
|
---|
170 | DiscardedWithSplitting++;
|
---|
171 | else {
|
---|
172 | AccptedWithSplitting++;
|
---|
173 | }
|
---|
174 |
|
---|
175 | for (var i = 0; i < noSplitting.Count(); ++i) {
|
---|
176 | if (splitting.ToArray()[i] < noSplitting.ToArray()[i]) {
|
---|
177 | BetterWithSplitting++;
|
---|
178 | }
|
---|
179 | /*if (splitting.ToArray()[i] < noSplitting.ToArray()[i]) {
|
---|
180 | BetterWithSplitting++;
|
---|
181 | } else if (splitting.ToArray()[i] > noSplitting.ToArray()[i] && !splitting.ToArray()[i].IsAlmost(noSplitting.ToArray()[i])) {
|
---|
182 | WorseWithSplitting++;
|
---|
183 | noSplitting = IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution);
|
---|
184 | splitting = IntervalUtil.IntervalConstraintsViolation(constraints, splittingEstimator, intervalCollection, solution);
|
---|
185 | }*/
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | BetterWithSplittingParameter.ActualValue.Value = BetterWithSplitting;
|
---|
190 | NoSplitAcceptedParameter.ActualValue.Value = AccpetedWithoutSplitting;
|
---|
191 | NoSplitDiscardedParameter.ActualValue.Value = DiscardedWithoutSplitting;
|
---|
192 | SplitAcceptedParameter.ActualValue.Value = AccptedWithSplitting;
|
---|
193 | SplitDiscardedParameter.ActualValue.Value = DiscardedWithSplitting;
|
---|
194 | return base.Apply();
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|