Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MetatransApps committed Dec 16, 2024
1 parent 8f4c106 commit 6a0f212
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions EnginesRunner/src/bagaturchess/montecarlo/MCTS_V2.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class MCTS_V2 {

public static void main(String[] args) {

String fen = Constants.INITIAL_BOARD;
//String fen = Constants.INITIAL_BOARD;
//String fen = "rnbqk1nr/pppp1pp1/1p2p2p/8/8/1P1P2PP/P1PbPPB1/RNBQK1NR w KQkq - 0 6";
//String fen = "5r2/1p1RRrk1/4Qq1p/1PP3p1/8/4B3/1b3P1P/6K1 w - - 0 1"; //bm Qxf7+ Rxf7+; id WAC.235
String fen = "5r2/1p1RRrk1/4Qq1p/1PP3p1/8/4B3/1b3P1P/6K1 w - - 0 1"; //bm Qxf7+ Rxf7+; id WAC.235
//String fen = "8/7p/5k2/5p2/p1p2P2/Pr1pPK2/1P1R3P/8 b - - 0 1"; //bm Rxb2
//String fen = "2r1n2r/1q4k1/2p1pn2/ppR4p/4PNbP/P1BBQ3/1P4P1/R5K1 b - - 1 32";

Expand Down Expand Up @@ -115,8 +115,6 @@ private static class MCTS {

private static final double EXPLORATION_FACTOR = Math.sqrt(2);

//private Random random = new Random();

private IMoveList movesBuffer = new BaseMoveList(333);


Expand Down Expand Up @@ -163,6 +161,17 @@ public List<Integer> findBestMove(IBitBoard bitboard, IEvaluator evaluator, int
backpropagate(expandedNode, simulationResult);
}


for (MCTSNode node: rootNode.children) {
System.out.println(
bitboard.getMoveOps().moveToString(node.originating_move)
+ " " + node.value
+ " " + node.visits
+ " " + node.value / node.visits
);
}


return getMostVisitedChildrenMoves(rootNode);
}

Expand Down Expand Up @@ -376,7 +385,7 @@ private void backpropagate(MCTSNode node, double result) {

while (currentNode != null) {
currentNode.visits++;
currentNode.value += result;
currentNode.value += (currentNode.colour_to_move == Constants.COLOUR_WHITE ? result : -result);
currentNode = currentNode.parent;
}
}
Expand Down Expand Up @@ -404,8 +413,8 @@ private MCTSNode getBestChild(MCTSNode node, double explorationFactor) {

private double getUCTValue(MCTSNode node, double explorationFactor) {
if (node.visits == 0) return Double.MAX_VALUE;
//double exploitation = node.value / node.visits;
double exploitation = (node.colour_to_move == Constants.COLOUR_WHITE ? node.value : -node.value) / node.visits;
double exploitation = node.value / node.visits;
//double exploitation = (node.colour_to_move == Constants.COLOUR_WHITE ? node.value : -node.value) / node.visits;
double exploration = explorationFactor * Math.sqrt(Math.log(node.parent.visits) / node.visits);
return exploitation + exploration;
}
Expand All @@ -432,10 +441,10 @@ private MCTSNode getMostVisitedChild(MCTSNode node) {

private MCTSNode getBestValuedChild(MCTSNode node) {
MCTSNode bestChild = null;
double best_eval = Double.NEGATIVE_INFINITY; // Fixed comparison value
double best_eval = Double.NEGATIVE_INFINITY;

for (MCTSNode child : node.children) {
double score = (child.colour_to_move == Constants.COLOUR_WHITE ? child.value : -child.value) / child.visits; // Adjusted value
double score = child.value / child.visits;
if (score > best_eval) {
best_eval = score;
bestChild = child;
Expand Down

0 comments on commit 6a0f212

Please sign in to comment.