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:
66
src/net/woodyfolsom/msproj/policy/RandomMovePolicy.java
Normal file
66
src/net/woodyfolsom/msproj/policy/RandomMovePolicy.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.woodyfolsom.msproj.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.woodyfolsom.msproj.GameConfig;
|
||||
import net.woodyfolsom.msproj.GameState;
|
||||
|
||||
|
||||
public class RandomMovePolicy implements Policy {
|
||||
|
||||
/**
|
||||
* Does NOT modify the gameState.
|
||||
*/
|
||||
public String getAction(GameConfig gameConfig, GameState gameState,
|
||||
String color) {
|
||||
GameState gameStateCopy = new GameState(gameState);
|
||||
List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
|
||||
|
||||
while (emptyCoordinates.size() > 0) {
|
||||
String randomMove = emptyCoordinates
|
||||
.get((int) (Math.random() * emptyCoordinates.size()));
|
||||
if (gameStateCopy.playStone(color, randomMove)) {
|
||||
return randomMove;
|
||||
} else {
|
||||
emptyCoordinates.remove(randomMove);
|
||||
}
|
||||
}
|
||||
|
||||
return PASS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to generate up to nMoves random moves on behalf of the specified
|
||||
* player. Will return at least one move, which may be 'pass' if random
|
||||
* search does not success in discovering a valid move. Does NOT modify the
|
||||
* gameState.
|
||||
*
|
||||
* @param gameConfig
|
||||
* @param gameState
|
||||
* @param color
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<String> genMoves(GameConfig gameConfig, GameState gameState,
|
||||
String color, int nMoves) {
|
||||
GameState gameStateCopy = new GameState(gameState);
|
||||
List<String> emptyCoordinates = gameStateCopy.getEmptyCoords();
|
||||
List<String> randomMoves = new ArrayList<String>();
|
||||
|
||||
while (emptyCoordinates.size() > 0 && randomMoves.size() < nMoves) {
|
||||
String randomMove = emptyCoordinates
|
||||
.get((int) (Math.random() * emptyCoordinates.size()));
|
||||
if (gameStateCopy.playStone(color, randomMove)) {
|
||||
randomMoves.add(randomMove);
|
||||
}
|
||||
emptyCoordinates.remove(randomMove);
|
||||
}
|
||||
|
||||
if (randomMoves.size() == 0) {
|
||||
randomMoves.add(PASS);
|
||||
}
|
||||
|
||||
return randomMoves;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user