Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/30/16 19:35:15 (8 years ago)
Author:
gkronber
Message:

#2650: added simplifier unit tests for factor symbols

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r14185 r14534  
    3535    private const string INVOKESTART = "CALL";
    3636    private const string TIMELAGSTART = "LAG";
    37     private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>() 
     37    private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
    3838      {
    3939        {"+", new Addition()},
     
    4545        {"POW", new Power()},
    4646        {"ROOT", new Root()},
     47        {"SQR", new Square()},
     48        {"SQRT", new SquareRoot()},
    4749        {"SIN",new Sine()},
    4850        {"COS", new Cosine()},
     
    7476        {"PROG", new ProgramRootSymbol()},
    7577        {"MAIN", new StartSymbol()},
     78        {"FACTOR", new FactorVariable() },
     79        {"BINARYFACTOR", new BinaryFactorVariable()}
    7680      };
    7781
     
    8286    TimeLag timeLag = new TimeLag();
    8387    Integral integral = new Integral();
     88    FactorVariable factorVar = new FactorVariable();
     89    BinaryFactorVariable binFactorVar = new BinaryFactorVariable();
    8490
    8591    ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
     
    9197      ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
    9298      ISymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str)));
    93       if (mainBranch.Symbol is ProgramRootSymbol) {
     99      if(mainBranch.Symbol is ProgramRootSymbol) {
    94100        // when a root symbol was parsed => use main branch as root
    95101        root = mainBranch;
     
    111117
    112118    private ISymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) {
    113       if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
     119      if(tokens.Peek().Symbol == TokenSymbol.LPAR) {
    114120        ISymbolicExpressionTreeNode tree;
    115121        Expect(Token.LPAR, tokens);
    116         if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
     122        if(tokens.Peek().StringValue.StartsWith(VARSTART)) {
    117123          tree = ParseVariable(tokens);
    118         } else if (tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) {
     124        } else if(tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) {
    119125          tree = ParseLaggedVariable(tokens);
    120         } else if (tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) {
     126        } else if(tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) {
    121127          tree = ParseTimeLag(tokens);
    122128          tree.AddSubtree(ParseSexp(tokens));
    123         } else if (tokens.Peek().StringValue.StartsWith(INTEGRALSTART)) {
     129        } else if(tokens.Peek().StringValue.StartsWith(INTEGRALSTART)) {
    124130          tree = ParseIntegral(tokens);
    125131          tree.AddSubtree(ParseSexp(tokens));
    126         } else if (tokens.Peek().StringValue.StartsWith(DEFUNSTART)) {
     132        } else if(tokens.Peek().StringValue.StartsWith(DEFUNSTART)) {
    127133          tree = ParseDefun(tokens);
    128           while (!tokens.Peek().Equals(Token.RPAR)) {
     134          while(!tokens.Peek().Equals(Token.RPAR)) {
    129135            tree.AddSubtree(ParseSexp(tokens));
    130136          }
    131         } else if (tokens.Peek().StringValue.StartsWith(ARGSTART)) {
     137        } else if(tokens.Peek().StringValue.StartsWith(ARGSTART)) {
    132138          tree = ParseArgument(tokens);
    133         } else if (tokens.Peek().StringValue.StartsWith(INVOKESTART)) {
     139        } else if(tokens.Peek().StringValue.StartsWith(INVOKESTART)) {
    134140          tree = ParseInvoke(tokens);
    135           while (!tokens.Peek().Equals(Token.RPAR)) {
     141          while(!tokens.Peek().Equals(Token.RPAR)) {
    136142            tree.AddSubtree(ParseSexp(tokens));
    137143          }
     144        } else if(tokens.Peek().StringValue.StartsWith("FACTOR")) {
     145          tree = ParseFactor(tokens);
     146        } else if(tokens.Peek().StringValue.StartsWith("BINARYFACTOR")) {
     147          tree = ParseBinaryFactor(tokens);
    138148        } else {
    139149          Token curToken = tokens.Dequeue();
    140150          tree = CreateTree(curToken);
    141           while (!tokens.Peek().Equals(Token.RPAR)) {
     151          while(!tokens.Peek().Equals(Token.RPAR)) {
    142152            tree.AddSubtree(ParseSexp(tokens));
    143153          }
     
    145155        Expect(Token.RPAR, tokens);
    146156        return tree;
    147       } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     157      } else if(tokens.Peek().Symbol == TokenSymbol.NUMBER) {
    148158        ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
    149159        t.Value = tokens.Dequeue().DoubleValue;
     
    201211    }
    202212
     213    private ISymbolicExpressionTreeNode ParseFactor(Queue<Token> tokens) {
     214      Token tok = tokens.Dequeue();
     215      Debug.Assert(tok.StringValue == "FACTOR");
     216      FactorVariableTreeNode t = (FactorVariableTreeNode)(new FactorVariable()).CreateTreeNode(); // create a new symbol each time on purpose
     217      var varNameTok = tokens.Dequeue();
     218      Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
     219      t.VariableName = varNameTok.StringValue;
     220
     221      var weights = new List<double>();
     222      while(tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     223        weights.Add(tokens.Dequeue().DoubleValue);
     224      }
     225
     226      t.Weights = weights.ToArray();
     227
     228      // create a set of (virtual) values to match the number of weights
     229      t.Symbol.VariableNames = new string[] { t.VariableName };
     230      t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "x" + i).ToList()) };
     231      return t;
     232    }
     233
     234    private ISymbolicExpressionTreeNode ParseBinaryFactor(Queue<Token> tokens) {
     235      Token tok = tokens.Dequeue();
     236      Debug.Assert(tok.StringValue == "BINFACTOR");
     237      var t = (BinaryFactorVariableTreeNode)binFactorVar.CreateTreeNode();
     238      var varNameTok = tokens.Dequeue();
     239      Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
     240      t.VariableName = varNameTok.StringValue;
     241
     242      var varValTok = tokens.Dequeue();
     243      Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
     244      t.VariableValue = varNameTok.StringValue;
     245
     246      var weightTok = tokens.Dequeue();
     247      Debug.Assert(tok.Symbol == TokenSymbol.NUMBER);
     248      t.Weight = weightTok.DoubleValue;
     249
     250      return t;
     251    }
     252
     253
    203254    private ISymbolicExpressionTreeNode ParseLaggedVariable(Queue<Token> tokens) {
    204255      Token varTok = tokens.Dequeue();
     
    212263
    213264    private ISymbolicExpressionTreeNode CreateTree(Token token) {
    214       if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
     265      if(token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
    215266      return knownSymbols[token.StringValue].CreateTreeNode();
    216267    }
     
    218269    private void Expect(Token token, Queue<Token> tokens) {
    219270      Token cur = tokens.Dequeue();
    220       if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
     271      if(!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
    221272    }
    222273  }
Note: See TracChangeset for help on using the changeset viewer.