Exemple #1
0
        public void setPGLValue()
        {
            if (base.isShowConsole)
            {
                openConsole();
            }
            DeterministicNode tmpNode = new DeterministicNode(this.board.getBoardState(), null, Constant.NONE, Constant.NONE);

            DeterministicNode.side = this.sideToMove;
            double x = 0;

            if (base.isShowConsole)
            {
                Console.WriteLine("\nSET PGL VALUE : 500 Kali");
            }
            for (int i = 0; i < 500; i++)
            {
                tmpNode.rollOut();
                x += tmpNode.s;
            }
            Node.PGL = x / 500;
            if (base.isShowConsole)
            {
                Console.WriteLine("SELESAI SET PGL VALUE");
                Console.WriteLine("=====================");
            }
        }
Exemple #2
0
        public override double rollOut()
        {
            this.s = 0;
            DeterministicNode tmp = new DeterministicNode(this.board.getBoardState(), null, Constant.NONE, Constant.NONE);

            while (tmp.board.isEnd() == END_STATE.CONTINUE)
            {
                this.s += 1;
                CESPFMove tmpMove = tmp.board.CESPF();
                if (tmpMove.isFlippingAction())
                {
                    tmp.board.flip(tmpMove.move.from.row, tmpMove.move.from.column);
                }
                else
                {
                    tmp.board.move(tmpMove.move.from.row, tmpMove.move.from.column, tmpMove.move.to.row, tmpMove.move.to.column);
                }
            }
            END_STATE end = tmp.board.isEnd();

            if (end == END_STATE.DRAW)
            {
                return(0.5);
            }
            else if (DeterministicNode.side == (int)end)
            {
                return(1);
            }

            return(0);
        }
Exemple #3
0
        public Node startNMCTS()
        {
            timer.Start();
            tree = new DeterministicNode(this.board.getBoardState(), null, Constant.NONE, Constant.NONE);
            tree.expand();
            tree.updateStatus(tree.rollOut(), tree.s);
            taskQueue jlhParallelThread = new taskQueue(base.jlhParallel);

            //Task<DeterministicNode> [] searchTree = new Task<DeterministicNode>[base.jlhParallel];

            //for (int i = 0; i < base.jlhParallel; i++)
            //{
            //    searchTree[i] = jlhParallelThread.enqueueReturn(move);
            //}


            Task <DeterministicNode> searchTree = null;

            for (int i = 0; i < base.jlhParallel; i++)
            {
                searchTree = jlhParallelThread.enqueueReturn(move);
            }
            //sum winrate nondeterministic node
            for (int j = 0; j < searchTree.Result.children.Length; j++)
            {
                if (searchTree.Result.children[j] is NondeterministicNode)
                {
                    ((NondeterministicNode)searchTree.Result.children[j]).sumWinRate();
                }
            }

            DeterministicNode result = searchTree.Result;

            Node maxWinRate = result.children[0];

            for (int i = 1; i < result.children.Length; i++)
            {
                if (maxWinRate.winRate <= result.children[i].winRate)
                {
                    maxWinRate = result.children[i];
                }
            }

            jlhParallelThread.Dispose();

            return(maxWinRate);
        }
