Changeset 17606


Ignore:
Timestamp:
06/17/20 16:22:18 (4 weeks ago)
Author:
pfleck
Message:

#3040

  • Extended importer (vectorvariable, vec-aggregations, ...).
  • Started adding unit test for vector simplifications.
Location:
branches/3040_VectorBasedGP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r17180 r17606  
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2727
     28using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
     29
    2830namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2931  public class SymbolicExpressionImporter {
    3032    private const string VARSTART = "VAR";
    3133    private const string LAGGEDVARSTART = "LAGVARIABLE";
     34    private const string VECVARSTART = "VEC";
    3235    private const string INTEGRALSTART = "INTEG";
    3336    private const string DEFUNSTART = "DEFUN";
     
    7073        {"EXPINT", new ExponentialIntegralEi()},
    7174        {"AQ", new AnalyticQuotient() },
    72         {"MEAN", new Average()},
     75        //{"MEAN", new Average()},
    7376        {"IF", new IfThenElse()},
    7477        {">", new GreaterThan()},
     
    8285        {"MAIN", new StartSymbol()},
    8386        {"FACTOR", new FactorVariable() },
    84         {"BINFACTOR", new BinaryFactorVariable()}
     87        {"BINFACTOR", new BinaryFactorVariable()},
     88        {"SUM", new Sum() },
     89        {"MEAN", new Mean()},
     90        {"LENGTH", new Length()},
     91        {"STDEV", new StandardDeviation()},
     92        {"VAR", new Variance()}
    8593      };
    8694
    8795    Constant constant = new Constant();
    88     Variable variable = new Variable();
     96    Variable variable = new Variable() { VariableDataType = typeof(double) };
    8997    LaggedVariable laggedVariable = new LaggedVariable();
     98    Variable vectorVariable = new Variable() { VariableDataType = typeof(DoubleVector) };
    9099    Defun defun = new Defun();
    91100    TimeLag timeLag = new TimeLag();
     
    129138        } else if (tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) {
    130139          tree = ParseLaggedVariable(tokens);
     140        } else if (tokens.Peek().StringValue.StartsWith(VECVARSTART)) {
     141          tree = ParseVectorVariable(tokens);
    131142        } else if (tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) {
    132143          tree = ParseTimeLag(tokens);
     
    216227    }
    217228
     229    private ISymbolicExpressionTreeNode ParseVectorVariable(Queue<Token> tokens) {
     230      Token varTok = tokens.Dequeue();
     231      Debug.Assert(varTok.StringValue == "VECTORVARIABLE");
     232      VariableTreeNode t = (VariableTreeNode)vectorVariable.CreateTreeNode();
     233      t.Weight = tokens.Dequeue().DoubleValue;
     234      t.VariableName = tokens.Dequeue().StringValue;
     235      return t;
     236    }
     237
    218238    private ISymbolicExpressionTreeNode ParseFactor(Queue<Token> tokens) {
    219239      Token tok = tokens.Dequeue();
  • branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs

    r17180 r17606  
    193193
    194194      AssertEqualAfterSimplification("(+ 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val 4.0 ) 3.0))"); // not allowed
    195       AssertEqualAfterSimplification("(- 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val -4.0 ) 3.0)"); 
     195      AssertEqualAfterSimplification("(- 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val -4.0 ) 3.0)");
    196196      AssertEqualAfterSimplification("(+ (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) 3.0)");  // not allowed
    197       AssertEqualAfterSimplification("(- (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) -3.0)"); 
     197      AssertEqualAfterSimplification("(- (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) -3.0)");
    198198      AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )");
    199199      AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )");
     
    202202
    203203      // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
    204       AssertEqualAfterSimplification("(exp (binfactor a val 3.0))", 
     204      AssertEqualAfterSimplification("(exp (binfactor a val 3.0))",
    205205        string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Exp(3.0) - 1)
    206         ); 
     206        );
    207207      AssertEqualAfterSimplification("(sqrt (binfactor a val 16.0))", "(binfactor a val 4.0))"); // sqrt(0) = 0
    208208      AssertEqualAfterSimplification("(sqr (binfactor a val 3.0))", "(binfactor a val 9.0))"); // 0*0 = 0
     
    212212      AssertEqualAfterSimplification("(sin (binfactor a val 2.0) )",
    213213        string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Sin(2.0))); // sin(0) = 0
    214       AssertEqualAfterSimplification("(cos (binfactor a val 2.0) )", 
     214      AssertEqualAfterSimplification("(cos (binfactor a val 2.0) )",
    215215        string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Cos(2.0) - 1)); // cos(0) = 1
    216216      AssertEqualAfterSimplification("(tan (binfactor a val 2.0) )",
     
    269269
    270270      #endregion
     271
     272      #region vectors
     273      AssertEqualAfterVectorSimplification(
     274        "(sum (vectorvariable 2.0 a))",
     275        "(* (sum (vectorvariable 1.0 a)) 2.0)");
     276      AssertEqualAfterVectorSimplification(
     277        "(sum (* 2.0 (vectorvariable 1.0 a)))",
     278        "(* (sum (vectorvariable 1.0 a)) 2.0)");
     279      AssertEqualAfterVectorSimplification(
     280        "(sum (+ 2.0 (vectorvariable 1.0 a)))",
     281        "(+ (* (length (vectorvariable 1.0 a)) 2.0)(sum (vectorvariable 1.0 a)) )");
     282      #endregion
    271283    }
    272284
     
    278290      var expectedTree = importer.Import(expected);
    279291      Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    280 
     292    }
     293
     294    private void AssertEqualAfterVectorSimplification(string original, string expected) {
     295      var formatter = new SymbolicExpressionTreeStringFormatter();
     296      var importer = new SymbolicExpressionImporter();
     297      var simplifier = new VectorTreeSimplifier(new SymbolicDataAnalysisExpressionTreeVectorInterpreter());
     298      var actualTree = simplifier.Simplify(importer.Import(original));
     299      var expectedTree = importer.Import(expected);
     300      Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    281301    }
    282302  }
  • branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r17400 r17606  
    598598    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\OnlineCalculatorPerformanceTest.cs" />
    599599    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\StatisticCalculatorsTest.cs" />
    600     <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\ConstantOptimizationWithVectorsTest.cs" />
    601600    <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\DeriveTest.cs" />
    602601    <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\InfixExpressionParserTest.cs" />
Note: See TracChangeset for help on using the changeset viewer.