Changeset 15724
- Timestamp:
- 02/06/18 13:52:00 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15723 r15724 155 155 #endregion 156 156 */ 157 157 158 158 #region Hashing 159 159 public int CalcHashCode(SymbolString sentence) { … … 282 282 } 283 283 #endregion 284 285 #region Parse to Infix string 286 287 public SymbolString PostfixToInfixParser(SymbolString phrase) { 288 Stack<Symbol> parseStack = new Stack<Symbol>(phrase); 289 290 return PostfixToInfixSubtreeParser(parseStack); 291 } 292 293 private SymbolString PostfixToInfixSubtreeParser(Stack<Symbol> parseStack) { 294 Symbol head = parseStack.Pop(); 295 296 SymbolString result = new SymbolString(); 297 298 if (ReferenceEquals(head, Addition) || ReferenceEquals(head, Multiplication)) { 299 result.AddRange(PostfixToInfixSubtreeParser(parseStack)); 300 result.Add(head); 301 result.AddRange(PostfixToInfixSubtreeParser(parseStack)); 302 } else { 303 result.Add(head); 304 } 305 return result; 306 } 307 308 #endregion 284 309 } 285 310 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15723 r15724 20 20 public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> { 21 21 private readonly string BestTrainingSolution = "Best solution (training)"; 22 private readonly string BestTrainingSolutionString = "Best solution string (training)";23 22 private readonly string BestTrainingSolutionQuality = "Best solution quality (training)"; 24 23 private readonly string BestTestSolution = "Best solution (test)"; 25 private readonly string BestTestSolutionString = "Best solution string (test)";26 24 private readonly string BestTestSolutionQuality = "Best solution quality (test)"; 27 25 … … 56 54 } 57 55 56 public SymbolString BestTrainingSentence; 57 public SymbolString BestTestSentence; 58 58 59 #endregion 59 60 60 p rivate Grammar grammar;61 public Grammar Grammar; 61 62 62 63 … … 85 86 86 87 protected override void Run(CancellationToken cancellationToken) { 88 BestTrainingSentence = null; 89 BestTrainingSentence = null; 90 87 91 List<SymbolString> allGenerated = new List<SymbolString>(); 88 92 List<SymbolString> distinctGenerated = new List<SymbolString>(); … … 92 96 HashSet<int> evaluatedHashes = new HashSet<int>(); 93 97 94 grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());98 Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray()); 95 99 96 100 Stack<SymbolString> remainingTrees = new Stack<SymbolString>(); 97 remainingTrees.Push(new SymbolString(new[] { grammar.StartSymbol }));101 remainingTrees.Push(new SymbolString(new[] { Grammar.StartSymbol })); 98 102 99 103 while (remainingTrees.Any()) { … … 103 107 104 108 if (currSymbolString.IsSentence()) { 105 allGenerated.Add( currSymbolString);109 allGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString)); 106 110 107 111 //if (evaluatedHashes.Add(grammar.CalcHashCode(currSymbolString))) { 108 112 EvaluateSentence(currSymbolString); 109 //distinctGenerated.Add(currSymbolString);113 distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString)); 110 114 //} 111 115 … … 155 159 156 160 private void EvaluateSentence(SymbolString symbolString) { 157 SymbolicExpressionTree tree = grammar.ParseSymbolicExpressionTree(symbolString);161 SymbolicExpressionTree tree = Grammar.ParseSymbolicExpressionTree(symbolString); 158 162 SymbolicRegressionModel model = new SymbolicRegressionModel( 159 163 Problem.ProblemData.TargetVariable, … … 168 172 || !Results.TryGetValue(BestTestSolution, out currBestTestSolutionResult)) { 169 173 174 BestTrainingSentence = symbolString; 170 175 Results.Add(new Result(BestTrainingSolution, newSolution)); 171 Results.Add(new Result(BestTrainingSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));172 176 Results.Add(new Result(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly())); 177 178 BestTestSentence = symbolString; 173 179 Results.Add(new Result(BestTestSolution, newSolution)); 174 Results.Add(new Result(BestTestSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));175 180 Results.Add(new Result(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly())); 176 181 … … 178 183 IRegressionSolution currBestTrainingSolution = (IRegressionSolution)currBestTrainingSolutionResult.Value; 179 184 if (currBestTrainingSolution.TrainingRSquared < newSolution.TrainingRSquared) { 185 BestTrainingSentence = symbolString; 180 186 currBestTrainingSolutionResult.Value = newSolution; 181 Results.AddOrUpdateResult(BestTrainingSolutionString, new StringValue(symbolString.ToString()));182 187 Results.AddOrUpdateResult(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly()); 183 188 } … … 185 190 IRegressionSolution currBestTestSolution = (IRegressionSolution)currBestTestSolutionResult.Value; 186 191 if (currBestTestSolution.TestRSquared < newSolution.TestRSquared) { 192 BestTestSentence = symbolString; 187 193 currBestTestSolutionResult.Value = newSolution; 188 Results.AddOrUpdateResult(BestTestSolutionString, new StringValue(symbolString.ToString()));189 194 Results.AddOrUpdateResult(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly()); 190 195 } -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15723 r15724 2 2 using System.Linq; 3 3 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration; 4 using HeuristicLab.Common; 4 5 using HeuristicLab.Core; 5 6 using HeuristicLab.Data; 7 using HeuristicLab.Optimization; 6 8 using HeuristicLab.Problems.DataAnalysis; 7 9 using HeuristicLab.Random; … … 9 11 10 12 namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression { 11 [TestClass ()]13 [TestClass] 12 14 public class MctsSymbolicRegressionTest { 13 15 private const int Seed = 1234; … … 30 32 } 31 33 34 [TestCleanup()] 35 public void Cleanup() { 36 if (alg.BestTrainingSentence != null) { 37 Console.WriteLine("Training: " + alg.Grammar.PostfixToInfixParser(alg.BestTrainingSentence)); 38 } 39 if (alg.BestTestSentence != null) { 40 Console.WriteLine("Test: " + alg.Grammar.PostfixToInfixParser(alg.BestTestSentence)); 41 } 42 } 43 32 44 33 45 private void TestGrammarEnumeration(IRegressionProblemData problemData) { … … 49 61 50 62 // Check overfitting 51 Assert.AreEqual(alg.Results["Best solution string (training)"].Value.ToString(), 52 alg.Results["Best solution string (test)"].Value.ToString()); 63 Assert.AreEqual(alg.Grammar.CalcHashCode(alg.BestTrainingSentence), 64 alg.Grammar.CalcHashCode(alg.BestTestSentence), 65 "Training and Test solution are not equal!"); 53 66 } 54 67 … … 63 76 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 "))); 64 77 TestGrammarEnumeration(regProblem); 65 66 Console.WriteLine("Nguyen1: " + alg.Results["Best solution string (training)"].Value);67 78 } 68 79 … … 75 86 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 "))); 76 87 TestGrammarEnumeration(regProblem); 77 78 Console.WriteLine("Nguyen2: " + alg.Results["Best solution string (training)"].Value);79 88 } 80 89
Note: See TracChangeset
for help on using the changeset viewer.