private reinforcement2Child[] PickBestPopulation2() { reinforcement2Child[] newPopulation2 = new reinforcement2Child[initialPopulation]; for (int i = 0; i < bestAgentSelection; i++) { newPopulation2[naturallySelected] = population2[i].InitialiseCopy2(player2.LAYERS, player2.NEURONS); newPopulation2[naturallySelected].fitness = 0; naturallySelected++; int f = Mathf.RoundToInt(population[i].fitness * 10); for (int c = 0; c < f; c++) { genePool.Add(i); } } for (int i = 0; i < worstAgentSelection; i++) { int last = population.Length - 1; last -= i; int f = Mathf.RoundToInt(population[last].fitness * 10); for (int c = 0; c < f; c++) { genePool.Add(last); } } return(newPopulation2); }
public void ResetWithNetwork(reinforcement2Child net) { network = net; Reset(); Debug.Log("resetting"); }
public reinforcement2Child InitialiseCopy2(int hiddenLayerCount, int hiddenNeuronCount) { reinforcement2Child n = new reinforcement2Child(); List <Matrix <float> > newWeights = new List <Matrix <float> >(); for (int i = 0; i < this.weights.Count; i++) { Matrix <float> currentWeight = Matrix <float> .Build.Dense(weights[i].RowCount, weights[i].ColumnCount); for (int x = 0; x < currentWeight.RowCount; x++) { for (int y = 0; y < currentWeight.ColumnCount; y++) { currentWeight[x, y] = weights[i][x, y]; } } newWeights.Add(currentWeight); } List <float> newBiases = new List <float>(); newBiases.AddRange(biases); n.weights = newWeights; n.biases = newBiases; n.InitialiseHidden2(hiddenLayerCount, hiddenNeuronCount); return(n); }
private void FillPopulationWithRandomValues(reinforcementNet[] newPopulation, reinforcement2Child[] newPopulation2, int startingIndex) { while (startingIndex < initialPopulation) { newPopulation[startingIndex] = new reinforcementNet(); newPopulation[startingIndex].Initialise(controller.LAYERS, controller.NEURONS); //second person newPopulation2[startingIndex] = new reinforcement2Child(); newPopulation2[startingIndex].Initialise2(player2.LAYERS, player2.NEURONS); startingIndex++; } }
public void Death2(float fitness2, reinforcement2Child network2) { if (currentGenome2 < population2.Length - 1) { population2[currentGenome2].fitness = fitness2; Debug.Log("death first layer"); currentGenome2++; Debug.Log("death active"); ResetToCurrentGenome(); } else { RePopulate(); } }
private void SortPopulation() { for (int i = 0; i < population.Length; i++) { for (int j = i; j < population.Length; j++) { if (population[i].fitness < population[j].fitness) { reinforcementNet temp = population[i]; population[i] = population[j]; population[j] = temp; } if (population2[i].fitness < population2[j].fitness) { reinforcement2Child temp2 = population2[i]; population2[i] = population2[j]; population2[j] = temp2; } } } }
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++; } }