Changeset 10749
- Timestamp:
- 04/15/14 13:16:51 (11 years ago)
- Location:
- branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs
r9456 r10749 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; … … 98 97 99 98 //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions 100 IList<double[]> trainingBestQualities;99 List<double[]> trainingBestQualities; 101 100 if (UpdateAlways.Value) { 102 101 trainingBestQualities = new List<double[]>(); … … 105 104 } 106 105 107 #region find best trees 108 IList<int> nonDominatedIndexes = new List<int>(); 109 ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); 106 ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray(); 110 107 List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList(); 111 108 bool[] maximization = Maximization.ToArray(); 112 List<double[]> newNonDominatedQualities = new List<double[]>(); 113 for (int i = 0; i < tree.Length; i++) { 114 if (IsNonDominated(qualities[i], trainingBestQualities, maximization) && 115 IsNonDominated(qualities[i], qualities, maximization)) { 116 if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) { 117 newNonDominatedQualities.Add(qualities[i]); 118 nonDominatedIndexes.Add(i); 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 } 119 121 } 122 nonDominatedInvididuals.Add(new { Tree = trees[i], Qualities = qualities[i] }); 120 123 } 121 124 } 122 #endregion 125 126 var nonDominatedSolutions = nonDominatedInvididuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList(); 127 123 128 #region update Pareto-optimal solution archive 124 if (nonDominatedIndexes.Count > 0) { 125 ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>(); 126 ItemList<T> nonDominatedSolutions = new ItemList<T>(); 127 // add all new non-dominated solutions to the archive 128 foreach (var index in nonDominatedIndexes) { 129 T solution = CreateSolution(tree[index], qualities[index]); 130 nonDominatedSolutions.Add(solution); 131 nonDominatedQualities.Add(new DoubleArray(qualities[index])); 132 } 133 // add old non-dominated solutions only if they are not dominated by one of the new solutions 129 if (nonDominatedSolutions.Count > 0) { 130 //add old non-dominated solutions only if they are not dominated by one of the new solutions 134 131 for (int i = 0; i < trainingBestQualities.Count; i++) { 135 if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) { 136 if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) { 137 nonDominatedSolutions.Add(TrainingBestSolutions[i]); 138 nonDominatedQualities.Add(TrainingBestSolutionQualities[i]); 139 } 132 if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization)) { 133 nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() }); 140 134 } 141 135 } 142 136 143 results[TrainingBestSolutionsParameter.Name].Value = nonDominatedSolutions; 144 results[TrainingBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities; 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))); 145 140 } 146 141 #endregion … … 148 143 } 149 144 150 private class DoubleArrayComparer : IEqualityComparer<double[]> {151 public bool Equals(double[] x, double[] y) {152 if (y.Length != x.Length) throw new ArgumentException();153 for (int i = 0; i < x.Length; i++) {154 if (!x[i].IsAlmost(y[i])) return false;155 }156 return true;157 }158 159 public int GetHashCode(double[] obj) {160 int c = obj.Length;161 for (int i = 0; i < obj.Length; i++)162 c ^= obj[i].GetHashCode();163 return c;164 }165 }166 167 145 protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality); 168 146 169 private bool IsNonDominated(double[] point, I List<double[]> points, bool[] maximization) {147 private bool IsNonDominated(double[] point, IEnumerable<double[]> points, bool[] maximization) { 170 148 foreach (var refPoint in points) { 171 bool refPointDominatesPoint = true; 172 for (int i = 0; i < point.Length; i++) { 173 refPointDominatesPoint &= IsBetterOrEqual(refPoint[i], point[i], maximization[i]); 174 } 149 bool refPointDominatesPoint = IsBetterOrEqual(refPoint, point, maximization); 175 150 if (refPointDominatesPoint) return false; 176 151 } 177 152 return true; 178 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 179 163 private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) { 180 if (maximization) return lhs > rhs;181 else return lhs < rhs;164 if (maximization) return lhs >= rhs; 165 else return lhs <= rhs; 182 166 } 183 167 } -
branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r10562 r10749 112 112 </ItemGroup> 113 113 <ItemGroup> 114 <Compile Include="Analyzers\SymbolicDataAnalysisModelComplexityAnalyzer.cs" /> 114 115 <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" /> 115 116 <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
Note: See TracChangeset
for help on using the changeset viewer.