private void Crossover(reinforcementNet[] newPopulation, reinforcement2Child[] newPopulation2) { for (int i = 0; i < numberToCrossover; i += 2) { int AIndex = i; int BIndex = i + 1; if (genePool.Count >= 1) { for (int l = 0; l < 100; l++) { AIndex = genePool[Random.Range(0, genePool.Count)]; BIndex = genePool[Random.Range(0, genePool.Count)]; if (AIndex != BIndex) { break; } } } reinforcementNet Child1 = new reinforcementNet(); reinforcementNet Child2 = new reinforcementNet(); // reinforcement2Child Child1_A = new reinforcement2Child(); reinforcement2Child Child2_A = new reinforcement2Child(); Child1.Initialise(controller.LAYERS, controller.NEURONS); Child2.Initialise(controller.LAYERS, controller.NEURONS); Child1_A.Initialise2(player2.LAYERS, player2.NEURONS); Child2_A.Initialise2(player2.LAYERS, player2.NEURONS); Child1.fitness = 0; Child2.fitness = 0; Child1_A.fitness = 0; Child2_A.fitness = 0; for (int w = 0; w < Child1.weights.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { Child1.weights[w] = population[AIndex].weights[w]; Child2.weights[w] = population[BIndex].weights[w]; //second person Child2_A.weights[w] = population2[AIndex].weights[w]; Child1_A.weights[w] = population2[BIndex].weights[w]; } else { Child2.weights[w] = population[AIndex].weights[w]; Child1.weights[w] = population[BIndex].weights[w]; //second person Child1_A.weights[w] = population2[AIndex].weights[w]; Child2_A.weights[w] = population2[BIndex].weights[w]; } } for (int w = 0; w < Child1.biases.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { Child1.biases[w] = population[AIndex].biases[w]; Child2.biases[w] = population[BIndex].biases[w]; //second person Child2_A.biases[w] = population2[AIndex].biases[w]; Child1_A.biases[w] = population2[BIndex].biases[w]; } else { Child2.biases[w] = population[AIndex].biases[w]; Child1.biases[w] = population[BIndex].biases[w]; //second person Child1_A.biases[w] = population2[AIndex].biases[w]; Child2_A.biases[w] = population2[BIndex].biases[w]; } } newPopulation[naturallySelected] = Child1; newPopulation2[naturallySelected] = Child1_A; naturallySelected++; newPopulation[naturallySelected] = Child2; newPopulation2[naturallySelected] = Child2_A; naturallySelected++; } }
private void Crossover(reinforcementNet[] newPopulation) { //cross over based on gene pool //increment by two for (int i = 0; i < numberToCrossover; i += 2) { //first parent index int AIndex = i; //second parent int BIndex = i + 1; if (genePool.Count >= 1) { for (int l = 0; l < 100; l++) { //find random elemnt in gene pool AIndex = genePool[Random.Range(0, genePool.Count)]; BIndex = genePool[Random.Range(0, genePool.Count)]; //ensure parents are not the same //we found what we looking for break if (AIndex != BIndex) { break; } } } reinforcementNet Child1 = new reinforcementNet(); reinforcementNet Child2 = new reinforcementNet(); Child1.Initialise(controller.LAYERS, controller.NEURONS); Child2.Initialise(controller.LAYERS, controller.NEURONS); Child1.fitness = 0; Child2.fitness = 0; //should be more subtle in practice to much transfer of info for (int w = 0; w < Child1.weights.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { //taje weights from parents Child1.weights[w] = population[AIndex].weights[w]; Child2.weights[w] = population[BIndex].weights[w]; } else { //random chance for kids Child2.weights[w] = population[AIndex].weights[w]; Child1.weights[w] = population[BIndex].weights[w]; } } //indv values instead of matices for (int w = 0; w < Child1.biases.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { Child1.biases[w] = population[AIndex].biases[w]; Child2.biases[w] = population[BIndex].biases[w]; } else { Child2.biases[w] = population[AIndex].biases[w]; Child1.biases[w] = population[BIndex].biases[w]; } } //add to new population newPopulation[naturallySelected] = Child1; naturallySelected++; //add to new population newPopulation[naturallySelected] = Child2; naturallySelected++; } }