public void StopSimulation(bool stopForGood = false) { simulationStarted = false; if (!stopForGood) { double[] scores = new double[Specimen.Length]; for (int i = 0; i < Specimen.Length; i++) { scores[i] = (double)(Specimen[i].FinalScore()); } learningProcess.Learn(scores); int temp = learningProcess.HistoricalData.Count; ProcessData tmpData = learningProcess.HistoricalData[temp - 1]; if (OnRoundEnded != null) { OnRoundEnded(tmpData); } Debug.Log( "Generation " + tmpData.GenerationIndex + ", Avg: " + tmpData.AverageScore.ToString("n2") + ", Med: " + tmpData.MedianScore + ", Best: " + tmpData.BestScore + ", Worst: " + tmpData.WorstScore); } else { Specimen = null; learningProcess = null; GenerationList.ResetList(); plot.RestartedSimulation(null); OnRoundEnded(null); } }
public void Run() { int counter = 0; while (true) { double[] scores = new double[populationCount]; int dataCount = 50; var testData = new double[dataCount][]; for (int i = 0; i < dataCount; i++) { testData[i] = new[] { rand.NextDouble() * 2 - 1, rand.NextDouble() * 2 - 1 }; } double[,] scores2 = new double[populationCount, testData.Length]; for (int k = 0; k < testData.Length; k++) { for (int i = 0; i < populationCount; i++) { var result = process.Population[i].Calculate(testData[k]); var a = FitnessFunction(testData[k], result); scores[i] += a; scores2[i, k] = a; } } process.Learn(scores); var data = process.HistoricalData.Last(); Console.WriteLine( $"Gen: {data.GenerationIndex} Max: {data.BestScore} Min: {data.WorstScore} Avg: {data.AverageScore} Med: {data.MedianScore}"); //Thread.Sleep(1000); if (counter == 50) { var test = new[] { rand.NextDouble() * 2 - 1, rand.NextDouble() * 2 - 1, rand.NextDouble() * 2 - 1 }; var spec = process.HistoricalData.Last().BestSpecimen; Console.WriteLine("----------------------------"); var a1 = spec.Calculate(test); Console.WriteLine($"network: {a1[0]} {a1[1]}"); a1 = Problem(test); Console.WriteLine($"exptected: {a1[0]} {a1[1]}"); Console.WriteLine("----------------------------"); Console.ReadKey(); counter = 0; } counter++; } }
public void Run() { Stopwatch t = new Stopwatch(); t.Start(); int counter = 0; while (true) { double[] scores = new double[populationCount]; var testData = new[] { new[] { 0.2, -1.1, 0.9 }, new[] { 0.3, -2.1, -3 }, new[] { 1, -1, 1.0 }, new[] { 0, 0, 0.0 }, new[] { -0.2, 1.1, -0.9 }, }; double[,] scores2 = new double[populationCount, testData.Length]; for (int k = 0; k < testData.Length; k++) { for (int i = 0; i < populationCount; i++) { var result = process.Population[i].Calculate(testData[k]); var a = FitnessFunction(testData[k], result); scores[i] += a; scores2[i, k] = a; } } process.Learn(scores); var data = process.HistoricalData.Last(); Console.WriteLine( $"Gen: {data.GenerationIndex} Max: {data.BestScore} Min: {data.WorstScore} Avg: {data.AverageScore} Med: {data.MedianScore}"); //Thread.Sleep(1000); if (counter == 50) { var test = new[] { rand.NextDouble() * 2 - 1, rand.NextDouble() * 2 - 1, rand.NextDouble() * 2 - 1 }; var spec = process.HistoricalData.Last().BestSpecimen; for (int i = 0; i < testData.Length; i++) { Console.WriteLine("----------------------------"); var a = spec.Calculate(testData[i]); Console.WriteLine($"network: {a[0]} {a[1]}"); var a2 = Problem(testData[i]); Console.WriteLine($"exptected: {a2[0]} {a2[1]}"); Console.WriteLine("Fitness " + FitnessFunction(testData[i], a)); } Console.WriteLine("----------------------------"); var a1 = spec.Calculate(test); Console.WriteLine($"network: {a1[0]} {a1[1]}"); var fit = FitnessFunction(test, a1); a1 = Problem(test); Console.WriteLine($"exptected: {a1[0]} {a1[1]}"); var neuron0 = spec.Layers[0].Neurons[0].GetWeights(); var neuron1 = spec.Layers[0].Neurons[1].GetWeights(); Console.WriteLine("Fitness " + fit); Console.WriteLine("----------------------------"); Console.WriteLine( $"({Math.Round(neuron0[0], 2)}x1 {Math.Round(neuron0[1], 2)}x2 {Math.Round(neuron0[2], 2)}x3 {Math.Round(neuron0[3],3)})({Math.Round(neuron1[0], 2)}x1 {Math.Round(neuron1[1], 2)}x2 {Math.Round(neuron1[2], 2)}x3 {Math.Round(neuron1[3], 3)})"); Console.WriteLine("Expected: "); Console.WriteLine($"(x1 + x2 - 2x3 + 0)(x1 - x2 + x3 + 1)"); t.Stop(); TimeSpan ts = t.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); var key = Console.ReadKey(); if (key.Key == ConsoleKey.S) { process.Serialize("leariningtest.xml"); } counter = 0; t.Reset(); t.Start(); } counter++; } }