private void TPLM(int i, int jlhThread, Board board) { double jlhSimulasi = 0; int panjangPermainan = 0; if (radioCR.IsChecked.Value) { while (board.isEnd() == END_STATE.CONTINUE) { if (currentTurn == PLAYER.COMPUTER) { cumulativeTP.TreeParallelization a = new cumulativeTP.TreeParallelization(jlhThread / 2, wktMove.Value.Value, board.getBoardState(), false); a.setPGLValue(); cumulativeTP.Node tmp = a.startNMCTS(); action(tmp.action.from, tmp.action.to, board); } else { panjangPermainan++; cumulativeTP.TreeParallelization a = new cumulativeTP.TreeParallelization(jlhThread, wktMove.Value.Value, board.getBoardState(), false); a.setPGLValue(); cumulativeTP.Node tmp = a.startNMCTS(); action(tmp.action.from, tmp.action.to, board); jlhSimulasi += a.getTotalSimulasi(); } } } else { while (board.isEnd() == END_STATE.CONTINUE) { if (currentTurn == PLAYER.COMPUTER) { SimpleCumulativeTP.TreeParallelization a = new SimpleCumulativeTP.TreeParallelization(jlhThread / 2, wktMove.Value.Value, board.getBoardState(), false); a.setPGLValue(); SimpleCumulativeTP.Node tmp = a.startNMCTS(); action(tmp.action.from, tmp.action.to, board); } else { panjangPermainan++; SimpleCumulativeTP.TreeParallelization a = new SimpleCumulativeTP.TreeParallelization(jlhThread, wktMove.Value.Value, board.getBoardState(), false); a.setPGLValue(); SimpleCumulativeTP.Node tmp = a.startNMCTS(); action(tmp.action.from, tmp.action.to, board); jlhSimulasi += a.getTotalSimulasi(); } } } dg.Items.Add(new Result(i + 1, getResult(), jlhSimulasi / panjangPermainan)); }
static void testParallelSSAB() { for (int xy = 0; xy < 1; xy++) { Console.WriteLine(xy + " uuuuuu"); time = new Stopwatch(); time.Start(); Board board = new Board(); board.flip(0, 0); SimpleTP.DeterministicNode.side = board.sideToMove; SimpleTP.DeterministicNode b = new SimpleTP.DeterministicNode(board.getBoardState(), null, Constant.NONE, Constant.NONE); double x = 0; int u = 0; List <SimpleTP.Node> visited = new List <SimpleTP.Node>(); SimpleTP.Node cur = b; visited.Add(cur); cur.expand(); //cur.updateStatus(cur.rollOut(cur)); object a = new object(); ParallelOptions po = new ParallelOptions(); po.MaxDegreeOfParallelism = 2; List <string> threadid = new List <string>(); Parallel.For(0, 2000, po, i => { u++; Console.WriteLine(u); b.selectAction(); } ); time.Stop(); SimpleTP.Node.PGL = x / 500; Console.WriteLine("PGL:" + SimpleTP.Node.PGL); Console.WriteLine(time.Elapsed.TotalSeconds); } }
static void consoleTesting() { Board logicalCDC = new Board(); logicalCDC.flip(3, 3); //DeterministicNode.side = logicalCDC.sideToMove; Console.WriteLine("\nsidetoMove : " + logicalCDC.sideToMove); logicalCDC.flip(3, 4); while (logicalCDC.isEnd() == END_STATE.CONTINUE) { Console.WriteLine("\nsidetoMove : " + logicalCDC.sideToMove); SimpleTP.DeterministicNode a = new SimpleTP.DeterministicNode(logicalCDC.getBoardState(), null, Constant.NONE, Constant.NONE); //double x = 0; //for (int i = 0; i < 500; i++) //{ // a.selectAction(); // x += Node.s; // //Console.WriteLine(Node.s); //} //Node.PGL = x / 500; SimpleTP.Node.PGL = 0; Console.WriteLine("Mulai"); SimpleTP.DeterministicNode b = new SimpleTP.DeterministicNode(logicalCDC.getBoardState(), null, Constant.NONE, Constant.NONE); for (int i = 0; i < 5000; i++) { b.selectAction(); Console.WriteLine(i); } Console.WriteLine("End"); SimpleTP.Node maxWinRate = b.children[0]; Console.WriteLine("0" + " , " + b.children[0].winRate + " , " + b.children[0].nVisits + " , " + b.children[0].action.from.ToString() + b.children[0].action.to.ToString()); for (int i = 1; i < b.children.Length; i++) { Console.WriteLine(i + " , " + b.children[i].winRate + " , " + b.children[i].nVisits + " , " + b.children[i].action.from.ToString() + b.children[i].action.to.ToString()); if (maxWinRate.winRate <= b.children[i].winRate) { maxWinRate = b.children[i]; } } if (maxWinRate.action.from.row == maxWinRate.action.to.row && maxWinRate.action.from.column == maxWinRate.action.to.column) { Console.WriteLine("Flip"); Console.WriteLine("Posisi " + maxWinRate.action.from.ToString()); Console.WriteLine("Sebelum"); logicalCDC.printArrayStateFlip(); logicalCDC.flip(maxWinRate.action.from.row, maxWinRate.action.from.column); } else { Console.WriteLine("move"); Console.WriteLine("From " + maxWinRate.action.from.ToString()); Console.WriteLine("to " + maxWinRate.action.to.ToString()); Console.WriteLine("Sebelum"); logicalCDC.printArrayStateFlip(); logicalCDC.move(maxWinRate.action.from.row, maxWinRate.action.from.column, maxWinRate.action.to.row, maxWinRate.action.to.column); } Console.WriteLine("Sesudah"); logicalCDC.printArrayStateFlip(); Console.WriteLine("\nsidetoMove : " + logicalCDC.sideToMove); Console.WriteLine("Input Move (0,4,1,0) : "); string move = Console.ReadLine(); string[] mymove = move.Split(','); if (mymove.Length == 2) { Console.WriteLine("Sebelum"); logicalCDC.printArrayStateFlip(); logicalCDC.flip(Convert.ToInt16(mymove[0]), Convert.ToInt16(mymove[1])); } else { Console.WriteLine("Sebelum"); logicalCDC.printArrayStateFlip(); logicalCDC.move(Convert.ToInt16(mymove[0]), Convert.ToInt16(mymove[1]), Convert.ToInt16(mymove[2]), Convert.ToInt16(mymove[3])); } Console.WriteLine("Sesudah"); logicalCDC.printArrayStateFlip(); Console.WriteLine("Tekan Sembarangan key untuk lanjut"); Console.ReadKey(); System.GC.Collect(); } }
private void switchTurn() { END_STATE tmpResult = this.logicalCDC.isEnd(); if (tmpResult == END_STATE.BLACK_WIN) { //MAINKAN SUARANYA if (Properties.Settings.Default["Sound"].ToString() == "Enable") { Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["EndingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute)); Player.Play(); } MessageBox.Show("BLACK WIN"); main.newGame(); } else if (tmpResult == END_STATE.RED_WIN) { //MAINKAN SUARANYA if (Properties.Settings.Default["Sound"].ToString() == "Enable") { Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["EndingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute)); Player.Play(); } MessageBox.Show("RED WIN"); main.newGame(); } else if (tmpResult == END_STATE.DRAW) { //MAINKAN SUARANYA if (Properties.Settings.Default["Sound"].ToString() == "Enable") { Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["EndingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute)); Player.Play(); } MessageBox.Show("DRAW"); main.newGame(); } else { if (this.currentTurn == PLAYER.COMPUTER) { this.currentTurn = PLAYER.HUMAN; changeDisable(); } else { this.currentTurn = PLAYER.COMPUTER; changeDisable(); if (this.tournament == TOURNAMENT.HUMAN_VS_COMPUTER) { Task.Run(() => { string metode = Properties.Settings.Default["Metode"].ToString(); string fungsiEvaluasi = Properties.Settings.Default["Evaluasi"].ToString(); int jlhParallelTask = int.Parse(Properties.Settings.Default["ParallelTask"].ToString()); int moveTime = int.Parse(Properties.Settings.Default["MoveTime"].ToString()); if (metode.Equals("Root")) { if (fungsiEvaluasi.Equals("Cumulative")) { cumulativeRP.RootParallelization a = new cumulativeRP.RootParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); cumulativeRP.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new CumulativeTree(a.result, CumulativeTree.METODE.ROOT); //switchTurn(); })); } else { SimpleCumulativeRP.RootParallelization a = new SimpleCumulativeRP.RootParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); SimpleCumulativeRP.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new SimpleTree(a.result, SimpleTree.METODE.ROOT); //switchTurn(); })); } } else if (metode.Equals("Tree")) { if (fungsiEvaluasi.Equals("Cumulative")) { cumulativeTP.TreeParallelization a = new cumulativeTP.TreeParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); cumulativeTP.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new CumulativeTree(a.tree, CumulativeTree.METODE.TREE); //switchTurn(); })); } else { SimpleCumulativeTP.TreeParallelization a = new SimpleCumulativeTP.TreeParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); SimpleCumulativeTP.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new SimpleTree(a.tree, SimpleTree.METODE.TREE); //switchTurn(); })); } } else { if (fungsiEvaluasi.Equals("Cumulative")) { cumulativeTPVL.TreeParallelization a = new cumulativeTPVL.TreeParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); cumulativeTPVL.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new CumulativeTree(a.tree, CumulativeTree.METODE.TREEVL); //switchTurn(); })); } else { SimpleCumulativeTPVL.TreeParallelization a = new SimpleCumulativeTPVL.TreeParallelization(jlhParallelTask, moveTime, this.logicalCDC.getBoardState(), false); a.setPGLValue(); SimpleCumulativeTPVL.Node tmp = a.startNMCTS(); this.Dispatcher.BeginInvoke((Action)(() => {//jalankan moveByCoding(tmp.action.from, tmp.action.to); main.tree.Content = new SimpleTree(a.tree, SimpleTree.METODE.TREEVL); //switchTurn(); })); } } }); } } } }