From feac90b9b911646dc710e62eeab92cc74a3962a8 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:29:03 +0530 Subject: [PATCH 1/4] Add tests, remove `main` in `MonteCarloTreeSearch` --- .../searches/MonteCarloTreeSearch.java | 6 - .../searches/MonteCarloTreeSearchTest.java | 127 ++++++++++++++++++ 2 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java diff --git a/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java b/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java index 268c33cef610..aa74398b708b 100644 --- a/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java +++ b/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java @@ -39,12 +39,6 @@ public Node(Node parent, boolean isPlayersTurn) { static final int WIN_SCORE = 10; static final int TIME_LIMIT = 500; // Time the algorithm will be running for (in milliseconds). - public static void main(String[] args) { - MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); - - mcts.monteCarloTreeSearch(mcts.new Node(null, true)); - } - /** * Explores a game tree using Monte Carlo Tree Search (MCTS) and returns the * most promising node. diff --git a/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java b/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java new file mode 100644 index 000000000000..ea36cc4d8a10 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java @@ -0,0 +1,127 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class MonteCarloTreeSearchTest { + + /** + * Test the creation of a node and its initial state. + */ + @Test + void testNodeCreation() { + MonteCarloTreeSearch.Node node = new MonteCarloTreeSearch().new Node(null, true); + assertNotNull(node, "Node should be created"); + assertNull(node.parent, "Parent should be null for root node"); + assertTrue(node.childNodes.isEmpty(), "Child nodes should be empty upon creation"); + assertTrue(node.isPlayersTurn, "Initial turn should be player's turn"); + assertEquals(0, node.score, "Initial score should be zero"); + assertEquals(0, node.visitCount, "Initial visit count should be zero"); + } + + /** + * Test adding child nodes to a parent node. + */ + @Test + void testAddChildNodes() { + MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); + MonteCarloTreeSearch.Node parentNode = mcts.new Node(null, true); + + mcts.addChildNodes(parentNode, 5); + + assertEquals(5, parentNode.childNodes.size(), "Parent should have 5 child nodes"); + for (MonteCarloTreeSearch.Node child : parentNode.childNodes) { + assertEquals(false, child.isPlayersTurn, "Child node should not be player's turn"); + assertEquals(0, child.visitCount, "Child node visit count should be zero"); + } + } + + /** + * Test the UCT selection of a promising node. + */ + @Test + void testGetPromisingNode() { + MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); + MonteCarloTreeSearch.Node parentNode = mcts.new Node(null, true); + + // Create child nodes with different visit counts and scores + for (int i = 0; i < 3; i++) { + MonteCarloTreeSearch.Node child = mcts.new Node(parentNode, false); + child.visitCount = i + 1; // Visits 1, 2, 3 + child.score = i * 2; // Scores 0, 2, 4 + parentNode.childNodes.add(child); + } + + // Get promising node + MonteCarloTreeSearch.Node promisingNode = mcts.getPromisingNode(parentNode); + + // The child with the highest UCT value should be chosen. + assertNotNull(promisingNode, "Promising node should not be null"); + assertEquals(0, parentNode.childNodes.indexOf(promisingNode), "The first child should be the most promising"); + } + + /** + * Test simulation of random play and backpropagation. + */ + @Test + void testSimulateRandomPlay() { + MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); + MonteCarloTreeSearch.Node node = mcts.new Node(null, true); + node.visitCount = 10; // Simulating existing visits + + // Simulate random play + mcts.simulateRandomPlay(node); + + // Check visit count after simulation + assertEquals(11, node.visitCount, "Visit count should increase after simulation"); + + // Check if score is updated correctly + assertTrue(node.score >= 0 && node.score <= MonteCarloTreeSearch.WIN_SCORE, "Score should be between 0 and WIN_SCORE"); + } + + /** + * Test retrieving the winning node based on scores. + */ + @Test + void testGetWinnerNode() { + MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); + MonteCarloTreeSearch.Node parentNode = mcts.new Node(null, true); + + // Create child nodes with varying scores + MonteCarloTreeSearch.Node winningNode = mcts.new Node(parentNode, false); + winningNode.score = 10; // Highest score + parentNode.childNodes.add(winningNode); + + MonteCarloTreeSearch.Node losingNode = mcts.new Node(parentNode, false); + losingNode.score = 5; + parentNode.childNodes.add(losingNode); + + MonteCarloTreeSearch.Node anotherLosingNode = mcts.new Node(parentNode, false); + anotherLosingNode.score = 3; + parentNode.childNodes.add(anotherLosingNode); + + // Get the winning node + MonteCarloTreeSearch.Node winnerNode = mcts.getWinnerNode(parentNode); + + assertEquals(winningNode, winnerNode, "Winning node should have the highest score"); + } + + /** + * Test the full Monte Carlo Tree Search process. + */ + @Test + void testMonteCarloTreeSearch() { + MonteCarloTreeSearch mcts = new MonteCarloTreeSearch(); + MonteCarloTreeSearch.Node rootNode = mcts.new Node(null, true); + + // Execute MCTS and check the resulting node + MonteCarloTreeSearch.Node optimalNode = mcts.monteCarloTreeSearch(rootNode); + + assertNotNull(optimalNode, "MCTS should return a non-null optimal node"); + assertTrue(rootNode.childNodes.contains(optimalNode), "Optimal node should be a child of the root"); + } +} From ef2fc9dc4d8f61f752e364de2c6fa3a632d1c5b3 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 06:59:24 +0000 Subject: [PATCH 2/4] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..27cb6b9692ae 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,6 +31,7 @@ * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) + * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) @@ -638,6 +639,7 @@ * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) + * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) @@ -970,6 +972,7 @@ * [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java) * [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java) * [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java) + * [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java) * [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java) * [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java) * [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java) From 3e0ba91b3ae94aeef6378072178b8380a1573cd7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:46:59 +0530 Subject: [PATCH 3/4] Fix --- .../thealgorithms/searches/MonteCarloTreeSearchTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java b/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java index ea36cc4d8a10..69c958f67a40 100644 --- a/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java @@ -1,8 +1,8 @@ package com.thealgorithms.searches; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -16,7 +16,6 @@ class MonteCarloTreeSearchTest { void testNodeCreation() { MonteCarloTreeSearch.Node node = new MonteCarloTreeSearch().new Node(null, true); assertNotNull(node, "Node should be created"); - assertNull(node.parent, "Parent should be null for root node"); assertTrue(node.childNodes.isEmpty(), "Child nodes should be empty upon creation"); assertTrue(node.isPlayersTurn, "Initial turn should be player's turn"); assertEquals(0, node.score, "Initial score should be zero"); @@ -35,7 +34,7 @@ void testAddChildNodes() { assertEquals(5, parentNode.childNodes.size(), "Parent should have 5 child nodes"); for (MonteCarloTreeSearch.Node child : parentNode.childNodes) { - assertEquals(false, child.isPlayersTurn, "Child node should not be player's turn"); + assertFalse(child.isPlayersTurn, "Child node should not be player's turn"); assertEquals(0, child.visitCount, "Child node visit count should be zero"); } } @@ -51,8 +50,8 @@ void testGetPromisingNode() { // Create child nodes with different visit counts and scores for (int i = 0; i < 3; i++) { MonteCarloTreeSearch.Node child = mcts.new Node(parentNode, false); - child.visitCount = i + 1; // Visits 1, 2, 3 - child.score = i * 2; // Scores 0, 2, 4 + child.visitCount = i + 1; + child.score = i * 2; parentNode.childNodes.add(child); } From 6a7b177ed002b22a7ffc3018ba10e711339863ff Mon Sep 17 00:00:00 2001 From: siriak Date: Thu, 10 Oct 2024 20:16:18 +0000 Subject: [PATCH 4/4] Update directory --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index a4d137d56436..cfc8841fbcbe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1009,8 +1009,8 @@ * [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java) * [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java) * [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java) - * [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java) * [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java) + * [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java) * [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java) * [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java) * [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)