1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
34 | /// <summary>
|
---|
35 | /// An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.
|
---|
36 | /// </summary>
|
---|
37 | [Item("SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]
|
---|
38 | [StorableClass]
|
---|
39 | public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>, IIterationBasedOperator
|
---|
40 | where S : class, ISymbolicDataAnalysisSolution
|
---|
41 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
42 | where U : class, IDataAnalysisProblemData {
|
---|
43 | private const string ValidationBestSolutionParameterName = "Best validation solution";
|
---|
44 | private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
|
---|
45 | private const string ValidationBestSolutionGenerationParameterName = "Best validation solution generation";
|
---|
46 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
47 | private const string IterationsParameterName = "Iterations";
|
---|
48 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
49 |
|
---|
50 | #region parameter properties
|
---|
51 | public ILookupParameter<S> ValidationBestSolutionParameter {
|
---|
52 | get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
|
---|
55 | get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<IntValue> ValidationBestSolutionGenerationParameter {
|
---|
58 | get { return (ILookupParameter<IntValue>)Parameters[ValidationBestSolutionGenerationParameterName]; }
|
---|
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 | #endregion
|
---|
70 | #region properties
|
---|
71 | public S ValidationBestSolution {
|
---|
72 | get { return ValidationBestSolutionParameter.ActualValue; }
|
---|
73 | set { ValidationBestSolutionParameter.ActualValue = value; }
|
---|
74 | }
|
---|
75 | public DoubleValue ValidationBestSolutionQuality {
|
---|
76 | get { return ValidationBestSolutionQualityParameter.ActualValue; }
|
---|
77 | set { ValidationBestSolutionQualityParameter.ActualValue = value; }
|
---|
78 | }
|
---|
79 | public BoolValue UpdateAlways {
|
---|
80 | get { return UpdateAlwaysParameter.Value; }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
86 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
87 | public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
|
---|
88 | : base() {
|
---|
89 | Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
|
---|
90 | Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
|
---|
91 | Parameters.Add(new LookupParameter<IntValue>(ValidationBestSolutionGenerationParameterName, "The generation in which the best validation solution was found."));
|
---|
92 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
93 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
94 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
95 | UpdateAlwaysParameter.Hidden = true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | [StorableHook(HookType.AfterDeserialization)]
|
---|
99 | private void AfterDeserialization() {
|
---|
100 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
101 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
102 | UpdateAlwaysParameter.Hidden = true;
|
---|
103 | }
|
---|
104 | if (!Parameters.ContainsKey(ValidationBestSolutionGenerationParameterName))
|
---|
105 | Parameters.Add(new LookupParameter<IntValue>(ValidationBestSolutionGenerationParameterName, "The generation in which the best validation solution was found."));
|
---|
106 | if (!Parameters.ContainsKey(IterationsParameterName))
|
---|
107 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
108 | if (!Parameters.ContainsKey(MaximumIterationsParameterName))
|
---|
109 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
110 | }
|
---|
111 |
|
---|
112 | public override IOperation Apply() {
|
---|
113 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
114 | if (!rows.Any()) return base.Apply();
|
---|
115 |
|
---|
116 | #region find best tree
|
---|
117 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
118 | var problemData = ProblemDataParameter.ActualValue;
|
---|
119 | double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
120 | ISymbolicExpressionTree bestTree = null;
|
---|
121 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
122 |
|
---|
123 | // sort is ascending and we take the first n% => order so that best solutions are smallest
|
---|
124 | // sort order is determined by maximization parameter
|
---|
125 | double[] trainingQuality;
|
---|
126 | if (Maximization.Value) {
|
---|
127 | // largest values must be sorted first
|
---|
128 | trainingQuality = Quality.Select(x => -x.Value).ToArray();
|
---|
129 | } else {
|
---|
130 | // smallest values must be sorted first
|
---|
131 | trainingQuality = Quality.Select(x => x.Value).ToArray();
|
---|
132 | }
|
---|
133 |
|
---|
134 | // sort trees by training qualities
|
---|
135 | Array.Sort(trainingQuality, tree);
|
---|
136 |
|
---|
137 | // number of best training solutions to validate (at least 1)
|
---|
138 | int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
|
---|
139 |
|
---|
140 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
141 | // evaluate best n training trees on validiation set
|
---|
142 | var quality = tree
|
---|
143 | .Take(topN)
|
---|
144 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
145 | .ToArray();
|
---|
146 |
|
---|
147 | for (int i = 0; i < quality.Length; i++) {
|
---|
148 | if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {
|
---|
149 | bestValidationQuality = quality[i];
|
---|
150 | bestTree = tree[i];
|
---|
151 | }
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | var results = ResultCollection;
|
---|
156 | if (UpdateAlways.Value || ValidationBestSolutionQuality == null ||
|
---|
157 | IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
|
---|
158 | ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality);
|
---|
159 | ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality);
|
---|
160 | if (IterationsParameter.ActualValue != null)
|
---|
161 | ValidationBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
|
---|
162 |
|
---|
163 | if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
|
---|
164 | results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
|
---|
165 | results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
|
---|
166 | if (ValidationBestSolutionGenerationParameter.ActualValue != null)
|
---|
167 | results.Add(new Result(ValidationBestSolutionGenerationParameter.Name, ValidationBestSolutionGenerationParameter.Description, ValidationBestSolutionGenerationParameter.ActualValue));
|
---|
168 | } else {
|
---|
169 | results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
|
---|
170 | results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
|
---|
171 | if (ValidationBestSolutionGenerationParameter.ActualValue != null)
|
---|
172 | results[ValidationBestSolutionGenerationParameter.Name].Value = ValidationBestSolutionGenerationParameter.ActualValue;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return base.Apply();
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
|
---|
179 |
|
---|
180 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
181 | if (maximization) return lhs > rhs;
|
---|
182 | else return lhs < rhs;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|