Changeset 17622
- Timestamp:
- 06/22/20 13:48:42 (4 years ago)
- 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 29 29 using HEAL.Attic; 30 30 31 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>; 32 31 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 34 /// <summary> … … 58 60 /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param> 59 61 /// <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") { 61 63 // skip root and start symbols 62 64 StringBuilder strBuilder = new StringBuilder(); … … 141 143 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 142 144 } 145 if (varNode.DataType == typeof(DoubleVector)) 146 strBuilder.Append("{}"); 143 147 if (!varNode.Weight.IsAlmost(1.0)) { 144 148 strBuilder.Append(")"); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r17180 r17622 28 28 using HeuristicLab.Common; 29 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>; 30 32 31 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 50 52 /// | number 51 53 /// ArgList = Expr { ',' Expr } 52 /// VarExpr = varId OptFactorPart54 /// VarExpr = varId ( OptFactorPart | OptVectorPart ) 53 55 /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ] 56 /// OptVectorPart = [ '{' [ number {',' number } ] '}' ] 54 57 /// varId = ident | ' ident ' | " ident " 55 58 /// varVal = ident | ' ident ' | " ident " … … 57 60 /// </summary> 58 61 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 }; 60 63 private class Token { 61 64 internal double doubleVal; … … 86 89 private BinaryFactorVariable binaryFactorVar = new BinaryFactorVariable(); 87 90 private FactorVariable factorVar = new FactorVariable(); 91 private Variable vectorVariable = new Variable() { VariableDataType = typeof(DoubleVector) }; 88 92 89 93 private ProgramRootSymbol programRootSymbol = new ProgramRootSymbol(); … … 255 259 } else if (str[pos] == '{') { 256 260 pos++; 257 yield return new Token { TokenType = TokenType.Left Par, strVal = "{" };261 yield return new Token { TokenType = TokenType.LeftBrace, strVal = "{" }; 258 262 } else if (str[pos] == '}') { 259 263 pos++; 260 yield return new Token { TokenType = TokenType.Right Par, strVal = "}" };264 yield return new Token { TokenType = TokenType.RightBrace, strVal = "}" }; 261 265 } else if (str[pos] == '=') { 262 266 pos++; … … 403 407 /// | number 404 408 /// ArgList = Expr { ',' Expr } 405 /// VarExpr = varId OptFactorPart409 /// VarExpr = varId ( OptFactorPart | OptVectorPart ) 406 410 /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ] 411 /// OptVectorPart = [ '{' [ number {',' number } ] '}' ] 407 412 /// varId = ident | ' ident ' | " ident " 408 413 /// varVal = ident | ' ident ' | " ident " … … 516 521 factorVariableNode.Weights = weights.ToArray(); 517 522 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; 518 544 } else { 519 545 // variable -
branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r17180 r17622 121 121 Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla"))); 122 122 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"))); 123 127 } 124 128 } -
branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r17606 r17622 12 12 <RootNamespace>HeuristicLab.Tests</RootNamespace> 13 13 <AssemblyName>HeuristicLab.Tests</AssemblyName> 14 <TargetFrameworkVersion>v4. 6.1</TargetFrameworkVersion>14 <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> 15 15 <FileAlignment>512</FileAlignment> 16 16 <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: See TracChangeset
for help on using the changeset viewer.