Changeset 15849
- Timestamp:
- 03/20/18 13:49:19 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15834 r15849 42 42 public TerminalSymbol Inv; 43 43 44 // For infix notation 45 public TerminalSymbol OpeningBracket; 46 public TerminalSymbol ClosingBracket; 44 public TerminalSymbol Const; 47 45 48 46 #endregion … … 93 91 Inv = new TerminalSymbol("inv"); 94 92 95 OpeningBracket = new TerminalSymbol("("); 96 ClosingBracket = new TerminalSymbol(")"); 93 Const = new TerminalSymbol("c"); 97 94 #endregion 98 95 … … 107 104 108 105 productions[Expr] = new[] { 109 new Production( Term, Expr, Addition),110 new Production( Term)106 new Production(Const, Term, Multiplication, Expr, Addition), 107 new Production(Const, Term, Multiplication, Const, Addition) 111 108 }; 112 109 … … 126 123 127 124 productions[LogFactor] = new[] { new Production(SimpleExpr, Log) }; 128 productions[ExpFactor] = new[] { new Production( SimpleTerm, Exp) };125 productions[ExpFactor] = new[] { new Production(Const, SimpleTerm, Multiplication, Exp) }; 129 126 productions[SinFactor] = new[] { new Production(SimpleExpr, Sin) }; 130 127 productions[CosFactor] = new[] { new Production(SimpleExpr, Cos) }; 131 128 132 129 productions[SimpleExpr] = new[] { 133 new Production( SimpleTerm, SimpleExpr, Addition),134 new Production( SimpleTerm)130 new Production(Const, SimpleTerm, Multiplication, SimpleExpr, Addition), 131 new Production(Const, SimpleTerm, Multiplication, Const, Addition) 135 132 }; 136 133 … … 141 138 142 139 productions[InvExpr] = new[] { 143 new Production( InvTerm, InvExpr, Addition),144 new Production( InvTerm)140 new Production(Const, InvTerm, Multiplication, InvExpr, Addition), 141 new Production(Const, InvTerm, Multiplication, Const, Addition) 145 142 }; 146 143 … … 206 203 if (currentSymbol == Addition) { 207 204 parsedSubTree = addSy.CreateTreeNode(); 208 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part 209 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part 205 ISymbolicExpressionTreeNode rightSubtree = ParseSymbolicExpressionTree(parseStack); 206 if (rightSubtree is ConstantTreeNode) { 207 ((ConstantTreeNode)rightSubtree).Value = 0.0; 208 } 209 parsedSubTree.AddSubtree(rightSubtree); // left part 210 211 ISymbolicExpressionTreeNode leftSubtree = ParseSymbolicExpressionTree(parseStack); 212 if (leftSubtree is ConstantTreeNode) { 213 ((ConstantTreeNode)leftSubtree).Value = 0.0; 214 } 215 parsedSubTree.AddSubtree(leftSubtree); // right part 210 216 211 217 } else if (currentSymbol == Multiplication) { … … 236 242 parsedSubTree.AddSubtree(dividend); 237 243 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 244 245 } else if (currentSymbol == Const) { 246 ConstantTreeNode constNode = (ConstantTreeNode)constSy.CreateTreeNode(); 247 constNode.Value = 1.0; 248 parsedSubTree = constNode; 238 249 239 250 } else if (currentSymbol is VariableTerminalSymbol) { -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs
r15835 r15849 44 44 } 45 45 46 // var or nonterminal symbol46 // var, const or nonterminal symbol 47 47 return new[] { AggregateHashes(currentSymbol, new THashType[0]) }; 48 48 } -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15834 r15849 31 31 alg.Problem = problem; 32 32 alg.GuiUpdateInterval = int.MaxValue; 33 foreach (IGrammarEnumerationAnalyzer grammarEnumerationAnalyzer in alg.Analyzers) { 34 alg.Analyzers.SetItemCheckedState(grammarEnumerationAnalyzer, grammarEnumerationAnalyzer is RSquaredEvaluator); 35 } 33 36 } 34 37 … … 57 60 public void NoConstants_Nguyen1() { 58 61 // x³ + x² + x 59 alg.MaxTreeSize = 12;62 alg.MaxTreeSize = 20; 60 63 alg.Problem.ProblemData = new NguyenFunctionOne(Seed).GenerateRegressionData(); 61 64 62 65 alg.Start(); 63 66 67 TerminalSymbol constSymbol = alg.Grammar.Const; 64 68 TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First(); 65 69 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; … … 67 71 68 72 SymbolString targetSolution = new SymbolString(new[] { 69 varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, 70 varSymbol, varSymbol, mulSymbol, addSymbol, 71 varSymbol, addSymbol 73 constSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol, 74 constSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol, 75 constSymbol, varSymbol, mulSymbol, addSymbol, 76 constSymbol, addSymbol 72 77 }); 73 78 … … 88 93 public void NoConstants_Nguyen2() { 89 94 // x^4 + x³ + x² + x 90 alg.MaxTreeSize = 20;95 alg.MaxTreeSize = 30; 91 96 alg.Problem.ProblemData = new NguyenFunctionTwo(Seed).GenerateRegressionData(); 92 97 … … 112 117 public void NoConstants_Nguyen6() { 113 118 // sin(x) + sin(x + x²) 114 alg.MaxTreeSize = 10;119 alg.MaxTreeSize = 25; 115 120 alg.Problem.ProblemData = new NguyenFunctionSix(Seed).GenerateRegressionData(); 116 121 117 122 alg.Start(); 118 123 124 TerminalSymbol constSymbol = alg.Grammar.Const; 119 125 TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First(); 120 126 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; … … 122 128 TerminalSymbol sinSymbol = alg.Grammar.Sin; 123 129 130 // c * sin(c x + c) + c * sin(c * x * x + c * x) + c 124 131 SymbolString targetSolution = new SymbolString(new[] { 125 varSymbol, sinSymbol, 126 varSymbol, varSymbol, mulSymbol, varSymbol, addSymbol, sinSymbol, addSymbol 132 varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, 133 varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, varSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol, 134 constSymbol, addSymbol 127 135 }); 128 136 … … 140 148 public void NoConstants_Nguyen9() { 141 149 // sin(x) + sin(y²) 142 alg.MaxTreeSize = 11;150 alg.MaxTreeSize = 22; 143 151 alg.Problem.ProblemData = new NguyenFunctionNine(Seed).GenerateRegressionData(); 144 152 … … 147 155 TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "X"); 148 156 TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "Y"); 157 TerminalSymbol constSymbol = alg.Grammar.Const; 149 158 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 150 159 TerminalSymbol addSymbol = alg.Grammar.Addition; 151 160 TerminalSymbol sinSymbol = alg.Grammar.Sin; 152 161 162 // c*sin(c*x + c) + c*sin(c*y*y + c) + c 153 163 SymbolString targetSolution = new SymbolString(new[] { 154 xSymbol, sinSymbol, 155 ySymbol, ySymbol, mulSymbol, sinSymbol, addSymbol 164 xSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, 165 ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol, 166 constSymbol, addSymbol 156 167 }); 157 168 … … 180 191 public void NoConstants_Inverse() { 181 192 // x / (log(x)*x + x) 182 alg.MaxTreeSize = 12;193 alg.MaxTreeSize = 23; 183 194 184 195 var x = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() + 1.1).ToList(); -
branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs
r15834 r15849 12 12 private TerminalSymbol varB; 13 13 private TerminalSymbol varC; 14 private TerminalSymbol c; 14 15 15 16 [TestInitialize] … … 20 21 varB = grammar.VarTerminals.First(s => s.StringRepresentation == "b"); 21 22 varC = grammar.VarTerminals.First(s => s.StringRepresentation == "c"); 23 c = grammar.Const; 22 24 } 23 25 … … 132 134 } 133 135 136 // Constants 137 [TestMethod] 138 [TestCategory("TreeHashing")] 139 public void SimpleConst() { 140 SymbolString s1 = new SymbolString(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition}); 141 SymbolString s2 = new SymbolString(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition }); 142 143 int hash1 = grammar.Hasher.CalcHashCode(s1); 144 int hash2 = grammar.Hasher.CalcHashCode(s2); 145 146 Assert.AreEqual(hash1, hash2); 147 } 148 134 149 /* DEPRECATED; SINCE WE DO NOT ALLOW COMPOUND DIVISIONS 135 150 [TestMethod]
Note: See TracChangeset
for help on using the changeset viewer.