예제 #1
0
        /// <summary>
        /// This is where RLM is being configured and trained. Call this method and pass the needed parameters to start the training.
        /// </summary>
        /// <param name="maze">Contains all the details of the maze that encog will be going to solve.</param>
        /// <param name="gameTimeout">The given time for solving the maze per session.</param>
        public static void MazeTrain(MazeInfo maze, int gameTimeout)
        {
            Console.WriteLine("RLM network settings:");

            int sessions          = Util.GetInput("Number of sessions [default 50]: ", 50); //Gets user input for the number of sessions
            int startRandomness   = Util.GetInput("Start randomness [default 100]: ", 100); //Gets user input for the start randomness
            int endRandomness     = Util.GetInput("End randomness [default 0]: ", 0);       //Gets user input for the end randomness
            int randomnessThrough = Util.GetInput("Number of sessions to enforce randomness [default 1]: ", 1);

            Console.WriteLine();

            try
            {
                RLMMazeTraveler traveler = new RLMMazeTraveler(maze, true, randomnessThrough, startRandomness, endRandomness); //Instantiate RlmMazeTraveler game lib to configure the network.
                traveler.SessionComplete += SesionComplete;

                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                //Start the training (Play the game)
                for (int i = 0; i < randomnessThrough; i++)
                {
                    traveler.Travel(gameTimeout);
                }

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

                for (int i = 0; i < sessions - randomnessThrough; i++)
                {
                    traveler.Travel(gameTimeout);
                }

                watch.Stop();

                Console.WriteLine($"Elapsed: {watch.Elapsed}");
                traveler.TrainingDone();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException is RlmDefaultConnectionStringException)
                {
                    Console.WriteLine($"Error: {e.InnerException.Message}");
                }
                else
                {
                    Console.WriteLine($"ERROR: {e.Message}");
                }
            }
        }
예제 #2
0
        private async Task StartGame(RLMMazeTraveler traveler)
        {
            int currentSession = 0;

            while (currentSession < TotalIterations)
            {
                currentSession++;
                lblCurrentSession.Content = currentSession;

                //var outcome = await traveler.Travel(currentSession);
                var outcome = traveler.Travel(currentSession);
                //var data = traveler.BumpedIntoWallPercentage;
                //lblBumpedIntoWall.Content = $"{data.ToString("#0.##")}%";
                lblMoveCount.Content         = outcome.Moves;
                lblSessionsCompleted.Content = outcome.FinalScore;
            }
        }
예제 #3
0
 private async void windowLessTraining_Loaded_1(object sender, RoutedEventArgs e)
 {
     if (isEncog)
     {
         //EncogMaze encogMaze = new EncogMaze(maze);
         //encogMaze.MazeCycleComplete += Traveler_MazeCycleComplete;
         ////encogMaze.EncogCycleComplete += EncogMaze_EncogCycleComplete;
         //encogMaze.TrainingIterationComplete += EncogMaze_TrainingIterationComplete;
         //await StartGame(encogMaze);
     }
     else
     {
         // reset session count
         //RNNMazeTraveler.ResestStaticSessionData();
         traveler = new RLMMazeTraveler(maze, Learn, Temp_num_sessions, StartRandomness, EndRandomness);
         traveler.MazeCycleComplete += Traveler_MazeCycleComplete;
         traveler.SessionStarted    += Traveler_SessionStarted;
         await StartGame(traveler);
     }
 }
예제 #4
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);
        }
        /// <summary>
        /// This is where RLM is being configured and trained.
        /// </summary>
        public void MazeTrain()
        {
            int mazeSize = Util.GetInput($"Enter Maze size [must be {MIN_MAZE_SIZE} or greater]: ", MIN_MAZE_SIZE);
            var maze     = GenerateOrGetExistingMaze(mazeSize);

            int gameTimeout = Util.GetInput($"Game timeouts after [{MAZE_GAME_TIMEOUT} seconds default]: ", MAZE_GAME_TIMEOUT);

            Console.WriteLine($"Perfect move count for this maze would be {maze.PerfectGameMovesCount}");
            Console.WriteLine();

            Console.WriteLine("RLM network settings:");

            int sessions          = Util.GetInput("Number of sessions [default 50]: ", 50); //Gets user input for the number of sessions
            int startRandomness   = Util.GetInput("Start randomness [default 100]: ", 100); //Gets user input for the start randomness
            int endRandomness     = Util.GetInput("End randomness [default 0]: ", 0);       //Gets user input for the end randomness
            int randomnessThrough = Util.GetInput("Number of sessions to enforce randomness [default 1]: ", 1);

            try
            {
                RLMMazeTraveler traveler = new RLMMazeTraveler(maze, true, randomnessThrough, startRandomness, endRandomness); //Instantiate RlmMazeTraveler game lib to configure the network.
                traveler.SessionComplete += SesionComplete;

                // execute it on another thread as not to block the RLM training
                Console.WriteLine("\nPress 'd' to show Data persistence progress\n");
                Task.Run(() =>
                {
                    while (!Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.D)
                    {
                        traveler.ShowDataPersistenceProgress = true;
                    }
                });

                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                traveler.ResetRLMRandomization();

                //Start the training (Play the game)
                for (int i = 0; i < randomnessThrough; i++)
                {
                    traveler.Travel(gameTimeout * 1000);
                }

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

                for (int i = 0; i < sessions - randomnessThrough; i++)
                {
                    traveler.Travel(gameTimeout * 1000);
                }

                watch.Stop();

                Console.WriteLine($"Elapsed: {watch.Elapsed}");
                traveler.TrainingDone();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException is RlmDefaultConnectionStringException)
                {
                    Console.WriteLine($"Error: {e.InnerException.Message}");
                }
                else
                {
                    Console.WriteLine($"ERROR: {e.Message}");
                }
            }
        }