protected virtual void Reset() { pause = true; Debug.Log("Entered resetting"); //Posoruj agentów pod względem funkcji przystosowania List <GameObject> SortedList = agents.OrderByDescending(c => c.GetComponent <Fitness>().GetFitness()).ToList(); agents = SortedList; Debug.Log("Sorted cars"); int chromosomeSize = agents[0].GetComponent <Core>().GetWeights().Length; //Preserve car with best fitness if (agents[0].GetComponent <Fitness>().GetFitness() > bestFitness) { bestFitness = agents[0].GetComponent <Fitness>().GetFitness(); bestCar = new double[chromosomeSize]; agents[0].GetComponent <Core>().GetWeights().CopyTo(bestCar, 0); Debug.Log("New best fitness: " + bestFitness); } if (agents[0].GetComponent <Fitness>().GetFitness() >= 306) { //SavePopulation(); } //save log Debug.Log("Calculating avg fitness"); float avgFitness = 0; for (int i = 0; i < agents.Count; i++) { avgFitness += agents[i].GetComponent <Fitness>().GetFitness(); } avgFitness /= agents.Count; string line = generation + " " + agents[0].GetComponent <Fitness>().GetFitness() + " " + bestFitness + " " + avgFitness; log.Add(line); Debug.Log("Saving log"); SaveLog(); //Pobierz wartosci funkcji przystosowania //od agentow double[] fitness = new double[agents.Count]; for (int i = 0; i < agents.Count; i++) { fitness[i] = agents[i].GetComponent <Fitness>().GetFitness(); } Debug.Log("Creating new children"); double[][] childrenWeights = new double[amount][]; for (int i = 0; i < amount; i++) { childrenWeights[i] = new double[chromosomeSize]; } //Assign best car and best car from current generation //childrenWeights[0] = cars[0].GetComponent<NeuralNetwork2>().GetWeights(); agents[0].GetComponent <Core>().GetWeights().CopyTo(childrenWeights[0], 0); if (bestCar != null) { bestCar.CopyTo(childrenWeights[1], 0); } else { agents[1].GetComponent <Core>().GetWeights().CopyTo(childrenWeights[1], 0); } Debug.Log("Creating new generation"); int index = 2; int limit = amount; if (addNewEachGen) { limit = amount - 2; } while (index < limit) { //Wybierz indeksy rodzicow int parent1index = genetics.rouletteSelect(fitness); int parent2index = genetics.rouletteSelect(fitness); //Upewnij sie ze rodzice nie sa tacy sami while (parent1index == parent2index) { parent2index = genetics.rouletteSelect(fitness); } //Pobierz wagi rodzicow double[] parent1 = agents[parent1index].GetComponent <Core>().GetWeights(); double[] parent2 = agents[parent2index].GetComponent <Core>().GetWeights(); //Dokonaj krzyzowania double[] child1; double[] child2; genetics.Crossover(parent1, parent2, out child1, out child2, crossType); child1.CopyTo(childrenWeights[index], 0); index++; child2.CopyTo(childrenWeights[index], 0); index++; //Debug.Log("Parent1: " + parent1index + ", parent2: " + parent2index); } while (index < amount) { Debug.Log("nowe typy"); double[] randWeights = agents[0].GetComponent <Core>().GetRandomWeights(); randWeights.CopyTo(childrenWeights[index], 0); index++; } Debug.Log("Starting mutation"); //Mutate for (int i = 0; i < childrenWeights.Length; i++) { System.Random rnd = new System.Random((int)DateTime.Now.Ticks); if (rnd.NextDouble() < mutationProbability) { genetics.Mutation(childrenWeights[i], mutationProbability); } } if (mutationType == Genetics.MutationType.IRREGULAR) { mutationProbability -= 0.0033f; Debug.Log("Mutation probability: " + mutationProbability); if (mutationProbability < 0.01) { mutationProbability = 0.01f; } } for (int i = 0; i < agents.Count; i++) { agents[i].GetComponent <Fitness>().Reset(); agents[i].GetComponent <Core>().SetWeights(childrenWeights[i]); agents[i].transform.position = spawnPoint.position + GetRandVec(); agents[i].transform.rotation = spawnPoint.rotation; //cars[i].GetComponent<FitnessScript>().StartCar(); } //agents[0].GetComponent<Renderer>().material.color = new Color(0, 1, 0); generation++; Debug.Log("Current generation: " + generation + ". Best fitness so far: " + bestFitness); }