Changeset 14834 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs
- Timestamp:
- 04/10/17 00:27:31 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs
r14777 r14834 6 6 using HeuristicLab.BenchmarkSuite.Problems; 7 7 using HeuristicLab.Core; 8 using HeuristicLab.Optimization;9 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; … … 19 18 interpreter.Run(program); 20 19 21 return GetDiff(example.OutputInt, interpreter.IntegerStack, worstResult, LongDiffer) 22 + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, DoubleDiffer) 23 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer); 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); 23 24 return result; 24 25 } 25 26 26 public static double Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) { 27 if (endIndex - startIndex <= 0) return default(double); 28 var result = 0d; 27 public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) { 28 var length = endIndex - startIndex; 29 if (length <= 0) return null; 30 31 var evaluationResult = new EvaluationResult(length); 29 32 30 33 using (var interpreter = pool.Create(random)) { 31 34 for (var i = startIndex; i < endIndex; i++) { 32 result += Evaluate(interpreter, program, data.Examples[i], data.WorstResult); 33 interpreter.Clear(); 35 var result = Evaluate(interpreter, program, data.Examples[i], data.WorstResult); 36 evaluationResult.ExampleQualities[i - startIndex] = result; 37 evaluationResult.TotalQuality += result; 38 interpreter.Reset(); 34 39 } 35 40 } 36 41 37 return result / (endIndex - startIndex); 42 evaluationResult.TotalQuality /= length; 43 44 return evaluationResult; 38 45 } 39 46 40 public static doubleEvaluate(Individual individual, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {41 var program = individual.PushProgram(pool.PushGpConfiguration.EnabledExpressions as IReadOnlyList<string>);42 return Evaluate(program, pool, random, data, startIndex, endIndex);43 }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 //} 44 51 45 52 private static double DoubleDiffer(double a, double b) { 46 53 var result = a - b; 47 54 48 return result == double.MinValue ? double.MaxValue : Math.Abs(result); 55 if (result == double.MinValue || double.IsPositiveInfinity(result) || double.IsNaN(result)) 56 return double.MaxValue; 57 58 if (result == double.MaxValue || double.IsNegativeInfinity(result)) 59 return double.MinValue; 60 61 return Math.Abs(result); 49 62 } 50 63 … … 59 72 } 60 73 61 private static double GetDiff<T>(IReadOnlyList<T> estimated, I Stack<T> resultStack, double worstResult, Func<T, T, double> differ)74 private static double GetDiff<T>(IReadOnlyList<T> estimated, IPushStack<T> resultStack, double worstResult, Func<T, T, double> differ) 62 75 where T : IComparable { 76 if (estimated.Count == 0) return 0d; 63 77 64 var count = Math.Min(estimated.Count, resultStack.Count);65 var result = resultStack.Peek(count);66 var comparableLength = Math.Min(estimated.Count, result.Length);67 78 var diff = 0d; 79 var comparableLength = 0; 68 80 69 for (var i = 0; i < comparableLength; i++) { 70 diff += Math.Min(differ(estimated[i], result[0]), worstResult); 81 if (!resultStack.IsEmpty) { 82 var count = Math.Min(estimated.Count, resultStack.Count); 83 var result = resultStack.Peek(count); 84 comparableLength = Math.Min(estimated.Count, result.Length); 85 86 for (var i = 0; i < comparableLength; i++) { 87 diff += Math.Min(differ(estimated[i], result[i]), worstResult); 88 } 71 89 } 72 90 73 if (estimated.Count > result.Length) {74 diff += worstResult * (estimated.Count - comparableLength);91 for (var i = comparableLength; i < estimated.Count - comparableLength; i++) { 92 diff += differ(estimated[i], default(T)); 75 93 } 76 94
Note: See TracChangeset
for help on using the changeset viewer.