Changeset 17622


Ignore:
Timestamp:
06/22/20 13:48:42 (3 months ago)
Author:
pfleck
Message:

#3040 Added vector variables to infix parser/formatter.

Location:
branches/3040_VectorBasedGP
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r17180 r17622  
    2929using HEAL.Attic;
    3030
     31using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
     32
    3133namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3234  /// <summary>
     
    5860    /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
    5961    /// <returns>Infix expression</returns>
    60     public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") {
     62    public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString = "G") {
    6163      // skip root and start symbols
    6264      StringBuilder strBuilder = new StringBuilder();
     
    141143            strBuilder.AppendFormat("'{0}'", varNode.VariableName);
    142144          }
     145          if (varNode.DataType == typeof(DoubleVector))
     146            strBuilder.Append("{}");
    143147          if (!varNode.Weight.IsAlmost(1.0)) {
    144148            strBuilder.Append(")");
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r17180 r17622  
    2828using HeuristicLab.Common;
    2929using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     30
     31using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
    3032
    3133namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    5052  ///                 | number
    5153  /// ArgList       = Expr { ',' Expr }
    52   /// VarExpr       = varId OptFactorPart
     54  /// VarExpr       = varId ( OptFactorPart | OptVectorPart )
    5355  /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ]
     56  /// OptVectorPart = [ '{' [ number {',' number } ] '}' ]
    5457  /// varId         =  ident | ' ident ' | " ident "
    5558  /// varVal        =  ident | ' ident ' | " ident "
     
    5760  /// </summary>
    5861  public sealed class InfixExpressionParser {
    59     private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, LeftBracket, RightBracket, Comma, Eq, End, NA };
     62    private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, LeftBracket, RightBracket, Comma, Eq, End, NA, LeftBrace, RightBrace };
    6063    private class Token {
    6164      internal double doubleVal;
     
    8689    private BinaryFactorVariable binaryFactorVar = new BinaryFactorVariable();
    8790    private FactorVariable factorVar = new FactorVariable();
     91    private Variable vectorVariable = new Variable() { VariableDataType = typeof(DoubleVector) };
    8892
    8993    private ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
     
    255259        } else if (str[pos] == '{') {
    256260          pos++;
    257           yield return new Token { TokenType = TokenType.LeftPar, strVal = "{" };
     261          yield return new Token { TokenType = TokenType.LeftBrace, strVal = "{" };
    258262        } else if (str[pos] == '}') {
    259263          pos++;
    260           yield return new Token { TokenType = TokenType.RightPar, strVal = "}" };
     264          yield return new Token { TokenType = TokenType.RightBrace, strVal = "}" };
    261265        } else if (str[pos] == '=') {
    262266          pos++;
     
    403407    ///                 | number
    404408    /// ArgList       = Expr { ',' Expr }
    405     /// VarExpr       = varId OptFactorPart
     409    /// VarExpr       = varId ( OptFactorPart | OptVectorPart )
    406410    /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ]
     411    /// OptVectorPart = [ '{' [ number {',' number } ] '}' ]
    407412    /// varId         =  ident | ' ident ' | " ident "
    408413    /// varVal        =  ident | ' ident ' | " ident "
     
    516521            factorVariableNode.Weights = weights.ToArray();
    517522            return factorVariableNode;
     523          } else if (tokens.Peek().TokenType == TokenType.LeftBrace) {
     524            // vector variable
     525            var vecVarNode = (VariableTreeNodeBase)vectorVariable.CreateTreeNode();
     526            vecVarNode.VariableName = idTok.strVal;
     527            vecVarNode.Weight = 1.0;
     528            tokens.Dequeue(); // skip {
     529            // optional vector length (currently unused)
     530            var lengths = new List<int>();
     531            if (tokens.Peek().TokenType == TokenType.Number) {
     532              var lengthToken = tokens.Dequeue();
     533              lengths.Add((int)lengthToken.doubleVal);
     534              while (tokens.Peek().TokenType == TokenType.Comma) {
     535                tokens.Dequeue(); // skip comma
     536                lengthToken = tokens.Dequeue();
     537                if (lengthToken.TokenType != TokenType.Number) throw new ArgumentException("number expected");
     538                lengths.Add((int)lengthToken.doubleVal);
     539              }
     540            }
     541            var rightBraceToken = tokens.Dequeue();
     542            if (rightBraceToken.TokenType != TokenType.RightBrace) throw new ArgumentException("closing brace } expected");
     543            return vecVarNode;
    518544          } else {
    519545            // variable
  • branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs

    r17180 r17622  
    121121      Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla")));
    122122      Console.WriteLine(formatter.Format(parser.Parse("+1.0 * \"x\" = bla + y = \"bla2\"")));
     123
     124      Console.WriteLine(formatter.Format(parser.Parse("x { } * y")));
     125      Console.WriteLine(formatter.Format(parser.Parse("x { 2 } * y")));
     126      Console.WriteLine(formatter.Format(parser.Parse("x { 2, 3 } * y")));
    123127    }
    124128  }
  • branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r17606 r17622  
    1212    <RootNamespace>HeuristicLab.Tests</RootNamespace>
    1313    <AssemblyName>HeuristicLab.Tests</AssemblyName>
    14     <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
     14    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    1515    <FileAlignment>512</FileAlignment>
    1616    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: See TracChangeset for help on using the changeset viewer.