Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -1,7 +1,7 @@
package com.thealgorithms.searches;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
@ -23,8 +23,7 @@ public class MonteCarloTreeSearch {
int score;
int visitCount;
public Node() {
}
public Node() {}
public Node(Node parent, boolean isPlayersTurn) {
this.parent = parent;
@ -78,7 +77,10 @@ public class MonteCarloTreeSearch {
winnerNode = getWinnerNode(rootNode);
printScores(rootNode);
System.out.format("\nThe optimal node is: %02d\n", rootNode.childNodes.indexOf(winnerNode) + 1);
System.out.format(
"\nThe optimal node is: %02d\n",
rootNode.childNodes.indexOf(winnerNode) + 1
);
return winnerNode;
}
@ -118,8 +120,13 @@ public class MonteCarloTreeSearch {
break;
}
uctTemp = ((double) childNode.score / childNode.visitCount)
+ 1.41 * Math.sqrt(Math.log(promisingNode.visitCount) / (double) childNode.visitCount);
uctTemp =
((double) childNode.score / childNode.visitCount) +
1.41 *
Math.sqrt(
Math.log(promisingNode.visitCount) /
(double) childNode.visitCount
);
if (uctTemp > uctIndex) {
uctIndex = uctTemp;
@ -148,7 +155,7 @@ public class MonteCarloTreeSearch {
// To use the MCTS algorithm correctly this should be a simulation of the nodes' current
// state of the game until it finishes (if possible) and use an evaluation function to
// determine how good or bad the play was.
// e.g. Play tic tac toe choosing random squares until the game ends.
// e.g. Play tic tac toe choosing random squares until the game ends.
promisingNode.playerWon = (rand.nextInt(6) == 0);
isPlayerWinner = promisingNode.playerWon;
@ -158,8 +165,10 @@ public class MonteCarloTreeSearch {
tempNode.visitCount++;
// Add wining scores to bouth player and opponent depending on the turn.
if ((tempNode.isPlayersTurn && isPlayerWinner)
|| (!tempNode.isPlayersTurn && !isPlayerWinner)) {
if (
(tempNode.isPlayersTurn && isPlayerWinner) ||
(!tempNode.isPlayersTurn && !isPlayerWinner)
) {
tempNode.score += WIN_SCORE;
}
@ -168,15 +177,24 @@ public class MonteCarloTreeSearch {
}
public Node getWinnerNode(Node rootNode) {
return Collections.max(rootNode.childNodes, Comparator.comparing(c -> c.score));
return Collections.max(
rootNode.childNodes,
Comparator.comparing(c -> c.score)
);
}
public void printScores(Node rootNode) {
System.out.println("N.\tScore\t\tVisits");
for (int i = 0; i < rootNode.childNodes.size(); i++) {
System.out.println(String.format("%02d\t%d\t\t%d", i + 1,
rootNode.childNodes.get(i).score, rootNode.childNodes.get(i).visitCount));
System.out.println(
String.format(
"%02d\t%d\t\t%d",
i + 1,
rootNode.childNodes.get(i).score,
rootNode.childNodes.get(i).visitCount
)
);
}
}
}