Exemplo n.º 1
0
    public bool LoadState(int key)
    {
        MazeEnvironment fromEnv = this;

        if (fromEnv.savedState.ContainsKey(key))
        {
            GameState state = fromEnv.savedState[key];
            System.Buffer.BlockCopy(state.map, 0, map, 0, map.Length * sizeof(float));
            currentPlayerPosition = state.currentPlayerPosition;
            goalPosition          = state.goalPosition;
            startPosition         = state.startPosition;
            Win = state.win;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Exemplo n.º 2
0
        static void AbsoluteMazeProblem()
        {
            //Declare random
            MyRandom.RandomPool.Declare("action",(int)DateTime.Now.Ticks);

            //環境
            MazeEnvironment mazeEnv = new MazeEnvironment();
            mazeEnv.Map = new MazeMap(new int[,]{
                {  1,  0,  0,  0,  0 },
                {  0,  0,  0,  0,  0 },
                {  0,  0,  0,  0,  0 },
                {  0,  0,  0,  0,  3 },
                {  0,  0,  3,  0,  2 },
            }) ;

            //エージェントを宣言
            var mazeAgent
                = new QLearningAgent<MazeEnvironment,PositionState,MoveAction>();
                //= new ActorCritic<MazeEnvironment,PositionState,MoveAction>();

                //エージェントに環境をセット
                mazeAgent.Environment = mazeEnv;

                //環境にエージェントをセット
                mazeEnv.AddAgent(mazeAgent);

                mazeAgent.Id = 1;

            //エージェントを宣言
            var mazeAgent2
                //= new QLearningAgent<MazeEnvironment,PositionState,FourDirectionAction>();
                = new ActorCritic<MazeEnvironment,PositionState,MoveAction>();

                //エージェントに環境をセット
                mazeAgent2.Environment = mazeEnv;

                //環境にエージェントをセット
                mazeEnv.AddAgent(mazeAgent2);

                mazeAgent2.Id = 2;

            //初期状態をセット
            mazeAgent2.CurrentState = mazeEnv.StartState;

            //初期状態をセット
            mazeAgent.CurrentState = mazeEnv.StartState;

            //View のセット
            MazeView mazeView = new MazeView();
            mazeView.Maze = mazeEnv;

            while(true)
            {

                mazeAgent.Act();
                mazeAgent2.Act();

                mazeView.show();

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                mazeAgent.Show();
                Console.WriteLine();
                mazeAgent2.Show();

                string command = Console.ReadLine();  //を、キーをおすたびにやる。

                if (command.Equals("init"))
                {
                    mazeAgent.Init();
                    mazeAgent2.Init();
                }

                else if (command.Equals("q"))
                {
                    mazeAgent.Show ();
                    continue;
                }

                try //数字としてパースしてみて
                {   //パースできたらその回数ステップ進める
                    int num = int.Parse(command);

                    foreach (int j in Enumerable.Range(0, num))
                    {
                        mazeAgent.Act();
                        mazeAgent2.Act();
                    }
                }
                catch (Exception)
                {
                }
            }
        }