Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/10/16 20:10:25 (8 years ago)
Author:
gkronber
Message:

#2650:

  • extended non-linear regression to work with factors
  • fixed bugs in constants optimizer and tree interpreter
  • improved simplification of factor variables
  • added support for factors to ERC view
  • added support for factors to solution comparison view
  • activated view for all factors
File:
1 edited

Legend:

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

    r14026 r14251  
    3232  /// Parses mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3)
    3333  /// Identifier format (functions or variables): '_' | letter { '_' | letter | digit }
    34   /// Variables names can be set under quotes "" or '' because variable names might contain spaces.
     34  /// Variables names and variable values can be set under quotes "" or '' because variable names might contain spaces.
     35  ///   Variable = ident | " ident " | ' ident '
    3536  /// It is also possible to use functions e.g. log("x1") or real-valued constants e.g. 3.1415 .
    3637  /// Variable names are case sensitive. Function names are not case sensitive.
     38  ///
     39  ///
     40  /// S       = Expr EOF
     41  /// Expr    = ['-' | '+'] Term { '+' Term | '-' Term }
     42  /// Term    = Fact { '*' Fact | '/' Fact }
     43  /// Fact    = '(' Expr ')' | funcId '(' Expr ')' | VarExpr | number
     44  /// VarExpr = varId [ '=' varVal]
     45  /// varId   =  ident | ' ident ' | " ident "
     46  /// varVal  =  ident | ' ident ' | " ident "
     47  /// ident   =  '_' | letter { '_' | letter | digit }
    3748  /// </summary>
    3849  public sealed class InfixExpressionParser {
    39     private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, End, NA };
     50    private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Eq, End, NA };
    4051    private class Token {
    4152      internal double doubleVal;
     
    6475    private Constant constant = new Constant();
    6576    private Variable variable = new Variable();
     77    private BinaryFactorVariable binaryFactorVar = new BinaryFactorVariable();
    6678
    6779    private ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
     
    143155          pos++;
    144156          while (pos < str.Length && !char.IsWhiteSpace(str[pos])
    145             && (str[pos] != '+' || str[pos-1] == 'e' || str[pos-1] == 'E')     // continue reading exponents
     157            && (str[pos] != '+' || str[pos - 1] == 'e' || str[pos - 1] == 'E')     // continue reading exponents
    146158            && (str[pos] != '-' || str[pos - 1] == 'e' || str[pos - 1] == 'E')
    147             && str[pos] != '*'           
     159            && str[pos] != '*'
    148160            && str[pos] != '/'
    149161            && str[pos] != ')') {
     
    211223          pos++;
    212224          yield return new Token { TokenType = TokenType.RightPar, strVal = ")" };
    213         }
    214       }
    215     }
    216 
    217     // S = Expr EOF
    218     // Expr = ['-' | '+'] Term { '+' Term | '-' Term }
    219     // Term = Fact { '*' Fact | '/' Fact }
    220     // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number
     225        } else if (str[pos] == '=') {
     226          pos++;
     227          yield return new Token { TokenType = TokenType.Eq, strVal = "=" };
     228        }
     229      }
     230    }
     231
    221232    private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) {
    222233      var expr = ParseExpr(tokens);
     
    326337    }
    327338
    328     // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number
     339    // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId [ = valId ] | number
    329340    private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) {
    330341      var next = tokens.Peek();
     
    355366        } else {
    356367          // variable
    357           var varNode = (VariableTreeNode)variable.CreateTreeNode();
    358           varNode.Weight = 1.0;
    359           varNode.VariableName = idTok.strVal;
    360           return varNode;
     368          if (tokens.Peek().TokenType == TokenType.Eq) {
     369            // binary factor
     370            tokens.Dequeue(); // skip Eq
     371            var valTok = tokens.Dequeue();
     372            if (valTok.TokenType != TokenType.Identifier) throw new ArgumentException("expected identifier");
     373            var binFactorNode = (BinaryFactorVariableTreeNode)binaryFactorVar.CreateTreeNode();
     374            binFactorNode.Weight = 1.0;
     375            binFactorNode.VariableName = idTok.strVal;
     376            binFactorNode.VariableValue = valTok.strVal;
     377            return binFactorNode;
     378          } else {
     379            // variable
     380            var varNode = (VariableTreeNode)variable.CreateTreeNode();
     381            varNode.Weight = 1.0;
     382            varNode.VariableName = idTok.strVal;
     383            return varNode;
     384          }
    361385        }
    362386      } else if (next.TokenType == TokenType.Number) {
     
    369393      }
    370394    }
     395
    371396  }
    372397}
Note: See TracChangeset for help on using the changeset viewer.