// Create a new generation public void Generate() { NeuralNetwork[] temp = new NeuralNetwork[cars.Count]; // Refill the population with children from the mating pool for (int i = 0; i < cars.Count - 1; i++) { if (i == bestCar) { temp[i] = cars[i].GetComponent <CarDNA>().neuralNetwork; } else { CarDNA partnerA = PickOne(cars); CarDNA partnerB = PickOne(cars); NeuralNetwork child = partnerA.CrossOver(partnerB); Mutate(ref child, mutationRate); temp[i] = child; } } for (int i = 0; i < cars.Count - 1; i++) { cars[i].GetComponent <CarDNA>().neuralNetwork = temp[i]; } cars[cars.Count - 1].GetComponent <CarDNA>().neuralNetwork = new NeuralNetwork(inputNodes, hiddenNodes, outputNodes); //if (matingPool.Count != 0) { generations++; } RestartCars(spawnPosition); }
void Crossover() { for (int i = 30; i < 101; i++) { List <CarDNA> parent_DNA = new List <CarDNA>(); parent1 = Random.Range(0, 30); do { parent2 = Random.Range(0, 30); } while (parent1 == parent2); parent_DNA.Add(PopulationCopy[parent1]); parent_DNA.Add(PopulationCopy[parent2]); CarDNA Child = new CarDNA(); Child.model = new List <DNA>(); for (int j = 0; j < NumberOfPositions - 1; j++) { DNA child = new DNA(); int num = Random.Range(0, 2); child.position = parent_DNA[num].model[j].position; Child.model.Add(child); } Population.Add(Child); Population[i].fitness = -1000.0; } }
// Start is called before the first frame update void Start() { rigidbody = GetComponent <Rigidbody>(); carDNA = GetComponent <CarDNA>(); headObject = transform.Find("Head"); //TEST //carDNA.InitCar(5, 5, 2); }
void CreatePopulation() { double distance = System.Math.Sqrt(System.Math.Pow(transform.position.z - 6.23, 2)); double distance_all = System.Math.Sqrt(System.Math.Pow(transform.position.z - 6.23, 2) + System.Math.Pow(transform.position.x + 12.68, 2)); CarDNA entity = new CarDNA(); entity.model = new List <DNA>(); entity.fitness = -distance * 0.2 - distance_all * 0.8; CarPositions.ForEach(x => entity.model.Add(x)); Population.Add(entity); }
void BestCars_fun() { CarDNA entity = new CarDNA(); entity.model = new List <DNA>(); entity.fitness = 200 - Car_count; CarPositions.ForEach(x => entity.model.Add(x)); BestCars.Add(entity); Population.Add(entity); gameObject.SetActive(false); }
// Crossover public NeuralNetwork CrossOver(CarDNA partner) { // A new child NeuralNetwork child = new NeuralNetwork(neuralNetwork.inputNodes, neuralNetwork.hiddenNodes, neuralNetwork.outputNodes); //Do crossover DoCrossOver(ref child.ihWeights, neuralNetwork.ihWeights, partner.neuralNetwork.ihWeights); //for(int i = 0; i < neuralNetwork.ihWeights.rowNb; i++) //{ // for(int j = 0; j < neuralNetwork.ihWeights.columnNb; j++) // { // child.ihWeights[i][j] = Random.Range(0, 3) == 0 ? neuralNetwork.ihWeights[i][j] : partner.neuralNetwork.ihWeights[i][j]; // } //} DoCrossOver(ref child.hoWeights, neuralNetwork.hoWeights, partner.neuralNetwork.hoWeights); //for (int i = 0; i < neuralNetwork.hoWeights.rowNb; i++) //{ // for (int j = 0; j < neuralNetwork.hoWeights.columnNb; j++) // { // child.hoWeights[i][j] = Random.Range(0, 3) == 0 ? neuralNetwork.hoWeights[i][j] : partner.neuralNetwork.hoWeights[i][j]; // } //} DoCrossOver(ref child.biasH, neuralNetwork.biasH, partner.neuralNetwork.biasH); //for (int i = 0; i < neuralNetwork.biasH.rowNb; i++) //{ // for (int j = 0; j < neuralNetwork.biasH.columnNb; j++) // { // child.biasH[i][j] = Random.Range(0, 3) == 0 ? neuralNetwork.biasH[i][j] : partner.neuralNetwork.biasH[i][j]; // } //} DoCrossOver(ref child.biasO, neuralNetwork.biasO, partner.neuralNetwork.biasO); //for (int i = 0; i < neuralNetwork.biasO.rowNb; i++) //{ // for (int j = 0; j < neuralNetwork.biasO.columnNb; j++) // { // child.biasO[i][j] = Random.Range(0, 3) == 0 ? neuralNetwork.biasO[i][j] : partner.neuralNetwork.biasO[i][j]; // } //} return(child); }