Changeset 260 for trunk/sources/HeuristicLab.Functions
- Timestamp:
- 05/14/08 20:14:07 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.Functions
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/BakedFunctionTree.cs
r259 r260 34 34 private List<int> code; 35 35 private List<double> data; 36 private static BakedTreeEvaluator evaluator = new BakedTreeEvaluator();36 private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable; 37 37 public BakedFunctionTree() { 38 38 code = new List<int>(); … … 43 43 : this() { 44 44 code.Add(0); 45 code.Add( evaluator.MapFunction(function));45 code.Add(symbolTable.MapFunction(function)); 46 46 code.Add(0); 47 47 treesExpanded = true; … … 59 59 : this() { 60 60 code.Add(0); 61 code.Add( evaluator.MapFunction(tree.Function));61 code.Add(symbolTable.MapFunction(tree.Function)); 62 62 code.Add(tree.LocalVariables.Count); 63 63 foreach(IVariable variable in tree.LocalVariables) { … … 159 159 if(!variablesExpanded) { 160 160 variables = new List<IVariable>(); 161 IFunction function = evaluator.MapSymbol(code[1]);161 IFunction function = symbolTable.MapSymbol(code[1]); 162 162 int localVariableIndex = 0; 163 163 foreach(IVariableInfo variableInfo in function.VariableInfos) { … … 187 187 188 188 public IFunction Function { 189 get { return evaluator.MapSymbol(code[1]); }189 get { return symbolTable.MapSymbol(code[1]); } 190 190 } 191 191 … … 221 221 } 222 222 223 private BakedTreeEvaluator evaluator = null; 223 224 public double Evaluate(Dataset dataset, int sampleIndex) { 224 225 FlattenVariables(); 225 226 FlattenTrees(); 226 evaluator.SetCode(code, data);227 if(evaluator == null) evaluator = new BakedTreeEvaluator(code, data); 227 228 return evaluator.Evaluate(dataset, sampleIndex); 228 229 } … … 250 251 XmlNode evaluatorNode = node.SelectSingleNode("Evaluator"); 251 252 if(evaluatorNode != null) { 252 BakedTreeEvaluator evaluator = (BakedTreeEvaluator)PersistenceManager.Restore(evaluatorNode, restoredObjects); 253 BakedFunctionTree.evaluator = evaluator; 253 this.evaluator = (BakedTreeEvaluator)PersistenceManager.Restore(evaluatorNode, restoredObjects); 254 254 } 255 255 code = GetList<int>(node.Attributes["Code"].Value, s => int.Parse(s, CultureInfo.InvariantCulture)); 256 256 data = GetList<double>(node.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture)); 257 treesExpanded = false; 258 variablesExpanded = false; 257 259 } 258 260 -
trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs
r259 r260 9 9 namespace HeuristicLab.Functions { 10 10 internal class BakedTreeEvaluator : StorableBase { 11 private const int ADDITION = 10010; 12 private const int AND = 10020; 13 private const int AVERAGE = 10030; 14 private const int CONSTANT = 10040; 15 private const int COSINUS = 10050; 16 private const int DIVISION = 10060; 17 private const int EQU = 10070; 18 private const int EXP = 10080; 19 private const int GT = 10090; 20 private const int IFTE = 10100; 21 private const int LT = 10110; 22 private const int LOG = 10120; 23 private const int MULTIPLICATION = 10130; 24 private const int NOT = 10140; 25 private const int OR = 10150; 26 private const int POWER = 10160; 27 private const int SIGNUM = 10170; 28 private const int SINUS = 10180; 29 private const int SQRT = 10190; 30 private const int SUBSTRACTION = 10200; 31 private const int TANGENS = 10210; 32 private const int VARIABLE = 10220; 33 private const int XOR = 10230; 34 35 private int nextFunctionSymbol = 10240; 36 private Dictionary<int, IFunction> symbolTable; 37 private Dictionary<IFunction, int> reverseSymbolTable; 38 private Dictionary<Type, int> staticTypes; 39 private const int MAX_CODE_LENGTH = 4096; 40 private const int MAX_DATA_LENGTH = 4096; 41 private int[] codeArr = new int[MAX_CODE_LENGTH]; 42 private double[] dataArr = new double[MAX_DATA_LENGTH]; 43 11 private int[] codeArr; 12 private double[] dataArr; 13 private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable; 14 15 // for persistence mechanism only 44 16 public BakedTreeEvaluator() { 45 symbolTable = new Dictionary<int, IFunction>(); 46 reverseSymbolTable = new Dictionary<IFunction, int>(); 47 staticTypes = new Dictionary<Type, int>(); 48 staticTypes[typeof(Addition)] = ADDITION; 49 staticTypes[typeof(And)] = AND; 50 staticTypes[typeof(Average)] = AVERAGE; 51 staticTypes[typeof(Constant)] = CONSTANT; 52 staticTypes[typeof(Cosinus)] = COSINUS; 53 staticTypes[typeof(Division)] = DIVISION; 54 staticTypes[typeof(Equal)] = EQU; 55 staticTypes[typeof(Exponential)] = EXP; 56 staticTypes[typeof(GreaterThan)] = GT; 57 staticTypes[typeof(IfThenElse)] = IFTE; 58 staticTypes[typeof(LessThan)] = LT; 59 staticTypes[typeof(Logarithm)] = LOG; 60 staticTypes[typeof(Multiplication)] = MULTIPLICATION; 61 staticTypes[typeof(Not)] = NOT; 62 staticTypes[typeof(Or)] = OR; 63 staticTypes[typeof(Power)] = POWER; 64 staticTypes[typeof(Signum)] = SIGNUM; 65 staticTypes[typeof(Sinus)] = SINUS; 66 staticTypes[typeof(Sqrt)] = SQRT; 67 staticTypes[typeof(Substraction)] = SUBSTRACTION; 68 staticTypes[typeof(Tangens)] = TANGENS; 69 staticTypes[typeof(Variable)] = VARIABLE; 70 staticTypes[typeof(Xor)] = XOR; 71 } 72 73 internal int MapFunction(IFunction function) { 74 if(!reverseSymbolTable.ContainsKey(function)) { 75 int curFunctionSymbol; 76 if(staticTypes.ContainsKey(function.GetType())) curFunctionSymbol = staticTypes[function.GetType()]; 77 else { 78 curFunctionSymbol = nextFunctionSymbol; 79 nextFunctionSymbol++; 80 } 81 reverseSymbolTable[function] = curFunctionSymbol; 82 symbolTable[curFunctionSymbol] = function; 83 } 84 return reverseSymbolTable[function]; 85 } 86 87 internal IFunction MapSymbol(int symbol) { 88 return symbolTable[symbol]; 89 } 90 91 internal void SetCode(List<int> code, List<double> data) { 92 code.CopyTo(codeArr); 93 data.CopyTo(dataArr); 17 } 18 19 public BakedTreeEvaluator(List<int> code, List<double> data) { 20 codeArr = code.ToArray(); 21 dataArr = data.ToArray(); 94 22 } 95 23 … … 113 41 PC += 3; 114 42 switch(functionSymbol) { 115 case VARIABLE: {43 case EvaluatorSymbolTable.VARIABLE: { 116 44 int var = (int)dataArr[DP]; 117 45 double weight = dataArr[DP + 1]; … … 121 49 else return weight * dataset.GetValue(row, var); 122 50 } 123 case CONSTANT: {51 case EvaluatorSymbolTable.CONSTANT: { 124 52 return dataArr[DP++]; 125 53 } 126 case MULTIPLICATION: {54 case EvaluatorSymbolTable.MULTIPLICATION: { 127 55 double result = EvaluateBakedCode(); 128 56 for(int i = 1; i < arity; i++) { … … 131 59 return result; 132 60 } 133 case ADDITION: {61 case EvaluatorSymbolTable.ADDITION: { 134 62 double sum = EvaluateBakedCode(); 135 63 for(int i = 1; i < arity; i++) { … … 138 66 return sum; 139 67 } 140 case SUBSTRACTION: {68 case EvaluatorSymbolTable.SUBSTRACTION: { 141 69 if(arity == 1) { 142 70 return -EvaluateBakedCode(); … … 149 77 } 150 78 } 151 case DIVISION: {79 case EvaluatorSymbolTable.DIVISION: { 152 80 double result; 153 81 if(arity == 1) { … … 162 90 else return result; 163 91 } 164 case AVERAGE: {92 case EvaluatorSymbolTable.AVERAGE: { 165 93 double sum = EvaluateBakedCode(); 166 94 for(int i = 1; i < arity; i++) { … … 169 97 return sum / arity; 170 98 } 171 case COSINUS: {99 case EvaluatorSymbolTable.COSINUS: { 172 100 return Math.Cos(EvaluateBakedCode()); 173 101 } 174 case SINUS: {102 case EvaluatorSymbolTable.SINUS: { 175 103 return Math.Sin(EvaluateBakedCode()); 176 104 } 177 case E XP: {105 case EvaluatorSymbolTable.EXP: { 178 106 return Math.Exp(EvaluateBakedCode()); 179 107 } 180 case LOG: {108 case EvaluatorSymbolTable.LOG: { 181 109 return Math.Log(EvaluateBakedCode()); 182 110 } 183 case POWER: {111 case EvaluatorSymbolTable.POWER: { 184 112 double x = EvaluateBakedCode(); 185 113 double p = EvaluateBakedCode(); 186 114 return Math.Pow(x, p); 187 115 } 188 case SIGNUM: {116 case EvaluatorSymbolTable.SIGNUM: { 189 117 double value = EvaluateBakedCode(); 190 118 if(double.IsNaN(value)) return double.NaN; 191 119 else return Math.Sign(value); 192 120 } 193 case SQRT: {121 case EvaluatorSymbolTable.SQRT: { 194 122 return Math.Sqrt(EvaluateBakedCode()); 195 123 } 196 case TANGENS: {124 case EvaluatorSymbolTable.TANGENS: { 197 125 return Math.Tan(EvaluateBakedCode()); 198 126 } 199 case AND: {127 case EvaluatorSymbolTable.AND: { 200 128 double result = 1.0; 201 129 // have to evaluate all sub-trees, skipping would probably not lead to a big gain because … … 208 136 return result; 209 137 } 210 case E QU: {138 case EvaluatorSymbolTable.EQU: { 211 139 double x = EvaluateBakedCode(); 212 140 double y = EvaluateBakedCode(); 213 141 if(x == y) return 1.0; else return 0.0; 214 142 } 215 case GT: {143 case EvaluatorSymbolTable.GT: { 216 144 double x = EvaluateBakedCode(); 217 145 double y = EvaluateBakedCode(); … … 219 147 else return 0.0; 220 148 } 221 case IFTE: {149 case EvaluatorSymbolTable.IFTE: { 222 150 double condition = Math.Round(EvaluateBakedCode()); 223 151 double x = EvaluateBakedCode(); … … 227 155 else return double.NaN; 228 156 } 229 case LT: {157 case EvaluatorSymbolTable.LT: { 230 158 double x = EvaluateBakedCode(); 231 159 double y = EvaluateBakedCode(); … … 233 161 else return 0.0; 234 162 } 235 case NOT: {163 case EvaluatorSymbolTable.NOT: { 236 164 double result = Math.Round(EvaluateBakedCode()); 237 165 if(result == 0.0) return 1.0; … … 239 167 else return double.NaN; 240 168 } 241 case OR: {169 case EvaluatorSymbolTable.OR: { 242 170 double result = 0.0; // default is false 243 171 for(int i = 0; i < arity; i++) { … … 248 176 return result; 249 177 } 250 case XOR: {178 case EvaluatorSymbolTable.XOR: { 251 179 double x = Math.Round(EvaluateBakedCode()); 252 180 double y = Math.Round(EvaluateBakedCode()); … … 258 186 } 259 187 default: { 260 IFunction function = symbolTable [functionSymbol];188 IFunction function = symbolTable.MapSymbol(functionSymbol); 261 189 double[] args = new double[nLocalVariables + arity]; 262 190 for(int i = 0; i < nLocalVariables; i++) { … … 277 205 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 278 206 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 279 XmlAttribute nextFunctionSymbolAttribute = document.CreateAttribute("NextFunctionSymbol"); 280 nextFunctionSymbolAttribute.Value = nextFunctionSymbol.ToString(); 281 node.Attributes.Append(nextFunctionSymbolAttribute); 282 XmlNode symbolTableNode = document.CreateNode(XmlNodeType.Element, "SymbolTable", null); 283 foreach(KeyValuePair<int, IFunction> entry in symbolTable) { 284 XmlNode entryNode = PersistenceManager.Persist("Entry", entry.Value, document, persistedObjects); 285 XmlAttribute symbolAttr = document.CreateAttribute("Symbol"); 286 symbolAttr.Value = entry.Key.ToString(); 287 entryNode.Attributes.Append(symbolAttr); 288 symbolTableNode.AppendChild(entryNode); 289 } 290 node.AppendChild(symbolTableNode); 207 node.AppendChild(PersistenceManager.Persist("SymbolTable", symbolTable, document, persistedObjects)); 291 208 return node; 292 209 } … … 294 211 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 295 212 base.Populate(node, restoredObjects); 296 symbolTable.Clear(); 297 reverseSymbolTable.Clear(); 298 nextFunctionSymbol = int.Parse(node.Attributes["NextFunctionSymbol"].Value); 299 XmlNode symbolTableNode = node.SelectSingleNode("SymbolTable"); 300 foreach(XmlNode entry in symbolTableNode.ChildNodes) { 301 IFunction function = (IFunction)PersistenceManager.Restore(entry, restoredObjects); 302 int symbol = int.Parse(entry.Attributes["Symbol"].Value); 303 symbolTable[symbol] = function; 304 reverseSymbolTable[function] = symbol; 305 } 213 PersistenceManager.Restore(node.SelectSingleNode("SymbolTable"), restoredObjects); 306 214 } 307 215 } -
trunk/sources/HeuristicLab.Functions/HeuristicLab.Functions.csproj
r229 r260 83 83 <Compile Include="Power.cs" /> 84 84 <Compile Include="Division.cs" /> 85 <Compile Include="SymbolTable.cs" /> 85 86 <Compile Include="Tangens.cs" /> 86 87 <Compile Include="Cosinus.cs" />
Note: See TracChangeset
for help on using the changeset viewer.