public virtual IMazeGame GetNewGameInstance()
        {
            var game = new MazeGame();

            game.traveler = this;
            game.InitGame(maze);
            return(game);
        }
Пример #2
0
        /// <summary>
        /// This method will let the AI play the maze for one game (windowless)
        /// </summary>
        public MazeCycleOutcome Travel(int timerTimeout = 5000)
        {
            MazeGame game = new MazeGame();

            game.traveler = this;
            game.InitGame(maze);

            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);
        }
        //public double Travel(ref int timeout, out int movesCnt)
        public double Travel(int timerTimeout, out int movesCnt)
        {
            MazeGame game = new MazeGame();

            game.InitGame(maze);
            game.traveler            = this;
            game.traveler.location.X = maze.StartingPosition.X;
            game.traveler.location.Y = maze.StartingPosition.Y;

            var recentOutcome = new MazeCycleOutcome();

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

            while (!recentOutcome.GameOver && timeout == 0)
            {
                movesCnt++;
                var input = new BasicMLData(2);
                input[0] = xInput.Normalize(Convert.ToDouble(game.traveler.location.X));
                input[1] = yInput.Normalize(Convert.ToDouble(game.traveler.location.Y));

                IMLData output = network.Compute(input);

                double maxVal    = double.MinValue;
                int    direction = 0;
                for (int i = 0; i < output.Count; i++)
                {
                    if (output[i] > maxVal)
                    {
                        direction = i;
                        maxVal    = output[i];
                    }
                }
                recentOutcome = game.CycleMaze(direction);
                MazeCycleComplete?.Invoke(game.traveler.location.X, game.traveler.location.Y, recentOutcome.BumpedIntoWall);
            }

            tmr.Stop();

            var score = game.CalculateFinalScore(movesCnt);

            return(score);
        }
Пример #4
0
        public MainWindow(int mazeId, PlayerType type, Boolean learn = false, int temp_num_sessions = 1, int currentIteration = 1, int totalIterations = 1, int startRandomness = 1, int endRandomness = 1)
        {
            InitializeComponent();
            mazerepo = new MazeRepo();

            mazeInfo            = mazerepo.GetByID(mazeId);
            RandomnessOver      = temp_num_sessions;
            ClosedDueToGameOver = false;
            Learn = learn;
            Type  = type;
            if (Type == PlayerType.Human)
            {
                this.Title            = "Human Player";
                StatusText.Visibility = Visibility.Visible;
            }
            else if (Type == PlayerType.RNN)
            {
                if (learn)
                {
                    Temp_num_sessions = totalIterations;
                    StartRandomness   = startRandomness;
                    EndRandomness     = endRandomness;

                    this.Title                = "RNN Learning";
                    statGrid.Visibility       = Visibility.Visible;
                    lblMazeName.Content       = mazeInfo.Name;
                    lblTotalSession.Content   = totalIterations;
                    lblCurrentSession.Content = CurrentIteration = currentIteration;
                    lblScore.Content          = LastScore;
                    lblMoves.Content          = LastMovesCount;
                }
                else
                {
                    this.Title = "RNN Player";
                }
            }
            else
            {
                this.Title = "Encog Player";
            }

            game = new MazeGameLib.MazeGame();
            maze = new Maze();
            game.InitGame(mazeInfo);
            //Initialize Grid
            game.TheMazeGrid = mazeInfo.Grid;
            maze.InitializeMaze(game, mazeGrid);

            //Init Game
            //game.InitGame(this.MainGrid, new RunThreadUI_Delegate(RunUIThread));

            //Send Keys to Game
            if (Type == PlayerType.Human)
            {
                this.KeyDown += MainWindow_KeyDown;
            }

            // reset static session count
            if (currentIteration == 1)
            {
                //RNNMazeTraveler.ResestStaticSessionData();
            }

            //Send shutdown event to game
            //this.Closed += MainWindow_Closed;
        }