/// <summary> /// Updates the current TopKAgents with any new highscores in the passed agents. /// </summary> public void Update(List <Agent> agents) { AddAnyTopAgents(agents); BestAgents.Sort(); // If highscore list is filled, remove lowest score if (BestAgents.Count > K) { BestAgents.RemoveRange(K, BestAgents.Count - K); } // Calculate average. AverageFitness = BestAgents.Average(b => b.Agent.Fitness); // Assign best. Best = BestAgents.First(); }
// Goes through the passed agents and add agents who's fitness value exceeds the current top agents. private void AddAnyTopAgents(List <Agent> agents) { if (BestAgents.Count == 0) // Check if list is empty { BestAgents.Add(new TopKAgent(agents.First())); } for (int i = 0; i < agents.Count; i++) { // Check if new highscore. if (agents[i].Fitness > BestAgents.Last().Agent.Fitness) { BestAgents.Add(new TopKAgent(agents[i].GetCopy())); } } }