- Timestamp:
- 09/17/17 23:27:05 (7 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Evaluator/PushBenchmarkSuiteEvaluator.cs
r15345 r15366 220 220 long result; 221 221 if (long.TryParse(printResult, out result)) { 222 var diff = Math.Abs(result -example.OutputInteger[0]);222 var diff = result.AbsoluteDiff(example.OutputInteger[0]); 223 223 224 224 return Math.Min(Data.WorstResult, diff); … … 236 236 double value; 237 237 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); 239 239 diff = Math.Min(diff, Data.WorstResult); 240 240 … … 263 263 var printResult = interpreter.PrintStack.ToString(); 264 264 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 }); 285 288 286 289 var lineCountWithInvalidFormat = example.OutputPrintLineCount - lineCountWithCorrectFormat; … … 300 303 } 301 304 302 var distance = example.OutputPrint.LevenshteinDistance(interpreter.PrintStack.ToString()); 305 var printResult = interpreter.PrintStack.ToString(); 306 var distance = example.OutputPrint.LevenshteinDistance(printResult); 307 303 308 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()); 308 313 309 314 var compareLength = Math.Min(printLines.Length, example.OutputPrintLineCount); 310 long integerError = 0;315 var integerError = 0L; 311 316 312 317 for (var i = 0; i < compareLength; i++) { 313 318 long value; 314 319 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]); 320 321 } 321 322 } 322 323 323 var result = distance + lineCountWithInvalidFormat + integerError ;324 var result = distance + lineCountWithInvalidFormat + integerError / compareLength; 324 325 return Math.Min(result, Data.WorstResult); 325 326 } … … 347 348 348 349 // 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; 352 359 353 360 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]; 355 366 } 356 367 … … 372 383 var distance = example.OutputPrint.LevenshteinDistance(printResult); 373 384 var lineCount = interpreter.PrintStack.Count; 374 var lineCountError = Math.Abs(example.OutputPrintLineCount -lineCount);385 var lineCountError = example.OutputPrintLineCount.AbsoluteDiff(lineCount); 375 386 var totalWordsPerLineError = 0L; 376 387 377 388 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); 379 390 } 380 391 381 392 // last line 382 393 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); 384 395 385 396 var result = distance + lineCountError + totalWordsPerLineError; … … 396 407 397 408 // 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; 401 418 402 419 for (var i = 0; i < comparableLength; i++) { 403 420 var expectedStr = expectedVector[i].ToString(); 404 421 var resultStr = resultVector[i].ToString(); 422 405 423 result += expectedStr.LevenshteinDistance(resultStr); 424 } 425 426 for (var i = comparableLength; i < expectedVector.Length; i++) { 427 result += expectedVector[i].ToString().Length; 406 428 } 407 429 … … 424 446 var expectedNumberOfSentences = example.OutputInteger[0]; 425 447 statsError += BenchmarkSuite.Problems.WordStats.GetNumberOfSentences(printStr, out numberOfSentences) 426 ? Math.Abs(expectedNumberOfSentences -numberOfSentences)448 ? expectedNumberOfSentences.AbsoluteDiff(numberOfSentences) 427 449 : Data.WorstResult / 3.0; 428 450 … … 430 452 var expectedAverageSentenceLength = example.OutputFloat[0]; 431 453 statsError += BenchmarkSuite.Problems.WordStats.GetAverageSentenceLength(printStr, out averageSentenceLength) 432 ? Math.Abs(expectedAverageSentenceLength -averageSentenceLength)454 ? expectedAverageSentenceLength.AbsoluteDiff(averageSentenceLength) 433 455 : Data.WorstResult / 3.0; 434 456 … … 445 467 446 468 var expectedLastCharIndex = example.OutputPrint.Length - 1; 469 var resultLastCharIndex = printStr.Length - 1; 447 470 var result = distance; 448 471 449 if (expectedLastCharIndex >= 0 ) {472 if (expectedLastCharIndex >= 0 && resultLastCharIndex >= 0) { 450 473 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); 457 477 } 458 478 … … 470 490 var expectedNumberOfSyllables = example.OutputInteger[0]; 471 491 var numberOfSyllablesError = BenchmarkSuite.Problems.Syllables.GetNumberOfSyllables(printStr, out numberOfSyllables) 472 ? Math.Abs(expectedNumberOfSyllables -numberOfSyllables)492 ? expectedNumberOfSyllables.AbsoluteDiff(numberOfSyllables) 473 493 : Data.WorstResult / 2.0; 474 494 … … 487 507 var expectedGrade = example.OutputChar[0]; 488 508 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; 491 511 492 512 var result = distance + gradeError; … … 500 520 if (result == double.MinValue || double.IsPositiveInfinity(result) || 501 521 double.IsNaN(result) || 522 // ReSharper disable once CompareOfFloatsByEqualityOperator 502 523 result == double.MaxValue || double.IsNegativeInfinity(result)) 503 524 return worst; … … 576 597 return double.IsNaN(diff) || double.IsInfinity(diff) 577 598 ? worstResult 578 : diff;599 : Math.Min(diff, worstResult); 579 600 } 580 601 … … 599 620 return double.IsNaN(diff) || double.IsInfinity(diff) 600 621 ? worstResult 601 : diff;622 : Math.Min(diff, worstResult); 602 623 } 603 624 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r15344 r15366 228 228 229 229 [NonSerialized] 230 private int? total ExpressionCount;230 private int? totalInstructionCount; 231 231 /// <summary> 232 232 /// Returns the amount of none program expressions 233 233 /// </summary> 234 234 /// <returns></returns> 235 public int Total ExpressionCount236 { 237 get 238 { 239 if (total ExpressionCount == null) {240 total ExpressionCount = 0;235 public int TotalInstructionCount 236 { 237 get 238 { 239 if (totalInstructionCount == null) { 240 totalInstructionCount = 0; 241 241 242 242 for (var i = 0; i < Count; i++) { 243 243 var expression = expressions[i]; 244 244 245 total ExpressionCount += expression.IsProgram246 ? ((PushProgram)expression).Total ExpressionCount245 totalInstructionCount += expression.IsProgram 246 ? ((PushProgram)expression).TotalInstructionCount 247 247 : 1; 248 248 } 249 249 } 250 250 251 return total ExpressionCount.Value;251 return totalInstructionCount.Value; 252 252 } 253 253 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs
r15334 r15366 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 2 2 using System; 3 4 using HeuristicLab.Common; 3 5 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 4 6 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Selector/LexicaseSelector.cs
r15345 r15366 64 64 var random = RandomParameter.ActualValue; 65 65 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 72 66 var caseQualities = CaseQualitiesParameter.ActualValue.ToList(); 73 67 … … 98 92 } 99 93 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)) { 101 97 throw new ArgumentException("Not all case qualities have the same length"); 102 98 } 103 99 104 100 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); 107 107 108 108 for (var i = 0; i < count; i++) { … … 140 140 : double.PositiveInfinity; 141 141 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)) { 144 147 // if the individuals is as good as the best one, add it 145 148 nextCandidates.Add(candidate); 146 149 } else if ( 147 (maximization && (caseQualit ies[candidate][curCase]> best)) ||148 (!maximization && (caseQualit ies[candidate][curCase]< best))) {149 // if the individual sis better than the best one, remove all previous candidates and add the new one150 (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 150 153 nextCandidates.Clear(); 151 154 nextCandidates.Add(candidate); 152 155 // also set the next best quality value 153 best = caseQualit ies[candidate][curCase];156 best = caseQuality; 154 157 } 155 158 // else {do nothing} -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Views/PushProgramTreeView.cs
r15032 r15366 55 55 return EmptyList; 56 56 57 return string.Format(PushProgramStringFormat, program.Total ExpressionCount, program.Depth);57 return string.Format(PushProgramStringFormat, program.TotalInstructionCount, program.Depth); 58 58 } 59 59
Note: See TracChangeset
for help on using the changeset viewer.