- Timestamp:
- 05/10/10 08:15:00 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3
- Files:
-
- 4 added
- 1 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SimpleArithmeticExpressionInterpreter.cs
r3545 r3733 115 115 s -= Evaluate(); 116 116 } 117 if (currentInstr.nArguments == 1) s = -s; 117 118 return s; 118 119 } … … 129 130 p /= Evaluate(); 130 131 } 132 if (currentInstr.nArguments == 1) p = 1.0 / p; 131 133 return p; 132 134 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SimpleArithmeticExpressionInterpreterTest.cs
r3728 r3733 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using HeuristicLab.GP.StructureIdentification;23 using Microsoft.VisualStudio.TestTools.UnitTesting;24 using HeuristicLab.GP.Interfaces;25 22 using System.IO; 26 using HeuristicLab.DataAnalysis;27 23 using System; 28 24 using HeuristicLab.Random; 29 25 using System.Collections.Generic; 30 using HeuristicLab.GP.Operators;31 26 using System.Diagnostics; 32 namespace HeuristicLab.GP.Test { 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using HeuristicLab.Problems.DataAnalysis.Symbolic; 29 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using System.Linq; 31 namespace HeuristicLab.Problems.DataAnalysis.Tests { 33 32 34 33 35 34 /// <summary> 36 ///This is a test class for HL3TreeEvaluatorTestand is intended37 ///to contain all HL3TreeEvaluatorTestUnit Tests35 ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended 36 ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests 38 37 ///</summary> 39 38 [TestClass()] 40 public class HL3TreeEvaluatorTest {39 public class SimpleArithmeticExpressionInterpreterTest { 41 40 private const int N = 1000; 42 41 private const int Rows = 1000; 43 42 private const int Columns = 50; 44 private static IFunctionTree[] randomTrees;43 private static SymbolicExpressionTree[] randomTrees; 45 44 private static Dataset dataset; 46 45 private static MersenneTwister twister; … … 64 63 twister = new MersenneTwister(); 65 64 dataset = Util.CreateRandomDataset(twister, Rows, Columns); 66 randomTrees = Util.CreateRandomTrees(twister, dataset, N, 1, 100); 65 var grammar = new GlobalSymbolicExpressionGrammar(new ArithmeticExpressionGrammar()); 66 grammar.MaxFunctionArguments = 0; 67 grammar.MaxFunctionDefinitions = 0; 68 grammar.MinFunctionArguments = 0; 69 grammar.MinFunctionDefinitions = 0; 70 randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 67 71 } 68 72 69 73 70 74 [TestMethod()] 71 public void PerformanceTest() {75 public void SimpleArithmeticExpressionInterpreterPerformanceTest() { 72 76 double[] estimation = new double[Rows]; 73 foreach ( IFunctionTree tree in randomTrees) {77 foreach (SymbolicExpressionTree tree in randomTrees) { 74 78 Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); 75 79 } 76 HL3TreeEvaluator hl3TreeEvaluator = new HL3TreeEvaluator();77 Util.EvaluateTrees(randomTrees, hl3TreeEvaluator, dataset, 10);80 SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter(); 81 Util.EvaluateTrees(randomTrees, interpreter, dataset, 10); 78 82 } 79 83 … … 83 87 ///</summary> 84 88 [TestMethod()] 85 [DeploymentItem("HeuristicLab.GP.StructureIdentification-3.3.dll")] 86 public void HL3TreeEvaluatorEvaluateTest() { 89 public void SimpleArithmeticExpressionInterpreterEvaluateTest() { 87 90 88 Dataset ds = new Dataset(new double[,] {91 Dataset ds = new Dataset(new string[] { "y", "a", "b" }, new double[,] { 89 92 { 1.0, 1.0, 1.0 }, 90 93 { 2.0, 2.0, 2.0 }, … … 92 95 }); 93 96 94 ds.SetVariableName(0, "y"); 95 ds.SetVariableName(1, "a"); 96 ds.SetVariableName(2, "b"); 97 98 HL3TreeEvaluator evaluator = new HL3TreeEvaluator(); 97 SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter(); 99 98 100 99 // constants 101 Evaluate( evaluator, ds, "(+ 1,5 3,5)", 0, 5.0);100 Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0); 102 101 103 102 // variables 104 Evaluate(evaluator, ds, "(variable 2,0 a 0)", 0, 2.0); 105 Evaluate(evaluator, ds, "(variable 2,0 a 0)", 1, 4.0); 106 Evaluate(evaluator, ds, "(variable 2,0 a -1)", 1, 2.0); 107 Evaluate(evaluator, ds, "(variable 2,0 a -2)", 2, 2.0); 103 Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0); 104 Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0); 108 105 109 // differentials110 Evaluate(evaluator, ds, "(differential 2,0 a 0)", 1, 2.0);111 Evaluate(evaluator, ds, "(differential 2,0 a 0)", 2, -2.0);112 Evaluate(evaluator, ds, "(differential 2,0 a -1)", 2, 2.0);113 Evaluate(evaluator, ds, "(differential 2,0 a -2)", 3, 2.0);114 106 115 107 // addition 116 Evaluate( evaluator, ds, "(+ (variable 2,0 a 0))", 1, 4.0);117 Evaluate( evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 0, 5.0);118 Evaluate( evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 1, 10.0);119 Evaluate( evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 2, 8.0);120 Evaluate( evaluator, ds, "(+ 8,0 2,0 2,0)", 0, 12.0);108 Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0); 109 Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0); 110 Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0); 111 Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0); 112 Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0); 121 113 122 114 // subtraction 123 Evaluate( evaluator, ds, "(- (variable 2,0 a 0))", 1, -4.0);124 Evaluate( evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 0, -1.0);125 Evaluate( evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 1, -2.0);126 Evaluate( evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 2, -4.0);127 Evaluate( evaluator, ds, "(- 8,0 2,0 2,0)", 0, 4.0);115 Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0); 116 Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0); 117 Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0); 118 Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0); 119 Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0); 128 120 129 121 // multiplication 130 Evaluate( evaluator, ds, "(* (variable 2,0 a 0))", 0, 2.0);131 Evaluate( evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 0, 6.0);132 Evaluate( evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 1, 24.0);133 Evaluate( evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 2, 12.0);134 Evaluate( evaluator, ds, "(* 8,0 2,0 2,0)", 0, 32.0);122 Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0); 123 Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0); 124 Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0); 125 Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0); 126 Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0); 135 127 136 128 // division 137 Evaluate( evaluator, ds, "(/ (variable 2,0 a 0))", 1, 1.0 / 4.0);138 Evaluate( evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 0, 1.0);139 Evaluate( evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 1, 2.0);140 Evaluate( evaluator, ds, "(/ (variable 3,0 b 0) 2,0)", 2, 3.0);141 Evaluate( evaluator, ds, "(/ 8,0 2,0 2,0)", 0, 2.0);129 Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0); 130 Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0); 131 Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0); 132 Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0); 133 Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0); 142 134 143 // boolean values semantic: false = x <= 0 , true x > 0 144 // equ 145 Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 2,0)", 0, 1.0); 146 Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 1,0)", 0, -1.0); 147 Evaluate(evaluator, ds, "(equ (sqrt -1,0) (log -1,0))", 0, -1.0); // (equ nan nan) should be false 148 149 150 // gt 151 Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 2,0)", 0, -1.0); 152 Evaluate(evaluator, ds, "(> 2,0 (variable 2,0 a 0))", 0, -1.0); 153 Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 1,9)", 0, 1.0); 154 Evaluate(evaluator, ds, "(> 1,9 (variable 2,0 a 0))", 0, -1.0); 155 Evaluate(evaluator, ds, "(> (sqrt -1,0) (log -1,0))", 0, -1.0); // (> nan nan) should be false 156 157 // lt 158 Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 2,0)", 0, -1.0); 159 Evaluate(evaluator, ds, "(< 2,0 (variable 2,0 a 0))", 0, -1.0); 160 Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 1,9)", 0, -1.0); 161 Evaluate(evaluator, ds, "(< 1,9 (variable 2,0 a 0))", 0, 1.0); 162 Evaluate(evaluator, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false 163 164 // If 165 Evaluate(evaluator, ds, "(if -10 2,0 3,0)", 0, 3.0); 166 Evaluate(evaluator, ds, "(if -1,0 2,0 3,0)", 0, 3.0); 167 Evaluate(evaluator, ds, "(if 0,0 2,0 3,0)", 0, 3.0); 168 Evaluate(evaluator, ds, "(if 1,0 2,0 3,0)", 0, 2.0); 169 Evaluate(evaluator, ds, "(if 10 2,0 3,0)", 0, 2.0); 170 Evaluate(evaluator, ds, "(if (sqrt -1,0) 2,0 3,0)", 0, 3.0); // if(nan) should return the else branch 171 172 // signum 173 Evaluate(evaluator, ds, "(sign -1,0)", 0, -1.0); 174 Evaluate(evaluator, ds, "(sign -2,0)", 0, -1.0); 175 Evaluate(evaluator, ds, "(sign 1,0)", 0, 1.0); 176 Evaluate(evaluator, ds, "(sign 2,0)", 0, 1.0); 177 Evaluate(evaluator, ds, "(sign 0,0)", 0, 0.0); 178 179 // NOT 180 Evaluate(evaluator, ds, "(not -1,0)", 0, 1.0); 181 Evaluate(evaluator, ds, "(not -2,0)", 0, 2.0); 182 Evaluate(evaluator, ds, "(not 1,0)", 0, -1.0); 183 Evaluate(evaluator, ds, "(not 2,0)", 0, -2.0); 184 Evaluate(evaluator, ds, "(not 0,0)", 0, 0.0); 185 186 // AND 187 Evaluate(evaluator, ds, "(and -1,0 -2,0)", 0, -1.0); 188 Evaluate(evaluator, ds, "(and -1,0 2,0)", 0, -1.0); 189 Evaluate(evaluator, ds, "(and 1,0 -2,0)", 0, -1.0); 190 Evaluate(evaluator, ds, "(and 1,0 0,0)", 0, -1.0); 191 Evaluate(evaluator, ds, "(and 0,0 0,0)", 0, -1.0); 192 Evaluate(evaluator, ds, "(and 1,0 2,0)", 0, 1.0); 193 Evaluate(evaluator, ds, "(and 1,0 2,0 3,0)", 0, 1.0); 194 Evaluate(evaluator, ds, "(and 1,0 -2,0 3,0)", 0, -1.0); 195 196 // OR 197 Evaluate(evaluator, ds, "(or -1,0 -2,0)", 0, -1.0); 198 Evaluate(evaluator, ds, "(or -1,0 2,0)", 0, 1.0); 199 Evaluate(evaluator, ds, "(or 1,0 -2,0)", 0, 1.0); 200 Evaluate(evaluator, ds, "(or 1,0 2,0)", 0, 1.0); 201 Evaluate(evaluator, ds, "(or 0,0 0,0)", 0, -1.0); 202 Evaluate(evaluator, ds, "(or -1,0 -2,0 -3,0)", 0, -1.0); 203 Evaluate(evaluator, ds, "(or -1,0 -2,0 3,0)", 0, 1.0); 204 205 // XOR 206 Evaluate(evaluator, ds, "(xor -1,0 -2,0)", 0, -1.0); 207 Evaluate(evaluator, ds, "(xor -1,0 2,0)", 0, 1.0); 208 Evaluate(evaluator, ds, "(xor 1,0 -2,0)", 0, 1.0); 209 Evaluate(evaluator, ds, "(xor 1,0 2,0)", 0, -1.0); 210 Evaluate(evaluator, ds, "(xor 0,0 0,0)", 0, -1.0); 211 212 // sin, cos, tan 213 Evaluate(evaluator, ds, "(sin " + Math.PI + ")", 0, 0.0); 214 Evaluate(evaluator, ds, "(sin 0,0)", 0, 0.0); 215 Evaluate(evaluator, ds, "(cos " + Math.PI + ")", 0, -1.0); 216 Evaluate(evaluator, ds, "(cos 0,0)", 0, 1.0); 217 Evaluate(evaluator, ds, "(tan " + Math.PI + ")", 0, Math.Tan(Math.PI)); 218 Evaluate(evaluator, ds, "(tan 0,0)", 0, Math.Tan(Math.PI)); 219 220 221 // expt 222 Evaluate(evaluator, ds, "(expt 2,0 2,0)", 0, 4.0); 223 Evaluate(evaluator, ds, "(expt 2,0 3,0)", 0, 8.0); 224 Evaluate(evaluator, ds, "(expt 3,0 2,0)", 0, 9.0); 225 226 // exp, log 227 Evaluate(evaluator, ds, "(log (exp 7,0))", 0, Math.Log(Math.Exp(7))); 228 Evaluate(evaluator, ds, "(exp (log 7,0))", 0, Math.Exp(Math.Log(7))); 229 Evaluate(evaluator, ds, "(log -3,0)", 0, Math.Log(-3)); 230 231 // sqrt 232 Evaluate(evaluator, ds, "(sqrt 4,0)", 0, 2.0); 233 Evaluate(evaluator, ds, "(sqrt (sqrt 16,0))", 0, 2.0); 234 135 // TODO ADF 235 136 } 236 137 237 private void Evaluate( HL3TreeEvaluator evaluator, Dataset ds, string expr, int index, double expected) {138 private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) { 238 139 var importer = new SymbolicExpressionImporter(); 239 IFunctionTree tree = importer.Import(expr);140 SymbolicExpressionTree tree = importer.Import(expr); 240 141 241 evaluator.PrepareForEvaluation(ds, tree);142 double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First(); 242 143 243 double actual = evaluator.Evaluate(index);244 144 Assert.AreEqual(expected, actual, 1.0E-12, expr); 245 145 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicExpressionImporter.cs
r3728 r3733 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.GP.Interfaces;27 26 using System.IO; 28 27 using System.Diagnostics; 29 using HeuristicLab.GP.StructureIdentification; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 29 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 31 namespace HeuristicLab.GP.Test { 32 class SymbolicExpressionImporter { 33 private const string DIFFSTART = "dif"; 32 namespace HeuristicLab.Problems.DataAnalysis.Tests { 33 internal class SymbolicExpressionImporter { 34 34 private const string VARSTART = "var"; 35 private const string OPENPARAMSTART = "open-param"; 36 private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>() 35 private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>() 37 36 { 38 37 {"+", new Addition()}, 39 {"and", new And()},40 {"mean", new Average()},41 {"cos", new Cosinus()},42 38 {"/", new Division()}, 43 {"equ", new Equal()},44 {"exp", new Exponential()},45 {">", new GreaterThan()},46 {"if", new IfThenElse()},47 {"<", new LessThan()},48 {"log", new Logarithm()},49 39 {"*", new Multiplication()}, 50 {"not", new Not()},51 {"or", new Or()},52 {"expt", new Power()},53 {"sign", new Signum()},54 {"sin",new Sinus()},55 {"sqrt", new Sqrt()},56 40 {"-", new Subtraction()}, 57 {"tan", new Tangens()},58 {"xor", new Xor()},59 {"open-param", new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter()},60 {"open-+", new HeuristicLab.GP.StructureIdentification.Networks.OpenAddition()},61 {"open--", new HeuristicLab.GP.StructureIdentification.Networks.OpenSubtraction()},62 {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()},63 {"open-/", new HeuristicLab.GP.StructureIdentification.Networks.OpenDivision()},64 {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()},65 {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()},66 //{"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()},67 //{"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()},68 {"f1-+", new HeuristicLab.GP.StructureIdentification.Networks.AdditionF1()},69 {"f1--", new HeuristicLab.GP.StructureIdentification.Networks.SubtractionF1()},70 {"f1-/", new HeuristicLab.GP.StructureIdentification.Networks.DivisionF1()},71 {"f1-*", new HeuristicLab.GP.StructureIdentification.Networks.MultiplicationF1()},72 {"cycle", new HeuristicLab.GP.StructureIdentification.Networks.Cycle()},73 {"flip", new HeuristicLab.GP.StructureIdentification.Networks.Flip()},74 41 75 42 }; 76 43 Constant constant = new Constant(); 77 HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable(); 78 Differential differential = new Differential(); 79 HeuristicLab.GP.StructureIdentification.Networks.OpenParameter openParam = new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter(); 44 Variable variable = new Variable(); 45 ProgramRootSymbol programRootSymbol = new ProgramRootSymbol(); 46 StartSymbol startSymbol = new StartSymbol(); 47 80 48 public SymbolicExpressionImporter() { 81 49 } 82 50 83 internal IFunctionTree Import(string str) {51 internal SymbolicExpressionTree Import(string str) { 84 52 str = str.Replace("(", " ( ").Replace(")", " ) "); 85 return ParseSexp(new Queue<Token>(GetTokenStream(str))); 53 SymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode(); 54 SymbolicExpressionTreeNode start = startSymbol.CreateTreeNode(); 55 SymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str))); 56 root.AddSubTree(start); 57 start.AddSubTree(mainBranch); 58 return new SymbolicExpressionTree(root); 86 59 } 87 60 … … 94 67 } 95 68 96 private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {69 private SymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) { 97 70 if (tokens.Peek().Symbol == TokenSymbol.LPAR) { 98 IFunctionTree tree;71 SymbolicExpressionTreeNode tree; 99 72 Expect(Token.LPAR, tokens); 100 73 if (tokens.Peek().StringValue.StartsWith(VARSTART)) { 101 74 tree = ParseVariable(tokens); 102 } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) {103 tree = ParseDifferential(tokens);104 } else if (tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) {105 tree = ParseOpenParameter(tokens);106 75 } else { 107 76 Token curToken = tokens.Dequeue(); … … 114 83 return tree; 115 84 } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) { 116 Constant FunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();85 ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode(); 117 86 t.Value = tokens.Dequeue().DoubleValue; 118 87 return t; … … 120 89 } 121 90 122 private IFunctionTree ParseOpenParameter(Queue<Token> tokens) {123 Token tok = tokens.Dequeue();124 Debug.Assert( tok.StringValue == "open-param");125 HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode();91 private SymbolicExpressionTreeNode ParseVariable(Queue<Token> tokens) { 92 Token varTok = tokens.Dequeue(); 93 Debug.Assert(varTok.StringValue == "variable"); 94 VariableTreeNode t = (VariableTreeNode)variable.CreateTreeNode(); 126 95 t.Weight = tokens.Dequeue().DoubleValue; 127 96 t.VariableName = tokens.Dequeue().StringValue; 128 t.SampleOffset = (int)tokens.Dequeue().DoubleValue;129 97 return t; 130 98 } 131 99 132 private IFunctionTree ParseDifferential(Queue<Token> tokens) { 133 Token diffTok = tokens.Dequeue(); 134 Debug.Assert(diffTok.StringValue == "differential"); 135 VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode(); 136 t.Weight = tokens.Dequeue().DoubleValue; 137 t.VariableName = tokens.Dequeue().StringValue; 138 t.SampleOffset = (int)tokens.Dequeue().DoubleValue; 139 return t; 140 } 141 142 private IFunctionTree ParseVariable(Queue<Token> tokens) { 143 Token varTok = tokens.Dequeue(); 144 Debug.Assert(varTok.StringValue == "variable"); 145 VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode(); 146 t.Weight = tokens.Dequeue().DoubleValue; 147 t.VariableName = tokens.Dequeue().StringValue; 148 t.SampleOffset = (int)tokens.Dequeue().DoubleValue; 149 return t; 150 } 151 152 private IFunctionTree CreateTree(Token token) { 100 private SymbolicExpressionTreeNode CreateTree(Token token) { 153 101 if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue); 154 return known Functions[token.StringValue].GetTreeNode();102 return knownSymbols[token.StringValue].CreateTreeNode(); 155 103 } 156 104 157 105 private void Expect(Token token, Queue<Token> tokens) { 158 106 Token cur = tokens.Dequeue(); 159 if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);107 if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue); 160 108 } 161 109 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Token.cs
r3728 r3733 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 25 25 using System.Text; 26 26 using System.IO; 27 using HeuristicLab.GP;28 using HeuristicLab.GP.Interfaces;29 using HeuristicLab.GP.StructureIdentification;30 27 using System.Diagnostics; 31 28 using System.Globalization; 32 29 33 namespace HeuristicLab. GP.Test{34 publicenum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };35 publicclass Token {30 namespace HeuristicLab.Problems.DataAnalysis.Tests { 31 internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER }; 32 internal class Token { 36 33 public static readonly Token LPAR = Token.Parse("("); 37 34 public static readonly Token RPAR = Token.Parse(")"); -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Util.cs
r3728 r3733 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using HeuristicLab.GP.StructureIdentification;23 22 using Microsoft.VisualStudio.TestTools.UnitTesting; 24 using HeuristicLab.DataAnalysis;25 23 using System; 26 using HeuristicLab.GP.Interfaces;27 24 using HeuristicLab.Random; 28 using HeuristicLab.GP.Operators;29 25 using System.Collections.Generic; 30 26 using System.Text; 31 27 using System.Diagnostics; 32 namespace HeuristicLab.GP.Test { 33 public class Util { 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators; 31 using HeuristicLab.Problems.DataAnalysis.Symbolic; 32 using System.Linq; 33 namespace HeuristicLab.Problems.DataAnalysis.Tests { 34 internal class Util { 34 35 35 public static void InitTree( IFunctionTree tree, MersenneTwister twister, List<string> varNames) {36 foreach (var node in FunctionTreeIterator.IteratePostfix(tree)) {37 if (node is Variable FunctionTree) {38 var varNode = node as Variable FunctionTree;36 public static void InitTree(SymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) { 37 foreach (var node in tree.IterateNodesPostfix()) { 38 if (node is VariableTreeNode) { 39 var varNode = node as VariableTreeNode; 39 40 varNode.Weight = twister.NextDouble() * 20.0 - 10.0; 40 varNode.SampleOffset = 0;41 41 varNode.VariableName = varNames[twister.Next(varNames.Count)]; 42 } else if (node is Constant FunctionTree) {43 var constantNode = node as Constant FunctionTree;42 } else if (node is ConstantTreeNode) { 43 var constantNode = node as ConstantTreeNode; 44 44 constantNode.Value = twister.NextDouble() * 20.0 - 10.0; 45 45 } … … 47 47 } 48 48 49 public static FunctionLibrary CreateFunctionLibrary() {50 FunctionLibrary functionLibrary = new FunctionLibrary();51 49 52 Variable variable = new Variable(); 53 Constant constant = new Constant(); 54 Differential differential = new Differential(); 55 Addition addition = new Addition(); 56 And and = new And(); 57 //Average average = new Average(); 58 Cosinus cosinus = new Cosinus(); 59 Division division = new Division(); 60 Equal equal = new Equal(); 61 Exponential exponential = new Exponential(); 62 GreaterThan greaterThan = new GreaterThan(); 63 IfThenElse ifThenElse = new IfThenElse(); 64 LessThan lessThan = new LessThan(); 65 Logarithm logarithm = new Logarithm(); 66 Multiplication multiplication = new Multiplication(); 67 Not not = new Not(); 68 Or or = new Or(); 69 Power power = new Power(); 70 Signum signum = new Signum(); 71 Sinus sinus = new Sinus(); 72 Sqrt sqrt = new Sqrt(); 73 Subtraction subtraction = new Subtraction(); 74 Tangens tangens = new Tangens(); 75 Xor xor = new Xor(); 76 77 78 List<IFunction> booleanFunctions = new List<IFunction>(); 79 booleanFunctions.Add(and); 80 booleanFunctions.Add(equal); 81 booleanFunctions.Add(greaterThan); 82 booleanFunctions.Add(lessThan); 83 booleanFunctions.Add(not); 84 booleanFunctions.Add(or); 85 booleanFunctions.Add(xor); 86 87 List<IFunction> doubleFunctions = new List<IFunction>(); 88 doubleFunctions.Add(differential); 89 doubleFunctions.Add(variable); 90 doubleFunctions.Add(constant); 91 doubleFunctions.Add(addition); 92 // doubleFunctions.Add(average); 93 doubleFunctions.Add(cosinus); 94 doubleFunctions.Add(division); 95 doubleFunctions.Add(exponential); 96 doubleFunctions.Add(ifThenElse); 97 doubleFunctions.Add(logarithm); 98 doubleFunctions.Add(multiplication); 99 doubleFunctions.Add(power); 100 doubleFunctions.Add(signum); 101 doubleFunctions.Add(sinus); 102 doubleFunctions.Add(sqrt); 103 doubleFunctions.Add(subtraction); 104 doubleFunctions.Add(tangens); 105 106 SetAllowedSubOperators(and, booleanFunctions); 107 SetAllowedSubOperators(equal, doubleFunctions); 108 SetAllowedSubOperators(greaterThan, doubleFunctions); 109 SetAllowedSubOperators(lessThan, doubleFunctions); 110 SetAllowedSubOperators(not, booleanFunctions); 111 SetAllowedSubOperators(or, booleanFunctions); 112 SetAllowedSubOperators(xor, booleanFunctions); 113 SetAllowedSubOperators(addition, doubleFunctions); 114 //SetAllowedSubOperators(average, doubleFunctions); 115 SetAllowedSubOperators(cosinus, doubleFunctions); 116 SetAllowedSubOperators(division, doubleFunctions); 117 SetAllowedSubOperators(exponential, doubleFunctions); 118 SetAllowedSubOperators(ifThenElse, 0, booleanFunctions); 119 SetAllowedSubOperators(ifThenElse, 1, doubleFunctions); 120 SetAllowedSubOperators(ifThenElse, 2, doubleFunctions); 121 SetAllowedSubOperators(logarithm, doubleFunctions); 122 SetAllowedSubOperators(multiplication, doubleFunctions); 123 SetAllowedSubOperators(power, doubleFunctions); 124 SetAllowedSubOperators(signum, doubleFunctions); 125 SetAllowedSubOperators(sinus, doubleFunctions); 126 SetAllowedSubOperators(sqrt, doubleFunctions); 127 SetAllowedSubOperators(subtraction, doubleFunctions); 128 SetAllowedSubOperators(tangens, doubleFunctions); 129 130 functionLibrary.AddFunction(differential); 131 functionLibrary.AddFunction(variable); 132 functionLibrary.AddFunction(constant); 133 functionLibrary.AddFunction(addition); 134 // functionLibrary.AddFunction(average); 135 functionLibrary.AddFunction(and); 136 functionLibrary.AddFunction(cosinus); 137 functionLibrary.AddFunction(division); 138 functionLibrary.AddFunction(equal); 139 functionLibrary.AddFunction(exponential); 140 functionLibrary.AddFunction(greaterThan); 141 functionLibrary.AddFunction(ifThenElse); 142 functionLibrary.AddFunction(lessThan); 143 functionLibrary.AddFunction(logarithm); 144 functionLibrary.AddFunction(multiplication); 145 functionLibrary.AddFunction(not); 146 functionLibrary.AddFunction(power); 147 functionLibrary.AddFunction(or); 148 functionLibrary.AddFunction(signum); 149 functionLibrary.AddFunction(sinus); 150 functionLibrary.AddFunction(sqrt); 151 functionLibrary.AddFunction(subtraction); 152 functionLibrary.AddFunction(tangens); 153 functionLibrary.AddFunction(xor); 154 155 variable.MinTimeOffset = variable.MaxTimeOffset = 0; 156 differential.MinTimeOffset = differential.MaxTimeOffset = 0; 157 158 return functionLibrary; 159 50 public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) { 51 return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3); 160 52 } 161 53 162 private static void SetAllowedSubOperators(IFunction f, IEnumerable<IFunction> gs) { 163 for (int i = 0; i < f.MaxSubTrees; i++) { 164 SetAllowedSubOperators(f, i, gs); 54 public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, 55 int popSize, int minSize, int maxSize, 56 int maxFunctionDefinitions, int maxFunctionArguments) { 57 foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) { 58 variableSymbol.VariableNames = dataset.VariableNames.Skip(1); 165 59 } 166 } 167 168 private static void SetAllowedSubOperators(IFunction f, int i, IEnumerable<IFunction> gs) { 169 foreach (var g in gs) { 170 f.AddAllowedSubFunction(g, i); 171 } 172 } 173 174 public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize) { 175 return CreateRandomTrees(twister, dataset, funLib, popSize, 1, 200); 176 } 177 178 public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize) { 179 return CreateRandomTrees(twister, dataset, popSize, 1, 200); 180 } 181 182 public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize, int minSize, int maxSize) { 183 return CreateRandomTrees(twister, dataset, Util.CreateFunctionLibrary(), popSize, minSize, maxSize); 184 } 185 186 public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize, int minSize, int maxSize) { 187 IFunctionTree[] randomTrees = new IFunctionTree[popSize]; 60 SymbolicExpressionTree[] randomTrees = new SymbolicExpressionTree[popSize]; 188 61 for (int i = 0; i < randomTrees.Length; i++) { 189 randomTrees[i] = ProbabilisticTreeCreator.Create(twister, funLib, minSize, maxSize, maxSize + 1);62 randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10, maxFunctionDefinitions, maxFunctionArguments); 190 63 } 191 64 return randomTrees; … … 200 73 } 201 74 } 202 Dataset ds = new Dataset(data);203 ds.SetVariableName(0, "y");75 IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString())); 76 Dataset ds = new Dataset(variableNames, data); 204 77 return ds; 205 78 } … … 209 82 } 210 83 211 public static void EvaluateTrees( IFunctionTree[] trees, ITreeEvaluator evaluator, Dataset dataset, int repetitions) {84 public static void EvaluateTrees(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) { 212 85 double[] estimation = new double[dataset.Rows]; 213 86 // warm up 214 87 for (int i = 0; i < trees.Length; i++) { 215 evaluator.PrepareForEvaluation(dataset, trees[i]); 216 for (int row = 1; row < dataset.Rows; row++) { 217 estimation[row] = evaluator.Evaluate(row); 218 } 88 estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray(); 219 89 } 220 90 221 91 Stopwatch watch = new Stopwatch(); 222 Stopwatch compileWatch = new Stopwatch();223 92 long nNodes = 0; 224 93 for (int rep = 0; rep < repetitions; rep++) { 225 94 watch.Start(); 226 95 for (int i = 0; i < trees.Length; i++) { 227 compileWatch.Start(); 228 evaluator.PrepareForEvaluation(dataset, trees[i]); 229 nNodes += trees[i].GetSize() * (dataset.Rows - 1); 230 compileWatch.Stop(); 231 for (int row = 1; row < dataset.Rows; row++) { 232 estimation[row] = evaluator.Evaluate(row); 233 } 96 nNodes += trees[i].Size * (dataset.Rows - 1); 97 estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray(); 234 98 } 235 99 watch.Stop(); 236 100 } 237 Assert.Inconclusive("Random tree evaluation performance of " + evaluator.GetType() + ":" +238 watch.ElapsedMilliseconds + "ms (" + compileWatch.ElapsedMilliseconds + " ms)" +101 Assert.Inconclusive("Random tree evaluation performance of " + interpreter.GetType() + ":" + 102 watch.ElapsedMilliseconds + "ms " + 239 103 Util.NodesPerSecond(nNodes, watch) + " nodes/sec"); 240 104 }
Note: See TracChangeset
for help on using the changeset viewer.