1 | using HEAL.Attic;
|
---|
2 | using HeuristicLab.Collections;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis;
|
---|
11 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
12 | using System;
|
---|
13 | using System.Collections.Generic;
|
---|
14 | using System.Linq;
|
---|
15 | using System.Text;
|
---|
16 | using System.Threading.Tasks;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.Algorithms.OESRALPS.Analyzers
|
---|
19 | {
|
---|
20 | [Item("SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for single objective symbolic data analysis problems.")]
|
---|
21 | [StorableType("DD82C026-CF68-40D7-A898-77EA61272DE9")]
|
---|
22 | public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer<S, T, U>
|
---|
23 | : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator
|
---|
24 | where S : class, ISymbolicDataAnalysisSolution
|
---|
25 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
26 | where U : class, IDataAnalysisProblemData
|
---|
27 | {
|
---|
28 | private const string TrainingBestSolutionParameterName = "Best training solution";
|
---|
29 | private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
|
---|
30 | private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
|
---|
31 | private const string TrainingBestSolutionsHistoryParameterName = "Best training solutions history";
|
---|
32 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
33 | private const string IterationsParameterName = "Generations";
|
---|
34 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
35 | private const string StoreHistoryParameterName = "Store History";
|
---|
36 |
|
---|
37 | private const string TrainingPartitionParameterName = "TrainingPartition";
|
---|
38 |
|
---|
39 | private const string RandomParameterName = "GlobalRandom";
|
---|
40 | private const string ProblemDataParameterName = "ProblemData";
|
---|
41 | private const string EvaluatorParameterName = "Evaluator";
|
---|
42 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
43 |
|
---|
44 | private bool isTrainingEventInitialized = false;
|
---|
45 | private bool reevaluateTrainingResult = false;
|
---|
46 |
|
---|
47 | #region parameter properties
|
---|
48 | public ILookupParameter<S> TrainingBestSolutionParameter {
|
---|
49 | get { return (ILookupParameter<S>)Parameters[TrainingBestSolutionParameterName]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<DoubleValue> TrainingBestSolutionQualityParameter {
|
---|
52 | get { return (ILookupParameter<DoubleValue>)Parameters[TrainingBestSolutionQualityParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<IntValue> TrainingBestSolutionGenerationParameter {
|
---|
55 | get { return (ILookupParameter<IntValue>)Parameters[TrainingBestSolutionGenerationParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<ItemList<S>> TrainingBestSolutionsHistoryParameter {
|
---|
58 | get { return (ILookupParameter<ItemList<S>>)Parameters[TrainingBestSolutionsHistoryParameterName]; }
|
---|
59 | }
|
---|
60 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
61 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
62 | }
|
---|
63 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
64 | get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
67 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IFixedValueParameter<BoolValue> StoreHistoryParameter {
|
---|
71 | get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
72 | }
|
---|
73 | public IValueLookupParameter<IntRange> TrainingPartitionParameter {
|
---|
74 | get { return (IValueLookupParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
|
---|
75 | }
|
---|
76 | public ILookupParameter<IRandom> RandomParameter {
|
---|
77 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
78 | }
|
---|
79 | public ILookupParameter<U> ProblemDataParameter {
|
---|
80 | get { return (ILookupParameter<U>)Parameters[ProblemDataParameterName]; }
|
---|
81 | }
|
---|
82 | public ILookupParameter<T> EvaluatorParameter {
|
---|
83 | get { return (ILookupParameter<T>)Parameters[EvaluatorParameterName]; }
|
---|
84 | }
|
---|
85 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
|
---|
86 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | #region properties
|
---|
91 | public S TrainingBestSolution {
|
---|
92 | get { return TrainingBestSolutionParameter.ActualValue; }
|
---|
93 | set { TrainingBestSolutionParameter.ActualValue = value; }
|
---|
94 | }
|
---|
95 | public DoubleValue TrainingBestSolutionQuality {
|
---|
96 | get { return TrainingBestSolutionQualityParameter.ActualValue; }
|
---|
97 | set { TrainingBestSolutionQualityParameter.ActualValue = value; }
|
---|
98 | }
|
---|
99 | public bool UpdateAlways {
|
---|
100 | get { return UpdateAlwaysParameter.Value.Value; }
|
---|
101 | set { UpdateAlwaysParameter.Value.Value = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public bool StoreHistory {
|
---|
105 | get { return StoreHistoryParameter.Value.Value; }
|
---|
106 | set { StoreHistoryParameter.Value.Value = value; }
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | [StorableConstructor]
|
---|
111 | protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
112 | protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
113 | public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionSlidingWindowAnalyzer()
|
---|
114 | : base()
|
---|
115 | {
|
---|
116 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator."));
|
---|
117 | Parameters.Add(new LookupParameter<U>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
|
---|
118 | Parameters.Add(new LookupParameter<T>(EvaluatorParameterName, "The operator to use for fitness evaluation on the validation partition."));
|
---|
119 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter for symbolic data analysis expression trees."));
|
---|
120 |
|
---|
121 | Parameters.Add(new LookupParameter<S>(TrainingBestSolutionParameterName, "The best training symbolic data analyis solution."));
|
---|
122 | Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
|
---|
123 | Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
|
---|
124 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(true)));
|
---|
125 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
126 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
127 | Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, "Flag that determines whether all encountered best solutions should be stored as results.", new BoolValue(false)));
|
---|
128 | Parameters.Add(new LookupParameter<ItemList<S>>(TrainingBestSolutionsHistoryParameterName, "The history of the best training symbolic data analysis solutions."));
|
---|
129 |
|
---|
130 | Parameters.Add(new ValueLookupParameter<IntRange>(TrainingPartitionParameterName, "The current training sliding window position or range."));
|
---|
131 | UpdateAlwaysParameter.Hidden = true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public override IOperation Apply()
|
---|
135 | {
|
---|
136 | var results = ResultCollection;
|
---|
137 | #region create results
|
---|
138 | if (!results.ContainsKey(TrainingBestSolutionParameter.Name))
|
---|
139 | results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, typeof(S)));
|
---|
140 | if (!results.ContainsKey(TrainingBestSolutionQualityParameter.Name))
|
---|
141 | results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, typeof(DoubleValue)));
|
---|
142 | if (!results.ContainsKey(TrainingBestSolutionGenerationParameter.Name) && IterationsParameter.ActualValue != null)
|
---|
143 | results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, typeof(IntValue)));
|
---|
144 | if (StoreHistory && !results.ContainsKey(TrainingBestSolutionsHistoryParameter.Name))
|
---|
145 | {
|
---|
146 | results.Add(new Result(TrainingBestSolutionsHistoryParameter.Name, TrainingBestSolutionsHistoryParameter.Description, typeof(ItemList<S>)));
|
---|
147 | TrainingBestSolutionsHistoryParameter.ActualValue = new ItemList<S>();
|
---|
148 | results[TrainingBestSolutionsHistoryParameter.Name].Value = TrainingBestSolutionsHistoryParameter.ActualValue;
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 |
|
---|
152 | if (TrainingPartitionParameter.ActualValue != null && !isTrainingEventInitialized) {
|
---|
153 | TrainingPartitionParameter.ActualValue.ValueChanged += TrainingPartition_ValueChanged;
|
---|
154 | isTrainingEventInitialized = true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | #region find best tree
|
---|
158 | double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
159 | ISymbolicExpressionTree bestTree = null;
|
---|
160 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
161 | double[] quality = Quality.Select(x => x.Value).ToArray();
|
---|
162 | for (int i = 0; i < tree.Length; i++)
|
---|
163 | {
|
---|
164 | if (IsBetter(quality[i], bestQuality, Maximization.Value))
|
---|
165 | {
|
---|
166 | bestQuality = quality[i];
|
---|
167 | bestTree = tree[i];
|
---|
168 | }
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | if (TrainingPartitionParameter.ActualValue != null && (UpdateAlways || (reevaluateTrainingResult
|
---|
173 | && TrainingBestSolutionQuality != null
|
---|
174 | && TrainingPartitionParameter.ActualValue != null)))
|
---|
175 | {
|
---|
176 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
177 | var problemData = ProblemDataParameter.ActualValue;
|
---|
178 |
|
---|
179 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
180 | var currentValidationBestSolution = (S)results[TrainingBestSolutionParameterName].Value;
|
---|
181 | TrainingBestSolutionQuality.Value =
|
---|
182 | evaluator.Evaluate(
|
---|
183 | childContext,
|
---|
184 | currentValidationBestSolution.Model.SymbolicExpressionTree,
|
---|
185 | problemData,
|
---|
186 | Enumerable.Range(
|
---|
187 | TrainingPartitionParameter.ActualValue.Start,
|
---|
188 | TrainingPartitionParameter.ActualValue.End - TrainingPartitionParameter.ActualValue.Start
|
---|
189 | ));
|
---|
190 | results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
|
---|
191 | results[TrainingBestSolutionGenerationParameter.Name].Value = IterationsParameter.ActualValue;
|
---|
192 | reevaluateTrainingResult = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (bestTree != null && (UpdateAlways || TrainingBestSolutionQuality == null ||
|
---|
196 | IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value)))
|
---|
197 | {
|
---|
198 | TrainingBestSolution = CreateSolution(bestTree, bestQuality);
|
---|
199 | TrainingBestSolutionQuality = new DoubleValue(bestQuality);
|
---|
200 |
|
---|
201 | if (IterationsParameter.ActualValue != null)
|
---|
202 | TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
|
---|
203 |
|
---|
204 | results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
|
---|
205 | results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
|
---|
206 | if (TrainingBestSolutionGenerationParameter.ActualValue != null)
|
---|
207 | results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
|
---|
208 |
|
---|
209 | if (StoreHistory)
|
---|
210 | {
|
---|
211 | TrainingBestSolutionsHistoryParameter.ActualValue.Add(TrainingBestSolution);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
|
---|
216 | subScopesProcessor.Operator = EvaluatorParameter.ActualValue;
|
---|
217 | subScopesProcessor.Depth.Value = 1;
|
---|
218 | var operation = ExecutionContext.CreateChildOperation(subScopesProcessor);
|
---|
219 | var successor = base.Apply();
|
---|
220 |
|
---|
221 | return new OperationCollection() { operation, successor };
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void TrainingPartition_ValueChanged(object sender, EventArgs e)
|
---|
225 | {
|
---|
226 | reevaluateTrainingResult = true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
|
---|
230 |
|
---|
231 | private bool IsBetter(double lhs, double rhs, bool maximization)
|
---|
232 | {
|
---|
233 | if (maximization) return lhs > rhs;
|
---|
234 | else return lhs < rhs;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|