public QapResult <T> SearchBest <T>(Func <IEnumerable <T>, float> scoringStrategy, IEnumerable <T> startingPermutation, CancelationToken cancelationToken) { var score = scoringStrategy.Invoke(startingPermutation); var steps = 0L; var best = new QapResult <T>(startingPermutation, score, steps); var seenSolutions = 0L; var watch = new Stopwatch(); watch.Start(); while (!cancelationToken.IsCancelationPending()) { bool hasNewBest = false; foreach (var permutation in QapMath.Opt2(best.Solution)) { steps++; seenSolutions++; score = scoringStrategy.Invoke(permutation); if (score < best.Score) { best = new QapResult <T>(permutation, score, steps, seenSolutions); best.FoundIn = watch.Elapsed; hasNewBest = true; } } if (!hasNewBest) { break; } } watch.Stop(); best.TotalSolutionsSeen = seenSolutions; return(best); }
public QapResult <T> SearchBest <T>(Func <IEnumerable <T>, float> scoringStrategy, IEnumerable <T> startingPermutation, CancelationToken cancelationToken) { var steps = 0L; var best = new QapResult <T>(startingPermutation, scoringStrategy.Invoke(startingPermutation), steps); var last = startingPermutation; var watch = new Stopwatch(); watch.Start(); while (!cancelationToken.IsCancelationPending()) { steps++; last = QapMath.Permutate(last); var score = scoringStrategy.Invoke(last); if (score < best.Score) { best = new QapResult <T>(last, score, steps, steps); best.FoundIn = watch.Elapsed; } } watch.Stop(); best.TotalSolutionsSeen = steps; return(best); }