/// <summary>
        /// This method will let the AI play the maze for one game (windowless)
        /// </summary>
        public virtual MazeCycleOutcome Travel(int timerTimeout = 5000)
        {
            IMazeGame game = GetNewGameInstance();

            MazeCycleOutcome outcome = new MazeCycleOutcome();

            // Start AI Training
            currentSessionID = rlmNet.SessionStart();

            SessionStarted?.Invoke(rlmNet.RandomnessCurrentValue);

            tmr.Interval = timerTimeout;
            tmr.Start();
            timeout = 0;
            int movesCnt = 0;

            while (!outcome.GameOver && timeout == 0)
            {
                // set up input for x and y based on our current location on the maze
                var inputs = new List <RlmIOWithValue>()
                {
                    new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "X"), game.traveler.location.X.ToString()),
                    new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "Y"), game.traveler.location.Y.ToString()),
                };

                // get AI output based on inputs
                RlmCycle cycle     = new RlmCycle();
                var      aiResult  = cycle.RunCycle(rlmNet, currentSessionID, inputs, Learn);
                var      direction = Convert.ToInt16(aiResult.CycleOutput.Outputs.First().Value);

                // make the move
                outcome = game.CycleMaze(direction);

                // score AI output
                double score = ScoreAI(outcome, game.traveler);
                rlmNet.ScoreCycle(aiResult.CycleOutput.CycleID, score);

                MazeCycleComplete?.Invoke(game.traveler.location.X, game.traveler.location.Y, outcome.BumpedIntoWall);
                movesCnt++;
            }

            tmr.Stop();

            // compute final score
            outcome.FinalScore = game.CalculateFinalScore(game.Moves);

            // End AI Training
            rlmNet.SessionEnd(outcome.FinalScore);

            SessionComplete?.Invoke(rlmNet.SessionCount, outcome.FinalScore, movesCnt);

            return(outcome);
        }
Exemple #2
0
        private MazeCycleOutcome Travel(bool learn, int?timerTimeout = null, CancellationToken?token = null, bool perMoveDisplay = false)
        {
            IMazeGame game = GetNewGameInstance();

            MazeCycleOutcome outcome = new MazeCycleOutcome();

            // Start AI Training
            var currentSessionID = network.SessionStart();

            SessionStarted?.Invoke(network.RandomnessCurrentValue);

            recentMoves.Clear();

            //tmr.Interval = timerTimeout;
            //tmr.Start();
            //timeout = 0;
            int movesCnt = 1;

            while (!outcome.GameOver) // && timeout == 0)
            {
                if (token?.IsCancellationRequested == true)
                {
                    break;
                }

                // set up input for x and y based on our current location on the maze
                var inputs = new List <RlmIOWithValue>()
                {
                    //new RlmIOWithValue(network.Inputs.First(a => a.Name == "X"), game.traveler.location.X.ToString()),
                    //new RlmIOWithValue(network.Inputs.First(a => a.Name == "Y"), game.traveler.location.Y.ToString()),
                    new RlmIOWithValue(network.Inputs.First(a => a.Name == "Move"), movesCnt.ToString())
                };

                // get AI output based on inputs
                RlmCycle cycle     = new RlmCycle();
                var      aiResult  = cycle.RunCycle(network, currentSessionID, inputs, learn);
                var      direction = Convert.ToInt16(aiResult.CycleOutput.Outputs.First().Value);

                // make the move
                outcome = game.CycleMaze(direction);


                // score AI output
                double score = 0.0;
                //score = ScoreAI(outcome, game.traveler);
                network.ScoreCycle(aiResult.CycleOutput.CycleID, score);

                if (timerTimeout.HasValue)
                {
                    Task.Delay(timerTimeout.Value).Wait();
                }

                if (perMoveDisplay)
                {
                    MazeCycleComplete?.Invoke(game.traveler.location.X, game.traveler.location.Y, outcome.BumpedIntoWall);
                }

                if (!learn)
                {
                    recentMoves.Add(new MoveDetails()
                    {
                        Direction = direction, MoveNumber = movesCnt
                    });
                }

                movesCnt++;
            }

            //tmr.Stop();

            // compute final score
            outcome.FinalScore = game.CalculateFinalScore(game.Moves);

            // End AI Training
            network.SessionEnd(outcome.FinalScore);

            SessionComplete?.Invoke(network.SessionCount, outcome.FinalScore, movesCnt - 1);

            if (movesCnt > HighestMoveCount)
            {
                HighestMoveCount = movesCnt;
            }

            return(outcome);
        }