1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
34 | /// <summary>
|
---|
35 | /// An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.
|
---|
36 | /// </summary>
|
---|
37 | [Item("SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
|
---|
38 | [StorableType("796b0d95-840a-43d3-a2c5-5d03b339d4d2")]
|
---|
39 | public abstract class SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisMultiObjectiveValidationAnalyzer<T, U>,
|
---|
40 | ISymbolicDataAnalysisMultiObjectiveAnalyzer
|
---|
41 | where S : class, ISymbolicDataAnalysisSolution
|
---|
42 | where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<U>
|
---|
43 | where U : class, IDataAnalysisProblemData {
|
---|
44 | private const string ValidationBestSolutionsParameterName = "Best validation solutions";
|
---|
45 | private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
|
---|
46 | private const string UpdateAlwaysParameterName = "Always update best solutions";
|
---|
47 |
|
---|
48 | #region parameter properties
|
---|
49 | public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
|
---|
50 | get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
|
---|
53 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
|
---|
54 | }
|
---|
55 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
56 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 | #region properties
|
---|
60 | public ItemList<S> ValidationBestSolutions {
|
---|
61 | get { return ValidationBestSolutionsParameter.ActualValue; }
|
---|
62 | set { ValidationBestSolutionsParameter.ActualValue = value; }
|
---|
63 | }
|
---|
64 | public ItemList<DoubleArray> ValidationBestSolutionQualities {
|
---|
65 | get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
|
---|
66 | set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
67 | }
|
---|
68 | public BoolValue UpdateAlways {
|
---|
69 | get { return UpdateAlwaysParameter.Value; }
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
75 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
76 | public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
|
---|
77 | : base() {
|
---|
78 | Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
79 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
|
---|
80 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
81 | UpdateAlwaysParameter.Hidden = true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
85 | private void AfterDeserialization() {
|
---|
86 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
87 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
88 | UpdateAlwaysParameter.Hidden = true;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override IOperation Apply() {
|
---|
93 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
94 | if (!rows.Any()) return base.Apply();
|
---|
95 |
|
---|
96 | var results = ResultCollection;
|
---|
97 | // create empty parameter and result values
|
---|
98 | if (ValidationBestSolutions == null) {
|
---|
99 | ValidationBestSolutions = new ItemList<S>();
|
---|
100 | ValidationBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
101 | results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
|
---|
102 | results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
|
---|
103 | }
|
---|
104 |
|
---|
105 | //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
|
---|
106 | IList<double[]> trainingBestQualities;
|
---|
107 | if (UpdateAlways.Value) {
|
---|
108 | trainingBestQualities = new List<double[]>();
|
---|
109 | } else {
|
---|
110 | trainingBestQualities = ValidationBestSolutionQualities.Select(x => x.ToArray()).ToList();
|
---|
111 | }
|
---|
112 |
|
---|
113 | #region find best trees
|
---|
114 | IList<int> nonDominatedIndexes = new List<int>();
|
---|
115 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
116 | bool[] maximization = Maximization.ToArray();
|
---|
117 | List<double[]> newNonDominatedQualities = new List<double[]>();
|
---|
118 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
119 | var problemData = ProblemDataParameter.ActualValue;
|
---|
120 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
121 |
|
---|
122 | var qualities = tree
|
---|
123 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
124 | .ToArray();
|
---|
125 | for (int i = 0; i < tree.Length; i++) {
|
---|
126 | if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
|
---|
127 | IsNonDominated(qualities[i], qualities, maximization)) {
|
---|
128 | if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
|
---|
129 | newNonDominatedQualities.Add(qualities[i]);
|
---|
130 | nonDominatedIndexes.Add(i);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 | #region update Pareto-optimal solution archive
|
---|
136 | if (nonDominatedIndexes.Count > 0) {
|
---|
137 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
138 | ItemList<S> nonDominatedSolutions = new ItemList<S>();
|
---|
139 | // add all new non-dominated solutions to the archive
|
---|
140 | foreach (var index in nonDominatedIndexes) {
|
---|
141 | S solution = CreateSolution(tree[index], qualities[index]);
|
---|
142 | nonDominatedSolutions.Add(solution);
|
---|
143 | nonDominatedQualities.Add(new DoubleArray(qualities[index]));
|
---|
144 | }
|
---|
145 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
146 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
147 | if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
148 | if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
|
---|
149 | nonDominatedSolutions.Add(ValidationBestSolutions[i]);
|
---|
150 | nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
|
---|
156 | results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
|
---|
157 | }
|
---|
158 | #endregion
|
---|
159 | return base.Apply();
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
163 |
|
---|
164 | private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
|
---|
165 | foreach (var refPoint in points) {
|
---|
166 | bool refPointDominatesPoint = true;
|
---|
167 | for (int i = 0; i < point.Length; i++) {
|
---|
168 | refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
|
---|
169 | }
|
---|
170 | if (refPointDominatesPoint) return false;
|
---|
171 | }
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
175 | if (maximization) return lhs > rhs;
|
---|
176 | else return lhs < rhs;
|
---|
177 | }
|
---|
178 |
|
---|
179 | private class DoubleArrayComparer : IEqualityComparer<double[]> {
|
---|
180 | public bool Equals(double[] x, double[] y) {
|
---|
181 | if (y.Length != x.Length) throw new ArgumentException();
|
---|
182 | for (int i = 0; i < x.Length; i++) {
|
---|
183 | if (!x[i].IsAlmost(y[i])) return false;
|
---|
184 | }
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public int GetHashCode(double[] obj) {
|
---|
189 | int c = obj.Length;
|
---|
190 | for (int i = 0; i < obj.Length; i++)
|
---|
191 | c ^= obj[i].GetHashCode();
|
---|
192 | return c;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | }
|
---|
197 | }
|
---|