1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Operators;
|
---|
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> : SymbolicDataAnalysisValidationAnalyzer<T, U>,
|
---|
40 | ISymbolicDataAnalysisMultiObjectiveAnalyzer
|
---|
41 | where S : class, ISymbolicDataAnalysisSolution
|
---|
42 | where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<U>
|
---|
43 | where U : class, IDataAnalysisProblemData {
|
---|
44 | private const string QualitiesParameterName = "Qualities";
|
---|
45 | private const string MaximizationParameterName = "Maximization";
|
---|
46 | private const string ValidationBestSolutionsParameterName = "Best validation solutions";
|
---|
47 | private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
|
---|
48 | private const string ValidationBestSolutionsResultName = ValidationBestSolutionsParameterName;
|
---|
49 | private const string ValidationBestSolutionQualitiesResultName = ValidationBestSolutionQualitiesParameterName;
|
---|
50 |
|
---|
51 | #region parameter properties
|
---|
52 | public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
|
---|
53 | get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[QualitiesParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<BoolArray> MaximizationParameter {
|
---|
56 | get { return (ILookupParameter<BoolArray>)Parameters[MaximizationParameterName]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
|
---|
59 | get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
|
---|
62 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 | #region properties
|
---|
66 | public ItemArray<DoubleArray> Qualities {
|
---|
67 | get { return QualitiesParameter.ActualValue; }
|
---|
68 | }
|
---|
69 | public BoolArray Maximization {
|
---|
70 | get { return MaximizationParameter.ActualValue; }
|
---|
71 | }
|
---|
72 | public ItemList<S> ValidationBestSolutions {
|
---|
73 | get { return ValidationBestSolutionsParameter.ActualValue; }
|
---|
74 | set { ValidationBestSolutionsParameter.ActualValue = value; }
|
---|
75 | }
|
---|
76 | public ItemList<DoubleArray> ValidationBestSolutionQualities {
|
---|
77 | get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
|
---|
78 | set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | [StorableConstructor]
|
---|
83 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
84 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
85 | public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
|
---|
86 | : base() {
|
---|
87 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(QualitiesParameterName, "The qualities of the trees that should be analyzed."));
|
---|
88 | Parameters.Add(new LookupParameter<BoolArray>(MaximizationParameterName, "The directions of optimization for each dimension."));
|
---|
89 | Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
90 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override IOperation Apply() {
|
---|
94 | var results = ResultCollection;
|
---|
95 | // create empty parameter and result values
|
---|
96 | if (ValidationBestSolutions == null) {
|
---|
97 | ValidationBestSolutions = new ItemList<S>();
|
---|
98 | ValidationBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
99 | results.Add(new Result(ValidationBestSolutionQualitiesResultName, ValidationBestSolutionQualities));
|
---|
100 | results.Add(new Result(ValidationBestSolutionsResultName, ValidationBestSolutions));
|
---|
101 | }
|
---|
102 |
|
---|
103 | IList<double[]> trainingBestQualities = ValidationBestSolutionQualities
|
---|
104 | .Select(x => x.ToArray())
|
---|
105 | .ToList();
|
---|
106 |
|
---|
107 | #region find best trees
|
---|
108 | IList<int> nonDominatedIndexes = new List<int>();
|
---|
109 | ISymbolicExpressionTree[] tree = SymbolicExpressionTrees.ToArray();
|
---|
110 | List<double[]> qualities = new List<double[]>();
|
---|
111 | bool[] maximization = Maximization.ToArray();
|
---|
112 | List<double[]> newNonDominatedQualities = new List<double[]>();
|
---|
113 | var evaluator = Evaluator;
|
---|
114 | int start = ValidationSamplesStart.Value;
|
---|
115 | int end = ValidationSamplesEnd.Value;
|
---|
116 | IEnumerable<int> rows = Enumerable.Range(start, end - start);
|
---|
117 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
118 | for (int i = 0; i < tree.Length; i++) {
|
---|
119 | qualities.Add(evaluator.Evaluate(childContext, tree[i], ProblemData, rows)); // qualities[i] = ...
|
---|
120 | if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
|
---|
121 | IsNonDominated(qualities[i], qualities, maximization)) {
|
---|
122 | newNonDominatedQualities.Add(qualities[i]);
|
---|
123 | nonDominatedIndexes.Add(i);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 | #region update Pareto-optimal solution archive
|
---|
128 | if (nonDominatedIndexes.Count > 0) {
|
---|
129 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
130 | ItemList<S> nonDominatedSolutions = new ItemList<S>();
|
---|
131 | // add all new non-dominated solutions to the archive
|
---|
132 | foreach (var index in nonDominatedIndexes) {
|
---|
133 | S solution = CreateSolution(tree[index], qualities[index]);
|
---|
134 | nonDominatedSolutions.Add(solution);
|
---|
135 | nonDominatedQualities.Add(new DoubleArray(qualities[index]));
|
---|
136 | }
|
---|
137 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
138 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
139 | if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
140 | nonDominatedSolutions.Add(ValidationBestSolutions[i]);
|
---|
141 | nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | results[ValidationBestSolutionsResultName].Value = nonDominatedSolutions;
|
---|
146 | results[ValidationBestSolutionQualitiesResultName].Value = nonDominatedQualities;
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 | return base.Apply();
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
153 |
|
---|
154 | private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
|
---|
155 | foreach (var refPoint in points) {
|
---|
156 | bool refPointDominatesPoint = true;
|
---|
157 | for (int i = 0; i < point.Length; i++) {
|
---|
158 | refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
|
---|
159 | }
|
---|
160 | if (refPointDominatesPoint) return false;
|
---|
161 | }
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
165 | if (maximization) return lhs > rhs;
|
---|
166 | else return lhs < rhs;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|