1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 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 | [StorableClass]
|
---|
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 | .AsParallel()
|
---|
124 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
125 | .ToArray();
|
---|
126 | for (int i = 0; i < tree.Length; i++) {
|
---|
127 | if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
|
---|
128 | IsNonDominated(qualities[i], qualities, maximization)) {
|
---|
129 | if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
|
---|
130 | newNonDominatedQualities.Add(qualities[i]);
|
---|
131 | nonDominatedIndexes.Add(i);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 | #region update Pareto-optimal solution archive
|
---|
137 | if (nonDominatedIndexes.Count > 0) {
|
---|
138 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
139 | ItemList<S> nonDominatedSolutions = new ItemList<S>();
|
---|
140 | // add all new non-dominated solutions to the archive
|
---|
141 | foreach (var index in nonDominatedIndexes) {
|
---|
142 | S solution = CreateSolution(tree[index], qualities[index]);
|
---|
143 | nonDominatedSolutions.Add(solution);
|
---|
144 | nonDominatedQualities.Add(new DoubleArray(qualities[index]));
|
---|
145 | }
|
---|
146 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
147 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
148 | if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
149 | if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
|
---|
150 | nonDominatedSolutions.Add(ValidationBestSolutions[i]);
|
---|
151 | nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
|
---|
157 | results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
|
---|
158 | }
|
---|
159 | #endregion
|
---|
160 | return base.Apply();
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
164 |
|
---|
165 | private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
|
---|
166 | foreach (var refPoint in points) {
|
---|
167 | bool refPointDominatesPoint = true;
|
---|
168 | for (int i = 0; i < point.Length; i++) {
|
---|
169 | refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
|
---|
170 | }
|
---|
171 | if (refPointDominatesPoint) return false;
|
---|
172 | }
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
176 | if (maximization) return lhs > rhs;
|
---|
177 | else return lhs < rhs;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private class DoubleArrayComparer : IEqualityComparer<double[]> {
|
---|
181 | public bool Equals(double[] x, double[] y) {
|
---|
182 | if (y.Length != x.Length) throw new ArgumentException();
|
---|
183 | for (int i = 0; i < x.Length; i++) {
|
---|
184 | if (!x[i].IsAlmost(y[i])) return false;
|
---|
185 | }
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public int GetHashCode(double[] obj) {
|
---|
190 | int c = obj.Length;
|
---|
191 | for (int i = 0; i < obj.Length; i++)
|
---|
192 | c ^= obj[i].GetHashCode();
|
---|
193 | return c;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 | }
|
---|