Esempio n. 1
0
        public void Simulate()
        {
            var simulationNetwork = Workspace.Instance.GetNetworkFile(NetworkName);
            var simulationPlugin  = new NeuralSimulationPlugin(simulationNetwork, null, 0);

            SimulationTemplate.CreateSimulationForm(simulationPlugin, "", SpaceTemplateFactory, TestingSimulationConfiguration, 50).Show();
        }
Esempio n. 2
0
        private void Test(NeuralSimulationPlugin trainingPlugin, FileInfo testingNetworkFile, FileInfo testingBestNetworkFile, ref double testingBestReward, ref int testingBestReachedGoals, ChartForm chartForm)
        {
            trainingPlugin.Network.Save(testingNetworkFile);
            var testingSeed       = Guid.NewGuid().GetHashCode();
            var testingPlugin     = new NeuralSimulationPlugin(testingNetworkFile, null, testingSeed);
            var testingSimulation = SimulationTemplate.CreateSimulation(testingPlugin, "Testing", SpaceTemplateFactory, TestingSimulationConfiguration);

            testingSimulation.Initialise();
            while (testingSimulation.Iterate())
            {
                ;
            }

            foreach (var serie in testingSimulation.Results.Series)
            {
                chartForm.InvokeAction(() => chartForm.AddPoint(serie.Key, serie.Value.Last()));
            }

            var predictions = string.Join(", ", testingPlugin.Predictions.Select(p => $"{p,4}"));

            if (lines % 20 == 0)
            {
                var headers = testingSimulation.Results.ToHeaderStrings();
                WriteLine($"      {headers[0]}  Predictions (-,N,S,E,W)");
                WriteLine($"      {headers[1]}  ============================");
            }

            if (testingPlugin.TotalReward - testingBestReward > -0.00001)
            {
                testingBestReward       = testingPlugin.TotalReward;
                testingBestReachedGoals = testingSimulation.TotalReachedGoals;
                trainingPlugin.Network.Save(testingBestNetworkFile.Ensure());
                WriteLine($"Saved {testingSimulation.Results.ToDataString()}  {predictions}");
            }
            else
            {
                WriteLine($"      {testingSimulation.Results.ToDataString()}  {predictions}");
            }
        }
Esempio n. 3
0
        public void Train()
        {
            var trainingNetwork    = Workspace.Instance.GetNetworkFile(NetworkName);
            var trainingPlugin     = new NeuralSimulationPlugin(trainingNetwork, TrainingPluginConfiguration, 0);
            var trainingSimulation = SimulationTemplate.CreateSimulation(trainingPlugin, "Training", SpaceTemplateFactory, TrainingSimulationConfiguration);

            if (TrainingProgressTrackerConfiguration.Enabled)
            {
                var testingNetworkFile     = TrainingOuputDirectory.GetFile("Last.model");
                var testingBestNetworkFile = TrainingOuputDirectory.GetFile("Best.model");
                trainingPlugin.Network.Save(testingNetworkFile);
                var testingBestReward       = 0.0;
                var testingBestReachedGoals = 0;
                var chartForm = new ChartForm("Neural Agent Reinforcement Learning");

                Task.Run(() =>
                {
                    try
                    {
                        DateTime startTime = DateTime.Now;
                        WriteLine($"Training Start: {startTime}");

                        while (trainingSimulation.Iterate())
                        {
                            Console.Title = trainingSimulation.ToString();

                            if (trainingSimulation.Iteration % TrainingProgressTrackerConfiguration.Interval == 0)
                            {
                                Test(trainingPlugin, testingNetworkFile, testingBestNetworkFile, ref testingBestReward, ref testingBestReachedGoals, chartForm);
                                lines++;
                            }
                        }

                        WriteLine($"Training Finished: {DateTime.Now}");
                        WriteLine($"Training Time: {DateTime.Now - startTime}");

                        chartForm.InvokeAction(() =>
                        {
                            Bitmap chartScreenshot = new Bitmap(chartForm.Width, chartForm.Height);
                            chartForm.DrawToBitmap(chartScreenshot, new Rectangle(0, 0, chartForm.Width, chartForm.Height));
                            var chartScreenshotFile = TrainingOuputDirectory.GetFile("Progress.png");
                            chartScreenshot.Save(chartScreenshotFile.FullName);
                        });

                        if (testingBestReward > 0)
                        {
                            string message = $"Do you want to update network '{NetworkName}'?\r\n\r\nTotal Reached Goals: {testingBestReachedGoals}\r\nTotal Reward: {testingBestReward}";

                            if (MessageBox.Show(message, "Training Results", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                new AgentNetwork(testingBestNetworkFile).Save(trainingPlugin.NetworkFile);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        WriteLine(exception.ToString());
                    }
                });

                chartForm.ShowDialog();
            }
        }