private Individual ChooseRandomLINQ() { double r = random.NextDouble(); var set = Individuals.OrderBy(x => r); Individual selected = set.SkipWhile(x => x.Probality > r).FirstOrDefault(); return((selected != null) ? selected : set.First()); }
private void AddEliteFromPreviousGeneration(List <Individual> elitisms) { if (Elitism > 0) { var worstIndividuals = Individuals.OrderBy(s => s.Fitness).Take(Elitism).ToList(); for (var i = 0; i < elitisms.Count; i++) { var index = Individuals.IndexOf(Individuals.Where(s => s.Fitness == worstIndividuals[i].Fitness).FirstOrDefault()); Individuals[index] = elitisms[i]; } } }
/// <summary> /// Returns a genome from the individual in this generation with the lowest number of completed matches. /// </summary> /// <returns>genome of a competetor from this generation</returns> public string PickCompetitor() { List <Individual> validCompetitors = Individuals .OrderBy(i => i.MatchesPlayed) .ThenBy(i => _rng.NextDouble()) .ToList(); var best = validCompetitors.FirstOrDefault(); //Debug.Log("Picked Individual has played " + best.MatchesPlayed); if (best != null) { return(best.Genome); } return(null); }
private Individual SelectIndividualByTournament() { var set = Individuals.OrderBy(x => random.NextDouble()); Individual[] competitors = set.Take(tournamentGroupSize).ToArray(); Individual winner = competitors[0]; for (int i = 1; i < competitors.Length; i++) { if (winner.Fitness < competitors[i].Fitness) { winner = competitors[i]; } } return(winner); }