Example #1
0
        async void Init(MazeInfo mazeInfo)
        {
            if (!Learn)
            {
                CurrentIteration  = 1;
                Temp_num_sessions = 1;
                StartRandomness   = 0;
                EndRandomness     = 0;
            }

            replayMemory       = new object[Temp_num_sessions];
            StatusText.Content = "Initializing RLM engine...";
            await Task.Run(() => {
                RLMMazeTraveler traveler = new RLMMazeTraveler(mazeInfo, Learn, Temp_num_sessions, StartRandomness, EndRandomness); //Instantiate RlmMazeTraveler game lib to configure the network.

                traveler.SessionComplete   += Traveler_SessionComplete;
                traveler.MazeCycleComplete += Traveler_MazeCycleComplete;
                traveler.SessionStarted    += Traveler_SessionStarted;
                traveler.MazeCycleError    += mazecycle_error;
                game.traveler = traveler;

                if (Learn)
                {
                    RunUIThread(() => {
                        StatusText.Content = "Training started...";
                    });
                    //Start the training (Play the game)
                    for (int i = 0; i < RandomnessOver; i++)
                    {
                        locations = new List <Location>();
                        RunUIThread(() => {
                            lblCurrentSession.Content = 1;
                        });
                        CurrentIteration = i + 1;
                        traveler.Travel(10000);
                    }

                    // set to predict instead of learn for the remaining sessions
                    traveler.Learn = false;

                    for (int i = 0; i < Temp_num_sessions - RandomnessOver; i++)
                    {
                        CurrentIteration++;
                        locations = new List <Location>();
                        RunUIThread(() => {
                            StatusText.Content = $"Training started... {CurrentIteration * 100 / Temp_num_sessions}%";
                        });
                        traveler.Travel(10000);
                    }


                    //traveler.TrainingDone();
                    RunUIThread(() => {
                        StatusText.Content = $"Training done... 100%";
                    });
                }
                else
                {
                    RunUIThread(() => {
                        StatusText.Content = $"RLM preparing to play...";
                    });

                    locations = new List <Location>();

                    RunUIThread(() => {
                        StatusText.Content = $"RLM Playing...";
                    });
                    traveler.Learn = false;

                    traveler.Travel(10000);
                }
            }).ContinueWith(async(t) => {
                //show AI playing game
                Stopwatch watch = new Stopwatch();

                foreach (dynamic obj in replayMemory)
                {
                    RunUIThread(() =>
                    {
                        lblCurrentSession.Content = (int)obj.cycleNum + 1;
                        lblRandomness.Content     = (int)obj.randomnessLeft;
                    });

                    watch.Start();
                    foreach (Location loc in obj.moves as List <Location> )
                    {
                        var x = loc.X;
                        var y = loc.Y;
                        RunUIThread(() =>
                        {
                            maze.ChangeCellColor(new TravelerLocation()
                            {
                                X = loc.X, Y = loc.Y
                            }, true);
                        });
                        await Task.Delay(TimeSpan.FromMilliseconds(2));
                        //If game is not solved within 5s, go to the next session.
                        if (watch.Elapsed.TotalSeconds >= 10)
                        {
                            break;
                        }
                    }

                    watch.Reset();

                    RunUIThread(() => {
                        lblScore.Content = (double)obj.score;
                        lblMoves.Content = (int)obj.movesCnt;

                        if (!Learn)
                        {
                            StatusText.Content = $"RLM done playing...";
                        }
                        else
                        {
                            StatusText.Content = $"Showing rlm replay...";
                        }

                        maze.setGoalRect();
                    });
                }

                RunUIThread(() => {
                    StatusText.Content = $"Done. Close the window to return back to the menu and train again...";
                });
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
Example #2
0
        private MazeCycleOutcome CycleMaze(int direction, bool windowless = false)
        {
            MazeCycleOutcome outcome = new MazeCycleOutcome();

            outcome.BumpedIntoWall = false; outcome.GameOver = false; outcome.FinalScore = 0;

            TravelerLocation new_location = new TravelerLocation();

            //Calculate new location
            switch (direction)
            {
            case 0:
                if (traveler.location.Y <= 0)
                {
                    OutOfBoundsOccurred();
                    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 >= 49)
                {
                    OutOfBoundsOccurred();
                    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 >= 49)
                {
                    OutOfBoundsOccurred();
                    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();
                    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 (maze.TheMazeGrid[new_location.X, new_location.Y])
            {
                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;

            traveler.location = new_location;

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


            if (!windowless)
            {
                //Clear old location
                Action act = new Action(delegate
                {
                    maze.ChangeCellColor(old_location, false);
                });
                RunThreadUI(act);

                //first blink at new location
                //Clear old location
                Action act2 = new Action(delegate
                {
                    maze.ChangeCellColor(traveler.location, true);
                });
                RunThreadUI(act2);
            }

            return(outcome);
        }