public RLMMazeTraveler(IMazeGame gameRef, bool learn = false) : base(gameRef) { Learn = learn; GameRef.GameStartEvent += GameRef_GameStartEvent; GameRef.GameCycleEvent += GameRef_GameCycleEvent; //GameRef.GameOverEvent += GameRef_GameOverEvent; tmr.Elapsed += Tmr_Elapsed; }
/// <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); }
public static void Main(string[] args) { var result = true; mazeGame = DependencyFactory.Resolve <IMazeGame>(); mazeGame.StartNew(new Coordinate(10, 10)); //ShowOptions(); while (result) { var input = Console.ReadKey(); result = Execute(input); if (!result) { result = Execute1(input); } } }
// for window version public RLMMazeTraveler(MazeInfo maze, IMazeGame gameref, bool learn = false, int numSessions = 1, int startRandomness = 1, int endRandomness = 1, SetRandomnessLeftDelegate setRandomnessLeft = null) : base(gameref) { Learn = learn; GameRef.GameStartEvent += GameRef_GameStartEvent; GameRef.GameCycleEvent += GameRef_GameCycleEvent; //GameRef.GameOverEvent += GameRef_GameOverEvent; rlmNet = CreateOrLoadNetwork(maze); // Temporary, while RFactor not yet implemented rlmNet.NumSessions = numSessions; rlmNet.StartRandomness = startRandomness; rlmNet.EndRandomness = endRandomness; rlmNet.CycleComplete += Rlm_net_CycleComplete; SetRandomnessLeft = setRandomnessLeft; tmr.Elapsed += Tmr_Elapsed; }
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); }
public Traveler(IMazeGame gameref) : this() { GameRef = gameref; }