Changeset 15366 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Evaluator/PushBenchmarkSuiteEvaluator.cs
- Timestamp:
- 09/17/17 23:27:05 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Evaluator/PushBenchmarkSuiteEvaluator.cs
r15345 r15366 220 220 long result; 221 221 if (long.TryParse(printResult, out result)) { 222 var diff = Math.Abs(result -example.OutputInteger[0]);222 var diff = result.AbsoluteDiff(example.OutputInteger[0]); 223 223 224 224 return Math.Min(Data.WorstResult, diff); … … 236 236 double value; 237 237 if (double.TryParse(printResult, NumberStyles.Number | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value)) { 238 var diff = Math.Abs(example.OutputFloat[0] -value);238 var diff = example.OutputFloat[0].AbsoluteDiff(value); 239 239 diff = Math.Min(diff, Data.WorstResult); 240 240 … … 263 263 var printResult = interpreter.PrintStack.ToString(); 264 264 var distance = example.OutputPrint.LevenshteinDistance(printResult); 265 var lineCountWithCorrectFormat = interpreter.PrintStack.AsStrings().Count(line => { 266 var parts = line.Split(' '); 267 268 if (parts.Length != 3) 269 return false; 270 271 var part0 = parts[0].Trim(); 272 if (part0.Length == 0 || !part0.IsNumeric()) 273 return false; 274 275 var part1 = parts[1].Trim(); 276 if (part1.Length != 1) 277 return false; 278 279 var part2 = parts[2].Trim(); 280 if (part2.Length != 1) 281 return false; 282 283 return true; 284 }); 265 266 var lineCountWithCorrectFormat = interpreter.PrintStack 267 .AsStrings() 268 .Count(line => { 269 var parts = line.Split(' '); 270 271 if (parts.Length != 3) 272 return false; 273 274 var part0 = parts[0].Trim(); 275 if (part0.Length == 0 || !part0.IsNumeric()) 276 return false; 277 278 var part1 = parts[1].Trim(); 279 if (part1.Length != 1) 280 return false; 281 282 var part2 = parts[2].Trim(); 283 if (part2.Length != 1) 284 return false; 285 286 return true; 287 }); 285 288 286 289 var lineCountWithInvalidFormat = example.OutputPrintLineCount - lineCountWithCorrectFormat; … … 300 303 } 301 304 302 var distance = example.OutputPrint.LevenshteinDistance(interpreter.PrintStack.ToString()); 305 var printResult = interpreter.PrintStack.ToString(); 306 var distance = example.OutputPrint.LevenshteinDistance(printResult); 307 303 308 var printLines = interpreter.PrintStack.AsStrings().ToArray(); 304 var lineCountWithCorrectFormat = printLines.Count(line => line.IsNumeric()); 305 var lineCountWithInvalidFormat = Math.Max(0, example.OutputPrintLineCount - lineCountWithCorrectFormat);306 307 lineCountWithInvalidFormat = Math.Abs(Math.Min(lineCountWithInvalidFormat, example.OutputPrintLineCount));309 310 var lineCountWithInvalidFormat = printLines 311 .Take(example.OutputPrintLineCount) 312 .Count(line => !line.IsNumeric()); 308 313 309 314 var compareLength = Math.Min(printLines.Length, example.OutputPrintLineCount); 310 long integerError = 0;315 var integerError = 0L; 311 316 312 317 for (var i = 0; i < compareLength; i++) { 313 318 long value; 314 319 if (long.TryParse(printLines[i], out value)) { 315 integerError += Math.Abs(value - example.OutputIntegerVector[0][i]); 316 } else { 317 integerError += example.OutputPrintLineCount > 0 318 ? (long)(Data.WorstResult / example.OutputPrintLineCount) 319 : 0; 320 integerError += value.AbsoluteDiff(example.OutputIntegerVector[0][i]); 320 321 } 321 322 } 322 323 323 var result = distance + lineCountWithInvalidFormat + integerError ;324 var result = distance + lineCountWithInvalidFormat + integerError / compareLength; 324 325 return Math.Min(result, Data.WorstResult); 325 326 } … … 347 348 348 349 // add penalty if length does not match 349 var result = expectedVector.Length > 0 350 ? (expectedVector.Length - comparableLength) * (Data.WorstResult / expectedVector.Length) 351 : 0; 350 //var result = expectedVector.Length > 0 351 // ? (expectedVector.Length - comparableLength) * (Data.WorstResult / expectedVector.Length) 352 // : 0; 353 354 if (comparableLength == 0) { 355 return Data.WorstResult; 356 } 357 358 var result = 0d; 352 359 353 360 for (var i = 0; i < comparableLength; i++) { 354 result += Math.Abs(resultVector[i] - expectedVector[i]); 361 result += resultVector[i].AbsoluteDiff(expectedVector[i]); 362 } 363 364 for (var i = comparableLength; i < expectedVector.Length; i++) { 365 result += expectedVector[i]; 355 366 } 356 367 … … 372 383 var distance = example.OutputPrint.LevenshteinDistance(printResult); 373 384 var lineCount = interpreter.PrintStack.Count; 374 var lineCountError = Math.Abs(example.OutputPrintLineCount -lineCount);385 var lineCountError = example.OutputPrintLineCount.AbsoluteDiff(lineCount); 375 386 var totalWordsPerLineError = 0L; 376 387 377 388 for (var i = 0; i < interpreter.PrintStack.Lines.Count - 1; i++) { 378 totalWordsPerLineError += Math.Abs(interpreter.PrintStack.Lines[i].Split(' ').Length -estimatedWordsPerLineCount);389 totalWordsPerLineError += interpreter.PrintStack.Lines[i].Split(' ').Length.AbsoluteDiff(estimatedWordsPerLineCount); 379 390 } 380 391 381 392 // last line 382 393 var lastLine = interpreter.PrintStack.Lines[interpreter.PrintStack.Lines.Count - 1]; 383 totalWordsPerLineError += Math.Abs(lastLine.Split(' ').Length -estimatedLastWordCount);394 totalWordsPerLineError += lastLine.Split(' ').Length.AbsoluteDiff(estimatedLastWordCount); 384 395 385 396 var result = distance + lineCountError + totalWordsPerLineError; … … 396 407 397 408 // add penalty if length does not match 398 var result = expectedVector.Length > 0 399 ? (expectedVector.Length - comparableLength) * (Data.WorstResult / expectedVector.Length) 400 : 0; 409 //var result = expectedVector.Length > 0 410 // ? (expectedVector.Length - comparableLength) * (Data.WorstResult / expectedVector.Length) 411 // : 0; 412 413 if (comparableLength == 0) { 414 return Data.WorstResult; 415 } 416 417 var result = 0d; 401 418 402 419 for (var i = 0; i < comparableLength; i++) { 403 420 var expectedStr = expectedVector[i].ToString(); 404 421 var resultStr = resultVector[i].ToString(); 422 405 423 result += expectedStr.LevenshteinDistance(resultStr); 424 } 425 426 for (var i = comparableLength; i < expectedVector.Length; i++) { 427 result += expectedVector[i].ToString().Length; 406 428 } 407 429 … … 424 446 var expectedNumberOfSentences = example.OutputInteger[0]; 425 447 statsError += BenchmarkSuite.Problems.WordStats.GetNumberOfSentences(printStr, out numberOfSentences) 426 ? Math.Abs(expectedNumberOfSentences -numberOfSentences)448 ? expectedNumberOfSentences.AbsoluteDiff(numberOfSentences) 427 449 : Data.WorstResult / 3.0; 428 450 … … 430 452 var expectedAverageSentenceLength = example.OutputFloat[0]; 431 453 statsError += BenchmarkSuite.Problems.WordStats.GetAverageSentenceLength(printStr, out averageSentenceLength) 432 ? Math.Abs(expectedAverageSentenceLength -averageSentenceLength)454 ? expectedAverageSentenceLength.AbsoluteDiff(averageSentenceLength) 433 455 : Data.WorstResult / 3.0; 434 456 … … 445 467 446 468 var expectedLastCharIndex = example.OutputPrint.Length - 1; 469 var resultLastCharIndex = printStr.Length - 1; 447 470 var result = distance; 448 471 449 if (expectedLastCharIndex >= 0 ) {472 if (expectedLastCharIndex >= 0 && resultLastCharIndex >= 0) { 450 473 var expectedLastChar = example.OutputPrint[expectedLastCharIndex]; 451 var resultLastCharIndex = printStr.Length - 1; 452 453 if (resultLastCharIndex >= 0) { 454 var resultLastChar = printStr[resultLastCharIndex]; 455 result += Math.Abs(expectedLastChar - resultLastChar); 456 } 474 var resultLastChar = printStr[resultLastCharIndex]; 475 476 result += expectedLastChar.AbsoluteDiff(resultLastChar); 457 477 } 458 478 … … 470 490 var expectedNumberOfSyllables = example.OutputInteger[0]; 471 491 var numberOfSyllablesError = BenchmarkSuite.Problems.Syllables.GetNumberOfSyllables(printStr, out numberOfSyllables) 472 ? Math.Abs(expectedNumberOfSyllables -numberOfSyllables)492 ? expectedNumberOfSyllables.AbsoluteDiff(numberOfSyllables) 473 493 : Data.WorstResult / 2.0; 474 494 … … 487 507 var expectedGrade = example.OutputChar[0]; 488 508 var gradeError = BenchmarkSuite.Problems.Grades.GetGrade(printStr, out grade) 489 ? Math.Abs(expectedGrade -grade)490 : Data.WorstResult / 2;509 ? expectedGrade.AbsoluteDiff(grade) 510 : char.MaxValue; 491 511 492 512 var result = distance + gradeError; … … 500 520 if (result == double.MinValue || double.IsPositiveInfinity(result) || 501 521 double.IsNaN(result) || 522 // ReSharper disable once CompareOfFloatsByEqualityOperator 502 523 result == double.MaxValue || double.IsNegativeInfinity(result)) 503 524 return worst; … … 576 597 return double.IsNaN(diff) || double.IsInfinity(diff) 577 598 ? worstResult 578 : diff;599 : Math.Min(diff, worstResult); 579 600 } 580 601 … … 599 620 return double.IsNaN(diff) || double.IsInfinity(diff) 600 621 ? worstResult 601 : diff;622 : Math.Min(diff, worstResult); 602 623 } 603 624 }
Note: See TracChangeset
for help on using the changeset viewer.