/// <summary> /// Reutrns a new position for the given position /// </summary> /// <param name="current">The position</param> /// <param name="target">The target</param> /// <param name="allowedTarget">The allowed target type</param> private Point GetNewPosition(Point current, Point target, CatAndMouseTile allowedTarget) { Point newPos = current; if (current.X != target.X) { newPos.X += (target.X - current.X) / Math.Abs(target.X - current.X); } if (current.Y != target.Y) { newPos.Y += (target.Y - current.Y) / Math.Abs(target.Y - current.Y); } //Check if the new position is valid if (this.IsValid(newPos, allowedTarget)) { return(newPos); } //Else make random move while (true) { newPos = current; newPos.X += this.Config.Random.Next(-1, 2); newPos.Y += this.Config.Random.Next(-1, 2); if (this.IsValid(newPos, allowedTarget)) { return(newPos); } } }
/// <summary> /// Runs the example /// </summary> public static void Run() { //var tiles = new CatAndMouseTile[10, 10]; //tiles[2, 4] = CatAndMouseTile.Wall; //tiles[3, 4] = CatAndMouseTile.Wall; //tiles[4, 4] = CatAndMouseTile.Wall; //tiles[5, 4] = CatAndMouseTile.Wall; //tiles[5, 5] = CatAndMouseTile.Wall; //tiles[5, 6] = CatAndMouseTile.Wall; //tiles[5, 7] = CatAndMouseTile.Wall; //tiles[6, 7] = CatAndMouseTile.Wall; //tiles[7, 7] = CatAndMouseTile.Wall; //tiles[8, 7] = CatAndMouseTile.Wall; var tiles = new CatAndMouseTile[5, 5]; tiles[2, 2] = CatAndMouseTile.Wall; tiles[3, 2] = CatAndMouseTile.Wall; tiles[1, 2] = CatAndMouseTile.Wall; tiles[2, 3] = CatAndMouseTile.Wall; tiles[2, 1] = CatAndMouseTile.Wall; var environment = new CatAndMouseEnvironment(new Configuration(500000), tiles); var agent = new MouseAgent(); environment.AddAgent(agent); environment.Initialize(); DrawWorld(environment); //Train Simulate(environment, environment.Config.MaxEpisodes); //Now for real environment.ResetScores(); agent.Learner.FollowPolicy = true; Simulate(environment, 10000); Console.WriteLine(string.Format("Cat: {0} - Mouse: {1}.", environment.CatScore, environment.MouseScore)); Console.WriteLine("Win ratio: " + ((environment.MouseScore) / (double)(environment.MouseScore + environment.CatScore)) * 100 + "%"); Console.ReadLine(); }