- Timestamp:
- 09/16/11 12:00:36 (13 years ago)
- Location:
- branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 10 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs
r6618 r6784 70 70 71 71 protected override Allele[] CalculateAlleles(ISymbolicExpressionTree solution) { 72 List<Allele> alleles = new List<Allele>(); 73 74 foreach (var subtree in GetAllSubtreesOfDepth(solution, AlleleTreeDepth)) { 75 alleles.Add(GetAlleleFromSubtreeOfDepth(subtree, AlleleTreeDepth)); 76 } 77 return alleles.ToArray(); 72 return GetAllSubtreesOfDepth(solution, AlleleTreeDepth) 73 .AsParallel() 74 .Select(t => GetAlleleFromSubtreeOfDepth(t, AlleleTreeDepth)) 75 .ToArray(); 78 76 } 79 77 -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs
r5907 r6784 93 93 IList<int> nonDominatedIndexes = new List<int>(); 94 94 ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); 95 List<double[]> qualities = new List<double[]>();96 95 bool[] maximization = Maximization.ToArray(); 97 96 List<double[]> newNonDominatedQualities = new List<double[]>(); 98 97 var evaluator = EvaluatorParameter.ActualValue; 98 var problemData = ProblemDataParameter.ActualValue; 99 99 IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator); 100 101 var qualities = tree 102 .AsParallel() 103 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 104 .ToArray(); 100 105 for (int i = 0; i < tree.Length; i++) { 101 qualities.Add(evaluator.Evaluate(childContext, tree[i], ProblemDataParameter.ActualValue, rows)); // qualities[i] = ...102 106 if (IsNonDominated(qualities[i], trainingBestQualities, maximization) && 103 107 IsNonDominated(qualities[i], qualities, maximization)) { -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs
r5907 r6784 76 76 ISymbolicExpressionTree bestTree = null; 77 77 ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray(); 78 double[] quality = new double[tree.Length];79 78 var evaluator = EvaluatorParameter.ActualValue; 79 var problemData = ProblemDataParameter.ActualValue; 80 80 IEnumerable<int> rows = GenerateRowsToEvaluate(); 81 81 if (!rows.Any()) return base.Apply(); 82 82 83 83 IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator); 84 var quality = tree 85 .AsParallel() 86 .Select(t => evaluator.Evaluate(childContext, t, problemData, rows)) 87 .ToArray(); 88 84 89 for (int i = 0; i < tree.Length; i++) { 85 quality[i] = evaluator.Evaluate(childContext, tree[i], ProblemDataParameter.ActualValue, rows);86 90 if (IsBetter(quality[i], bestQuality, Maximization.Value)) { 87 91 bestQuality = quality[i]; -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs
r5924 r6784 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 104 105 datatable.Rows.Add(row); 105 106 } 106 datatable.Rows[pair.Key].Values.Add( pair.Value);107 datatable.Rows[pair.Key].Values.Add(Math.Round(pair.Value, 3)); 107 108 } 108 109 … … 130 131 131 132 public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateLaggedVariables = true) { 132 Dictionary<string, double> variableFrequencies = new Dictionary<string, double>(); 133 int totalNumberOfSymbols = 0; 134 135 foreach (var tree in trees) { 136 var variableReferences = GetVariableReferences(tree, aggregateLaggedVariables); 137 foreach (var pair in variableReferences) { 138 totalNumberOfSymbols += pair.Value; 139 if (variableFrequencies.ContainsKey(pair.Key)) { 140 variableFrequencies[pair.Key] += pair.Value; 141 } else { 142 variableFrequencies.Add(pair.Key, pair.Value); 143 } 144 } 145 } 133 134 var variableFrequencies = trees 135 .AsParallel() 136 .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables)) 137 .GroupBy(pair => pair.Key, pair => pair.Value) 138 .ToDictionary(g => g.Key, g => (double)g.Sum()); 139 140 double totalNumberOfSymbols = variableFrequencies.Values.Sum(); 146 141 147 142 foreach (var pair in variableFrequencies) -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r6284 r6784 121 121 <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" /> 122 122 <Compile Include="Analyzers\SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs" /> 123 <Compile Include="SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs" /> 123 124 <Compile Include="Formatters\SymbolicDataAnalysisExpressionLatexFormatter.cs" /> 124 125 <Compile Include="Formatters\SymbolicDataAnalysisExpressionMATLABFormatter.cs" /> -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r6409 r6784 208 208 if (instr.opCode == OpCodes.Variable) { 209 209 var variableTreeNode = instr.dynamicNode as VariableTreeNode; 210 instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName);210 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); 211 211 code[i] = instr; 212 212 } else if (instr.opCode == OpCodes.LagVariable) { 213 var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;214 instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName);213 var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode; 214 instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName); 215 215 code[i] = instr; 216 216 } else if (instr.opCode == OpCodes.VariableCondition) { 217 217 var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode; 218 instr.iArg0 = (ushort)dataset.GetVariableIndex(variableConditionTreeNode.VariableName);218 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName); 219 219 } else if (instr.opCode == OpCodes.Call) { 220 220 necessaryArgStackSize += instr.nArguments + 1; … … 308 308 double result = Evaluate(dataset, ref row, state); 309 309 for (int i = 1; i < currentInstr.nArguments; i++) { 310 if (result <= 0.0) SkipInstructions(state);310 if (result > 0.0) result = Evaluate(dataset, ref row, state); 311 311 else { 312 result = Evaluate(dataset, ref row,state);312 SkipInstructions(state); 313 313 } 314 314 } 315 return result <= 0.0 ? -1.0 :1.0;315 return result > 0.0 ? 1.0 : -1.0; 316 316 } 317 317 case OpCodes.OR: { 318 318 double result = Evaluate(dataset, ref row, state); 319 319 for (int i = 1; i < currentInstr.nArguments; i++) { 320 if (result > 0.0) SkipInstructions(state);320 if (result <= 0.0) result = Evaluate(dataset, ref row, state); 321 321 else { 322 result = Evaluate(dataset, ref row,state);322 SkipInstructions(state); 323 323 } 324 324 } … … 390 390 int savedPc = state.ProgramCounter; 391 391 // set pc to start of function 392 state.ProgramCounter = currentInstr.iArg0;392 state.ProgramCounter = (ushort)currentInstr.iArg0; 393 393 // evaluate the function 394 394 double v = Evaluate(dataset, ref row, state); … … 402 402 } 403 403 case OpCodes.Arg: { 404 return state.GetStackFrameValue( currentInstr.iArg0);404 return state.GetStackFrameValue((ushort)currentInstr.iArg0); 405 405 } 406 406 case OpCodes.Variable: { 407 407 if (row < 0 || row >= dataset.Rows) 408 408 return double.NaN; 409 var variableTreeNode = currentInstr.dynamicNode as VariableTreeNode;410 return dataset[row, currentInstr.iArg0] * variableTreeNode.Weight;409 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 410 return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 411 411 } 412 412 case OpCodes.LagVariable: { 413 var laggedVariableTreeNode = currentInstr.dynamicNode as LaggedVariableTreeNode;413 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 414 414 int actualRow = row + laggedVariableTreeNode.Lag; 415 415 if (actualRow < 0 || actualRow >= dataset.Rows) 416 416 return double.NaN; 417 return dataset[actualRow, currentInstr.iArg0] * laggedVariableTreeNode.Weight;417 return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight; 418 418 } 419 419 case OpCodes.Constant: { … … 428 428 return double.NaN; 429 429 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; 430 double variableValue = dataset[row, currentInstr.iArg0];430 double variableValue = ((IList<double>)currentInstr.iArg0)[row]; 431 431 double x = variableValue - variableConditionTreeNode.Threshold; 432 432 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r6387 r6784 538 538 } else if (IsExp(node)) { 539 539 return node.GetSubtree(0); 540 } else if (IsMultiplication(node)) {541 return node.Subtrees.Select(s => MakeLog(s)).Aggregate((x, y) => MakeSum(x, y));542 } else if (IsDivision(node)) {543 var subtractionNode = subSymbol.CreateTreeNode();544 foreach (var subtree in node.Subtrees) {545 subtractionNode.AddSubtree(MakeLog(subtree));546 }547 return subtractionNode;548 540 } else { 549 541 var logNode = logSymbol.CreateTreeNode(); -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs
r5809 r6784 30 30 31 31 32 /// <summary>33 ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended34 ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests35 ///</summary>36 32 [TestClass()] 37 33 public class SymbolicDataAnalysisExpressionTreeInterpreterTest { … … 55 51 56 52 [TestMethod] 57 public void FullGrammarSimpleArithmeticExpressionInterpreterPerformanceTest() { 53 public void SymbolicDataAnalysisExpressionTreeInterpreterFullGrammarPerformanceTest() { 54 FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6); 55 } 56 [TestMethod] 57 public void SymbolicDataAnalysisExpressionTreeInterpreterArithmeticGrammarPerformanceTest() { 58 FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6); 59 } 60 61 [TestMethod] 62 public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterFullGrammarPerformanceTest() { 63 FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 10e6); 64 } 65 [TestMethod] 66 public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterArithmeticGrammarPerformanceTest() { 67 FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 10e6); 68 } 69 70 private void FullGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) { 58 71 var twister = new MersenneTwister(31415); 59 72 var dataset = Util.CreateRandomDataset(twister, Rows, Columns); … … 64 77 grammar.MinimumFunctionDefinitions = 0; 65 78 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 66 double[] estimation = new double[Rows];67 79 foreach (ISymbolicExpressionTree tree in randomTrees) { 68 80 Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); 69 81 } 70 SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();71 82 double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); 72 Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec 73 } 74 75 [TestMethod] 76 public void ArithmeticGrammarSimpleArithmeticExpressionInterpreterPerformanceTest() { 83 Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec 84 } 85 86 private void ArithmeticGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) { 77 87 var twister = new MersenneTwister(31415); 78 88 var dataset = Util.CreateRandomDataset(twister, Rows, Columns); … … 83 93 grammar.MinimumFunctionDefinitions = 0; 84 94 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 85 double[] estimation = new double[Rows];86 95 foreach (SymbolicExpressionTree tree in randomTrees) { 87 96 Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); 88 97 } 89 SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 98 90 99 double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); 91 Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec100 Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec 92 101 } 93 102 … … 97 106 ///</summary> 98 107 [TestMethod] 99 public void SimpleArithmeticExpressionInterpreterEvaluateTest() { 100 108 public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() { 101 109 Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] { 102 110 { 1.0, 1.0, 1.0 }, … … 105 113 }); 106 114 107 SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 108 115 var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 116 EvaluateTerminals(interpreter, ds); 117 EvaluateOperations(interpreter, ds); 118 EvaluateAdf(interpreter, ds); 119 } 120 121 [TestMethod] 122 public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() { 123 Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] { 124 { 1.0, 1.0, 1.0 }, 125 { 2.0, 2.0, 2.0 }, 126 { 3.0, 1.0, 2.0 } 127 }); 128 129 var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(); 130 EvaluateTerminals(interpreter, ds); 131 EvaluateOperations(interpreter, ds); 132 } 133 134 private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 109 135 // constants 110 136 Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0); … … 113 139 Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0); 114 140 Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0); 115 116 141 } 142 143 private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 144 145 // ADF 146 Evaluate(interpreter, ds, @"(PROG 147 (MAIN 148 (CALL ADF0)) 149 (defun ADF0 1.0))", 1, 1.0); 150 Evaluate(interpreter, ds, @"(PROG 151 (MAIN 152 (* (CALL ADF0) (CALL ADF0))) 153 (defun ADF0 2.0))", 1, 4.0); 154 Evaluate(interpreter, ds, @"(PROG 155 (MAIN 156 (CALL ADF0 2.0 3.0)) 157 (defun ADF0 158 (+ (ARG 0) (ARG 1))))", 1, 5.0); 159 Evaluate(interpreter, ds, @"(PROG 160 (MAIN (CALL ADF1 2.0 3.0)) 161 (defun ADF0 162 (- (ARG 1) (ARG 0))) 163 (defun ADF1 164 (+ (CALL ADF0 (ARG 1) (ARG 0)) 165 (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0); 166 Evaluate(interpreter, ds, @"(PROG 167 (MAIN (CALL ADF1 (variable 2.0 a) 3.0)) 168 (defun ADF0 169 (- (ARG 1) (ARG 0))) 170 (defun ADF1 171 (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0); 172 Evaluate(interpreter, ds, 173 @"(PROG 174 (MAIN (CALL ADF1 (variable 2.0 a) 3.0)) 175 (defun ADF0 176 (- (ARG 1) (ARG 0))) 177 (defun ADF1 178 (+ (CALL ADF0 (ARG 1) (ARG 0)) 179 (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0); 180 } 181 182 private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 117 183 // addition 118 184 Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0); … … 148 214 Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0); 149 215 Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0); 150 //Evaluate(interpreter, ds, "(> (sqrt-1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false216 Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false 151 217 152 218 // lt … … 155 221 Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0); 156 222 Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0); 157 //Evaluate(interpreter, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false223 Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false 158 224 159 225 // If … … 163 229 Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0); 164 230 Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0); 165 // Evaluate(interpreter, ds, "(if (sqrt-1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch231 Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch 166 232 167 233 // NOT … … 171 237 Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0); 172 238 Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0); 239 Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0); 173 240 174 241 // AND … … 181 248 Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0); 182 249 Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0); 250 Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN) 251 Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0) 252 183 253 184 254 // OR … … 190 260 Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0); 191 261 Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0); 262 Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN) 263 Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0) 192 264 193 265 // sin, cos, tan … … 204 276 Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3)); 205 277 278 // power 279 Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0); 280 Evaluate(interpreter, ds, "(pow 4.0 0.5)", 0, 1.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number) 281 Evaluate(interpreter, ds, "(pow 4.0 2.5)", 0, 16.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number) 282 Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0); 283 Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0); 284 Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0); 285 286 // root 287 Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0); 288 Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0); 289 Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0)); 290 206 291 // mean 207 292 Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0); 208 293 209 // ADF 210 Evaluate(interpreter, ds, @"(PROG 211 (MAIN 212 (CALL ADF0)) 213 (defun ADF0 1.0))", 1, 1.0); 214 Evaluate(interpreter, ds, @"(PROG 215 (MAIN 216 (* (CALL ADF0) (CALL ADF0))) 217 (defun ADF0 2.0))", 1, 4.0); 218 Evaluate(interpreter, ds, @"(PROG 219 (MAIN 220 (CALL ADF0 2.0 3.0)) 221 (defun ADF0 222 (+ (ARG 0) (ARG 1))))", 1, 5.0); 223 Evaluate(interpreter, ds, @"(PROG 224 (MAIN (CALL ADF1 2.0 3.0)) 225 (defun ADF0 226 (- (ARG 1) (ARG 0))) 227 (defun ADF1 228 (+ (CALL ADF0 (ARG 1) (ARG 0)) 229 (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0); 230 Evaluate(interpreter, ds, @"(PROG 231 (MAIN (CALL ADF1 (variable 2.0 a) 3.0)) 232 (defun ADF0 233 (- (ARG 1) (ARG 0))) 234 (defun ADF1 235 (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0); 236 Evaluate(interpreter, ds, @"(PROG 237 (MAIN (CALL ADF1 (variable 2.0 a) 3.0)) 238 (defun ADF0 239 (- (ARG 1) (ARG 0))) 240 (defun ADF1 241 (+ (CALL ADF0 (ARG 1) (ARG 0)) 242 (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0); 243 } 244 245 private void Evaluate(SymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) { 294 // lag 295 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0)); 296 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1)); 297 Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2)); 298 Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1)); 299 } 300 301 private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) { 246 302 var importer = new SymbolicExpressionImporter(); 247 303 ISymbolicExpressionTree tree = importer.Import(expr); -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs
r5915 r6784 131 131 } 132 132 { 133 // log transformation134 var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 2.0 a) (variable 3.0 b)))"));135 var expectedTree = importer.Import("( + (log (variable 1.0 a)) (log (variable 1.0 b)) 1.7918)");136 Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree)); 137 } 138 { 139 // log transformation140 var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b)))"));141 var expectedTree = importer.Import("( - (log (variable 2.0 a)) (log (variable 3.0 b)))");133 // must not transform logs in this way as we do not know wether both variables are positive 134 var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 1.0 a) (variable 1.0 b)))")); 135 var expectedTree = importer.Import("(log (* (variable 1.0 a) (variable 1.0 b)))"); 136 Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree)); 137 } 138 { 139 // must not transform logs in this way as we do not know wether both variables are positive 140 var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 1.0 a) (variable 1.0 b)))")); 141 var expectedTree = importer.Import("(log (/ (variable 1.0 a) (variable 1.0 b)))"); 142 142 Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree)); 143 143 } -
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicExpressionImporter.cs
r5809 r6784 29 29 internal class SymbolicExpressionImporter { 30 30 private const string VARSTART = "VAR"; 31 private const string LAGGEDVARSTART = "LAG"; 31 32 private const string DEFUNSTART = "DEFUN"; 32 33 private const string ARGSTART = "ARG"; … … 58 59 Constant constant = new Constant(); 59 60 Variable variable = new Variable(); 61 LaggedVariable laggedVariable = new LaggedVariable(); 60 62 Defun defun = new Defun(); 61 63 … … 96 98 if (tokens.Peek().StringValue.StartsWith(VARSTART)) { 97 99 tree = ParseVariable(tokens); 100 } else if (tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) { 101 tree = ParseLaggedVariable(tokens); 98 102 } else if (tokens.Peek().StringValue.StartsWith(DEFUNSTART)) { 99 103 tree = ParseDefun(tokens); … … 157 161 } 158 162 163 private ISymbolicExpressionTreeNode ParseLaggedVariable(Queue<Token> tokens) { 164 Token varTok = tokens.Dequeue(); 165 Debug.Assert(varTok.StringValue == "LAGVARIABLE"); 166 LaggedVariableTreeNode t = (LaggedVariableTreeNode)laggedVariable.CreateTreeNode(); 167 t.Weight = tokens.Dequeue().DoubleValue; 168 t.VariableName = tokens.Dequeue().StringValue; 169 t.Lag = (int)tokens.Dequeue().DoubleValue; 170 return t; 171 } 172 159 173 private ISymbolicExpressionTreeNode CreateTree(Token token) { 160 174 if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
Note: See TracChangeset
for help on using the changeset viewer.