1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.
|
---|
35 | /// </summary>
|
---|
36 | [Item("SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
|
---|
37 | [StorableClass]
|
---|
38 | public abstract class SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisMultiObjectiveAnalyzer
|
---|
39 | where T : class, ISymbolicDataAnalysisSolution {
|
---|
40 | private const string TrainingBestSolutionsParameterName = "Best training solutions";
|
---|
41 | private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
|
---|
42 | private const string UpdateAlwaysParameterName = "Always update best solutions";
|
---|
43 |
|
---|
44 | #region parameter properties
|
---|
45 | public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
|
---|
46 | get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
|
---|
49 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
|
---|
50 | }
|
---|
51 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
52 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 | #region properties
|
---|
56 | public ItemList<T> TrainingBestSolutions {
|
---|
57 | get { return TrainingBestSolutionsParameter.ActualValue; }
|
---|
58 | set { TrainingBestSolutionsParameter.ActualValue = value; }
|
---|
59 | }
|
---|
60 | public ItemList<DoubleArray> TrainingBestSolutionQualities {
|
---|
61 | get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
|
---|
62 | set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
63 | }
|
---|
64 | public BoolValue UpdateAlways {
|
---|
65 | get { return UpdateAlwaysParameter.Value; }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
71 | protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
72 | public SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer()
|
---|
73 | : base() {
|
---|
74 | Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
75 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
|
---|
76 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
77 | UpdateAlwaysParameter.Hidden = true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | [StorableHook(HookType.AfterDeserialization)]
|
---|
81 | private void AfterDeserialization() {
|
---|
82 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
83 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
84 | UpdateAlwaysParameter.Hidden = true;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override IOperation Apply() {
|
---|
89 | var results = ResultCollection;
|
---|
90 | // create empty parameter and result values
|
---|
91 | if (TrainingBestSolutions == null) {
|
---|
92 | TrainingBestSolutions = new ItemList<T>();
|
---|
93 | TrainingBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
94 | results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
|
---|
95 | results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
|
---|
96 | }
|
---|
97 |
|
---|
98 | //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
|
---|
99 | List<double[]> trainingBestQualities;
|
---|
100 | if (UpdateAlways.Value) {
|
---|
101 | trainingBestQualities = new List<double[]>();
|
---|
102 | } else {
|
---|
103 | trainingBestQualities = TrainingBestSolutionQualities.Select(x => x.ToArray()).ToList();
|
---|
104 | }
|
---|
105 |
|
---|
106 | ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray();
|
---|
107 | List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
|
---|
108 | bool[] maximization = Maximization.ToArray();
|
---|
109 |
|
---|
110 | var nonDominatedInvididuals = new[] { new { Tree = default(ISymbolicExpressionTree), Qualities = default(double[]) } }.ToList();
|
---|
111 | nonDominatedInvididuals.Clear();
|
---|
112 |
|
---|
113 | // build list of new non-dominated solutions
|
---|
114 | for (int i = 0; i < trees.Length; i++) {
|
---|
115 | if (IsNonDominated(qualities[i], nonDominatedInvididuals.Select(ind => ind.Qualities), maximization) &&
|
---|
116 | IsNonDominated(qualities[i], trainingBestQualities, maximization)) {
|
---|
117 | for (int j = nonDominatedInvididuals.Count - 1; j >= 0; j--) {
|
---|
118 | if (IsBetterOrEqual(qualities[i], nonDominatedInvididuals[j].Qualities, maximization)) {
|
---|
119 | nonDominatedInvididuals.RemoveAt(j);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | nonDominatedInvididuals.Add(new { Tree = trees[i], Qualities = qualities[i] });
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | var nonDominatedSolutions = nonDominatedInvididuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList();
|
---|
127 |
|
---|
128 | #region update Pareto-optimal solution archive
|
---|
129 | if (nonDominatedSolutions.Count > 0) {
|
---|
130 | //add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
131 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
132 | if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization)) {
|
---|
133 | nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() });
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | var sortedNonDominatedSolutions = nonDominatedSolutions.OrderByDescending(x => x.Qualities[0]);
|
---|
138 | results[TrainingBestSolutionsParameter.Name].Value = new ItemList<T>(sortedNonDominatedSolutions.Select(x => x.Solution));
|
---|
139 | results[TrainingBestSolutionQualitiesParameter.Name].Value = new ItemList<DoubleArray>(sortedNonDominatedSolutions.Select(x => new DoubleArray(x.Qualities)));
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 | return base.Apply();
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
146 |
|
---|
147 | private bool IsNonDominated(double[] point, IEnumerable<double[]> points, bool[] maximization) {
|
---|
148 | foreach (var refPoint in points) {
|
---|
149 | bool refPointDominatesPoint = IsBetterOrEqual(refPoint, point, maximization);
|
---|
150 | if (refPointDominatesPoint) return false;
|
---|
151 | }
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 | private bool IsBetterOrEqual(double[] lhs, double[] rhs, bool[] maximization) {
|
---|
156 | for (int i = 0; i < lhs.Length; i++) {
|
---|
157 | var result = IsBetterOrEqual(lhs[i], rhs[i], maximization[i]);
|
---|
158 | if (!result) return false;
|
---|
159 | }
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
|
---|
164 | if (maximization) return lhs >= rhs;
|
---|
165 | else return lhs <= rhs;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|