private void useSessionButton_Click(object sender, EventArgs e)
        {
            var episode = Convert.ToInt32(sesssionList.SelectedItems[0].Text);

            SelectedSession = trainingSessions.Where(s => s.Episode == episode).FirstOrDefault();

            this.DialogResult = DialogResult.OK;
        }
        private Task RunTests()
        {
            //var sessions = QLearningMaze.Core.TrainingSession
            //    .GetTrainingSessions(_agent.Environment.QualitySaveDirectory)
            //    .OrderBy(e => e.Episode).ToList();

            var sessions = _agent.TrainingSessions.OrderBy(e => e.Episode).ToList();

            var agent = GetTestAgent();

            agent.AgentCompleted += Maze_AgentCompleted;

            for (int i = sessions.Count - 1; i >= 0; --i)
            {
                var session = new TrainingSessionEx
                {
                    Episode = sessions[i].Episode,
                    Moves   = sessions[i].Moves,
                    Quality = sessions[i].Quality,
                    Score   = sessions[i].Score
                };

                _moves = 0;
                _score = 0;

                agent.Environment.QualityTable = session.Quality;

                try
                {
                    agent.Run(agent.StartPosition);
                    session.Succeeded = true;
                }
                catch
                {
                    session.Succeeded = false;
                }

                session.Moves = _moves;
                session.Score = _score;

                trainingSessions.Add(session);
            }

            var selection = trainingSessions
                            .GroupBy(x => new
            {
                x.Moves,
                x.Score,
                x.Succeeded
            })
                            .Select(s => new TrainingSessionEx()
            {
                MinEpisode = s.Last().Episode,
                MaxEpisode = s.First().Episode,
                Episode    = s.Last().Episode,
                Moves      = s.Key.Moves,
                Score      = s.Key.Score,
                Succeeded  = s.Key.Succeeded,
                Quality    = s.First().Quality
            });

            trainingSessions = selection.OrderByDescending(s => s.Succeeded).ThenByDescending(s => s.Score).ThenByDescending(m => m.Moves).ThenByDescending(e => e.MinEpisode).ToList();
            SelectedSession  = trainingSessions.FirstOrDefault();

            _isChecking = false;

            agent.AgentCompleted -= Maze_AgentCompleted;
            return(Task.CompletedTask);
        }