Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (!(obj is MazeState))
            {
                return(false);
            }

            MazeState other = (MazeState)obj;

            return
                (this.positionX == other.positionX &&
                 this.positionY == other.PositionY);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prints the best path
        /// </summary>
        private static void PrintPath(MazeEnvironment environment, MazeAgent agent)
        {
            var       learner = agent.Learner as QLearning <MazeState>;
            var       path    = new HashSet <MazeState>();
            MazeState state   = environment.GetDefaultState(agent);

            path.Add(state);

            while (true)
            {
                MazeAction action = (MazeAction)learner.SelectAction(state);

                switch (action)
                {
                case MazeAction.Up:
                    state = state.Add(y: -1);
                    break;

                case MazeAction.Down:
                    state = state.Add(y: 1);
                    break;

                case MazeAction.Right:
                    state = state.Add(x: 1);
                    break;

                case MazeAction.Left:
                    state = state.Add(x: -1);
                    break;;
                }

                path.Add(state);

                if (environment.Tiles[state.PositionX, state.PositionY] == MazeTile.Goal)
                {
                    break;
                }
            }


            for (int y = 0; y < environment.Length; y++)
            {
                for (int x = 0; x < environment.Width; x++)
                {
                    switch (environment.Tiles[x, y])
                    {
                    case MazeTile.Floor:
                        if (path.Contains(new MazeState(x, y)))
                        {
                            Console.Write("x");
                        }
                        else
                        {
                            Console.Write(" ");
                        }
                        break;

                    case MazeTile.Wall:
                        Console.Write("W");
                        break;

                    case MazeTile.Agent:
                        Console.Write("A");
                        break;

                    case MazeTile.Goal:
                        Console.Write("G");
                        break;
                    }
                }

                Console.WriteLine();
            }
        }