void NextGeneration() { UIScript.instance.generation.text = "Gen : " + ++genCount; //evalute agent fitness float maxScore = GetMaxScore(); UIScript.instance.prevScoreText.text = "PrevScore : " + maxScore; //group them //discard the bad agents //select the good agents List <GameObject> sucessors = GetGoodAgents(maxScore); if (sucessors.Count == 0) { UIScript.instance.generation.text = "Gen : Extinct"; return; } int goodAgentCount = sucessors.Count; int remainingAgent = noOfAgent - goodAgentCount; //fill remaining population crossover for (int i = 0; i < remainingAgent; i++) { GameObject temp = Instantiate(agentPrefab, Vector3.zero + agentParent.position, Quaternion.identity, agentParent); if (Random.value < crossOverProbability) { AgentScript a1 = sucessors[Random.Range(0, goodAgentCount)].GetComponent <AgentScript>(); AgentScript a2 = sucessors[Random.Range(0, goodAgentCount)].GetComponent <AgentScript>(); float[][,] tempWeights = CrossOverWeights(a1.neural.GetWeights(), a2.neural.GetWeights()); float[][] tempBias = CrossOverBias(a1.neural.GetBias(), a2.neural.GetBias()); temp.GetComponent <AgentScript>().Intialize(tempWeights, tempBias, neuronsPerLayer); } else { AgentScript a = sucessors[Random.Range(0, goodAgentCount)].GetComponent <AgentScript>(); float[][,] tempWeights = MutationWeights(a.neural.GetWeights()); float[][] tempBias = MutationBias(a.neural.GetBias()); temp.GetComponent <AgentScript>().Intialize(tempWeights, tempBias, neuronsPerLayer); } //mutate agent //add them to agentList agentList.Add(temp); } foreach (GameObject go in sucessors) { if (go) { if (elitism) { GameObject temp = Instantiate(agentPrefab, Vector3.zero + agentParent.position, Quaternion.identity, agentParent); AgentScript a = go.GetComponent <AgentScript>(); float[][,] tempWeights = a.neural.GetWeights(); float[][] tempBias = a.neural.GetBias(); AgentScript b = temp.GetComponent <AgentScript>(); b.Intialize(tempWeights, tempBias, neuronsPerLayer); //Debug.Log("bs : "+a.neural.CompareBias(b.neural.GetBias())); agentList.Add(temp); } Destroy(go); } } //adding //clearobstacle ClearObstacle(); //clear score score = 0f; //start obstacle spawner StartCoroutine(CreateObject()); CurrentAgent = agentList.Count; }