Exemple #4
0
        public override void expand()
        {
            int actionLength = this.board.getCountActions();

            if (actionLength != 0)
            {
                children = new Node[actionLength];

                List <Actions> tmp   = this.board.getActions();
                int            index = 0;
                for (int i = 0; i < tmp.Count; i++)
                {
                    if (tmp[i].action == ACTION.MOVE)
                    {
                        DeterministicActions action = (DeterministicActions)tmp[i];
                        int length = action.to.Count;
                        for (int x = 0; x < length; x++)
                        {
                            BoardState tmpBoardState = this.board.getBoardState();
                            this.board.move(action.from.row, action.from.column, action.to[x].row, action.to[x].column);
                            children[index] = new DeterministicNode(this.board.getBoardState(), new Move(action.from, action.to[x]), Constant.NONE, Constant.NONE);

                            this.board.restoreBoardState(tmpBoardState);
                            index++;
                        }
                    }
                    else
                    {
                        NondeterministicActions action  = (NondeterministicActions)tmp[i];
                        NondeterministicNode    tmpNode = new NondeterministicNode(action.probability, new Move(action.position, action.position));

                        tmpNode.children = new DeterministicNode[action.probability.Count];
                        for (int x = 0; x < action.piece.Count; x++)
                        {
                            BoardState tmpBoardState = this.board.getBoardState();
                            Position   tmpPosition   = this.board.getFlippedPositionByPiece(action.piece[x]);                                          //temukan real position
                            this.board.switchFlippedPieceByPosition(tmpPosition.row, tmpPosition.column, action.position.row, action.position.column); //pindahkan dari real position ke position yang diinginkan
                            this.board.flip(action.position.row, action.position.column);                                                              //buka piece
                            tmpNode.children[x] = new DeterministicNode(this.board.getBoardState(), new Move(action.position, action.position), action.piece[x], action.probability[x]);
                            this.board.restoreBoardState(tmpBoardState);
                        }
                        children[index] = tmpNode;
                        index++;
                    }
                }
            }
        }
Exemple #5
0
        static void testParallelSrVl()
        {
            for (int xy = 0; xy < 1; xy++)
            {
                Console.WriteLine(xy + " uuuuuu");
                time = new Stopwatch();
                time.Start();
                Board board = new Board();
                board.flip(0, 0);
                SimpleTPVL.DeterministicNode.side = board.sideToMove;
                SimpleTPVL.DeterministicNode b = new SimpleTPVL.DeterministicNode(board.getBoardState(), null, Constant.NONE, Constant.NONE);
                double x = 0;
                int    u = 0;
                List <SimpleTPVL.Node> visited = new List <SimpleTPVL.Node>();
                SimpleTPVL.Node        cur     = b;
                visited.Add(cur);
                cur.expand();
                //cur.updateStatus(cur.rollOut(cur));
                object          a  = new object();
                ParallelOptions po = new ParallelOptions();
                po.MaxDegreeOfParallelism = 100;

                List <string> threadid = new List <string>();
                Parallel.For(0, 4000, po, i =>
                {
                    //lock (a)
                    //{
                    //    bool found = false;
                    //    foreach (string y in threadid)
                    //    {
                    //        if (y == Thread.CurrentThread.ManagedThreadId.ToString())
                    //        {
                    //            found = true;
                    //            break;
                    //        }
                    //    }
                    //    if (!found)
                    //    {
                    //        threadid.Add(Thread.CurrentThread.ManagedThreadId.ToString());
                    //    }
                    //}
                    u++;
                    Console.WriteLine(u);
                    b.selectAction();
                    //lock (a)
                    //{
                    //    while (!(cur.isLeaf()))
                    //    {
                    //        cur = cur.select();
                    //        visited.Add(cur);
                    //        if (cur is TP.NondeterministicNode)
                    //        {
                    //            if (((TP.NondeterministicNode)cur).selected != null)
                    //            {
                    //                cur = ((TP.NondeterministicNode)cur).selected;
                    //            }
                    //            else
                    //            {
                    //                cur = ((TP.NondeterministicNode)cur.select()).selected;
                    //            }
                    //            visited.Add(cur);
                    //        }
                    //    }
                    //    cur.expand();
                    //}
                    //double value = cur.rollOut(cur);
                    //foreach (TP.Node node in visited)
                    //{
                    //    node.updateStatus(value);
                    //}
                    //lock (a)
                    //{
                    //    x +=Node.s;
                    //}
                }//;
                             );

                //foreach (string y in threadid)
                //{
                //    Console.WriteLine(y);
                //}

                //for (int i = 0; i < 2001; i++)
                //{
                //    Console.WriteLine(i);
                //    b.selectAction(cur);
                //    Console.WriteLine(i);
                //    x += Node.s;
                //}//;
                time.Stop();
                SimpleTP.Node.PGL = x / 500;
                Console.WriteLine("PGL:" + SimpleTP.Node.PGL);
                Console.WriteLine(time.Elapsed.TotalSeconds);
            }
        }