Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/17/17 23:27:05 (7 years ago)
Author:
pkimmesw
Message:

#2665 Solution Cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Evaluator/PushBenchmarkSuiteEvaluator.cs

    r15345 r15366  
    220220      long result;
    221221      if (long.TryParse(printResult, out result)) {
    222         var diff = Math.Abs(result - example.OutputInteger[0]);
     222        var diff = result.AbsoluteDiff(example.OutputInteger[0]);
    223223
    224224        return Math.Min(Data.WorstResult, diff);
     
    236236      double value;
    237237      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);
    239239        diff = Math.Min(diff, Data.WorstResult);
    240240
     
    263263      var printResult = interpreter.PrintStack.ToString();
    264264      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        });
    285288
    286289      var lineCountWithInvalidFormat = example.OutputPrintLineCount - lineCountWithCorrectFormat;
     
    300303      }
    301304
    302       var distance = example.OutputPrint.LevenshteinDistance(interpreter.PrintStack.ToString());
     305      var printResult = interpreter.PrintStack.ToString();
     306      var distance = example.OutputPrint.LevenshteinDistance(printResult);
     307
    303308      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());
    308313
    309314      var compareLength = Math.Min(printLines.Length, example.OutputPrintLineCount);
    310       long integerError = 0;
     315      var integerError = 0L;
    311316
    312317      for (var i = 0; i < compareLength; i++) {
    313318        long value;
    314319        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]);
    320321        }
    321322      }
    322323
    323       var result = distance + lineCountWithInvalidFormat + integerError;
     324      var result = distance + lineCountWithInvalidFormat + integerError / compareLength;
    324325      return Math.Min(result, Data.WorstResult);
    325326    }
     
    347348
    348349      // 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;
    352359
    353360      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];
    355366      }
    356367
     
    372383      var distance = example.OutputPrint.LevenshteinDistance(printResult);
    373384      var lineCount = interpreter.PrintStack.Count;
    374       var lineCountError = Math.Abs(example.OutputPrintLineCount - lineCount);
     385      var lineCountError = example.OutputPrintLineCount.AbsoluteDiff(lineCount);
    375386      var totalWordsPerLineError = 0L;
    376387
    377388      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);
    379390      }
    380391
    381392      // last line
    382393      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);
    384395
    385396      var result = distance + lineCountError + totalWordsPerLineError;
     
    396407
    397408      // 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;
    401418
    402419      for (var i = 0; i < comparableLength; i++) {
    403420        var expectedStr = expectedVector[i].ToString();
    404421        var resultStr = resultVector[i].ToString();
     422
    405423        result += expectedStr.LevenshteinDistance(resultStr);
     424      }
     425
     426      for (var i = comparableLength; i < expectedVector.Length; i++) {
     427        result += expectedVector[i].ToString().Length;
    406428      }
    407429
     
    424446      var expectedNumberOfSentences = example.OutputInteger[0];
    425447      statsError += BenchmarkSuite.Problems.WordStats.GetNumberOfSentences(printStr, out numberOfSentences)
    426         ? Math.Abs(expectedNumberOfSentences - numberOfSentences)
     448        ? expectedNumberOfSentences.AbsoluteDiff(numberOfSentences)
    427449        : Data.WorstResult / 3.0;
    428450
     
    430452      var expectedAverageSentenceLength = example.OutputFloat[0];
    431453      statsError += BenchmarkSuite.Problems.WordStats.GetAverageSentenceLength(printStr, out averageSentenceLength)
    432         ? Math.Abs(expectedAverageSentenceLength - averageSentenceLength)
     454        ? expectedAverageSentenceLength.AbsoluteDiff(averageSentenceLength)
    433455        : Data.WorstResult / 3.0;
    434456
     
    445467
    446468      var expectedLastCharIndex = example.OutputPrint.Length - 1;
     469      var resultLastCharIndex = printStr.Length - 1;
    447470      var result = distance;
    448471
    449       if (expectedLastCharIndex >= 0) {
     472      if (expectedLastCharIndex >= 0 && resultLastCharIndex >= 0) {
    450473        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);
    457477      }
    458478
     
    470490      var expectedNumberOfSyllables = example.OutputInteger[0];
    471491      var numberOfSyllablesError = BenchmarkSuite.Problems.Syllables.GetNumberOfSyllables(printStr, out numberOfSyllables)
    472         ? Math.Abs(expectedNumberOfSyllables - numberOfSyllables)
     492        ? expectedNumberOfSyllables.AbsoluteDiff(numberOfSyllables)
    473493        : Data.WorstResult / 2.0;
    474494
     
    487507      var expectedGrade = example.OutputChar[0];
    488508      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;
    491511
    492512      var result = distance + gradeError;
     
    500520      if (result == double.MinValue || double.IsPositiveInfinity(result) ||
    501521          double.IsNaN(result) ||
     522          // ReSharper disable once CompareOfFloatsByEqualityOperator
    502523          result == double.MaxValue || double.IsNegativeInfinity(result))
    503524        return worst;
     
    576597      return double.IsNaN(diff) || double.IsInfinity(diff)
    577598         ? worstResult
    578          : diff;
     599         : Math.Min(diff, worstResult);
    579600    }
    580601
     
    599620      return double.IsNaN(diff) || double.IsInfinity(diff)
    600621        ? worstResult
    601         : diff;
     622        : Math.Min(diff, worstResult);
    602623    }
    603624  }
Note: See TracChangeset for help on using the changeset viewer.