Esempio n. 1
0
        private void CtlStart_Click(object sender, EventArgs e)
        {
            if (SaveConfig())
            {
                ApplyChangesToStandingNetworks();

                CancellationTokenSource = new CancellationTokenSource();
                CancellationToken       = CancellationTokenSource.Token;

                CtlMenuStart.Enabled         = false;
                CtlMenuReset.Enabled         = false;
                CtlMenuStop.Enabled          = true;
                CtlMenuDeleteNetwork.Enabled = false;

                NetworksManager.PrepareModelsForRun();

                NetworksManager.PrepareModelsForRound();
                CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models.First());
                NetworksManager.FeedForward(); // initialize state

                Round     = 0;
                StartTime = DateTime.Now;

                DrawModels(NetworksManager.Models);

                WorkThread          = new Thread(new ThreadStart(RunNetwork));
                WorkThread.Priority = ThreadPriority.Highest;
                WorkThread.Start();
            }
        }
        private void CreateNetworksManager()
        {
            if (!StopRequest())
            {
                return;
            }

            var network = new NetworksManager(CtlTabs, null, OnNetworkUIChanged);

            if (network.Config != null)
            {
                NetworksManager = network;
                CtlInputDataPresenter.LoadConfig(NetworksManager.Config, this);

                ReplaceNetworksManagerControl(NetworksManager);
                if (NetworksManager.IsValid())
                {
                    ApplyChangesToStandingNetworks();
                }
                else
                {
                    MessageBox.Show("Network parameter is not valid.", "Error");
                }
            }
        }
Esempio n. 3
0
 private void ApplyChangesToStandingNetworks()
 {
     lock (ApplyChangesLocker)
     {
         CtlInputDataPresenter.RearrangeWithNewPointsCount();
         NetworksManager.RefreshNetworksDataModels();
         CtlNetworkPresenter.RenderStanding(NetworksManager.SelectedNetworkModel);
         ToggleApplyChanges(Const.Toggle.Off);
         CtlMenuStart.Enabled = true;
     }
 }
Esempio n. 4
0
 private void ApplyChangesToRunningNetworks()
 {
     lock (ApplyChangesLocker)
     {
         CtlInputDataPresenter.RearrangeWithNewPointsCount();
         var newModels = NetworksManager.CreateNetworksDataModels();
         NetworksManager.MergeModels(newModels);
         CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel);
         ToggleApplyChanges(Const.Toggle.Off);
     }
 }
Esempio n. 5
0
        protected override void OnResizeBegin(EventArgs e)
        {
            if (NetworksManager != null)
            {
                //NetworksManager.SelectedNetwork.ResizeBegin();
            }

            //CtlManagerPanel.SuspendLayout();
            //CtlBottomPanel.SuspendLayout();
            CtlInputDataPresenter.SuspendLayout();
            base.OnResizeBegin(e);
        }
