Changeset 767
- Timestamp:
- 11/16/08 00:39:50 (16 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs
r702 r767 41 41 private class Instr { 42 42 public double d_arg0; 43 public int i_arg0; 44 public int i_arg1; 45 public int arity; 46 public int symbol; 43 public short i_arg0; 44 public short i_arg1; 45 public byte arity; 46 public byte symbol; 47 public ushort exprLength; 47 48 public IFunction function; 48 49 } 49 50 50 private List<Instr> code;51 51 private Instr[] codeArr; 52 52 private int PC; … … 54 54 private int sampleIndex; 55 55 56 57 public BakedTreeEvaluator() {58 code = new List<Instr>();59 }60 61 56 public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) { 62 57 this.dataset = dataset; 63 58 double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable); 64 59 65 // get the mean of the values of the target variable to determin the max and min bounds of the estimated value60 // get the mean of the values of the target variable to determine the max and min bounds of the estimated value 66 61 double targetMean = dataset.GetMean(targetVariable, start, end - 1); 67 62 estimatedValueMin = targetMean - maximumPunishment; … … 69 64 70 65 List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation; 71 code.Clear(); 72 foreach(LightWeightFunction f in linearRepresentation) { 73 Instr curInstr = new Instr(); 74 TranslateToInstr(f, curInstr); 75 code.Add(curInstr); 76 } 77 78 codeArr = code.ToArray<Instr>(); 79 } 80 81 private void TranslateToInstr(LightWeightFunction f, Instr instr) { 66 codeArr = new Instr[linearRepresentation.Count]; 67 int i = 0; 68 foreach (LightWeightFunction f in linearRepresentation) { 69 codeArr[i++] = TranslateToInstr(f); 70 } 71 exprIndex = 0; 72 ushort exprLength; 73 bool constExpr; 74 PatchExpressionLengthsAndConstants(0, out constExpr, out exprLength); 75 } 76 77 ushort exprIndex; 78 private void PatchExpressionLengthsAndConstants(ushort index, out bool constExpr, out ushort exprLength) { 79 exprLength = 1; 80 if (codeArr[index].arity == 0) { 81 // when no children then it's a constant expression only if the terminal is a constant 82 constExpr = codeArr[index].symbol == EvaluatorSymbolTable.CONSTANT; 83 } else { 84 constExpr = true; // when there are children it's a constant expression if all children are constant; 85 } 86 for (int i = 0; i < codeArr[index].arity; i++) { 87 exprIndex++; 88 ushort branchLength; 89 bool branchConstExpr; 90 PatchExpressionLengthsAndConstants(exprIndex, out branchConstExpr, out branchLength); 91 exprLength += branchLength; 92 constExpr &= branchConstExpr; 93 } 94 codeArr[index].exprLength = exprLength; 95 96 if (constExpr) { 97 codeArr[index].symbol = EvaluatorSymbolTable.CONSTANT; 98 PC = index; 99 codeArr[index].d_arg0 = EvaluateBakedCode(); 100 } 101 } 102 103 private Instr TranslateToInstr(LightWeightFunction f) { 104 Instr instr = new Instr(); 82 105 instr.arity = f.arity; 83 106 instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); 84 switch (instr.symbol) {107 switch (instr.symbol) { 85 108 case EvaluatorSymbolTable.DIFFERENTIAL: 86 109 case EvaluatorSymbolTable.VARIABLE: { 87 instr.i_arg0 = ( int)f.data[0]; // var110 instr.i_arg0 = (byte)f.data[0]; // var 88 111 instr.d_arg0 = f.data[1]; // weight 89 instr.i_arg1 = (int)f.data[2]; // sample-offset 112 instr.i_arg1 = (byte)f.data[2]; // sample-offset 113 instr.exprLength = 1; 90 114 break; 91 115 } 92 116 case EvaluatorSymbolTable.CONSTANT: { 93 117 instr.d_arg0 = f.data[0]; // value 118 instr.exprLength = 1; 94 119 break; 95 120 } 96 121 case EvaluatorSymbolTable.UNKNOWN: { 97 122 instr.function = f.functionType; 123 instr.exprLength = 1; 98 124 break; 99 125 } 100 126 } 127 return instr; 101 128 } 102 129 … … 106 133 107 134 double estimated = EvaluateBakedCode(); 108 if (double.IsNaN(estimated) || double.IsInfinity(estimated)) {135 if (double.IsNaN(estimated) || double.IsInfinity(estimated)) { 109 136 estimated = estimatedValueMax; 110 } else if (estimated > estimatedValueMax) {137 } else if (estimated > estimatedValueMax) { 111 138 estimated = estimatedValueMax; 112 } else if (estimated < estimatedValueMin) {139 } else if (estimated < estimatedValueMin) { 113 140 estimated = estimatedValueMin; 114 141 } … … 118 145 // skips a whole branch 119 146 private void SkipBakedCode() { 120 int i = 1; 121 while(i > 0) { 122 i += code[PC++].arity; 123 i--; 124 } 147 PC += codeArr[PC].exprLength; 125 148 } 126 149 127 150 private double EvaluateBakedCode() { 128 151 Instr currInstr = codeArr[PC++]; 129 switch (currInstr.symbol) {152 switch (currInstr.symbol) { 130 153 case EvaluatorSymbolTable.VARIABLE: { 131 154 int row = sampleIndex + currInstr.i_arg1; 132 if (row < 0 || row >= dataset.Rows) return double.NaN;155 if (row < 0 || row >= dataset.Rows) return double.NaN; 133 156 else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0); 134 157 } 135 158 case EvaluatorSymbolTable.CONSTANT: { 159 PC += currInstr.exprLength - 1; 136 160 return currInstr.d_arg0; 137 161 } 138 162 case EvaluatorSymbolTable.DIFFERENTIAL: { 139 163 int row = sampleIndex + currInstr.i_arg1; 140 if (row < 1 || row >= dataset.Rows) return double.NaN;164 if (row < 1 || row >= dataset.Rows) return double.NaN; 141 165 else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - dataset.GetValue(row - 1, currInstr.i_arg0)); 142 166 } 143 167 case EvaluatorSymbolTable.MULTIPLICATION: { 144 168 double result = EvaluateBakedCode(); 145 for (int i = 1; i < currInstr.arity; i++) {169 for (int i = 1; i < currInstr.arity; i++) { 146 170 result *= EvaluateBakedCode(); 147 171 } … … 150 174 case EvaluatorSymbolTable.ADDITION: { 151 175 double sum = EvaluateBakedCode(); 152 for (int i = 1; i < currInstr.arity; i++) {176 for (int i = 1; i < currInstr.arity; i++) { 153 177 sum += EvaluateBakedCode(); 154 178 } … … 156 180 } 157 181 case EvaluatorSymbolTable.SUBTRACTION: { 158 if (currInstr.arity == 1) {182 if (currInstr.arity == 1) { 159 183 return -EvaluateBakedCode(); 160 184 } else { 161 185 double result = EvaluateBakedCode(); 162 for (int i = 1; i < currInstr.arity; i++) {186 for (int i = 1; i < currInstr.arity; i++) { 163 187 result -= EvaluateBakedCode(); 164 188 } … … 168 192 case EvaluatorSymbolTable.DIVISION: { 169 193 double result; 170 if (currInstr.arity == 1) {194 if (currInstr.arity == 1) { 171 195 result = 1.0 / EvaluateBakedCode(); 172 196 } else { 173 197 result = EvaluateBakedCode(); 174 for (int i = 1; i < currInstr.arity; i++) {198 for (int i = 1; i < currInstr.arity; i++) { 175 199 result /= EvaluateBakedCode(); 176 200 } 177 201 } 178 if (double.IsInfinity(result)) return 0.0;202 if (double.IsInfinity(result)) return 0.0; 179 203 else return result; 180 204 } 181 205 case EvaluatorSymbolTable.AVERAGE: { 182 206 double sum = EvaluateBakedCode(); 183 for (int i = 1; i < currInstr.arity; i++) {207 for (int i = 1; i < currInstr.arity; i++) { 184 208 sum += EvaluateBakedCode(); 185 209 } … … 205 229 case EvaluatorSymbolTable.SIGNUM: { 206 230 double value = EvaluateBakedCode(); 207 if (double.IsNaN(value)) return double.NaN;231 if (double.IsNaN(value)) return double.NaN; 208 232 else return Math.Sign(value); 209 233 } … … 216 240 case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0 217 241 double result = EvaluateBakedCode(); 218 for (int i = 1; i < currInstr.arity; i++) {219 if (result == 0.0) SkipBakedCode();242 for (int i = 1; i < currInstr.arity; i++) { 243 if (result == 0.0) SkipBakedCode(); 220 244 else { 221 245 result = EvaluateBakedCode(); … … 228 252 double x = EvaluateBakedCode(); 229 253 double y = EvaluateBakedCode(); 230 if (Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;254 if (Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0; 231 255 } 232 256 case EvaluatorSymbolTable.GT: { 233 257 double x = EvaluateBakedCode(); 234 258 double y = EvaluateBakedCode(); 235 if (x > y) return 1.0;259 if (x > y) return 1.0; 236 260 else return 0.0; 237 261 } … … 240 264 Debug.Assert(condition == 0.0 || condition == 1.0); 241 265 double result; 242 if (condition == 0.0) {266 if (condition == 0.0) { 243 267 result = EvaluateBakedCode(); SkipBakedCode(); 244 268 } else { … … 250 274 double x = EvaluateBakedCode(); 251 275 double y = EvaluateBakedCode(); 252 if (x < y) return 1.0;276 if (x < y) return 1.0; 253 277 else return 0.0; 254 278 } … … 260 284 case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1 261 285 double result = EvaluateBakedCode(); 262 for (int i = 1; i < currInstr.arity; i++) {263 if (result > 0.0) SkipBakedCode();286 for (int i = 1; i < currInstr.arity; i++) { 287 if (result > 0.0) SkipBakedCode(); 264 288 else { 265 289 result = EvaluateBakedCode(); -
trunk/sources/HeuristicLab.GP.StructureIdentification/SymbolTable.cs
r656 r767 29 29 namespace HeuristicLab.GP.StructureIdentification { 30 30 class EvaluatorSymbolTable : StorableBase { 31 public const intADDITION = 1;32 public const intAND = 2;33 public const intAVERAGE = 3;34 public const intCONSTANT = 4;35 public const intCOSINUS = 5;36 public const intDIFFERENTIAL = 25;37 public const intDIVISION = 6;38 public const intEQU = 7;39 public const intEXP = 8;40 public const intGT = 9;41 public const intIFTE = 10;42 public const intLT = 11;43 public const intLOG = 12;44 public const intMULTIPLICATION = 13;45 public const intNOT = 14;46 public const intOR = 15;47 public const intPOWER = 16;48 public const intSIGNUM = 17;49 public const intSINUS = 18;50 public const intSQRT = 19;51 public const intSUBTRACTION = 20;52 public const intTANGENS = 21;53 public const intVARIABLE = 22;54 public const intXOR = 23;55 public const intUNKNOWN = 24;31 public const byte ADDITION = 1; 32 public const byte AND = 2; 33 public const byte AVERAGE = 3; 34 public const byte CONSTANT = 4; 35 public const byte COSINUS = 5; 36 public const byte DIFFERENTIAL = 25; 37 public const byte DIVISION = 6; 38 public const byte EQU = 7; 39 public const byte EXP = 8; 40 public const byte GT = 9; 41 public const byte IFTE = 10; 42 public const byte LT = 11; 43 public const byte LOG = 12; 44 public const byte MULTIPLICATION = 13; 45 public const byte NOT = 14; 46 public const byte OR = 15; 47 public const byte POWER = 16; 48 public const byte SIGNUM = 17; 49 public const byte SINUS = 18; 50 public const byte SQRT = 19; 51 public const byte SUBTRACTION = 20; 52 public const byte TANGENS = 21; 53 public const byte VARIABLE = 22; 54 public const byte XOR = 23; 55 public const byte UNKNOWN = 24; 56 56 57 private static Dictionary<Type, int> staticTypes = new Dictionary<Type, int>();57 private static Dictionary<Type, byte> staticTypes = new Dictionary<Type, byte>(); 58 58 59 59 // needs to be public for persistence mechanism (Activator.CreateInstance needs empty constructor) 60 60 static EvaluatorSymbolTable() { 61 staticTypes = new Dictionary<Type, int>();61 staticTypes = new Dictionary<Type, byte>(); 62 62 staticTypes[typeof(Addition)] = ADDITION; 63 63 staticTypes[typeof(And)] = AND; … … 86 86 } 87 87 88 internal static intMapFunction(IFunction function) {88 internal static byte MapFunction(IFunction function) { 89 89 if(staticTypes.ContainsKey(function.GetType())) return staticTypes[function.GetType()]; 90 90 else return UNKNOWN; -
trunk/sources/HeuristicLab.GP/BakedFunctionTree.cs
r656 r767 32 32 33 33 public class LightWeightFunction { 34 public intarity = 0;34 public byte arity = 0; 35 35 public IFunction functionType; 36 36 public List<double> data = new List<double>(); … … 116 116 private void FlattenTrees() { 117 117 if(treesExpanded) { 118 linearRepresentation[0].arity = subTrees.Count;118 linearRepresentation[0].arity = (byte)subTrees.Count; 119 119 foreach(BakedFunctionTree subTree in subTrees) { 120 120 subTree.FlattenVariables(); … … 295 295 foreach(XmlNode entryNode in linearRepresentationNode.ChildNodes) { 296 296 LightWeightFunction f = new LightWeightFunction(); 297 f.arity = int.Parse(entryNode.Attributes["Arity"].Value, CultureInfo.InvariantCulture);297 f.arity = byte.Parse(entryNode.Attributes["Arity"].Value, CultureInfo.InvariantCulture); 298 298 if(entryNode.Attributes["Data"]!=null) 299 299 f.data = GetList<double>(entryNode.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));
Note: See TracChangeset
for help on using the changeset viewer.