Changeset 3733 for trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicExpressionImporter.cs
- Timestamp:
- 05/10/10 08:15:00 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests
- Files:
-
- 1 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.