Esempio n. 6
0
        protected override void OnResizeEnd(EventArgs e)
        {
            if (NetworksManager != null)
            {
                //NetworksManager.SelectedNetwork.ResizeEnd();
            }

            //CtlManagerPanel.ResumeLayout();
            //CtlBottomPanel.ResumeLayout();
            CtlInputDataPresenter.ResumeLayout();
            base.OnResizeEnd(e);

            Width = 16 * Height / 9;
        }
        private void CtlMenuStart_Click(object sender, RoutedEventArgs e)
        {
            if (SaveConfig())
            {
                ApplyChangesToStandingNetworks();

                CancellationTokenSource = new CancellationTokenSource();
                CancellationToken       = CancellationTokenSource.Token;

                CtlMenuStart.IsEnabled         = false;
                CtlMenuReset.IsEnabled         = false;
                CtlMenuStop.IsEnabled          = true;
                CtlMenuDeleteNetwork.IsEnabled = false;

                NetworksManager.PrepareModelsForRun();

                NetworksManager.PrepareModelsForRound();
                CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.SelectedNetworkModel);
                NetworksManager.FeedForward(); // initialize state

                DrawNetwork(NetworksManager.SelectedNetworkModel, CtlOnlyWeights.IsOn, CtlOnlyChangedWeights.IsOn, CtlHighlightChangedWeights.IsOn);

                GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                GC.Collect();
                GC.WaitForFullGCComplete();

                Round     = 0;
                StartTime = Stopwatch.StartNew();

                RunNetworkThread = new Thread(new ParameterizedThreadStart(RunNetwork))
                {
                    Name     = "RunNetwork",
                    Priority = ThreadPriority.Highest
                };
                RunNetworkThread.Start(new object[] { Processor.Proc1 });

                TimeThread = new Thread(new ThreadStart(RunTimer))
                {
                    Name     = "Timer",
                    Priority = ThreadPriority.BelowNormal
                };
                TimeThread.Start();
            }
        }
        private bool SaveConfig()
        {
            Config.Main.Set(Const.Param.ScreenWidth, ActualWidth);
            Config.Main.Set(Const.Param.ScreenHeight, ActualHeight);
            Config.Main.Set(Const.Param.ScreenTop, Top);
            Config.Main.Set(Const.Param.ScreenLeft, Left);
            Config.Main.Set(Const.Param.OnTop, Topmost);
            Config.Main.Set(Const.Param.DataWidth, DataWidth.ActualWidth);
            Config.Main.Set(Const.Param.NetworkHeight, NetworkHeight.ActualHeight);

            ConfigParams.ForEach(p => p.SaveConfig());

            if (!SaveSettings())
            {
                return(false);
            }

            if (NetworksManager != null)
            {
                CtlInputDataPresenter.SaveConfig(NetworksManager.Config);

                if (!NetworksManager.IsValid())
                {
                    MessageBox.Show("Network parameter is invalid", "Error");
                    return(false);
                }
                else
                {
                    NetworksManager.SaveConfig();
                }
            }

            Config.Main.FlushToDrive();
            if (NetworksManager != null)
            {
                NetworksManager.Config.FlushToDrive();
            }

            return(true);
        }
Esempio n. 9
0
        private void CtlTabs_SelectedIndexChanged(object sender, EventArgs e)
        {
            // newly selected network must not affect NetworksManager until it saved

            if (NetworksManager != null)
            {
                if (IsRunning)
                {
                    lock (ApplyChangesLocker)
                    {
                        CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models.First());
                        CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel);
                        CtlPlotPresenter.Draw(NetworksManager.Models, NetworksManager.SelectedNetworkModel);
                        CtlStatisticsPresenter.Draw(null);
                    }
                }
                else
                {
                    CtlNetworkPresenter.RenderStanding(NetworksManager.SelectedNetworkModel);
                }
            }
        }
Esempio n. 10
0
        private void CtlTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // newly selected network must not affect NetworksManager until it saved

            if (NetworksManager != null)
            {
                if (IsRunning)
                {
                    lock (ApplyChangesLocker)
                    {
                        CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models[0]);
                        CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel, CtlOnlyWeights.IsOn, CtlOnlyChangedWeights.IsOn, CtlHighlightChangedWeights.IsOn);
                        CtlPlotPresenter.Draw(NetworksManager.Models, NetworksManager.SelectedNetworkModel);
                        CtlStatisticsPresenter.Draw(NetworksManager.SelectedNetworkModel.LastStatistics);
                    }
                }
                else
                {
                    CtlNetworkPresenter.RenderStanding(NetworksManager.SelectedNetworkModel);
                }
            }
        }
