Changeset 13310 for stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 11/19/15 19:39:07 (9 years ago)
- Location:
- stable
- Files:
-
- 4 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13241,13300,13307
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs
r12009 r13310 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; … … 42 41 private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities"; 43 42 private const string UpdateAlwaysParameterName = "Always update best solutions"; 43 private const string TrainingBestSolutionParameterName = "Best training solution"; 44 44 45 45 #region parameter properties … … 55 55 #endregion 56 56 #region properties 57 p ublicItemList<T> TrainingBestSolutions {57 private ItemList<T> TrainingBestSolutions { 58 58 get { return TrainingBestSolutionsParameter.ActualValue; } 59 59 set { TrainingBestSolutionsParameter.ActualValue = value; } 60 60 } 61 p ublicItemList<DoubleArray> TrainingBestSolutionQualities {61 private ItemList<DoubleArray> TrainingBestSolutionQualities { 62 62 get { return TrainingBestSolutionQualitiesParameter.ActualValue; } 63 63 set { TrainingBestSolutionQualitiesParameter.ActualValue = value; } 64 64 } 65 public BoolValue UpdateAlways { 66 get { return UpdateAlwaysParameter.Value; } 65 public bool UpdateAlways { 66 get { return UpdateAlwaysParameter.Value.Value; } 67 set { UpdateAlwaysParameter.Value.Value = value; } 67 68 } 68 69 #endregion … … 97 98 } 98 99 100 if (!results.ContainsKey(TrainingBestSolutionParameterName)) { 101 results.Add(new Result(TrainingBestSolutionParameterName, "", typeof(ISymbolicDataAnalysisSolution))); 102 } 103 99 104 //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;101 if (UpdateAlways .Value) {105 List<double[]> trainingBestQualities; 106 if (UpdateAlways) { 102 107 trainingBestQualities = new List<double[]>(); 103 108 } else { … … 105 110 } 106 111 107 #region find best trees 108 IList<int> nonDominatedIndexes = new List<int>(); 109 ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); 112 ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray(); 110 113 List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList(); 111 114 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); 115 116 var nonDominatedIndividuals = new[] { new { Tree = default(ISymbolicExpressionTree), Qualities = default(double[]) } }.ToList(); 117 nonDominatedIndividuals.Clear(); 118 119 // build list of new non-dominated solutions 120 for (int i = 0; i < trees.Length; i++) { 121 if (IsNonDominated(qualities[i], nonDominatedIndividuals.Select(ind => ind.Qualities), maximization) && 122 IsNonDominated(qualities[i], trainingBestQualities, maximization)) { 123 for (int j = nonDominatedIndividuals.Count - 1; j >= 0; j--) { 124 if (IsBetterOrEqual(qualities[i], nonDominatedIndividuals[j].Qualities, maximization)) { 125 nonDominatedIndividuals.RemoveAt(j); 126 } 119 127 } 128 nonDominatedIndividuals.Add(new { Tree = trees[i], Qualities = qualities[i] }); 120 129 } 121 130 } 122 #endregion 131 132 var nonDominatedSolutions = nonDominatedIndividuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList(); 133 nonDominatedSolutions.ForEach(s => s.Solution.Name = string.Join(",", s.Qualities.Select(q => q.ToString()))); 134 123 135 #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 136 if (nonDominatedSolutions.Count > 0) { 137 //add old non-dominated solutions only if they are not dominated by one of the new solutions 134 138 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 } 139 if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization)) { 140 nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() }); 140 141 } 141 142 } 142 143 143 results[TrainingBestSolutionsParameter.Name].Value = nonDominatedSolutions; 144 results[TrainingBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities; 144 //assumes the the first objective is always the accuracy 145 var sortedNonDominatedSolutions = maximization[0] 146 ? nonDominatedSolutions.OrderByDescending(x => x.Qualities[0]) 147 : nonDominatedSolutions.OrderBy(x => x.Qualities[0]); 148 var trainingBestSolution = sortedNonDominatedSolutions.Select(s => s.Solution).First(); 149 results[TrainingBestSolutionParameterName].Value = trainingBestSolution; 150 TrainingBestSolutions = new ItemList<T>(sortedNonDominatedSolutions.Select(x => x.Solution)); 151 results[TrainingBestSolutionsParameter.Name].Value = TrainingBestSolutions; 152 TrainingBestSolutionQualities = new ItemList<DoubleArray>(sortedNonDominatedSolutions.Select(x => new DoubleArray(x.Qualities))); 153 results[TrainingBestSolutionQualitiesParameter.Name].Value = TrainingBestSolutionQualities; 145 154 } 146 155 #endregion … … 148 157 } 149 158 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 159 protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality); 168 160 169 private bool IsNonDominated(double[] point, I List<double[]> points, bool[] maximization) {161 private bool IsNonDominated(double[] point, IEnumerable<double[]> points, bool[] maximization) { 170 162 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 } 163 bool refPointDominatesPoint = IsBetterOrEqual(refPoint, point, maximization); 175 164 if (refPointDominatesPoint) return false; 176 165 } 177 166 return true; 178 167 } 168 169 private bool IsBetterOrEqual(double[] lhs, double[] rhs, bool[] maximization) { 170 for (int i = 0; i < lhs.Length; i++) { 171 var result = IsBetterOrEqual(lhs[i], rhs[i], maximization[i]); 172 if (!result) return false; 173 } 174 return true; 175 } 176 179 177 private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) { 180 if (maximization) return lhs > rhs;181 else return lhs < rhs;178 if (maximization) return lhs >= rhs; 179 else return lhs <= rhs; 182 180 } 183 181 } -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r13047 r13310 138 138 <Compile Include="Importer\Token.cs" /> 139 139 <Compile Include="Interfaces\IModelBacktransformator.cs" /> 140 <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" /> 141 <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" /> 140 142 <Compile Include="SymbolicExpressionTreeBacktransformator.cs" /> 141 143 <Compile Include="SymbolicDataAnalysisExpressionPruningOperator.cs" /> -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs
r13241 r13310 28 28 29 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 30 [Item("SymbolicExpressionTreeSimplificationOperator", "Simpl fies symbolic expression trees encoding a mathematical formula.")]30 [Item("SymbolicExpressionTreeSimplificationOperator", "Simplifies symbolic expression trees encoding a mathematical formula.")] 31 31 [StorableClass] 32 32 public class SymbolicDataAnalysisExpressionTreeSimplificationOperator : SingleSuccessorOperator, ISymbolicExpressionTreeOperator { -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModelComplexityCalculator.cs
r13241 r13310 42 42 return 2; 43 43 } 44 case OpCodes.Add: { 45 double complexity = 0; 46 for (int i = 0; i < node.SubtreeCount; i++) { 47 complexity += CalculateComplexity(node.GetSubtree(i)); 48 } 49 return complexity; 50 } 44 case OpCodes.Add: 51 45 case OpCodes.Sub: { 52 46 double complexity = 0; … … 56 50 return complexity; 57 51 } 58 case OpCodes.Mul: { 59 double complexity = 1; 60 for (int i = 0; i < node.SubtreeCount; i++) { 61 var nodeComplexity = CalculateComplexity(node.GetSubtree(i)); 62 complexity *= nodeComplexity + 1; 63 } 64 return complexity; 65 } 52 case OpCodes.Mul: 66 53 case OpCodes.Div: { 67 54 double complexity = 1; … … 72 59 return complexity; 73 60 } 74 case OpCodes.Sin: { 75 double complexity = CalculateComplexity(node.GetSubtree(0)); 76 return Math.Pow(2.0, complexity); 77 } 78 case OpCodes.Cos: { 79 double complexity = CalculateComplexity(node.GetSubtree(0)); 80 return Math.Pow(2.0, complexity); 81 } 82 case OpCodes.Tan: { 83 double complexity = CalculateComplexity(node.GetSubtree(0)); 84 return Math.Pow(2.0, complexity); 85 } 86 case OpCodes.Exp: { 87 double complexity = CalculateComplexity(node.GetSubtree(0)); 88 return Math.Pow(2.0, complexity); 89 } 61 case OpCodes.Sin: 62 case OpCodes.Cos: 63 case OpCodes.Tan: 64 case OpCodes.Exp: 90 65 case OpCodes.Log: { 91 66 double complexity = CalculateComplexity(node.GetSubtree(0)); … … 100 75 return complexity * complexity * complexity; 101 76 } 102 case OpCodes.Power: { 77 case OpCodes.Power: 78 case OpCodes.Root: { 103 79 double complexity = CalculateComplexity(node.GetSubtree(0)); 104 var exponent Node= node.GetSubtree(1) as ConstantTreeNode;105 if (exponent Node!= null) {106 double exp onent = exponentNode.Value;107 if (exp onent < 0) exponent = Math.Abs(exponent);108 if (exp onent < 1) exponent = 1 / exponent;109 return Math.Pow(complexity, Math.Round(exp onent));80 var exponent = node.GetSubtree(1) as ConstantTreeNode; 81 if (exponent != null) { 82 double expVal = exponent.Value; 83 if (expVal < 0) expVal = Math.Abs(expVal); 84 if (expVal < 1) expVal = 1 / expVal; 85 return Math.Pow(complexity, Math.Round(expVal)); 110 86 } 111 87 112 double exponentComplexity = CalculateComplexity(node.GetSubtree(1)); 113 return Math.Pow(complexity, 2 * exponentComplexity); 114 } 115 case OpCodes.Root: { 116 double complexity = CalculateComplexity(node.GetSubtree(0)); 117 var rootNode = node.GetSubtree(1) as ConstantTreeNode; 118 if (rootNode != null) { 119 double root = rootNode.Value; 120 if (root < 0) root = Math.Abs(root); 121 if (root < 1) root = 1 / root; 122 return Math.Pow(complexity, Math.Round(root)); 123 } 124 125 double rootComplexity = CalculateComplexity(node.GetSubtree(1)); 126 return Math.Pow(complexity, 2 * rootComplexity); 88 double expComplexity = CalculateComplexity(node.GetSubtree(1)); 89 return Math.Pow(complexity, 2 * expComplexity); 127 90 } 128 91
Note: See TracChangeset
for help on using the changeset viewer.