Changeset 17606 for branches/3040_VectorBasedGP
- Timestamp:
- 06/17/20 16:22:18 (5 years ago)
- 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 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 27 28 using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>; 29 28 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 29 31 public class SymbolicExpressionImporter { 30 32 private const string VARSTART = "VAR"; 31 33 private const string LAGGEDVARSTART = "LAGVARIABLE"; 34 private const string VECVARSTART = "VEC"; 32 35 private const string INTEGRALSTART = "INTEG"; 33 36 private const string DEFUNSTART = "DEFUN"; … … 70 73 {"EXPINT", new ExponentialIntegralEi()}, 71 74 {"AQ", new AnalyticQuotient() }, 72 {"MEAN", new Average()},75 //{"MEAN", new Average()}, 73 76 {"IF", new IfThenElse()}, 74 77 {">", new GreaterThan()}, … … 82 85 {"MAIN", new StartSymbol()}, 83 86 {"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()} 85 93 }; 86 94 87 95 Constant constant = new Constant(); 88 Variable variable = new Variable() ;96 Variable variable = new Variable() { VariableDataType = typeof(double) }; 89 97 LaggedVariable laggedVariable = new LaggedVariable(); 98 Variable vectorVariable = new Variable() { VariableDataType = typeof(DoubleVector) }; 90 99 Defun defun = new Defun(); 91 100 TimeLag timeLag = new TimeLag(); … … 129 138 } else if (tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) { 130 139 tree = ParseLaggedVariable(tokens); 140 } else if (tokens.Peek().StringValue.StartsWith(VECVARSTART)) { 141 tree = ParseVectorVariable(tokens); 131 142 } else if (tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) { 132 143 tree = ParseTimeLag(tokens); … … 216 227 } 217 228 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 218 238 private ISymbolicExpressionTreeNode ParseFactor(Queue<Token> tokens) { 219 239 Token tok = tokens.Dequeue(); -
branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs
r17180 r17606 193 193 194 194 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)"); 196 196 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)"); 198 198 AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )"); 199 199 AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )"); … … 202 202 203 203 // 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))", 205 205 string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Exp(3.0) - 1) 206 ); 206 ); 207 207 AssertEqualAfterSimplification("(sqrt (binfactor a val 16.0))", "(binfactor a val 4.0))"); // sqrt(0) = 0 208 208 AssertEqualAfterSimplification("(sqr (binfactor a val 3.0))", "(binfactor a val 9.0))"); // 0*0 = 0 … … 212 212 AssertEqualAfterSimplification("(sin (binfactor a val 2.0) )", 213 213 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) )", 215 215 string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Cos(2.0) - 1)); // cos(0) = 1 216 216 AssertEqualAfterSimplification("(tan (binfactor a val 2.0) )", … … 269 269 270 270 #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 271 283 } 272 284 … … 278 290 var expectedTree = importer.Import(expected); 279 291 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)); 281 301 } 282 302 } -
branches/3040_VectorBasedGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r17400 r17606 598 598 <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\OnlineCalculatorPerformanceTest.cs" /> 599 599 <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\StatisticCalculatorsTest.cs" /> 600 <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\ConstantOptimizationWithVectorsTest.cs" />601 600 <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\DeriveTest.cs" /> 602 601 <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\InfixExpressionParserTest.cs" />
Note: See TracChangeset
for help on using the changeset viewer.