Esempio n. 11
0
        private void LoadNetworksManager(string name)
        {
            if (!StopRequest())
            {
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                return;
            }

            if (!File.Exists(name))
            {
                name = "\\Networks\\" + System.IO.Path.GetFileName(name);
            }

            if (File.Exists(name))
            {
                NetworksManager = new NetworksManager(CtlTabs, name, OnNetworkUIChanged);
                Config.Main.Set(Const.Param.NetworksManagerName, name);
                CtlInputDataPresenter.LoadConfig(NetworksManager.Config, this);

                ReplaceNetworksManagerControl(NetworksManager);
                if (NetworksManager.IsValid())
                {
                    ApplyChangesToStandingNetworks();
                }
                else
                {
                    MessageBox.Show("Network parameter is not valid.", "Error");
                }
            }
            else
            {
                MessageBox.Show($"Network '{name}' is not found!", "Error", MessageBoxButton.OK);
                Config.Main.Set(Const.Param.NetworksManagerName, string.Empty);
            }
        }
Esempio n. 12
0
        private void DrawModels(List <NetworkDataModel> models)
        {
            var renderStart = DateTime.Now;

            CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel);
            CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models.First());

            foreach (var model in models)
            {
                model.DynamicStatistic.Add(model.Statistic.Percent, model.Statistic.AverageCost);
            }

            CtlPlotPresenter.Draw(models, NetworksManager.SelectedNetworkModel);

            var selected = NetworksManager.SelectedNetworkModel;

            if (selected == null)
            {
                CtlStatisticsPresenter.Draw(null);
            }
            else
            {
                var stat = new Dictionary <string, string>();
                var span = DateTime.Now.Subtract(StartTime);
                stat.Add("Time", new DateTime(span.Ticks).ToString(@"HH\:mm\:ss"));

                if (selected.Statistic.Percent > 0)
                {
                    var remains = new DateTime((long)(span.Ticks * 100 / selected.Statistic.Percent) - span.Ticks);
                    stat.Add("Time remaining", new DateTime(remains.Ticks).ToString(@"HH\:mm\:ss"));
                }
                else
                {
                    stat.Add("Time remaining", "N/A");
                }

                if (selected.Statistic.LastGoodOutput > -1)
                {
                    stat.Add("Last good output", $"{selected.Statistic.LastGoodInput}={selected.Statistic.LastGoodOutput} ({Converter.DoubleToText(100 * selected.Statistic.LastGoodOutputActivation, "N6")}%)");
                    stat.Add("Last good cost", Converter.DoubleToText(selected.Statistic.LastGoodCost, "N6"));
                }
                else
                {
                    stat.Add("Last good output", "none");
                    stat.Add("Last good cost", "none");
                }

                if (selected.Statistic.LastBadOutput > -1)
                {
                    stat.Add("Last bad output", $"{selected.Statistic.LastBadInput}={selected.Statistic.LastBadOutput} ({Converter.DoubleToText(100 * selected.Statistic.LastBadOutputActivation, "N6")}%)");
                    stat.Add("Last bad cost", Converter.DoubleToText(selected.Statistic.LastBadCost, "N6"));
                }
                else
                {
                    stat.Add("Last bad output", "none");
                    stat.Add("Last bad cost", "none");
                }

                stat.Add("Average cost", Converter.DoubleToText(selected.Statistic.AverageCost, "N6"));
                stat.Add("Percent", Converter.DoubleToText(selected.Statistic.Percent, "N6") + " %");
                stat.Add("Learning rate", Converter.DoubleToText(selected.LearningRate));
                stat.Add("Rounds", Round.ToString());
                stat.Add("Rounds/sec", ((int)((double)Round / DateTime.Now.Subtract(StartTime).TotalSeconds)).ToString());

                var renderStop = DateTime.Now;

                stat.Add("Render time, msec", ((int)(renderStop.Subtract(renderStart).TotalMilliseconds)).ToString());
                CtlStatisticsPresenter.Draw(stat);
            }

            NetworksManager.ResetModelsStatistic();
        }
Esempio n. 13
0
 private void DrawNetwork(NetworkDataModel model, bool isOnlyWeights, bool isOnlyChangedWeights, bool isHighlightChangedWeights)
 {
     CtlNetworkPresenter.RenderRunning(model, isOnlyWeights, isOnlyChangedWeights, isHighlightChangedWeights);
     CtlInputDataPresenter.SetInputDataAndDraw(model);
 }
