Exemplo n.º 1
0
        void GameRef_GameCycleEvent(MazeCycleOutcome mazeCycle, Traveler traveler)
        {
            try
            {
                // A Cycle Ended, Mark the Score, then make your next move

                // determine score
                double score = ScoreAI(mazeCycle, traveler);
                rlmNet.ScoreCycle(currentCycleID, score);

                if (!mazeCycle.GameOver)
                {
                    // start next AI's move
                    var inputs = new List <RlmIOWithValue>()
                    {
                        new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "X"), traveler.location.X.ToString()),
                        new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "Y"), traveler.location.Y.ToString()),
                        //new RlmIOWithValue(rnn_net.Inputs.First(a => a.Name == "BumpedIntoWall"), mazeCycle.BumpedIntoWall.ToString())
                    };

                    RlmCycle cycle = new RlmCycle();
                    cycle.RunCycle(rlmNet, currentSessionID, inputs, Learn);
                }
            }
            catch (Exception)
            {
                if (MazeCycleError != null)
                {
                    MazeCycleError(rlmNet.CurrentNetworkName);
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        private double ScoreAI(MazeCycleOutcome mazeCycle, Traveler traveler)
        {
            double retVal = WRONG_SCORE;

            if (mazeCycle.GameOver)
            {
                retVal = CORRECT_SCORE * 2;
            }
            else if (!mazeCycle.BumpedIntoWall)
            {
                retVal = CORRECT_SCORE;
            }

            return(retVal);
        }
Exemplo n.º 4
0
        private void GameLoop()
        {
            int i = 0;
            //Cycle
            MazeCycleOutcome LastOutcome = new MazeCycleOutcome();

            while (LastOutcome.GameOver == false)
            {
                if (CancelGame)
                {
                    return;
                }

                Int16 dir = -1;
                if (DirectionsStack.TryDequeue(out dir))
                {
                    //incriment the count
                    i++;
                    //Run the maze cycle
                    LastOutcome = CycleMaze(dir);

                    //GameCycledEvent
                    if (GameCycleEvent != null)
                    {
                        GameCycleEvent(LastOutcome, traveler);
                    }

                    //Check for Game Over
                    if (LastOutcome.GameOver)
                    {
                        //Build Final Game
                        MazeGameFinalOutcome FinalOutcome = new MazeGameFinalOutcome();
                        FinalOutcome.CycleCount = i;
                        FinalOutcome.FinalScore = CalculateFinalScore(i);

                        //Event
                        if (GameOverEvent != null)
                        {
                            GameOverEvent(FinalOutcome);
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
        private void GameRef_GameCycleEvent(MazeCycleOutcome cycle, Traveler traveler)
        {
            // inputs
            double[] inputs = new[]
            {
                Convert.ToDouble(traveler.location.X),
                Convert.ToDouble(traveler.location.Y),
                Convert.ToDouble(cycle.BumpedIntoWall)
            };

            Int16 direction = this.Traveler.Play(inputs);

            //tmp delay since encog going too fast
            System.Threading.Thread.Sleep(10);

            gameRef.DirectionsStack.Enqueue(direction);
        }
        //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);
        }
Exemplo n.º 7
0
        public MazeCycleOutcome CycleMaze(int direction)
        {
            MazeCycleOutcome outcome = new MazeCycleOutcome();

            outcome.BumpedIntoWall   = false;
            outcome.GameOver         = false;
            outcome.FinalScore       = 0;
            outcome.PreviousLocation = new Location()
            {
                X = traveler.location.X, Y = traveler.location.Y
            };

            TravelerLocation new_location = new TravelerLocation();

            outcome.Moves = Moves++;
            //Calculate new location
            switch (direction)
            {
            case 0:
                if (traveler.location.Y <= 0)
                {
                    //OutOfBoundsOccurred();
                    BumpIntoWall           = true;
                    outcome.BumpedIntoWall = true;
                    return(outcome);
                }
                new_location.X = traveler.location.X;
                new_location.Y = traveler.location.Y - 1;
                break;

            case 1:
                if (traveler.location.X >= Width - 1)
                {
                    //OutOfBoundsOccurred();
                    BumpIntoWall           = true;
                    outcome.BumpedIntoWall = true;
                    return(outcome);
                }
                new_location.X = traveler.location.X + 1;
                new_location.Y = traveler.location.Y;
                break;

            case 2:
                if (traveler.location.Y >= Height - 1)
                {
                    //OutOfBoundsOccurred();
                    BumpIntoWall           = true;
                    outcome.BumpedIntoWall = true;
                    return(outcome);
                }
                new_location.X = traveler.location.X;
                new_location.Y = traveler.location.Y + 1;
                break;

            case 3:
                if (traveler.location.X <= 0)
                {
                    //OutOfBoundsOccurred();
                    BumpIntoWall           = true;
                    outcome.BumpedIntoWall = true;
                    return(outcome);
                }
                new_location.X = traveler.location.X - 1;
                new_location.Y = traveler.location.Y;
                break;

            default:
                throw new Exception("Not valid input");
            }


            //Is BumpedIntoWall?
            if (this.TheMazeGrid[new_location.X, new_location.Y])
            {
                BumpIntoWall           = true;
                outcome.BumpedIntoWall = true;
                outcome.FinalScore     = 0;
                outcome.GameOver       = false;
                //Play sound
                //SystemSounds.Hand.Play();
                return(outcome);
            }

            //New location is now current location
            //TravelerLocation old_location = traveler.location;
            OldLocation       = traveler.location;
            traveler.location = new_location;

            //Is GameOver?
            if (traveler.location.X == GoalLocation.X && traveler.location.Y == GoalLocation.Y)
            {
                outcome.GameOver = true;
                return(outcome);
            }

            return(outcome);
        }