void InstantiateRodents(int amount) { rodents = new Rodent[amount]; Debug.Log("Instantiating " + amount + " Rodents"); Vector3 oldScale = worldParent.transform.localScale; worldParent.transform.localScale = new Vector3(oldScale.x, oldScale.y, (amount + 1) * zSpacing); Vector3 oldPos = worldParent.transform.position; worldParent.transform.position = new Vector3(oldPos.x, oldPos.y, .5f * amount * zSpacing); for (int i = 0; i < amount; i++) { Rodent newRodent = Instantiate <Rodent>(rodentPrefab, Vector3.zero, Quaternion.identity, rodentParent); newRodent.index = i; newRodent.transform.position = new Vector3(0f, 0f, i * zSpacing); rodents[i] = newRodent; } Debug.Log("Instantiated the Rodents"); cameraTarget = rodents[0]; }
void FixedUpdate() { if (!runningSimulation) { return; } float highestCurrentX = 0; for (int i = 0; i < rodents.Length; i++) { if (rodents[i].alive) { if (!cameraTarget.alive) { cameraTarget = rodents[i]; } float beforeX = rodents[i].transform.position.x; highestCurrentX = (beforeX > highestCurrentX) ? beforeX : highestCurrentX; rodents[i].move(Time.fixedDeltaTime * moveSpeedModifier); float[] inputs = rodents[i].getInput(); float[] result = generationNetworks[i].compute(inputs); // if(i == 0) // { // Debug.Log("<color=green>NN: " + string.Join(", ", System.Array.ConvertAll(result, val => val.ToString())) + "</color>"); // } if (result[0] > jumpTrigger) { rodents[i].jump(); } if (!rodents[i].alive) { // Debug.Log(i + " died"); nnManager.score(generationNetworks[i], beforeX); stillAlive--; } } } mapGenerator.currentPosition = highestCurrentX; currentFitness = (int)highestCurrentX; highestFitness = (currentFitness > highestFitness) ? currentFitness : highestFitness; uiManager.fitness = Mathf.RoundToInt(currentFitness); uiManager.highestFitness = Mathf.RoundToInt(highestFitness); uiManager.alive = stillAlive; if (stillAlive <= 0) { Debug.Log("ALL DEAD!"); runningSimulation = false; file.WriteLine(string.Format("{0}: {1}", generation, currentFitness)); StartGen(); } }