private void RespawnAllCars() { for (int carIndex = 0; carIndex < popSize; carIndex++) { GameObject carS = carSpecies[carIndex]; Respawn r = carS.GetComponent <Respawn>(); Car2DController c2Dc = carS.GetComponent <Car2DController>(); //float distanceFitness = Mathf.Pow(1.1f, c2Dc.TravelledDistance) + Mathf.Pow(1.1f, Vector2.Distance(rbCar[carIndex].position, r.respawnPoint.position)); float distanceFitness = c2Dc.TravelledDistance + Vector2.Distance(rbCar[carIndex].position, r.respawnPoint.position); population.Members[carIndex].Fitness += distanceFitness; if (!r.isAlive && level.penalizeDeath) { //double timeFactor = c2Dc.TimeAlive / (1.05 * level.MaxTime); //population.Members[carIndex].Fitness *= timeFactor; //double timeFactor = Functions.Map(c2Dc.TimeAlive, 0, level.MaxTime, 0.9, 0.95); //population.Members[carIndex].Fitness *= timeFactor; //double linearCoefficientOfTime = Functions.Line(c2Dc.TimeAlive, 10.0 / level.MaxTime, -5.0); //double constraintlinearCoefficientOfTime = Functions.Logistic(linearCoefficientOfTime); //population.Members[carIndex].Fitness *= constraintlinearCoefficientOfTime; population.Members[carIndex].Fitness *= TimeEffectWithRespectToCurrentGeneration(c2Dc.TimeAlive, population.CurrentGeneration, level.MaxTime, level.thresholdGeneration, level.timeMattersAtTheBeginning, level.lessTimeIsBetter); //Currently works! //double sensorCoefficient = 1 / c2Dc.CumulativeSensoryData; } //Debug.Log(population.Members[carIndex].Fitness + " of car #" + carIndex.ToString()); carS.GetComponent <SpriteRenderer>().color = new Color32(255, 255, 255, 255); r.RespawnCar(); } bestCar.GetComponent <Respawn>().RespawnCar(); bestCar.GetComponent <SpriteRenderer>().color = new Color32(0, 255, 0, 255); carsHaveRespawned = true; population.SortSpeciesByFitness(); population.NormalizeFitness(); population.FilterByPerformance(); double bestFitness = population.ToBreed(); // make mutation rate dynamic guiManager.startTime = Time.time; guiManager.numOfGenerations = population.CurrentGeneration; guiManager.bestFitness = bestFitness; elapsedTime = 0.0f; AssignBrains(); //Every 50 generation saves progress if (level.ToggleFileIO) { if (population.CurrentGeneration % 50 == 0) { SaveBestBrain("bestBrain.txt", population.BestMember); SavePopulationToFile("population.bin"); } Debug.Log(bestFitness); } }
// Update is called once per frame void Update() { // Find the best car and follow it EnvironmentManager manager = GameObject.FindObjectOfType <EnvironmentManager>(); manager.cars.Sort(); Car2DController bestCar = manager.cars[manager.populationSize - 1]; dashboard.UpdateMaxFitness(bestCar.fitness); transform.position = new Vector3(bestCar.gameObject.transform.position.x, bestCar.gameObject.transform.position.y, -30f); }
// Use this for initialization void Start() { isAlive = true; thisRbCar = GetComponent <Rigidbody2D>(); //car2D = FindObjectOfType<Car2DController>(); car2D = GetComponent <Car2DController>(); respawnPoint = GameObject.Find("Respawn Point").transform; RespawnCar(); }
// Start is called before the first frame update void Awake() { generation = 0; Time.timeScale = 1.0f; //If the size is not even we can't mutate properly if (populationSize % 2 != 0) { populationSize = 50; } cars = new List <Car2DController>(); for (int i = 0; i < populationSize; i++) { Car2DController car = (Instantiate(prefab, new Vector3(0, 0, -1), this.transform.rotation)).GetComponent <Car2DController>(); //car.myNN.Load("Assets/Scripts/NN-Weights/Fitness_Max.txt"); cars.Add(car); } dashboard.InitializeDashboard(this.GetGenerationData()); InvokeRepeating("RespawnPopulation", 3.0f, 3.0f); }