public void FillUpRestSpots(List <NeuralNetwork2D> betterNetworks) { while (betterNetworks.Count < populationCount) { NeuralNetwork2D newNetwork = playerController2D.gameObject.AddComponent <NeuralNetwork2D>(); newNetwork.Initialise(playerController2D.layers, playerController2D.neurons); betterNetworks.Add(newNetwork); } }
public void CreatePopulation() { while (startIndex < populationCount) { NeuralNetwork2D network = playerController2D.gameObject.AddComponent <NeuralNetwork2D>(); network.Initialise(playerController2D.layers, playerController2D.neurons); networks.Add(network); startIndex++; } startIndex = 0; text1.text = "Generation: " + currentGeneration; text2.text = "Genome: " + currentGenome; playerController2D.network = networks[currentGenome]; }
public void CrossOver(List <NeuralNetwork2D> betterNet) { for (int i = 0; i < numberToCrossover - 1; i += 2) { NeuralNetwork2D Child1 = playerController2D.gameObject.AddComponent <NeuralNetwork2D>(); NeuralNetwork2D Child2 = playerController2D.gameObject.AddComponent <NeuralNetwork2D>(); Child1.Initialise(playerController2D.layers, playerController2D.neurons); Child2.Initialise(playerController2D.layers, playerController2D.neurons); for (int j = 0; j < betterNet[i].weights.Count; j++) { if (Random.Range(0, 2) == 0) { Child1.weights[j] = betterNet[i].weights[j]; Child2.weights[j] = betterNet[i + 1].weights[j]; } else { Child2.weights[j] = betterNet[i].weights[j]; Child1.weights[j] = betterNet[i + 1].weights[j]; } } for (int j = 0; j < betterNet[i].biases.Count; j++) { if (Random.Range(0, 2) == 0) { Child1.biases[j] = betterNet[i].biases[j]; Child2.biases[j] = betterNet[i + 1].biases[j]; } else { Child2.biases[j] = betterNet[i].biases[j]; Child1.biases[j] = betterNet[i + 1].biases[j]; } } betterNet.Add(Child1); betterNet.Add(Child2); } }
public NeuralNetwork2D InitialiseCopy(int hiddenLayerCount, int hiddenNeuronCount) { NeuralNetwork2D n = FindObjectOfType <PlayerController2D>().gameObject.AddComponent <NeuralNetwork2D>(); 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; for (int i = 0; i < hiddenLayerCount + 1; i++) { Matrix <float> newHiddenLayer = Matrix <float> .Build.Dense(1, hiddenNeuronCount); n.hiddenLayers.Add(newHiddenLayer); } n.fitness = 0; return(n); }