Changeset 15724 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
- Timestamp:
- 02/06/18 13:52:00 (6 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
- Files:
-
- 2 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 }
Note: See TracChangeset
for help on using the changeset viewer.