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

Location:
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/ExpressionsClassDiagramm.cd

    r15344 r15366  
    66      <Method Name="Expression" Hidden="true" />
    77      <Method Name="IPooledObject.Reset" Hidden="true" />
     8      <Property Name="IsProgram" Hidden="true" />
    89    </Members>
    910    <TypeIdentifier>
  • 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  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs

    r15344 r15366  
    228228
    229229    [NonSerialized]
    230     private int? totalExpressionCount;
     230    private int? totalInstructionCount;
    231231    /// <summary>
    232232    /// Returns the amount of none program expressions
    233233    /// </summary>
    234234    /// <returns></returns>
    235     public int TotalExpressionCount
    236     {
    237       get
    238       {
    239         if (totalExpressionCount == null) {
    240           totalExpressionCount = 0;
     235    public int TotalInstructionCount
     236    {
     237      get
     238      {
     239        if (totalInstructionCount == null) {
     240          totalInstructionCount = 0;
    241241
    242242          for (var i = 0; i < Count; i++) {
    243243            var expression = expressions[i];
    244244
    245             totalExpressionCount += expression.IsProgram
    246               ? ((PushProgram)expression).TotalExpressionCount
     245            totalInstructionCount += expression.IsProgram
     246              ? ((PushProgram)expression).TotalInstructionCount
    247247              : 1;
    248248          }
    249249        }
    250250
    251         return totalExpressionCount.Value;
     251        return totalInstructionCount.Value;
    252252      }
    253253    }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs

    r15334 r15366  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
    22  using System;
     3
     4  using HeuristicLab.Common;
    35  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    46  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Selector/LexicaseSelector.cs

    r15345 r15366  
    6464      var random = RandomParameter.ActualValue;
    6565      var maximization = MaximizationParameter.ActualValue.Value;
    66 
    67       //var qualities = QualityParameter.ActualValue;
    68       //.Where(x => IsValidQuality(x.Value))
    69       //.Select(x => x.Value)
    70       //.ToList();
    71 
    7266      var caseQualities = CaseQualitiesParameter.ActualValue.ToList();
    7367
     
    9892      }
    9993
    100       if (caseQualities.Any(x => x.Length != caseQualities[0].Length)) {
     94      var qualitiesLength = caseQualities[0].Length;
     95
     96      if (caseQualities.Any(x => x.Length != qualitiesLength)) {
    10197        throw new ArgumentException("Not all case qualities have the same length");
    10298      }
    10399
    104100      var selected = new IScope[count];
    105       var candidates = Enumerable.Range(0, caseQualities.Count).ToList();
    106       var orderSource = Enumerable.Range(0, caseQualities[0].Length).ToList();
     101
     102      var candidates = new List<int>(caseQualities.Count);
     103      for (var i = 0; i < caseQualities.Count; i++) candidates.Add(i);
     104
     105      var orderSource = new List<int>(qualitiesLength);
     106      for (var i = 0; i < qualitiesLength; i++) orderSource.Add(i);
    107107
    108108      for (var i = 0; i < count; i++) {
     
    140140          : double.PositiveInfinity;
    141141
    142         foreach (var candidate in candidates) {
    143           if (caseQualities[candidate][curCase].IsAlmost(best)) {
     142        for (var i = 0; i < candidates.Count; i++) {
     143          var candidate = candidates[i];
     144          var caseQuality = caseQualities[candidate][curCase];
     145
     146          if (caseQuality.IsAlmost(best)) {
    144147            // if the individuals is as good as the best one, add it
    145148            nextCandidates.Add(candidate);
    146149          } else if (
    147              (maximization && (caseQualities[candidate][curCase] > best)) ||
    148              (!maximization && (caseQualities[candidate][curCase] < best))) {
    149             // if the individuals is better than the best one, remove all previous candidates and add the new one
     150             (maximization && (caseQuality > best)) ||
     151             (!maximization && (caseQuality < best))) {
     152            // if the individual is better than the best one, remove all previous candidates and add the new one
    150153            nextCandidates.Clear();
    151154            nextCandidates.Add(candidate);
    152155            // also set the next best quality value
    153             best = caseQualities[candidate][curCase];
     156            best = caseQuality;
    154157          }
    155158          // else {do nothing}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Views/PushProgramTreeView.cs

    r15032 r15366  
    5555          return EmptyList;
    5656
    57         return string.Format(PushProgramStringFormat, program.TotalExpressionCount, program.Depth);
     57        return string.Format(PushProgramStringFormat, program.TotalInstructionCount, program.Depth);
    5858      }
    5959
Note: See TracChangeset for help on using the changeset viewer.