Changeset 15883 for branches/2886_SymRegGrammarEnumeration
- Timestamp:
- 04/04/18 16:23:55 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs
r15861 r15883 13 13 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 14 14 public class RSquaredEvaluator : Item, IGrammarEnumerationAnalyzer { 15 p rivatereadonly string BestTrainingQualityResultName = "Best R² (Training)";16 p rivatereadonly string BestTrainingModelResultName = "Best model (Training)";17 p rivatereadonly string BestTrainingSolutionResultName = "Best solution (Training)";15 public static readonly string BestTrainingQualityResultName = "Best R² (Training)"; 16 public static readonly string BestTrainingModelResultName = "Best model (Training)"; 17 public static readonly string BestTrainingSolutionResultName = "Best solution (Training)"; 18 18 19 private readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();19 private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); 20 20 21 21 public bool OptimizeConstants { get; set; } … … 73 73 Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)); 74 74 75 // TODO: Initialize constant values randomly 76 // TODO: Restarts 75 double r2 = Evaluate(problemData, tree, OptimizeConstants); 77 76 78 double r2; 77 var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value; 78 bool better = r2 > bestR2Result.Value; 79 bool equallyGood = r2.IsAlmost(bestR2Result.Value); 80 bool shorter = false; 79 81 80 SymbolicRegressionModel model = new SymbolicRegressionModel( 82 if (!better && equallyGood) { 83 shorter = algorithm.BestTrainingSentence != null && 84 algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString); 85 } 86 if (better || (equallyGood && shorter)) { 87 bestR2Result.Value = r2; 88 89 SymbolicRegressionModel model = new SymbolicRegressionModel( 81 90 problemData.TargetVariable, 82 91 tree, 83 92 expressionTreeLinearInterpreter); 84 93 85 if (OptimizeConstants) { 94 algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model); 95 96 algorithm.BestTrainingSentence = symbolString; 97 } 98 } 99 100 public static double Evaluate(IRegressionProblemData problemData, SymbolicExpressionTree tree, bool optimizeConstants = true) { 101 double r2; 102 103 // TODO: Initialize constant values randomly 104 // TODO: Restarts 105 if (optimizeConstants) { 86 106 r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter, 87 107 tree, … … 100 120 } 101 121 102 103 122 } else { 104 123 var target = problemData.TargetVariableTrainingValues; 124 125 SymbolicRegressionModel model = new SymbolicRegressionModel( 126 problemData.TargetVariable, 127 tree, 128 expressionTreeLinearInterpreter); 129 105 130 var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices); 106 131 OnlineCalculatorError error; … … 109 134 } 110 135 111 var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value; 112 bool better = r2 > bestR2Result.Value; 113 bool equallyGood = r2.IsAlmost(bestR2Result.Value); 114 bool shorter = false; 115 116 if (!better && equallyGood) { 117 shorter = algorithm.BestTrainingSentence != null && 118 algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString); 119 } 120 if (better || (equallyGood && shorter)) { 121 bestR2Result.Value = r2; 122 algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model); 123 124 algorithm.BestTrainingSentence = symbolString; 125 } 136 return r2; 126 137 } 127 138 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15861 r15883 4 4 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration; 5 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 using HeuristicLab.Problems.DataAnalysis; 6 7 using HeuristicLab.Problems.DataAnalysis.Symbolic; 7 8 … … 174 175 } 175 176 177 public double EvaluatePhrase(SymbolString s, IRegressionProblemData problemData, bool optimizeConstants) { 178 // Create sentences without Expression symbols. 179 Symbol[] sEval = new Symbol[s.Count()]; 180 181 for (int i = 0; i < sEval.Length; i++) { 182 Symbol currSym = s[i]; 183 if (currSym is NonterminalSymbol && currSym != Expr) 184 return 0.0; 185 186 if (currSym == Expr) { 187 sEval[i] = Const; 188 } else { 189 sEval[i] = currSym; 190 } 191 } 192 193 SymbolicExpressionTree tree = ParseSymbolicExpressionTree(new SymbolString(sEval)); 194 double r2 = RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants); 195 196 return r2; 197 } 198 176 199 #region Parse to SymbolicExpressionTree 177 200 -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15861 r15883 153 153 #endregion 154 154 155 OpenPhrases.Store(phrase0Hash, phrase0);155 OpenPhrases.Store(phrase0Hash, 0.0, phrase0); 156 156 while (OpenPhrases.Count > 0) { 157 157 if (cancellationToken.IsCancellationRequested) break; … … 185 185 OnSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 186 186 187 // Is the best solution found? (only if RSquaredEvaluator is activated) 188 if (Results.ContainsKey(RSquaredEvaluator.BestTrainingQualityResultName) && ((DoubleValue)Results[RSquaredEvaluator.BestTrainingQualityResultName].Value).Value == 1.0) { 189 UpdateView(force: true); 190 return; 191 } 192 187 193 if (!DistinctSentencesComplexity.ContainsKey(phraseHash) || DistinctSentencesComplexity[phraseHash] > newPhraseComplexity) { 188 194 if (DistinctSentencesComplexity.ContainsKey(phraseHash)) OverwrittenSentencesCount++; // for analysis only … … 194 200 195 201 } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) { 196 OpenPhrases.Store(phraseHash, newPhrase); 202 double phrasePriority = GetPriority(newPhrase); 203 OpenPhrases.Store(phraseHash, phrasePriority, newPhrase); 197 204 } 198 205 } … … 200 207 } 201 208 UpdateView(force: true); 209 } 210 211 protected double GetPriority(SymbolString phrase) { 212 return 1.0 - Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants); 202 213 } 203 214 -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs
r15828 r15883 16 16 17 17 public enum StorageType { 18 Stack, Queue, RandomList18 PriorityQueue, Stack, Queue, RandomList 19 19 } 20 20 … … 22 22 23 23 private Dictionary<int, SymbolString> storedValues; // Store hash-references and associated, actual values 24 private Action<int > storeInternal; // Store hash-references24 private Action<int, double> storeInternal; // Store hash-references 25 25 private Func<int> fetchInternal; // Fetch hash-reference 26 26 … … 29 29 30 30 switch (storageType) { 31 case StorageType.PriorityQueue: 32 InitPriorityQueue(); 33 break; 31 34 case StorageType.Stack: 32 35 InitStack(); … … 44 47 #region SearchStrategies 45 48 49 private void InitPriorityQueue() { 50 PriorityQueue<double, int> queue = new PriorityQueue<double, int>(double.MaxValue, double.MinValue, (int)Math.Pow(2.0, 20.0) / 4); 51 storeInternal = (hash, prio) => queue.Insert(prio, hash); 52 fetchInternal = () => { 53 int ret = queue.PeekMinValue(); 54 queue.RemoveMin(); 55 return ret; 56 }; 57 } 58 46 59 private void InitStack() { 47 60 Stack<int> stack = new Stack<int>(); 48 61 49 storeInternal = hash=> stack.Push(hash);62 storeInternal = (hash, prio) => stack.Push(hash); 50 63 fetchInternal = () => stack.Pop(); 51 64 } … … 54 67 Queue<int> queue = new Queue<int>(); 55 68 56 storeInternal = hash=> queue.Enqueue(hash);69 storeInternal = (hash, prio) => queue.Enqueue(hash); 57 70 fetchInternal = () => queue.Dequeue(); 58 71 } … … 62 75 System.Random rand = new System.Random(999); 63 76 64 storeInternal = hash=> list.Add(hash);77 storeInternal = (hash, prio) => list.Add(hash); 65 78 fetchInternal = () => { 66 79 int indexOfHash = rand.Next(list.Count); 67 80 int result = list[indexOfHash]; 68 list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this 81 list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this. Maybe change to priority queue with random key. 69 82 return result; 70 83 }; … … 82 95 } 83 96 84 public void Store(int hash, SymbolString s) {85 storeInternal(hash );97 public void Store(int hash, double priority, SymbolString s) { 98 storeInternal(hash, priority); 86 99 storedValues[hash] = s; 87 100 } -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15861 r15883 34 34 alg.Analyzers.SetItemCheckedState(grammarEnumerationAnalyzer, grammarEnumerationAnalyzer is RSquaredEvaluator); 35 35 } 36 alg.SearchDataStructure = StorageType.PriorityQueue; 36 37 } 37 38 … … 250 251 251 252 [TestMethod] 252 [TestProperty("Goal", "s tructure search +const op")]253 [TestProperty("Goal", "sinnus const op")] 253 254 public void Constants_Keijzer3() { 254 255 // 0.3*x*sin(2*pi*x)
Note: See TracChangeset
for help on using the changeset viewer.