Changeset 9290
- Timestamp:
- 03/06/13 10:32:41 (12 years ago)
- Location:
- branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/Instruction.cs
r9271 r9290 32 32 // an optional object value (addresses for calls, argument index for arguments) 33 33 public object iArg0; 34 // hold the value of the instruction35 public double value;36 34 } 37 35 } -
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r9271 r9290 126 126 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 127 127 </Reference> 128 <Reference Include="HeuristicLab.Optimization.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">129 <SpecificVersion>False</SpecificVersion>130 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization.Views-3.3.dll</HintPath>131 </Reference>132 128 <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 133 129 <SpecificVersion>False</SpecificVersion> … … 141 137 <SpecificVersion>False</SpecificVersion> 142 138 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 143 </Reference>144 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">145 <SpecificVersion>False</SpecificVersion>146 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>147 139 </Reference> 148 140 <Reference Include="System" /> … … 151 143 </Reference> 152 144 <Reference Include="System.Drawing" /> 153 <Reference Include="System.Xml.Linq">154 <RequiredTargetFramework>3.5</RequiredTargetFramework>155 </Reference>156 <Reference Include="System.Data.DataSetExtensions">157 <RequiredTargetFramework>3.5</RequiredTargetFramework>158 </Reference>159 <Reference Include="System.Data" />160 <Reference Include="System.Xml" />161 145 </ItemGroup> 162 146 <ItemGroup> -
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r9271 r9290 124 124 } 125 125 126 double[] values = new double[code.Length]; 127 126 128 foreach (var rowEnum in rows) { 127 129 int row = rowEnum; 128 yield return Evaluate(dataset, ref row, code );130 yield return Evaluate(dataset, ref row, code, values); 129 131 } 130 132 } 131 133 132 protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code ) {134 protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code, double[] values) { 133 135 int count = 0; 134 136 for (int j = 0; j != code.Length; ++j) { 135 137 Instruction currentInstr = code[j]; 136 138 139 #region switch 137 140 switch (currentInstr.opCode) { 138 141 case OpCodes.Add: { 139 double s = code[j - currentInstr.nArguments].value;140 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 141 s += code[i].value;142 } 143 currentInstr.value= s;142 double s = values[j - currentInstr.nArguments]; 143 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 144 s += values[i]; 145 } 146 values[j] = s; 144 147 } 145 148 break; 146 149 case OpCodes.Sub: { 147 double s = code[j - currentInstr.nArguments].value;148 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 149 s -= code[i].value;150 double s = values[j - currentInstr.nArguments]; 151 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 152 s -= values[i]; 150 153 } 151 154 if (currentInstr.nArguments == 1) s = -s; 152 currentInstr.value= s;155 values[j] = s; 153 156 } 154 157 break; 155 158 case OpCodes.Mul: { 156 double p = code[j - currentInstr.nArguments].value;157 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 158 p *= code[i].value;159 } 160 currentInstr.value= p;159 double p = values[j - currentInstr.nArguments]; 160 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 161 p *= values[i]; 162 } 163 values[j] = p; 161 164 } 162 165 break; 163 166 case OpCodes.Div: { 164 double p = code[j - currentInstr.nArguments].value;165 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 166 p /= code[i].value;167 double p = values[j - currentInstr.nArguments]; 168 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 169 p /= values[i]; 167 170 } 168 171 if (currentInstr.nArguments == 1) p = 1.0 / p; 169 currentInstr.value= p;172 values[j] = p; 170 173 } 171 174 break; 172 175 case OpCodes.Average: { 173 double sum = code[j - currentInstr.nArguments].value;174 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 175 sum += code[i].value;176 } 177 currentInstr.value= sum / currentInstr.nArguments;176 double sum = values[j - currentInstr.nArguments]; 177 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 178 sum += values[i]; 179 } 180 values[j] = sum / currentInstr.nArguments; 178 181 } 179 182 break; 180 183 case OpCodes.Cos: { 181 currentInstr.value = Math.Cos(code[j - 1].value);184 values[j] = Math.Cos(values[j - currentInstr.nArguments]); 182 185 } 183 186 break; 184 187 case OpCodes.Sin: { 185 currentInstr.value = Math.Sin(code[j - 1].value);188 values[j] = Math.Sin(values[j - currentInstr.nArguments]); 186 189 break; 187 190 } 188 191 case OpCodes.Tan: { 189 currentInstr.value = Math.Tan(code[j - 1].value);192 values[j] = Math.Tan(values[j - currentInstr.nArguments]); 190 193 } 191 194 break; 192 195 case OpCodes.Square: { 193 currentInstr.value = Math.Pow(code[j - 1].value, 2);196 values[j] = Math.Pow(values[j - currentInstr.nArguments], 2); 194 197 } 195 198 break; 196 199 case OpCodes.Power: { 197 double x = code[j - 2].value;198 double y = Math.Round( code[j - 1].value);199 currentInstr.value= Math.Pow(x, y);200 double x = values[j - currentInstr.nArguments]; 201 double y = Math.Round(values[j - currentInstr.nArguments + 1]); 202 values[j] = Math.Pow(x, y); 200 203 } 201 204 break; 202 205 case OpCodes.SquareRoot: { 203 currentInstr.value = Math.Sqrt(code[j - 1].value);206 values[j] = Math.Sqrt(values[j - currentInstr.nArguments]); 204 207 } 205 208 break; 206 209 case OpCodes.Root: { 207 double x = code[j - 2].value;208 double y = Math.Round( code[j - 1].value);209 currentInstr.value= Math.Pow(x, 1 / y);210 double x = values[j - currentInstr.nArguments]; 211 double y = Math.Round(values[j - currentInstr.nArguments + 1]); 212 values[j] = Math.Pow(x, 1 / y); 210 213 } 211 214 break; 212 215 case OpCodes.Exp: { 213 currentInstr.value = Math.Exp(code[j - 1].value);216 values[j] = Math.Exp(values[j - currentInstr.nArguments]); 214 217 } 215 218 break; 216 219 case OpCodes.Log: { 217 currentInstr.value = Math.Log(code[j - 1].value);220 values[j] = Math.Log(values[j - currentInstr.nArguments]); 218 221 } 219 222 break; 220 223 case OpCodes.Gamma: { 221 currentInstr.value = double.IsNaN(code[j - 1].value) ? double.NaN : alglib.gammafunction(code[j - 1].value);224 values[j] = double.IsNaN(values[j - currentInstr.nArguments]) ? double.NaN : alglib.gammafunction(values[j - currentInstr.nArguments]); 222 225 } 223 226 break; 224 227 case OpCodes.Psi: { 225 var x = code[j - 1].value;226 if (double.IsNaN(x)) currentInstr.value= double.NaN;227 else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) currentInstr.value= double.NaN;228 else currentInstr.value= alglib.psi(x);228 var x = values[j - currentInstr.nArguments]; 229 if (double.IsNaN(x)) values[j] = double.NaN; 230 else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) values[j] = double.NaN; 231 else values[j] = alglib.psi(x); 229 232 } 230 233 break; 231 234 case OpCodes.Dawson: { 232 var x = code[j - 1].value;233 currentInstr.value= double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);235 var x = values[j - currentInstr.nArguments]; 236 values[j] = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x); 234 237 } 235 238 break; 236 239 case OpCodes.ExponentialIntegralEi: { 237 var x = code[j - 1].value;238 currentInstr.value= double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);240 var x = values[j - currentInstr.nArguments]; 241 values[j] = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x); 239 242 } 240 243 break; 241 244 case OpCodes.SineIntegral: { 242 245 double si, ci; 243 var x = code[j - 1].value;244 if (double.IsNaN(x)) currentInstr.value= double.NaN;246 var x = values[j - currentInstr.nArguments]; 247 if (double.IsNaN(x)) values[j] = double.NaN; 245 248 else { 246 249 alglib.sinecosineintegrals(x, out si, out ci); 247 currentInstr.value= si;250 values[j] = si; 248 251 } 249 252 } … … 251 254 case OpCodes.CosineIntegral: { 252 255 double si, ci; 253 var x = code[j - 1].value;254 if (double.IsNaN(x)) currentInstr.value= double.NaN;256 var x = values[j - currentInstr.nArguments]; 257 if (double.IsNaN(x)) values[j] = double.NaN; 255 258 else { 256 259 alglib.sinecosineintegrals(x, out si, out ci); 257 currentInstr.value= ci;260 values[j] = ci; 258 261 } 259 262 } … … 261 264 case OpCodes.HyperbolicSineIntegral: { 262 265 double shi, chi; 263 var x = code[j - 1].value;264 if (double.IsNaN(x)) currentInstr.value= double.NaN;266 var x = values[j - currentInstr.nArguments]; 267 if (double.IsNaN(x)) values[j] = double.NaN; 265 268 else { 266 269 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 267 currentInstr.value= shi;270 values[j] = shi; 268 271 } 269 272 } … … 271 274 case OpCodes.HyperbolicCosineIntegral: { 272 275 double shi, chi; 273 var x = code[j - 1].value;274 if (double.IsNaN(x)) currentInstr.value= double.NaN;276 var x = values[j - currentInstr.nArguments]; 277 if (double.IsNaN(x)) values[j] = double.NaN; 275 278 else { 276 279 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 277 currentInstr.value= chi;280 values[j] = chi; 278 281 } 279 282 } … … 281 284 case OpCodes.FresnelCosineIntegral: { 282 285 double c = 0, s = 0; 283 var x = code[j - 1].value;284 if (double.IsNaN(x)) currentInstr.value= double.NaN;286 var x = values[j - currentInstr.nArguments]; 287 if (double.IsNaN(x)) values[j] = double.NaN; 285 288 else { 286 289 alglib.fresnelintegral(x, ref c, ref s); 287 currentInstr.value= c;290 values[j] = c; 288 291 } 289 292 } … … 291 294 case OpCodes.FresnelSineIntegral: { 292 295 double c = 0, s = 0; 293 var x = code[j - 1].value;294 if (double.IsNaN(x)) currentInstr.value= double.NaN;296 var x = values[j - currentInstr.nArguments]; 297 if (double.IsNaN(x)) values[j] = double.NaN; 295 298 else { 296 299 alglib.fresnelintegral(x, ref c, ref s); 297 currentInstr.value= s;300 values[j] = s; 298 301 } 299 302 } … … 301 304 case OpCodes.AiryA: { 302 305 double ai, aip, bi, bip; 303 var x = code[j - 1].value;304 if (double.IsNaN(x)) currentInstr.value= double.NaN;306 var x = values[j - currentInstr.nArguments]; 307 if (double.IsNaN(x)) values[j] = double.NaN; 305 308 else { 306 309 alglib.airy(x, out ai, out aip, out bi, out bip); 307 currentInstr.value= ai;310 values[j] = ai; 308 311 } 309 312 } … … 311 314 case OpCodes.AiryB: { 312 315 double ai, aip, bi, bip; 313 var x = code[j - 1].value;314 if (double.IsNaN(x)) currentInstr.value= double.NaN;316 var x = values[j - currentInstr.nArguments]; 317 if (double.IsNaN(x)) values[j] = double.NaN; 315 318 else { 316 319 alglib.airy(x, out ai, out aip, out bi, out bip); 317 currentInstr.value= bi;320 values[j] = bi; 318 321 } 319 322 } 320 323 break; 321 324 case OpCodes.Norm: { 322 var x = code[j - 1].value;323 currentInstr.value= double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x);325 var x = values[j - currentInstr.nArguments]; 326 values[j] = double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x); 324 327 } 325 328 break; 326 329 case OpCodes.Erf: { 327 var x = code[j - 1].value;328 currentInstr.value= double.IsNaN(x) ? double.NaN : alglib.errorfunction(x);330 var x = values[j - currentInstr.nArguments]; 331 values[j] = double.IsNaN(x) ? double.NaN : alglib.errorfunction(x); 329 332 } 330 333 break; 331 334 case OpCodes.Bessel: { 332 var x = code[j - 1].value;333 currentInstr.value= double.IsNaN(x) ? double.NaN : alglib.besseli0(x);335 var x = values[j - currentInstr.nArguments]; 336 values[j] = double.IsNaN(x) ? double.NaN : alglib.besseli0(x); 334 337 } 335 338 break; 336 339 case OpCodes.IfThenElse: { 337 double condition = code[j - currentInstr.nArguments].value;338 double result = condition > 0.0 ? code[j - 2].value : code[j - 1].value;339 currentInstr.value= result;340 double condition = values[j - currentInstr.nArguments]; 341 double result = condition > 0.0 ? values[j - currentInstr.nArguments] : values[j - currentInstr.nArguments + 1]; 342 values[j] = result; 340 343 } 341 344 break; 342 345 case OpCodes.AND: { 343 double result = code[j - currentInstr.nArguments].value;344 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 345 if (result > 0.0) result = code[i].value;346 } 347 currentInstr.value= result > 0.0 ? 1.0 : -1.0;346 double result = values[j - currentInstr.nArguments]; 347 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 348 if (result > 0.0) result = values[i]; 349 } 350 values[j] = result > 0.0 ? 1.0 : -1.0; 348 351 } 349 352 break; 350 353 case OpCodes.OR: { 351 double result = code[j - currentInstr.nArguments].value;354 double result = values[j - currentInstr.nArguments]; 352 355 for (int i = 1; i < currentInstr.nArguments; i++) { 353 if (result <= 0.0) result = code[i].value; ;354 } 355 currentInstr.value= result > 0.0 ? 1.0 : -1.0;356 if (result <= 0.0) result = values[i]; ; 357 } 358 values[j] = result > 0.0 ? 1.0 : -1.0; 356 359 } 357 360 break; 358 361 case OpCodes.NOT: { 359 currentInstr.value = code[j - 1].value> 0.0 ? -1.0 : 1.0;362 values[j] = values[j - currentInstr.nArguments] > 0.0 ? -1.0 : 1.0; 360 363 } 361 364 break; 362 365 case OpCodes.GT: { 363 double x = code[j - 2].value;364 double y = code[j - 1].value;365 currentInstr.value= x > y ? 1.0 : -1.0;366 double x = values[j - currentInstr.nArguments]; 367 double y = values[j - currentInstr.nArguments + 1]; 368 values[j] = x > y ? 1.0 : -1.0; 366 369 } 367 370 break; 368 371 case OpCodes.LT: { 369 double x = code[j - 2].value;370 double y = code[j - 1].value;371 currentInstr.value= x < y ? 1.0 : -1.0;372 double x = values[j - currentInstr.nArguments]; 373 double y = values[j - currentInstr.nArguments + 1]; 374 values[j] = x < y ? 1.0 : -1.0; 372 375 } 373 376 break; … … 393 396 } 394 397 case OpCodes.Variable: { 395 if (row < 0 || row >= dataset.Rows) currentInstr.value= double.NaN;398 if (row < 0 || row >= dataset.Rows) values[j] = double.NaN; 396 399 else { 397 400 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 398 currentInstr.value= ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;401 values[j] = ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 399 402 } 400 403 } … … 403 406 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 404 407 int actualRow = row + laggedVariableTreeNode.Lag; 405 if (actualRow < 0 || actualRow >= dataset.Rows) currentInstr.value= double.NaN;406 else { 407 currentInstr.value= ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;408 if (actualRow < 0 || actualRow >= dataset.Rows) values[j] = double.NaN; 409 else { 410 values[j] = ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight; 408 411 } 409 412 } … … 411 414 case OpCodes.Constant: { 412 415 var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode; 413 currentInstr.value= constTreeNode.Value;416 values[j] = constTreeNode.Value; 414 417 } 415 418 break; … … 418 421 //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function 419 422 case OpCodes.VariableCondition: { 420 if (row < 0 || row >= dataset.Rows) currentInstr.value= double.NaN;423 if (row < 0 || row >= dataset.Rows) values[j] = double.NaN; 421 424 else { 422 425 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; … … 425 428 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 426 429 427 double trueBranch = code[j - currentInstr.nArguments].value;428 double falseBranch = code[j - currentInstr.nArguments + 1].value;429 430 currentInstr.value= trueBranch * p + falseBranch * (1 - p);430 double trueBranch = values[j - currentInstr.nArguments]; 431 double falseBranch = values[j - currentInstr.nArguments + 1]; 432 433 values[j] = trueBranch * p + falseBranch * (1 - p); 431 434 } 432 435 } … … 435 438 throw new NotSupportedException(); 436 439 } 440 #endregion 437 441 438 442 // book-keeping in order to keep all the values correct … … 444 448 if (count > narg) { 445 449 for (int i = 1; i <= count - narg; ++i) 446 code[j - i].value = code[j - i - narg].value;450 values[j - i] = values[j - i - narg]; 447 451 } 448 452 … … 450 454 } 451 455 452 return code[code.Length - 1].value;456 return values[values.Length - 1]; 453 457 } 454 458 }
Note: See TracChangeset
for help on using the changeset viewer.