public void AllAgentsKilledCallback(List <AgentObject> agents, List <Species> species, int generation) { //Destry all player foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("Player")) { Destroy(gameObject); } GenerationEvaluator eval = new GenerationEvaluator(agents, species, generation); AgentObject bestAgent = eval.BestAgent; Debug.Log("All agents dead. Best fitness: " + bestAgent.GetFitness() + ", Average: " + eval.AverageFitness); //GUI Values _bestFitnessLastGeneration = bestAgent.GetFitness(); _averageFitnessLastGeneration = eval.AverageFitness; if (_bestTotalFitness < _bestFitnessLastGeneration) { _bestTotalFitness = _bestFitnessLastGeneration; } if (_bestAverageFitness < _averageFitnessLastGeneration) { _bestAverageFitness = _averageFitnessLastGeneration; } //Start next generation if the evaluation is running if (_evaluationRunning) { _manager.GenerateNextGeneration(); } }
private AgentObject GetBestAgent(List <AgentObject> agents) { AgentObject best = null; foreach (AgentObject agent in agents) { if (best == null || best.GetFitness() < agent.GetFitness()) { best = agent; } } return(best); }
public void AllAgentsKilledCallback(List <AgentObject> agents, List <Species> species, int generation) { //Destry all player foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("Player")) { Destroy(gameObject); } GenerationEvaluator eval = new GenerationEvaluator(agents, species, generation); AgentObject bestAgent = eval.BestAgent; Debug.Log("All agents dead. Best fitness: " + bestAgent.GetFitness() + ", Average: " + eval.AverageFitness); //Fill the screen FillXorScreen(bestAgent.Genome, _screen, _screenObjPrefab); SetDisplay(bestAgent.Genome); if (CanAgentSolveXOR(bestAgent) || eval.Generation > 500) { foreach (NodeGene node in bestAgent.Genome.Nodes.Values) { Debug.Log("Node: " + node.ID + ", Type: " + node.Type); } foreach (ConnectionGene connection in bestAgent.Genome.Connections.Values) { if (connection.Expressed) { Debug.Log("Connection: In: " + connection.InNode + " Out: " + connection.OutNode + ", Weight: " + connection.Weight + ", InnovationNumber: " + connection.InnovationNumber); } } result.Add(eval.Generation); if (result.Count < amountRuns) { _manager.CreateInitialPopulation(startingGenome, new GeneCounter(6), new GeneCounter(9), 400); } else { Debug.Log("FINIIIISHED"); } } else { _manager.GenerateNextGeneration(); } }
/// <summary> /// Return a list with the best genomes in each species /// </summary> /// <param name="species">the list of species that will be searched</param> /// <returns>the List with the best genomes</returns> public List <Genome> GetBestGenomeForSpecies(List <Species> species) { AgentObject bestAgent = null; List <Genome> bestGenomes = new List <Genome>(); foreach (Species s in species) { s.SortMembersByFitness(); AgentObject agent = s.Members[0]; if (bestAgent == null || bestAgent.GetFitness() <= agent.GetFitness()) { bestAgent = agent; } // species with less than 5 members can not save reproduce if (s.Members.Count < 5) { continue; } if (agent == null) { throw new System.Exception("Agent is null!"); } bestGenomes.Add(agent.Genome); } //If the best agent is not yet in the list (because the species has less then 5 members) add the agenbt if (!bestGenomes.Contains(bestAgent.Genome)) { bestGenomes.Add(bestAgent.Genome); } return(bestGenomes); }