Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs
- Timestamp:
- 04/18/17 01:15:25 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs
r14834 r14875 3 3 4 4 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem { 5 5 6 using HeuristicLab.BenchmarkSuite; 6 7 using HeuristicLab.BenchmarkSuite.Problems; 8 using HeuristicLab.Common; 7 9 using HeuristicLab.Core; 8 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; … … 13 15 public static double Evaluate(IPushInterpreter interpreter, PushProgram program, Example example, double worstResult) { 14 16 interpreter.BooleanStack.Push(example.InputBoolean); 15 interpreter.IntegerStack.Push(example.InputInt );17 interpreter.IntegerStack.Push(example.InputInteger); 16 18 interpreter.FloatStack.Push(example.InputFloat); 17 19 18 20 interpreter.Run(program); 19 21 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)); 23 30 24 31 return result; 25 32 } 26 33 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) { 28 35 var length = endIndex - startIndex; 29 36 if (length <= 0) return null; … … 45 52 } 46 53 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) { 53 55 var result = a - b; 54 56 55 if (result == double.MinValue|| double.IsPositiveInfinity(result) || double.IsNaN(result))57 if (result.IsAlmost(double.MinValue) || double.IsPositiveInfinity(result) || double.IsNaN(result)) 56 58 return double.MaxValue; 57 59 58 if (result == double.MaxValue|| double.IsNegativeInfinity(result))60 if (result.IsAlmost(double.MaxValue) || double.IsNegativeInfinity(result)) 59 61 return double.MinValue; 60 62 … … 62 64 } 63 65 64 private static double LongDiffer(long a, long b) {66 private static double IntegerDiffer(long a, long b) { 65 67 var result = a - b; 66 68 … … 70 72 private static double BooleanDiffer(bool a, bool b) { 71 73 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; 72 124 } 73 125 … … 95 147 return diff; 96 148 } 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 } 97 197 } 98 198 }
Note: See TracChangeset
for help on using the changeset viewer.