Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/17 01:15:25 (7 years ago)
Author:
pkimmesw
Message:

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs

    r14834 r14875  
    33
    44namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
     5
    56  using HeuristicLab.BenchmarkSuite;
    67  using HeuristicLab.BenchmarkSuite.Problems;
     8  using HeuristicLab.Common;
    79  using HeuristicLab.Core;
    810  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     
    1315    public static double Evaluate(IPushInterpreter interpreter, PushProgram program, Example example, double worstResult) {
    1416      interpreter.BooleanStack.Push(example.InputBoolean);
    15       interpreter.IntegerStack.Push(example.InputInt);
     17      interpreter.IntegerStack.Push(example.InputInteger);
    1618      interpreter.FloatStack.Push(example.InputFloat);
    1719
    1820      interpreter.Run(program);
    1921
    20       var result = GetDiff(example.OutputInt, interpreter.IntegerStack, worstResult, LongDiffer)
    21                  + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, DoubleDiffer)
    22                  + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer);
     22      var result = GetDiff(example.OutputInteger, interpreter.IntegerStack, worstResult, IntegerDiffer)
     23                 + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, FloatDiffer)
     24                 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer)
     25                 + GetDiff(example.OutputString, interpreter.StringStack, worstResult, StringDiffer)
     26                 + GetDiff(example.OutputChar, interpreter.CharStack, worstResult, CharDiffer)
     27                 + GetVectorDiff(example.OutputIntegerVector, interpreter.IntegerVectorStack, worstResult, (a, b) => VectorDiffer(a, b, IntegerDiffer))
     28                 + GetVectorDiff(example.OutputFloatVector, interpreter.FloatVectorStack, worstResult, (a, b) => VectorDiffer(a, b, FloatDiffer))
     29                 + GetVectorDiff(example.OutputStringVector, interpreter.StringVectorStack, worstResult, (a, b) => VectorDiffer(a, b, StringDiffer));
    2330
    2431      return result;
    2532    }
    2633
    27     public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {
     34    public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, ProblemData data, int startIndex, int endIndex) {
    2835      var length = endIndex - startIndex;
    2936      if (length <= 0) return null;
     
    4552    }
    4653
    47     //public static EvaluationResult Evaluate(Individual individual, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {
    48     //  var program = individual.ToPushProgram(pool.PushGpConfiguration);
    49     //  return Evaluate(program, pool, random, data, startIndex, endIndex);
    50     //}
    51 
    52     private static double DoubleDiffer(double a, double b) {
     54    private static double FloatDiffer(double a, double b) {
    5355      var result = a - b;
    5456
    55       if (result == double.MinValue || double.IsPositiveInfinity(result) || double.IsNaN(result))
     57      if (result.IsAlmost(double.MinValue) || double.IsPositiveInfinity(result) || double.IsNaN(result))
    5658        return double.MaxValue;
    5759
    58       if (result == double.MaxValue || double.IsNegativeInfinity(result))
     60      if (result.IsAlmost(double.MaxValue) || double.IsNegativeInfinity(result))
    5961        return double.MinValue;
    6062
     
    6264    }
    6365
    64     private static double LongDiffer(long a, long b) {
     66    private static double IntegerDiffer(long a, long b) {
    6567      var result = a - b;
    6668
     
    7072    private static double BooleanDiffer(bool a, bool b) {
    7173      return a && b ? 0 : a || b ? 1 : 2;
     74    }
     75
     76    private static double StringDiffer(string a, string b) {
     77      return LevenshteinDistance(a, b);
     78    }
     79
     80    private static double CharDiffer(char a, char b) {
     81      return IntegerDiffer(a, b);
     82    }
     83
     84    private static double VectorDiffer<T>(IReadOnlyList<T> a, IReadOnlyList<T> b, Func<T, T, double> differ) {
     85      var length = Math.Min(a.Count, b.Count);
     86      var result = 0d;
     87
     88      for (var i = 0; i < length; i++) {
     89        result += differ(a[i], b[i]);
     90      }
     91
     92      if (a.Count > b.Count) {
     93        var remainingLength = a.Count - length;
     94        for (var i = 0; i < remainingLength; i++)
     95          result += differ(a[i], default(T));
     96      }
     97
     98      return result;
     99    }
     100
     101    private static double GetVectorDiff<T>(IReadOnlyList<IReadOnlyList<T>> estimated, IPushStack<List<T>> resultStack, double worstResult, Func<IReadOnlyList<T>, IReadOnlyList<T>, double> differ)
     102      where T : IComparable {
     103      if (estimated.Count == 0) return 0d;
     104
     105      var diff = 0d;
     106      var comparableLength = 0;
     107
     108      if (!resultStack.IsEmpty) {
     109        var count = Math.Min(estimated.Count, resultStack.Count);
     110        var result = resultStack.Peek(count);
     111        comparableLength = Math.Min(estimated.Count, result.Length);
     112
     113        for (var i = 0; i < comparableLength; i++) {
     114          diff += Math.Min(differ(estimated[i], result[i]), worstResult);
     115        }
     116      }
     117
     118      var emptyTArray = new T[0];
     119      for (var i = comparableLength; i < estimated.Count - comparableLength; i++) {
     120        diff += differ(estimated[i], emptyTArray);
     121      }
     122
     123      return diff;
    72124    }
    73125
     
    95147      return diff;
    96148    }
     149
     150    /// <summary>
     151    /// https://www.dotnetperls.com/levenshtein
     152    /// </summary>
     153    /// <param name="s"></param>
     154    /// <param name="t"></param>
     155    /// <returns></returns>
     156    private static int LevenshteinDistance(string s, string t) {
     157      if (s == null && t == null) return 0;
     158      if (s == null) return t.Length;
     159      if (t == null) return s.Length;
     160
     161      int n = s.Length;
     162      int m = t.Length;
     163      int[,] d = new int[n + 1, m + 1];
     164
     165      // Step 1
     166      if (n == 0) {
     167        return m;
     168      }
     169
     170      if (m == 0) {
     171        return n;
     172      }
     173
     174      // Step 2
     175      for (int i = 0; i <= n; d[i, 0] = i++) {
     176      }
     177
     178      for (int j = 0; j <= m; d[0, j] = j++) {
     179      }
     180
     181      // Step 3
     182      for (int i = 1; i <= n; i++) {
     183        //Step 4
     184        for (int j = 1; j <= m; j++) {
     185          // Step 5
     186          int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
     187
     188          // Step 6
     189          d[i, j] = Math.Min(
     190              Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
     191              d[i - 1, j - 1] + cost);
     192        }
     193      }
     194      // Step 7
     195      return d[n, m];
     196    }
    97197  }
    98198}
Note: See TracChangeset for help on using the changeset viewer.