Esempio n. 14
0
        unsafe private void RunNetwork(object args)
        {
            var arr       = (object[])args;
            var processor = (Processor)arr[0];

            SetProcessorAffinity(processor);
            SetThreadPriority(ThreadPriorityLevel.Highest);

            var forLimit = new List <ForLimit>
            {
                new ForLimit(Settings.SkipRoundsToDrawErrorMatrix),
                new ForLimit(Settings.SkipRoundsToDrawNetworks),
                new ForLimit(Settings.SkipRoundsToDrawStatistics)
            };

            forLimit = forLimit.OrderBy(fl => fl.Current).ToList();

            bool IsErrorMatrixRendering = false;
            bool IsNetworkRendering     = false;
            bool IsStatisticsRendering  = false;

            var currentForLimit = forLimit[0];

            var swPureSpeed = new Stopwatch();
            var swLock      = new Stopwatch();

            double k1 = 0.5;
            double k2 = 0.5;

            while (!CancellationToken.IsCancellationRequested)
            {
                lock (ApplyChangesLocker)
                {
                    swPureSpeed.Start();
                    for (int i = 0; i < currentForLimit.Current; ++i)
                    {
                        NetworksManager.PrepareModelsForRound();

                        var model = NetworksManager.Models[0];
                        while (model != null)
                        {
                            if (!model.IsEnabled)
                            {
                                continue;
                            }

                            model.FeedForward();

                            var output   = model.GetMaxActivatedOutputNeuron();
                            var outputId = output.Id;
                            var input    = model.TargetOutput;
                            var cost     = model.CostFunction.Do(model);
                            if (input == outputId)
                            {
                                ++model.Statistics.CorrectRoundsTotal;
                                ++model.Statistics.CorrectRounds;

                                model.Statistics.LastGoodInput            = model.Classes[input];
                                model.Statistics.LastGoodOutput           = model.Classes[outputId];
                                model.Statistics.LastGoodOutputActivation = output.Activation;
                                model.Statistics.LastGoodCost             = cost;
                            }
                            else
                            {
                                model.Statistics.LastBadInput            = model.Classes[input];
                                model.Statistics.LastBadOutput           = model.Classes[outputId];
                                model.Statistics.LastBadOutputActivation = output.Activation;
                                model.Statistics.LastBadCost             = cost;
                                model.Statistics.LastBadTick             = StartTime.Elapsed.Duration().Ticks;
                            }

                            model.Statistics.CostSum += cost;
                            model.ErrorMatrix.AddData(input, outputId);

                            model.BackPropagation();

                            model = model.Next;
                        }
                    }
                    swPureSpeed.Stop();

                    Round += currentForLimit.Current;

                    if (Round % Settings.SkipRoundsToDrawStatistics == 0)
                    {
                        var pureSpeedElapsedSeconds = swPureSpeed.Elapsed.Duration().TotalSeconds;
                        var totalTicksElapsed       = StartTime.Elapsed.Duration().Ticks;

                        var m = NetworksManager.Models[0];
                        while (m != null)
                        {
                            m.Statistics.Rounds            = Round;
                            m.Statistics.TotalTicksElapsed = totalTicksElapsed;
                            m.Statistics.CostSumTotal     += m.Statistics.CostSum;

                            m.Statistics.PureRoundsPerSecond = Round / pureSpeedElapsedSeconds;

                            var percent      = 100 * (double)m.Statistics.CorrectRounds / Settings.SkipRoundsToDrawStatistics;
                            var percentTotal = 100 * (double)m.Statistics.CorrectRoundsTotal / Round;

                            /*
                             * k1 = (totalTicksElapsed - m.Statistics.LastBadTick) / m.Statistics.LastBadTick;
                             * if (k1 > 1)
                             * {
                             *  k1 = 1;
                             * }
                             *
                             * k2 = 1 - k1;
                             */
                            k1 = 1;
                            k2 = 0;
                            m.Statistics.Percent = percent * k1 + percentTotal * k2;

                            var costAvg      = m.Statistics.CostSum / Settings.SkipRoundsToDrawStatistics;
                            var costAvgTotal = m.Statistics.CostSumTotal / Round;
                            m.Statistics.CostAvg = costAvg * k1 + costAvgTotal * k2;

                            m.DynamicStatistics.Add(m.Statistics.Percent, m.Statistics.CostAvg);

                            m.Statistics.CostSum       = 0;
                            m.Statistics.CorrectRounds = 0;

                            m = m.Next;
                        }
                    }

                    if (forLimit.Count > 1)
                    {
                        for (int i = 1; i < forLimit.Count; ++i)
                        {
                            forLimit[i].Current -= currentForLimit.Current;
                        }
                        currentForLimit.Current = currentForLimit.Original;

                        forLimit.RemoveAll(fl => fl.Current == 0);
                        forLimit        = forLimit.OrderBy(fl => fl.Current).ToList();
                        currentForLimit = forLimit[0];
                    }
                }

                var matrixNeeded     = !IsErrorMatrixRendering && Round % Settings.SkipRoundsToDrawErrorMatrix == 0;
                var networkNeeded    = !IsNetworkRendering && Round % Settings.SkipRoundsToDrawNetworks == 0;
                var statisticsNeeded = !IsStatisticsRendering && Round % Settings.SkipRoundsToDrawStatistics == 0;
                var anyNeeded        = matrixNeeded || networkNeeded || statisticsNeeded;

                if (anyNeeded)
                {
                    if (matrixNeeded)
                    {
                        IsErrorMatrixRendering = true;
                    }

                    if (networkNeeded)
                    {
                        IsNetworkRendering = true;
                    }

                    if (statisticsNeeded)
                    {
                        IsStatisticsRendering = true;
                    }

                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        if (matrixNeeded)
                        {
                            ErrorMatrix errorMatrix;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                errorMatrix = NetworksManager.SelectedNetworkModel.ErrorMatrix;
                                NetworksManager.SelectedNetworkModel.ErrorMatrix = errorMatrix.Next;

                                swLock.Stop();
                                RenderTime.ErrorMatrix = swLock.Elapsed.Ticks;
                            }

                            CtlMatrixPresenter.Draw(errorMatrix);
                            errorMatrix.ClearData();
                            IsErrorMatrixRendering = false;
                        }

                        if (networkNeeded)
                        {
                            NetworkDataModel modelCopy;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                modelCopy = NetworksManager.SelectedNetworkModel.GetCopyForRender();
                                CtlInputDataPresenter.SetInputStat(NetworksManager.Models[0]);

                                swLock.Stop();
                                RenderTime.Network = swLock.Elapsed.Ticks;
                            }

                            DrawNetwork(modelCopy, CtlOnlyWeights.IsOn, CtlOnlyChangedWeights.IsOn, CtlHighlightChangedWeights.IsOn);

                            IsNetworkRendering = false;
                        }

                        if (statisticsNeeded)
                        {
                            NetworkDataModel selectedModel;
                            Statistics statistics;
                            double learningRate;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                CtlPlotPresenter.Vanish(NetworksManager.Models);

                                selectedModel = NetworksManager.SelectedNetworkModel;
                                statistics = selectedModel?.Statistics.Copy();
                                learningRate = selectedModel == null ? 0 : selectedModel.LearningRate;

                                swLock.Stop();
                                RenderTime.Statistics = swLock.Elapsed.Ticks;
                            }

                            CtlPlotPresenter.Draw(NetworksManager.Models, selectedModel);

                            var lastStats = DrawStatistics(statistics, learningRate);
                            if (selectedModel != null)
                            {
                                selectedModel.LastStatistics = lastStats;
                            }

                            IsStatisticsRendering = false;
                        }
                    }), DispatcherPriority.Render);//  .Send);
                }
            }

            StartTime.Stop();
        }