Substantial refactoring to implement correct Naive, UCT Monte Carlo tree search methods.
Removed unnecessary distinction between policy and tree search (tree search is a special kind of policy). Calculation of all valid moves / arbitrary sets of moves is now a seperate class, as it serves a different purpose than a policy. Introduced regression error in AlphaBeta test.
This commit is contained in:
57
test/net/woodyfolsom/msproj/policy/AlphaBetaTest.java
Normal file
57
test/net/woodyfolsom/msproj/policy/AlphaBetaTest.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import net.woodyfolsom.msproj.GameBoard;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.policy.AlphaBeta;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AlphaBetaTest {
|
||||
@Test
|
||||
public void testGenmoveAsW() {
|
||||
Policy treeSearch = new AlphaBeta();
|
||||
GameState gameState = new GameState(6);
|
||||
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('B', 1, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
|
||||
|
||||
String move = treeSearch.getAction(new GameConfig(), gameState, "b");
|
||||
System.out.println(gameState);
|
||||
|
||||
System.out.println("Generated move: " + move);
|
||||
assertEquals("Expected B3 but was: " + move, "B3", move);
|
||||
gameState.playStone("b", move);
|
||||
|
||||
System.out.println(gameState);
|
||||
|
||||
assertEquals(Policy.PASS,
|
||||
treeSearch.getAction(new GameConfig(), gameState, "?"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenmoveAsB() {
|
||||
Policy treeSearch = new AlphaBeta();
|
||||
GameState gameState = new GameState(6);
|
||||
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 2, GameBoard.WHITE_STONE);
|
||||
|
||||
String move = treeSearch.getAction(new GameConfig(), gameState, "b");
|
||||
System.out.println(gameState);
|
||||
|
||||
System.out.println("Generated move: " + move);
|
||||
assertEquals("Expected B3 but was: " + move, "B3", move);
|
||||
gameState.playStone("b", move);
|
||||
|
||||
System.out.println(gameState);
|
||||
|
||||
assertEquals(Policy.PASS,
|
||||
treeSearch.getAction(new GameConfig(), gameState, "?"));
|
||||
}
|
||||
}
|
||||
34
test/net/woodyfolsom/msproj/policy/MinimaxTest.java
Normal file
34
test/net/woodyfolsom/msproj/policy/MinimaxTest.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import net.woodyfolsom.msproj.GameBoard;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class MinimaxTest {
|
||||
|
||||
@Test
|
||||
public void testGenmove() {
|
||||
Policy moveGenerator = new Minimax();
|
||||
GameState gameState = new GameState(5);
|
||||
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 4, GameBoard.BLACK_STONE);
|
||||
|
||||
String move = moveGenerator.getAction(new GameConfig(), gameState, "w");
|
||||
System.out.println("Generated move: " + move);
|
||||
gameState.playStone("w", move);
|
||||
|
||||
System.out.println(gameState);
|
||||
|
||||
assertEquals(Policy.PASS,moveGenerator.getAction(new GameConfig(), gameState, "?"));
|
||||
|
||||
System.out.println(gameState);
|
||||
}
|
||||
}
|
||||
53
test/net/woodyfolsom/msproj/policy/RandomTest.java
Normal file
53
test/net/woodyfolsom/msproj/policy/RandomTest.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import net.woodyfolsom.msproj.GameBoard;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.policy.Policy;
|
||||
import net.woodyfolsom.msproj.policy.RandomMovePolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class RandomTest {
|
||||
|
||||
@Test
|
||||
public void testGenmove() {
|
||||
Policy moveGenerator = new RandomMovePolicy();
|
||||
GameState gameState = new GameState(5);
|
||||
moveGenerator.getAction(new GameConfig(), gameState, "b");
|
||||
gameState = new GameState(5);
|
||||
moveGenerator.getAction(new GameConfig(), gameState, "w");
|
||||
|
||||
assertEquals(Policy.PASS,moveGenerator.getAction(new GameConfig(), gameState, "?"));
|
||||
|
||||
System.out.println(gameState);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlternativeToIllegalMove() {
|
||||
GameState gameState = new GameState(4);
|
||||
gameState.playStone('A', 1, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('A', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('A', 3, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('A', 4, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 1, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 2, GameBoard.BLACK_STONE);
|
||||
//gameState.playStone('B', 3, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('B', 4, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('C', 2, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('C', 3, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('C', 4, GameBoard.BLACK_STONE);
|
||||
gameState.playStone('D', 4, GameBoard.BLACK_STONE);
|
||||
assertTrue(gameState.playStone('C', 1, GameBoard.WHITE_STONE));
|
||||
assertTrue(gameState.playStone('D', 2, GameBoard.WHITE_STONE));
|
||||
assertTrue(gameState.playStone('D', 3, GameBoard.WHITE_STONE));
|
||||
System.out.println(gameState);
|
||||
//This is correct - checked vs. MFOG
|
||||
assertEquals("B3", new RandomMovePolicy().getAction(new GameConfig(), gameState, "w"));
|
||||
System.out.println(gameState);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.woodyfolsom.msproj.GameBoard;
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
import net.woodyfolsom.msproj.policy.ValidMoveGenerator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ValidMoveGeneratorTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
/* move generator should not include A1 here when playing as black:
|
||||
A B C D E
|
||||
5 . . . . . 5
|
||||
4 . O . . . 4
|
||||
3 . . . . . 3 WHITE(O) has captured 0 stones
|
||||
2 O . O . . 2 BLACK(X) has captured 0 stones
|
||||
1 . O . . . 1
|
||||
A B C D E
|
||||
*/
|
||||
GameState gameState = new GameState(5);
|
||||
gameState.playStone('A', 2, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('B', 1, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('B', 4, GameBoard.WHITE_STONE);
|
||||
gameState.playStone('C', 2, GameBoard.WHITE_STONE);
|
||||
assertFalse(gameState.playStone('A', 1, GameBoard.BLACK_STONE));
|
||||
|
||||
List<String> validMoves = new ValidMoveGenerator().getActions(new GameConfig(), gameState, "b",0);
|
||||
assertTrue(validMoves.size() > 0);
|
||||
for (String vm : validMoves) {
|
||||
System.out.println(vm);
|
||||
}
|
||||
assertFalse(validMoves.contains("A1"));
|
||||
|
||||
System.out.println(gameState);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user