public void InitiatePopulation() { for (int i = 0; i < populationSize; i++) { pop[i] = new GeneticBrain(inputSize, hiddenLayerSize, outputSize, minRandom, maxRandom, this); } StartCoroutine("WaitForStart"); }
static void Main() { double[] point = new double[2]; //BasicBrain naivebrain = new BasicBrain(); //point = naivebrain.BasicMaximum(); GeneticBrain geneticbrain = new GeneticBrain(); point = geneticbrain.GeneticMaximum(); Console.WriteLine("Found local maximum at (" + Convert.ToString(point[0]) + ", " + Convert.ToString(point[1]) + ")."); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
public void NextGeneration() { RemovePrevGen(); generationNum++; List <GeneticBrain> pool = CreatePool(); GeneticBrain[] newPop = new GeneticBrain[populationSize]; for (int i = 0; i < populationSize; i++) { newPop[i] = new GeneticBrain(inputSize, hiddenLayerSize, outputSize, minRandom, maxRandom, this); GeneticBrain parent1 = pool[Random.Range(0, pool.Count)]; GeneticBrain parent2 = pool[Random.Range(0, pool.Count)]; newPop[i].hoWeights = Matrix.MixedMatrix(parent1.hoWeights, parent2.hoWeights, mutationRate, minRandom, maxRandom); newPop[i].ihWeights = Matrix.MixedMatrix(parent1.ihWeights, parent2.ihWeights, mutationRate, minRandom, maxRandom); newPop[i].oBias = Matrix.MixedMatrix(parent1.oBias, parent2.oBias, mutationRate, minRandom, maxRandom); newPop[i].hBias = Matrix.MixedMatrix(parent1.hBias, parent2.hBias, mutationRate, minRandom, maxRandom); } pop = newPop; PopulateScene(); }
public double[] GeneticMaximum() { GeneticBrain thisBrain = new GeneticBrain(); const int numWalkers = 500; double[] final = new double[2]; Random rnd = new Random(); int stepIndex = new int(); // Define a number of walkers on the field to be bred. GeneticWalker[] walkers = new GeneticWalker[numWalkers]; // Initialize the walkers. for (int i = 0; i < numWalkers; i++) walkers[i] = new GeneticWalker(); for (int walkerIndex = 0; walkerIndex < numWalkers; walkerIndex++) { for (stepIndex = 0; stepIndex < walkers[walkerIndex].walkerHistory.Length; stepIndex++) { int step = rnd.Next(0, 3) - 1; if (rnd.Next(0, 2) == 1) { walkers[walkerIndex].walkerPosition[0] += step * walkers[walkerIndex].dx; walkers[walkerIndex].walkerHistory[stepIndex] = new Complex(step * walkers[walkerIndex].dx, 0); } else { walkers[walkerIndex].walkerPosition[1] += step * walkers[walkerIndex].dx; walkers[walkerIndex].walkerHistory[stepIndex] = new Complex(0, step * walkers[walkerIndex].dx); } } walkers[walkerIndex].height = walkers[walkerIndex].walkerHeight(); } thisBrain.SortWalkers(ref walkers); for (int i = 0; i < 1500; i++ ) { GeneticWalker[] temp = walkers; thisBrain.SortWalkers(ref temp); thisBrain.BreedWalkers(ref walkers); thisBrain.WalkFromHistory(ref walkers); thisBrain.SortWalkers(ref walkers); if (temp[0].height > walkers[0].height) walkers = temp; Console.WriteLine(Convert.ToString(i)+": "+Convert.ToString(Math.Sqrt(Math.Pow(walkers[0].walkerPosition[0]-3.4,2)+Math.Pow(walkers[0].walkerPosition[1]+4,2)))); } thisBrain.SortWalkers(ref walkers); final = walkers[0].walkerPosition; return final; }