Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15849


Ignore:
Timestamp:
03/20/18 13:49:19 (5 years ago)
Author:
lkammere
Message:

#2886: Add constants to grammar.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15834 r15849  
    4242    public TerminalSymbol Inv;
    4343
    44     // For infix notation
    45     public TerminalSymbol OpeningBracket;
    46     public TerminalSymbol ClosingBracket;
     44    public TerminalSymbol Const;
    4745
    4846    #endregion
     
    9391      Inv = new TerminalSymbol("inv");
    9492
    95       OpeningBracket = new TerminalSymbol("(");
    96       ClosingBracket = new TerminalSymbol(")");
     93      Const = new TerminalSymbol("c");
    9794      #endregion
    9895
     
    107104
    108105      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)
    111108      };
    112109
     
    126123
    127124      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) };
    129126      productions[SinFactor] = new[] { new Production(SimpleExpr, Sin) };
    130127      productions[CosFactor] = new[] { new Production(SimpleExpr, Cos) };
    131128
    132129      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)
    135132      };
    136133
     
    141138
    142139      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)
    145142      };
    146143
     
    206203      if (currentSymbol == Addition) {
    207204        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
    210216
    211217      } else if (currentSymbol == Multiplication) {
     
    236242        parsedSubTree.AddSubtree(dividend);
    237243        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack));
     244
     245      } else if (currentSymbol == Const) {
     246        ConstantTreeNode constNode = (ConstantTreeNode)constSy.CreateTreeNode();
     247        constNode.Value = 1.0;
     248        parsedSubTree = constNode;
    238249
    239250      } else if (currentSymbol is VariableTerminalSymbol) {
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs

    r15835 r15849  
    4444      }
    4545
    46       // var or nonterminal symbol
     46      // var, const or nonterminal symbol
    4747      return new[] { AggregateHashes(currentSymbol, new THashType[0]) };
    4848    }
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15834 r15849  
    3131      alg.Problem = problem;
    3232      alg.GuiUpdateInterval = int.MaxValue;
     33      foreach (IGrammarEnumerationAnalyzer grammarEnumerationAnalyzer in alg.Analyzers) {
     34        alg.Analyzers.SetItemCheckedState(grammarEnumerationAnalyzer, grammarEnumerationAnalyzer is RSquaredEvaluator);
     35      }
    3336    }
    3437
     
    5760    public void NoConstants_Nguyen1() {
    5861      // x³ + x² + x
    59       alg.MaxTreeSize = 12;
     62      alg.MaxTreeSize = 20;
    6063      alg.Problem.ProblemData = new NguyenFunctionOne(Seed).GenerateRegressionData();
    6164
    6265      alg.Start();
    6366
     67      TerminalSymbol constSymbol = alg.Grammar.Const;
    6468      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
    6569      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     
    6771
    6872      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
    7277      });
    7378
     
    8893    public void NoConstants_Nguyen2() {
    8994      // x^4 + x³ + x² + x
    90       alg.MaxTreeSize = 20;
     95      alg.MaxTreeSize = 30;
    9196      alg.Problem.ProblemData = new NguyenFunctionTwo(Seed).GenerateRegressionData();
    9297
     
    112117    public void NoConstants_Nguyen6() {
    113118      // sin(x) + sin(x + x²)
    114       alg.MaxTreeSize = 10;
     119      alg.MaxTreeSize = 25;
    115120      alg.Problem.ProblemData = new NguyenFunctionSix(Seed).GenerateRegressionData();
    116121
    117122      alg.Start();
    118123
     124      TerminalSymbol constSymbol = alg.Grammar.Const;
    119125      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
    120126      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     
    122128      TerminalSymbol sinSymbol = alg.Grammar.Sin;
    123129
     130      // c * sin(c x + c) + c * sin(c * x * x + c * x) + c
    124131      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
    127135      });
    128136
     
    140148    public void NoConstants_Nguyen9() {
    141149      // sin(x) + sin(y²)
    142       alg.MaxTreeSize = 11;
     150      alg.MaxTreeSize = 22;
    143151      alg.Problem.ProblemData = new NguyenFunctionNine(Seed).GenerateRegressionData();
    144152
     
    147155      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "X");
    148156      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "Y");
     157      TerminalSymbol constSymbol = alg.Grammar.Const;
    149158      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
    150159      TerminalSymbol addSymbol = alg.Grammar.Addition;
    151160      TerminalSymbol sinSymbol = alg.Grammar.Sin;
    152161
     162      // c*sin(c*x + c) + c*sin(c*y*y + c) + c
    153163      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
    156167      });
    157168
     
    180191    public void NoConstants_Inverse() {
    181192      // x / (log(x)*x + x)
    182       alg.MaxTreeSize = 12;
     193      alg.MaxTreeSize = 23;
    183194
    184195      var x = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() + 1.1).ToList();
  • branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs

    r15834 r15849  
    1212    private TerminalSymbol varB;
    1313    private TerminalSymbol varC;
     14    private TerminalSymbol c;
    1415
    1516    [TestInitialize]
     
    2021      varB = grammar.VarTerminals.First(s => s.StringRepresentation == "b");
    2122      varC = grammar.VarTerminals.First(s => s.StringRepresentation == "c");
     23      c = grammar.Const;
    2224    }
    2325
     
    132134    }
    133135
     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
    134149    /* DEPRECATED; SINCE WE DO NOT ALLOW COMPOUND DIVISIONS
    135150    [TestMethod]
Note: See TracChangeset for help on using the changeset viewer.